From 00a864e693434bce687f3f5145d8369583197b78 Mon Sep 17 00:00:00 2001 From: Christopher Brickley Date: Tue, 26 Aug 2014 08:20:18 -0400 Subject: add commit compare functionality --- models/git_diff.go | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) (limited to 'models/git_diff.go') diff --git a/models/git_diff.go b/models/git_diff.go index 4b4d1234..21a624de 100644 --- a/models/git_diff.go +++ b/models/git_diff.go @@ -175,25 +175,30 @@ func ParsePatch(pid int64, cmd *exec.Cmd, reader io.Reader) (*Diff, error) { return diff, nil } -func GetDiff(repoPath, commitid string) (*Diff, error) { +func GetDiffRange(repoPath, beforeCommitId string, afterCommitId string) (*Diff, error) { repo, err := git.OpenRepository(repoPath) if err != nil { return nil, err } - commit, err := repo.GetCommit(commitid) + commit, err := repo.GetCommit(afterCommitId) if err != nil { return nil, err } rd, wr := io.Pipe() var cmd *exec.Cmd - // First commit of repository. - if commit.ParentCount() == 0 { - cmd = exec.Command("git", "show", commitid) + // if "after" commit given + if beforeCommitId == "" { + // First commit of repository. + if commit.ParentCount() == 0 { + cmd = exec.Command("git", "show", afterCommitId) + } else { + c, _ := commit.Parent(0) + cmd = exec.Command("git", "diff", c.Id.String(), afterCommitId) + } } else { - c, _ := commit.Parent(0) - cmd = exec.Command("git", "diff", c.Id.String(), commitid) + cmd = exec.Command("git", "diff", beforeCommitId, afterCommitId) } cmd.Dir = repoPath cmd.Stdout = wr @@ -208,7 +213,7 @@ func GetDiff(repoPath, commitid string) (*Diff, error) { }() defer rd.Close() - desc := fmt.Sprintf("GetDiff(%s)", repoPath) + desc := fmt.Sprintf("GetDiffRange(%s)", repoPath) pid := process.Add(desc, cmd) go func() { // In case process became zombie. @@ -226,3 +231,7 @@ func GetDiff(repoPath, commitid string) (*Diff, error) { return ParsePatch(pid, cmd, rd) } + +func GetDiffCommit(repoPath, commitId string) (*Diff, error) { + return GetDiffRange(repoPath, "", commitId) +} -- cgit v1.2.3 From bba401a5dcf8af2fdc4c7c05b11db100019ee4fa Mon Sep 17 00:00:00 2001 From: Unknwon Date: Fri, 12 Sep 2014 19:42:11 -0400 Subject: Prepare 0.5 release --- .gopmfile | 47 ++++++++++++++++++------------------- README.md | 2 +- README_ZH.md | 2 +- models/git_diff.go | 3 +-- public/ng/css/gogs.css | 1 + public/ng/less/gogs/repository.less | 1 + 6 files changed, 28 insertions(+), 28 deletions(-) (limited to 'models/git_diff.go') diff --git a/.gopmfile b/.gopmfile index 6aac4953..6edec776 100644 --- a/.gopmfile +++ b/.gopmfile @@ -2,30 +2,29 @@ path = github.com/gogits/gogs [deps] -github.com/beego/memcache = -github.com/beego/redigo = -github.com/Unknwon/cae = -github.com/Unknwon/com = -github.com/Unknwon/goconfig = -github.com/Unknwon/i18n = -github.com/Unknwon/macaron = -github.com/codegangsta/cli = -github.com/go-sql-driver/mysql = -github.com/go-xorm/core = -github.com/go-xorm/xorm = -github.com/gogits/gfm = -github.com/gogits/git = -github.com/gogits/oauth2 = -github.com/lib/pq = -github.com/macaron-contrib/cache = -github.com/macaron-contrib/captcha = -github.com/macaron-contrib/csrf = -github.com/macaron-contrib/i18n = -github.com/macaron-contrib/session = -github.com/macaron-contrib/toolbox = -github.com/mattn/go-sqlite3 = -github.com/nfnt/resize = -github.com/saintfish/chardet = +github.com/beego/memcache = commit:2aea774416 +github.com/beego/redigo = commit:856744a0d5 +github.com/Unknwon/cae = commit:2e70a1351b +github.com/Unknwon/com = commit:2cbcbc6916 +github.com/Unknwon/goconfig = commit:0f8d8dc1c0 +github.com/Unknwon/i18n = commit:47baeff8d0 +github.com/Unknwon/macaron = commit:f22f45d79a +github.com/codegangsta/cli = commit:7381bc4e62 +github.com/go-sql-driver/mysql = commit:8111ee3ec3 +github.com/go-xorm/core = commit:750aae0fa5 +github.com/go-xorm/xorm = commit:2d8b3135b1 +github.com/gogits/gfm = commit:40f747a9c0 +github.com/gogits/oauth2 = commit:99cbec870a +github.com/lib/pq = commit:b021d0ef20 +github.com/macaron-contrib/cache = commit:204d8e5137 +github.com/macaron-contrib/captcha = commit:8f3f1ac0e3 +github.com/macaron-contrib/csrf = commit:cd84c01723 +github.com/macaron-contrib/i18n = commit:489cc194b5 +github.com/macaron-contrib/session = commit:80a88a1bba +github.com/macaron-contrib/toolbox = commit:57127bcc89 +github.com/mattn/go-sqlite3 = commit:a80c27ba33 +github.com/nfnt/resize = commit:581d15cb53 +github.com/saintfish/chardet = commit:3af4cd4741 [res] include = conf|etc|public|scripts|templates diff --git a/README.md b/README.md index 3115d3fc..05771144 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Gogs - Go Git Service [![wercker status](https://app.wercker.com/status/ad0bdb0b Gogs(Go Git Service) is a painless self-hosted Git Service written in Go. -![Demo](http://gowalker.org/public/gogs_demo.gif) +![Demo](https://gowalker.org/public/gogs_demo.gif) ##### Current version: 0.5.0 Beta diff --git a/README_ZH.md b/README_ZH.md index ef154d0e..2fa82e7d 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -3,7 +3,7 @@ Gogs - Go Git Service [![wercker status](https://app.wercker.com/status/ad0bdb0b Gogs(Go Git Service) 是一个基于 Go 语言的自助 Git 服务。 -![Demo](http://gowalker.org/public/gogs_demo.gif) +![Demo](https://gowalker.org/public/gogs_demo.gif) ##### 当前版本:0.5.0 Beta diff --git a/models/git_diff.go b/models/git_diff.go index 21a624de..bf7a9cd5 100644 --- a/models/git_diff.go +++ b/models/git_diff.go @@ -15,8 +15,7 @@ import ( "github.com/Unknwon/com" - "github.com/gogits/git" - + "github.com/gogits/gogs/modules/git" "github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/process" ) diff --git a/public/ng/css/gogs.css b/public/ng/css/gogs.css index ee364905..5dd35acc 100644 --- a/public/ng/css/gogs.css +++ b/public/ng/css/gogs.css @@ -1316,6 +1316,7 @@ The register and sign-in page style padding: 0; } .code-view .lines-num { + vertical-align: top; text-align: right; color: #999; background: #f5f5f5; diff --git a/public/ng/less/gogs/repository.less b/public/ng/less/gogs/repository.less index 955a617c..48959a62 100644 --- a/public/ng/less/gogs/repository.less +++ b/public/ng/less/gogs/repository.less @@ -399,6 +399,7 @@ border-top-right-radius: .25em; } } .lines-num { + vertical-align: top; text-align: right; color: #999; background: #f5f5f5; -- cgit v1.2.3 From ed84adb679f3de70b2bffaead20a87711c38ee3a Mon Sep 17 00:00:00 2001 From: lunnyxiao Date: Wed, 17 Sep 2014 12:03:03 +0800 Subject: toutf8 improved & add max git diff lines --- conf/app.ini | 3 +++ models/git_diff.go | 34 ++++++++++++++++++++++------------ modules/base/template.go | 27 +++++++++++++++++++++++++++ modules/setting/setting.go | 3 +++ routers/repo/commit.go | 7 +++++-- routers/repo/view.go | 20 +------------------- templates/repo/diff.tmpl | 2 +- 7 files changed, 62 insertions(+), 34 deletions(-) (limited to 'models/git_diff.go') diff --git a/conf/app.ini b/conf/app.ini index be49e064..cb907b22 100644 --- a/conf/app.ini +++ b/conf/app.ini @@ -255,3 +255,6 @@ CONN = [i18n] LANGS = en-US,zh-CN,de-DE NAMES = English,简体中文,Deutsch + +[git] +MAX_GITDIFF_LINES = 10000 \ No newline at end of file diff --git a/models/git_diff.go b/models/git_diff.go index bf7a9cd5..e093e7ab 100644 --- a/models/git_diff.go +++ b/models/git_diff.go @@ -70,7 +70,7 @@ func (diff *Diff) NumFiles() int { const DIFF_HEAD = "diff --git " -func ParsePatch(pid int64, cmd *exec.Cmd, reader io.Reader) (*Diff, error) { +func ParsePatch(pid int64, maxlines int, cmd *exec.Cmd, reader io.Reader) (*Diff, error) { scanner := bufio.NewScanner(reader) var ( curFile *DiffFile @@ -79,6 +79,7 @@ func ParsePatch(pid int64, cmd *exec.Cmd, reader io.Reader) (*Diff, error) { } leftLine, rightLine int + isTooLong bool ) diff := &Diff{Files: make([]*DiffFile, 0)} @@ -90,16 +91,17 @@ func ParsePatch(pid int64, cmd *exec.Cmd, reader io.Reader) (*Diff, error) { continue } + if line == "" { + continue + } + i = i + 1 - // Diff data too large. - if i == 5000 { + // Diff data too large, we only show the first about maxlines lines + if i == maxlines { + isTooLong = true log.Warn("Diff data too large") - return &Diff{}, nil - } - - if line == "" { - continue + //return &Diff{}, nil } switch { @@ -110,6 +112,10 @@ func ParsePatch(pid int64, cmd *exec.Cmd, reader io.Reader) (*Diff, error) { curSection.Lines = append(curSection.Lines, diffLine) continue case line[0] == '@': + if isTooLong { + return diff, nil + } + curSection = &DiffSection{} curFile.Sections = append(curFile.Sections, curSection) ss := strings.Split(line, "@@") @@ -143,6 +149,10 @@ func ParsePatch(pid int64, cmd *exec.Cmd, reader io.Reader) (*Diff, error) { // Get new file. if strings.HasPrefix(line, DIFF_HEAD) { + if isTooLong { + return diff, nil + } + fs := strings.Split(line[len(DIFF_HEAD):], " ") a := fs[0] @@ -174,7 +184,7 @@ func ParsePatch(pid int64, cmd *exec.Cmd, reader io.Reader) (*Diff, error) { return diff, nil } -func GetDiffRange(repoPath, beforeCommitId string, afterCommitId string) (*Diff, error) { +func GetDiffRange(repoPath, beforeCommitId string, afterCommitId string, maxlines int) (*Diff, error) { repo, err := git.OpenRepository(repoPath) if err != nil { return nil, err @@ -228,9 +238,9 @@ func GetDiffRange(repoPath, beforeCommitId string, afterCommitId string) (*Diff, } }() - return ParsePatch(pid, cmd, rd) + return ParsePatch(pid, maxlines, cmd, rd) } -func GetDiffCommit(repoPath, commitId string) (*Diff, error) { - return GetDiffRange(repoPath, "", commitId) +func GetDiffCommit(repoPath, commitId string, maxlines int) (*Diff, error) { + return GetDiffRange(repoPath, "", commitId, maxlines) } diff --git a/modules/base/template.go b/modules/base/template.go index f2ae00b9..64195729 100644 --- a/modules/base/template.go +++ b/modules/base/template.go @@ -8,13 +8,16 @@ import ( "bytes" "container/list" "encoding/json" + "errors" "fmt" "html/template" "runtime" "strings" "time" + "code.google.com/p/mahonia" "github.com/gogits/gogs/modules/setting" + "github.com/saintfish/chardet" ) func Str2html(raw string) template.HTML { @@ -45,6 +48,29 @@ func ShortSha(sha1 string) string { return sha1 } +func ToUtf8WithErr(content []byte) (error, string) { + detector := chardet.NewTextDetector() + result, err := detector.DetectBest(content) + if err != nil { + return err, "" + } + + if result.Charset == "utf8" { + return nil, string(content) + } + + decoder := mahonia.NewDecoder(result.Charset) + if decoder != nil { + return nil, decoder.ConvertString(string(content)) + } + return errors.New("unknow char decoder"), string(content) +} + +func ToUtf8(content string) string { + _, res := ToUtf8WithErr([]byte(content)) + return res +} + var mailDomains = map[string]string{ "gmail.com": "gmail.com", } @@ -103,6 +129,7 @@ var TemplateFuncs template.FuncMap = map[string]interface{}{ "ActionContent2Commits": ActionContent2Commits, "Oauth2Icon": Oauth2Icon, "Oauth2Name": Oauth2Name, + "ToUtf8": ToUtf8, } type Actioner interface { diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 199b4f2c..011c00c0 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -64,6 +64,7 @@ var ( // Picture settings. PictureService string DisableGravatar bool + MaxGitDiffLines int // Log settings. LogRootPath string @@ -241,6 +242,8 @@ func NewConfigContext() { []string{"server"}) DisableGravatar = Cfg.MustBool("picture", "DISABLE_GRAVATAR") + MaxGitDiffLines = Cfg.MustInt("git", "MAX_GITDIFF_LINES", 5000) + Langs = Cfg.MustValueArray("i18n", "LANGS", ",") Names = Cfg.MustValueArray("i18n", "NAMES", ",") } diff --git a/routers/repo/commit.go b/routers/repo/commit.go index 54acc85b..f7feb4d9 100644 --- a/routers/repo/commit.go +++ b/routers/repo/commit.go @@ -12,6 +12,7 @@ import ( "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/middleware" + "github.com/gogits/gogs/modules/setting" ) const ( @@ -114,7 +115,8 @@ func Diff(ctx *middleware.Context) { commit := ctx.Repo.Commit - diff, err := models.GetDiffCommit(models.RepoPath(userName, repoName), commitId) + diff, err := models.GetDiffCommit(models.RepoPath(userName, repoName), + commitId, setting.MaxGitDiffLines) if err != nil { ctx.Handle(404, "GetDiffCommit", err) return @@ -176,7 +178,8 @@ func CompareDiff(ctx *middleware.Context) { return } - diff, err := models.GetDiffRange(models.RepoPath(userName, repoName), beforeCommitId, afterCommitId) + diff, err := models.GetDiffRange(models.RepoPath(userName, repoName), beforeCommitId, + afterCommitId, setting.MaxGitDiffLines) if err != nil { ctx.Handle(404, "GetDiffRange", err) return diff --git a/routers/repo/view.go b/routers/repo/view.go index f98eee03..e42894ae 100644 --- a/routers/repo/view.go +++ b/routers/repo/view.go @@ -11,12 +11,9 @@ import ( "path/filepath" "strings" - "github.com/saintfish/chardet" - "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/git" "github.com/gogits/gogs/modules/log" - "github.com/gogits/gogs/modules/mahonia" "github.com/gogits/gogs/modules/middleware" ) @@ -24,21 +21,6 @@ const ( HOME base.TplName = "repo/home" ) -func toUtf8(content []byte) (error, string) { - detector := chardet.NewTextDetector() - result, err := detector.DetectBest(content) - if err != nil { - return err, "" - } - - if result.Charset == "utf8" { - return nil, string(content) - } - - decoder := mahonia.NewDecoder(result.Charset) - return nil, decoder.ConvertString(string(content)) -} - func Home(ctx *middleware.Context) { ctx.Data["Title"] = ctx.Repo.Repository.Name @@ -117,7 +99,7 @@ func Home(ctx *middleware.Context) { if readmeExist { ctx.Data["FileContent"] = string(base.RenderMarkdown(buf, "")) } else { - if err, content := toUtf8(buf); err != nil { + if err, content := base.ToUtf8WithErr(buf); err != nil { if err != nil { log.Error(4, "Convert content encoding: %s", err) } diff --git a/templates/repo/diff.tmpl b/templates/repo/diff.tmpl index 78733450..a2150f28 100644 --- a/templates/repo/diff.tmpl +++ b/templates/repo/diff.tmpl @@ -105,7 +105,7 @@ {{if .RightIdx}}{{.RightIdx}}{{end}} -
{{.Content}}
+
{{ToUtf8 .Content}}
{{end}} -- cgit v1.2.3