From bc8215721627fcdeaa23c6e3bf625e0c4e5c3407 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Mon, 2 Nov 2015 19:55:24 -0500 Subject: fix #1078 --- models/git_diff.go | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) (limited to 'models/git_diff.go') diff --git a/models/git_diff.go b/models/git_diff.go index 2335e468..3bf938ae 100644 --- a/models/git_diff.go +++ b/models/git_diff.go @@ -37,6 +37,7 @@ const ( DIFF_FILE_ADD = iota + 1 DIFF_FILE_CHANGE DIFF_FILE_DEL + DIFF_FILE_RENAME ) type DiffLine struct { @@ -57,12 +58,14 @@ type DiffSection struct { type DiffFile struct { Name string + OldName string Index int Addition, Deletion int Type int IsCreated bool IsDeleted bool IsBin bool + IsRenamed bool Sections []*DiffSection } @@ -158,17 +161,29 @@ func ParsePatch(pid int64, maxlines int, cmd *exec.Cmd, reader io.Reader) (*Diff // Get new file. if strings.HasPrefix(line, DIFF_HEAD) { - beg := len(DIFF_HEAD) - a := line[beg : (len(line)-beg)/2+beg] + middle := -1 + + // Note: In case file name is surrounded by double quotes(it happens only in git-shell). + hasQuote := strings.Index(line, `\"`) > -1 + if hasQuote { + line = strings.Replace(line, `\"`, `"`, -1) + middle = strings.Index(line, ` "b/`) + } else { + middle = strings.Index(line, " b/") + } - // In case file name is surrounded by double quotes(it happens only in git-shell). - if a[0] == '"' { + beg := len(DIFF_HEAD) + a := line[beg+2 : middle] + b := line[middle+3:] + if hasQuote { a = a[1 : len(a)-1] - a = strings.Replace(a, `\"`, `"`, -1) + b = b[1 : len(b)-1] } + fmt.Println(a, b) + curFile = &DiffFile{ - Name: a[strings.Index(a, "/")+1:], + Name: a, Index: len(diff.Files) + 1, Type: DIFF_FILE_CHANGE, Sections: make([]*DiffSection, 0, 10), @@ -180,16 +195,17 @@ func ParsePatch(pid int64, maxlines int, cmd *exec.Cmd, reader io.Reader) (*Diff switch { case strings.HasPrefix(scanner.Text(), "new file"): curFile.Type = DIFF_FILE_ADD - curFile.IsDeleted = false curFile.IsCreated = true case strings.HasPrefix(scanner.Text(), "deleted"): curFile.Type = DIFF_FILE_DEL - curFile.IsCreated = false curFile.IsDeleted = true case strings.HasPrefix(scanner.Text(), "index"): curFile.Type = DIFF_FILE_CHANGE - curFile.IsCreated = false - curFile.IsDeleted = false + case strings.HasPrefix(scanner.Text(), "similarity index 100%"): + curFile.Type = DIFF_FILE_RENAME + curFile.IsRenamed = true + curFile.OldName = curFile.Name + curFile.Name = b } if curFile.Type > 0 { break @@ -244,10 +260,10 @@ func GetDiffRange(repoPath, beforeCommitId string, afterCommitId string, maxline cmd = exec.Command("git", "show", afterCommitId) } else { c, _ := commit.Parent(0) - cmd = exec.Command("git", "diff", c.Id.String(), afterCommitId) + cmd = exec.Command("git", "diff", "-M", c.Id.String(), afterCommitId) } } else { - cmd = exec.Command("git", "diff", beforeCommitId, afterCommitId) + cmd = exec.Command("git", "diff", "-M", beforeCommitId, afterCommitId) } cmd.Dir = repoPath cmd.Stdout = wr -- cgit v1.2.3 From 25ec20d5251511ebd0b9e6b963e189b860c39704 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Tue, 3 Nov 2015 17:25:39 -0500 Subject: #1838 update merge base before generate new patch --- README.md | 2 +- gogs.go | 2 +- models/git_diff.go | 4 +--- models/pull.go | 24 ++++++++++++++++++++--- modules/git/repo_pull.go | 50 +++++++++++++++++++++++++++++++++++------------- templates/.VERSION | 2 +- 6 files changed, 62 insertions(+), 22 deletions(-) (limited to 'models/git_diff.go') diff --git a/README.md b/README.md index 61f11c66..a80f79f0 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Gogs - Go Git Service [![Build Status](https://travis-ci.org/gogits/gogs.svg?bra ![](public/img/gogs-large-resize.png) -##### Current version: 0.6.22 Beta +##### Current version: 0.6.23 Beta diff --git a/gogs.go b/gogs.go index 343ee5aa..a1e32a57 100644 --- a/gogs.go +++ b/gogs.go @@ -17,7 +17,7 @@ import ( "github.com/gogits/gogs/modules/setting" ) -const APP_VER = "0.6.22.1103 Beta" +const APP_VER = "0.6.23.1103 Beta" func init() { runtime.GOMAXPROCS(runtime.NumCPU()) diff --git a/models/git_diff.go b/models/git_diff.go index 3bf938ae..c1568a57 100644 --- a/models/git_diff.go +++ b/models/git_diff.go @@ -97,7 +97,7 @@ func ParsePatch(pid int64, maxlines int, cmd *exec.Cmd, reader io.Reader) (*Diff var i int for scanner.Scan() { line := scanner.Text() - // fmt.Println(i, line) + if strings.HasPrefix(line, "+++ ") || strings.HasPrefix(line, "--- ") { continue } @@ -180,8 +180,6 @@ func ParsePatch(pid int64, maxlines int, cmd *exec.Cmd, reader io.Reader) (*Diff b = b[1 : len(b)-1] } - fmt.Println(a, b) - curFile = &DiffFile{ Name: a, Index: len(diff.Files) + 1, diff --git a/models/pull.go b/models/pull.go index 0300c083..56af5f82 100644 --- a/models/pull.go +++ b/models/pull.go @@ -385,16 +385,18 @@ func (pr *PullRequest) UpdateCols(cols ...string) error { var PullRequestQueue = NewUniqueQueue(setting.Repository.PullRequestQueueLength) // UpdatePatch generates and saves a new patch. -func (pr *PullRequest) UpdatePatch() error { - if err := pr.GetHeadRepo(); err != nil { +func (pr *PullRequest) UpdatePatch() (err error) { + if err = pr.GetHeadRepo(); err != nil { return fmt.Errorf("GetHeadRepo: %v", err) } else if pr.HeadRepo == nil { log.Trace("PullRequest[%d].UpdatePatch: ignored cruppted data", pr.ID) return nil } - if err := pr.GetBaseRepo(); err != nil { + if err = pr.GetBaseRepo(); err != nil { return fmt.Errorf("GetBaseRepo: %v", err) + } else if err = pr.BaseRepo.GetOwner(); err != nil { + return fmt.Errorf("GetOwner: %v", err) } headRepoPath, err := pr.HeadRepo.RepoPath() @@ -407,6 +409,22 @@ func (pr *PullRequest) UpdatePatch() error { return fmt.Errorf("OpenRepository: %v", err) } + // Add a temporary remote. + tmpRemote := com.ToStr(time.Now().UnixNano()) + if err = headGitRepo.AddRemote(tmpRemote, RepoPath(pr.BaseRepo.Owner.Name, pr.BaseRepo.Name)); err != nil { + return fmt.Errorf("AddRemote: %v", err) + } + defer func() { + headGitRepo.RemoveRemote(tmpRemote) + }() + remoteBranch := "remotes/" + tmpRemote + "/" + pr.BaseBranch + pr.MergeBase, err = headGitRepo.GetMergeBase(remoteBranch, pr.HeadBranch) + if err != nil { + return fmt.Errorf("GetMergeBase: %v", err) + } else if err = pr.Update(); err != nil { + return fmt.Errorf("Update: %v", err) + } + patch, err := headGitRepo.GetPatch(pr.MergeBase, pr.HeadBranch) if err != nil { return fmt.Errorf("GetPatch: %v", err) diff --git a/modules/git/repo_pull.go b/modules/git/repo_pull.go index a9cc33a1..16b9536f 100644 --- a/modules/git/repo_pull.go +++ b/modules/git/repo_pull.go @@ -20,31 +20,55 @@ type PullRequestInfo struct { NumFiles int } +// GetMergeBase checks and returns merge base of two branches. +func (repo *Repository) GetMergeBase(remoteBranch, headBranch string) (string, error) { + // Get merge base commit. + stdout, stderr, err := com.ExecCmdDir(repo.Path, "git", "merge-base", remoteBranch, headBranch) + if err != nil { + return "", fmt.Errorf("get merge base: %v", concatenateError(err, stderr)) + } + return strings.TrimSpace(stdout), nil +} + +// AddRemote adds a remote to repository. +func (repo *Repository) AddRemote(name, path string) error { + _, stderr, err := com.ExecCmdDir(repo.Path, "git", "remote", "add", "-f", name, path) + if err != nil { + return fmt.Errorf("add remote(%s - %s): %v", name, path, concatenateError(err, stderr)) + } + return nil +} + +// RemoveRemote removes a remote from repository. +func (repo *Repository) RemoveRemote(name string) error { + _, stderr, err := com.ExecCmdDir(repo.Path, "git", "remote", "remove", name) + if err != nil { + return fmt.Errorf("remove remote(%s): %v", name, concatenateError(err, stderr)) + } + return nil +} + // GetPullRequestInfo generates and returns pull request information // between base and head branches of repositories. -func (repo *Repository) GetPullRequestInfo(basePath, baseBranch, headBranch string) (*PullRequestInfo, error) { +func (repo *Repository) GetPullRequestInfo(basePath, baseBranch, headBranch string) (_ *PullRequestInfo, err error) { // Add a temporary remote. tmpRemote := com.ToStr(time.Now().UnixNano()) - _, stderr, err := com.ExecCmdDir(repo.Path, "git", "remote", "add", "-f", tmpRemote, basePath) - if err != nil { - return nil, fmt.Errorf("add base as remote: %v", concatenateError(err, stderr)) + if err = repo.AddRemote(tmpRemote, basePath); err != nil { + return nil, fmt.Errorf("AddRemote: %v", err) } defer func() { - com.ExecCmdDir(repo.Path, "git", "remote", "remove", tmpRemote) + repo.RemoveRemote(tmpRemote) }() - prInfo := new(PullRequestInfo) - - var stdout string remoteBranch := "remotes/" + tmpRemote + "/" + baseBranch - // Get merge base commit. - stdout, stderr, err = com.ExecCmdDir(repo.Path, "git", "merge-base", remoteBranch, headBranch) + + prInfo := new(PullRequestInfo) + prInfo.MergeBase, err = repo.GetMergeBase(remoteBranch, headBranch) if err != nil { - return nil, fmt.Errorf("get merge base: %v", concatenateError(err, stderr)) + return nil, fmt.Errorf("GetMergeBase: %v", err) } - prInfo.MergeBase = strings.TrimSpace(stdout) - stdout, stderr, err = com.ExecCmdDir(repo.Path, "git", "log", prInfo.MergeBase+"..."+headBranch, prettyLogFormat) + stdout, stderr, err := com.ExecCmdDir(repo.Path, "git", "log", prInfo.MergeBase+"..."+headBranch, prettyLogFormat) if err != nil { return nil, fmt.Errorf("list diff logs: %v", concatenateError(err, stderr)) } diff --git a/templates/.VERSION b/templates/.VERSION index 364e36fc..815df4fe 100644 --- a/templates/.VERSION +++ b/templates/.VERSION @@ -1 +1 @@ -0.6.22.1103 Beta \ No newline at end of file +0.6.23.1103 Beta \ No newline at end of file -- cgit v1.2.3 From 3a81fdf092a39cc94f3bb896a42db8546bd5f39a Mon Sep 17 00:00:00 2001 From: Unknwon Date: Tue, 3 Nov 2015 22:49:06 -0500 Subject: rename fields --- models/git_diff.go | 2 +- models/release.go | 2 +- models/update.go | 2 +- modules/git/blob.go | 2 +- modules/git/commit.go | 14 +++++++------- modules/git/commit_archive.go | 2 +- modules/git/repo_commit.go | 8 ++++---- modules/git/repo_tag.go | 4 ++-- modules/git/sha1.go | 2 +- modules/git/signature.go | 16 ++++++++-------- modules/git/signature_test.go | 20 ++++++++++++++++++++ modules/git/submodule.go | 3 ++- modules/git/tag.go | 2 +- modules/git/tree.go | 10 +++++----- modules/git/tree_blob.go | 2 +- modules/git/tree_entry.go | 4 ++-- modules/middleware/repo.go | 6 +++--- routers/repo/release.go | 6 +++--- routers/repo/repo.go | 4 ++-- routers/repo/view.go | 2 +- 20 files changed, 67 insertions(+), 46 deletions(-) create mode 100644 modules/git/signature_test.go (limited to 'models/git_diff.go') diff --git a/models/git_diff.go b/models/git_diff.go index c1568a57..4cc9ebf8 100644 --- a/models/git_diff.go +++ b/models/git_diff.go @@ -258,7 +258,7 @@ func GetDiffRange(repoPath, beforeCommitId string, afterCommitId string, maxline cmd = exec.Command("git", "show", afterCommitId) } else { c, _ := commit.Parent(0) - cmd = exec.Command("git", "diff", "-M", c.Id.String(), afterCommitId) + cmd = exec.Command("git", "diff", "-M", c.ID.String(), afterCommitId) } } else { cmd = exec.Command("git", "diff", "-M", beforeCommitId, afterCommitId) diff --git a/models/release.go b/models/release.go index 027491d9..38f3e4f4 100644 --- a/models/release.go +++ b/models/release.go @@ -64,7 +64,7 @@ func createTag(gitRepo *git.Repository, rel *Release) error { return err } - if err = gitRepo.CreateTag(rel.TagName, commit.Id.String()); err != nil { + if err = gitRepo.CreateTag(rel.TagName, commit.ID.String()); err != nil { return err } } else { diff --git a/models/update.go b/models/update.go index 0cf62db4..9e8e5c91 100644 --- a/models/update.go +++ b/models/update.go @@ -140,7 +140,7 @@ func Update(refName, oldCommitId, newCommitId, userName, repoUserName, repoName actEmail = commit.Committer.Email } commits = append(commits, - &base.PushCommit{commit.Id.String(), + &base.PushCommit{commit.ID.String(), commit.Message(), commit.Author.Email, commit.Author.Name, diff --git a/modules/git/blob.go b/modules/git/blob.go index 3ce462a3..bdf0cae4 100644 --- a/modules/git/blob.go +++ b/modules/git/blob.go @@ -18,7 +18,7 @@ type Blob struct { } func (b *Blob) Data() (io.Reader, error) { - stdout, stderr, err := com.ExecCmdDirBytes(b.repo.Path, "git", "show", b.Id.String()) + stdout, stderr, err := com.ExecCmdDirBytes(b.repo.Path, "git", "show", b.ID.String()) if err != nil { return nil, errors.New(string(stderr)) } diff --git a/modules/git/commit.go b/modules/git/commit.go index da0ab644..674a0b85 100644 --- a/modules/git/commit.go +++ b/modules/git/commit.go @@ -14,7 +14,7 @@ import ( // Commit represents a git commit. type Commit struct { Tree - Id sha1 // The id of this commit object + ID sha1 // The id of this commit object Author *Signature Committer *Signature CommitMessage string @@ -35,7 +35,7 @@ func (c *Commit) Summary() string { // Return oid of the parent number n (0-based index). Return nil if no such parent exists. func (c *Commit) ParentId(n int) (id sha1, err error) { if n >= len(c.parents) { - err = IdNotExist + err = IDNotExist return } return c.parents[n], nil @@ -61,7 +61,7 @@ func (c *Commit) ParentCount() int { } func (c *Commit) CommitsBefore() (*list.List, error) { - return c.repo.getCommitsBefore(c.Id) + return c.repo.getCommitsBefore(c.ID) } func (c *Commit) CommitsBeforeUntil(commitId string) (*list.List, error) { @@ -73,19 +73,19 @@ func (c *Commit) CommitsBeforeUntil(commitId string) (*list.List, error) { } func (c *Commit) CommitsCount() (int, error) { - return c.repo.commitsCount(c.Id) + return c.repo.commitsCount(c.ID) } func (c *Commit) SearchCommits(keyword string) (*list.List, error) { - return c.repo.searchCommits(c.Id, keyword) + return c.repo.searchCommits(c.ID, keyword) } func (c *Commit) CommitsByRange(page int) (*list.List, error) { - return c.repo.commitsByRange(c.Id, page) + return c.repo.commitsByRange(c.ID, page) } func (c *Commit) GetCommitOfRelPath(relPath string) (*Commit, error) { - return c.repo.getCommitOfRelPath(c.Id, relPath) + return c.repo.getCommitOfRelPath(c.ID, relPath) } func (c *Commit) GetSubModule(entryname string) (*SubModule, error) { diff --git a/modules/git/commit_archive.go b/modules/git/commit_archive.go index 23b4b058..8bb6b129 100644 --- a/modules/git/commit_archive.go +++ b/modules/git/commit_archive.go @@ -28,7 +28,7 @@ func (c *Commit) CreateArchive(path string, archiveType ArchiveType) error { return fmt.Errorf("unknown format: %v", archiveType) } - _, stderr, err := com.ExecCmdDir(c.repo.Path, "git", "archive", "--format="+format, "-o", path, c.Id.String()) + _, stderr, err := com.ExecCmdDir(c.repo.Path, "git", "archive", "--format="+format, "-o", path, c.ID.String()) if err != nil { return fmt.Errorf("%s", stderr) } diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go index e8ac2dfc..28633058 100644 --- a/modules/git/repo_commit.go +++ b/modules/git/repo_commit.go @@ -70,7 +70,7 @@ l: if err != nil { return nil, err } - commit.Tree.Id = id + commit.Tree.ID = id case "parent": // A commit can have one or more parents oid, err := NewIdFromString(string(line[spacepos+1:])) @@ -121,7 +121,7 @@ func (repo *Repository) getCommit(id sha1) (*Commit, error) { return nil, err } commit.repo = repo - commit.Id = id + commit.ID = id repo.commitCache[id] = commit return commit, nil @@ -211,7 +211,7 @@ func (repo *Repository) CommitsBetween(last *Commit, before *Commit) (*list.List var err error cur := last for { - if cur.Id.Equal(before.Id) { + if cur.ID.Equal(before.ID) { break } l.PushBack(cur) @@ -240,7 +240,7 @@ func (repo *Repository) commitsBefore(lock *sync.Mutex, l *list.List, parent *li for { if in == nil { break - } else if in.Value.(*Commit).Id.Equal(commit.Id) { + } else if in.Value.(*Commit).ID.Equal(commit.ID) { return nil } else { if in.Next() == nil { diff --git a/modules/git/repo_tag.go b/modules/git/repo_tag.go index 8f37d31a..a1f81649 100644 --- a/modules/git/repo_tag.go +++ b/modules/git/repo_tag.go @@ -69,7 +69,7 @@ func (repo *Repository) getTag(id sha1) (*Tag, error) { // Tag is a commit. if ObjectType(tp) == COMMIT { tag := &Tag{ - Id: id, + ID: id, Object: id, Type: string(COMMIT), repo: repo, @@ -89,7 +89,7 @@ func (repo *Repository) getTag(id sha1) (*Tag, error) { return nil, err } - tag.Id = id + tag.ID = id tag.repo = repo repo.tagCache[id] = tag diff --git a/modules/git/sha1.go b/modules/git/sha1.go index 5c57e89b..f286f35a 100644 --- a/modules/git/sha1.go +++ b/modules/git/sha1.go @@ -12,7 +12,7 @@ import ( ) var ( - IdNotExist = errors.New("sha1 id not exist") + IDNotExist = errors.New("sha1 ID does not exist") ) type sha1 [20]byte diff --git a/modules/git/signature.go b/modules/git/signature.go index b77f7a44..6cd92943 100644 --- a/modules/git/signature.go +++ b/modules/git/signature.go @@ -26,23 +26,23 @@ type Signature struct { // FIXME: include timezone for timestamp! func newSignatureFromCommitline(line []byte) (_ *Signature, err error) { sig := new(Signature) - emailstart := bytes.IndexByte(line, '<') - sig.Name = string(line[:emailstart-1]) - emailstop := bytes.IndexByte(line, '>') - sig.Email = string(line[emailstart+1 : emailstop]) + emailStart := bytes.IndexByte(line, '<') + sig.Name = string(line[:emailStart-1]) + emailEnd := bytes.IndexByte(line, '>') + sig.Email = string(line[emailStart+1 : emailEnd]) // Check date format. - firstChar := line[emailstop+2] + firstChar := line[emailEnd+2] if firstChar >= 48 && firstChar <= 57 { - timestop := bytes.IndexByte(line[emailstop+2:], ' ') - timestring := string(line[emailstop+2 : emailstop+2+timestop]) + timestop := bytes.IndexByte(line[emailEnd+2:], ' ') + timestring := string(line[emailEnd+2 : emailEnd+2+timestop]) seconds, err := strconv.ParseInt(timestring, 10, 64) if err != nil { return nil, err } sig.When = time.Unix(seconds, 0) } else { - sig.When, err = time.Parse("Mon Jan _2 15:04:05 2006 -0700", string(line[emailstop+2:])) + sig.When, err = time.Parse("Mon Jan _2 15:04:05 2006 -0700", string(line[emailEnd+2:])) if err != nil { return nil, err } diff --git a/modules/git/signature_test.go b/modules/git/signature_test.go new file mode 100644 index 00000000..a84f9b3d --- /dev/null +++ b/modules/git/signature_test.go @@ -0,0 +1,20 @@ +// Copyright 2015 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package git + +import ( + "testing" + + . "github.com/smartystreets/goconvey/convey" +) + +func Test_newSignatureFromCommitline(t *testing.T) { + Convey("Parse signature from commit line", t, func() { + line := "Intern 1445412825 +0200" + sig, err := newSignatureFromCommitline([]byte(line)) + So(err, ShouldBeNil) + So(sig, ShouldNotBeNil) + }) +} diff --git a/modules/git/submodule.go b/modules/git/submodule.go index 0b8efc26..10680c24 100644 --- a/modules/git/submodule.go +++ b/modules/git/submodule.go @@ -6,6 +6,7 @@ package git import ( "strings" + "github.com/gogits/gogs/modules/setting" ) @@ -53,7 +54,7 @@ func (sf *SubModuleFile) RefUrl() string { j := strings.LastIndex(url, ":") if i > -1 && j > -1 { // fix problem with reverse proxy works only with local server - if strings.Contains(setting.AppUrl,url[i+1:j]) { + if strings.Contains(setting.AppUrl, url[i+1:j]) { return setting.AppUrl + url[j+1:] } else { return "http://" + url[i+1:j] + "/" + url[j+1:] diff --git a/modules/git/tag.go b/modules/git/tag.go index 7fbbcb1c..8010aa9d 100644 --- a/modules/git/tag.go +++ b/modules/git/tag.go @@ -11,7 +11,7 @@ import ( // Tag represents a Git tag. type Tag struct { Name string - Id sha1 + ID sha1 repo *Repository Object sha1 // The id of this commit object Type string diff --git a/modules/git/tree.go b/modules/git/tree.go index 27539f06..cc62a2d5 100644 --- a/modules/git/tree.go +++ b/modules/git/tree.go @@ -18,7 +18,7 @@ var ( // A tree is a flat directory listing. type Tree struct { - Id sha1 + ID sha1 repo *Repository // parent tree @@ -66,7 +66,7 @@ func parseTreeData(tree *Tree, data []byte) ([]*TreeEntry, error) { if err != nil { return nil, err } - entry.Id = id + entry.ID = id pos += step + 1 // Skip half of sha1. step = bytes.IndexByte(data[pos:], '\n') @@ -100,7 +100,7 @@ func (t *Tree) SubTree(rpath string) (*Tree, error) { return nil, err } - g, err = t.repo.getTree(te.Id) + g, err = t.repo.getTree(te.ID) if err != nil { return nil, err } @@ -117,7 +117,7 @@ func (t *Tree) ListEntries(relpath string) (Entries, error) { t.entriesParsed = true stdout, stderr, err := com.ExecCmdDirBytes(t.repo.Path, - "git", "ls-tree", t.Id.String()) + "git", "ls-tree", t.ID.String()) if err != nil { if strings.Contains(err.Error(), "exit status 128") { return nil, errors.New(strings.TrimSpace(string(stderr))) @@ -130,7 +130,7 @@ func (t *Tree) ListEntries(relpath string) (Entries, error) { func NewTree(repo *Repository, id sha1) *Tree { tree := new(Tree) - tree.Id = id + tree.ID = id tree.repo = repo return tree } diff --git a/modules/git/tree_blob.go b/modules/git/tree_blob.go index f996aba3..44c5d0c8 100644 --- a/modules/git/tree_blob.go +++ b/modules/git/tree_blob.go @@ -13,7 +13,7 @@ import ( func (t *Tree) GetTreeEntryByPath(relpath string) (*TreeEntry, error) { if len(relpath) == 0 { return &TreeEntry{ - Id: t.Id, + ID: t.ID, Type: TREE, mode: ModeTree, }, nil diff --git a/modules/git/tree_entry.go b/modules/git/tree_entry.go index e403e93e..18250257 100644 --- a/modules/git/tree_entry.go +++ b/modules/git/tree_entry.go @@ -24,7 +24,7 @@ const ( ) type TreeEntry struct { - Id sha1 + ID sha1 Type ObjectType mode EntryMode @@ -51,7 +51,7 @@ func (te *TreeEntry) Size() int64 { return te.size } - stdout, _, err := com.ExecCmdDir(te.ptree.repo.Path, "git", "cat-file", "-s", te.Id.String()) + stdout, _, err := com.ExecCmdDir(te.ptree.repo.Path, "git", "cat-file", "-s", te.ID.String()) if err != nil { return 0 } diff --git a/modules/middleware/repo.go b/modules/middleware/repo.go index 1b3288e5..1154907a 100644 --- a/modules/middleware/repo.go +++ b/modules/middleware/repo.go @@ -118,7 +118,7 @@ func RepoRef() macaron.Handler { ctx.Handle(500, "GetCommitOfBranch", err) return } - ctx.Repo.CommitID = ctx.Repo.Commit.Id.String() + ctx.Repo.CommitID = ctx.Repo.Commit.ID.String() ctx.Repo.IsBranch = true } else { @@ -149,7 +149,7 @@ func RepoRef() macaron.Handler { ctx.Handle(500, "GetCommitOfBranch", err) return } - ctx.Repo.CommitID = ctx.Repo.Commit.Id.String() + ctx.Repo.CommitID = ctx.Repo.Commit.ID.String() } else if ctx.Repo.GitRepo.IsTagExist(refName) { ctx.Repo.IsTag = true @@ -158,7 +158,7 @@ func RepoRef() macaron.Handler { ctx.Handle(500, "GetCommitOfTag", err) return } - ctx.Repo.CommitID = ctx.Repo.Commit.Id.String() + ctx.Repo.CommitID = ctx.Repo.Commit.ID.String() } else if len(refName) == 40 { ctx.Repo.IsCommit = true ctx.Repo.CommitID = refName diff --git a/routers/repo/release.go b/routers/repo/release.go index 30d5f60b..680ecd1b 100644 --- a/routers/repo/release.go +++ b/routers/repo/release.go @@ -88,10 +88,10 @@ func Releases(ctx *middleware.Context) { tags[i] = &models.Release{ Title: rawTag, TagName: rawTag, - Sha1: commit.Id.String(), + Sha1: commit.ID.String(), } - tags[i].NumCommits, err = ctx.Repo.GitRepo.CommitsCount(commit.Id.String()) + tags[i].NumCommits, err = ctx.Repo.GitRepo.CommitsCount(commit.ID.String()) if err != nil { ctx.Handle(500, "CommitsCount", err) return @@ -190,7 +190,7 @@ func NewReleasePost(ctx *middleware.Context, form auth.NewReleaseForm) { Title: form.Title, TagName: form.TagName, Target: form.Target, - Sha1: commit.Id.String(), + Sha1: commit.ID.String(), NumCommits: commitsCount, Note: form.Content, IsDraft: len(form.Draft) > 0, diff --git a/routers/repo/repo.go b/routers/repo/repo.go index dab9dba1..a13526ed 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -317,7 +317,7 @@ func Download(ctx *middleware.Context) { return } - archivePath = path.Join(archivePath, base.ShortSha(commit.Id.String())+ext) + archivePath = path.Join(archivePath, base.ShortSha(commit.ID.String())+ext) if !com.IsFile(archivePath) { if err := commit.CreateArchive(archivePath, archiveType); err != nil { ctx.Handle(500, "Download -> CreateArchive "+archivePath, err) @@ -325,5 +325,5 @@ func Download(ctx *middleware.Context) { } } - ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+"-"+base.ShortSha(commit.Id.String())+ext) + ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+"-"+base.ShortSha(commit.ID.String())+ext) } diff --git a/routers/repo/view.go b/routers/repo/view.go index 2a36db6b..bc05b680 100644 --- a/routers/repo/view.go +++ b/routers/repo/view.go @@ -151,7 +151,7 @@ func Home(ctx *middleware.Context) { ctx.Handle(500, "GetCommitOfRelPath", err) return } - files = append(files, []interface{}{te, git.NewSubModuleFile(c, smUrl, te.Id.String())}) + files = append(files, []interface{}{te, git.NewSubModuleFile(c, smUrl, te.ID.String())}) } } ctx.Data["Files"] = files -- cgit v1.2.3 From 902b5784659327a61ba7de56ef1885fcc6549b17 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Fri, 20 Nov 2015 01:18:50 -0500 Subject: better escape char handle --- .bra.toml | 1 - models/git_diff.go | 10 +++++----- modules/git/tree.go | 18 +++++++++--------- 3 files changed, 14 insertions(+), 15 deletions(-) (limited to 'models/git_diff.go') diff --git a/.bra.toml b/.bra.toml index f1da310a..7d49786d 100644 --- a/.bra.toml +++ b/.bra.toml @@ -13,7 +13,6 @@ watch_dirs = [ watch_exts = [".go"] build_delay = 1500 cmds = [ - ["go", "install"], ["go", "install", "-race"], # sqlite redis memcache cert pam tidb ["go", "build", "-race"], ["./gogs", "web"] diff --git a/models/git_diff.go b/models/git_diff.go index 4cc9ebf8..4b1ec09e 100644 --- a/models/git_diff.go +++ b/models/git_diff.go @@ -163,10 +163,10 @@ func ParsePatch(pid int64, maxlines int, cmd *exec.Cmd, reader io.Reader) (*Diff if strings.HasPrefix(line, DIFF_HEAD) { middle := -1 - // Note: In case file name is surrounded by double quotes(it happens only in git-shell). - hasQuote := strings.Index(line, `\"`) > -1 + // Note: In case file name is surrounded by double quotes (it happens only in git-shell). + // e.g. diff --git "a/xxx" "b/xxx" + hasQuote := line[len(DIFF_HEAD)] == '"' if hasQuote { - line = strings.Replace(line, `\"`, `"`, -1) middle = strings.Index(line, ` "b/`) } else { middle = strings.Index(line, " b/") @@ -176,8 +176,8 @@ func ParsePatch(pid int64, maxlines int, cmd *exec.Cmd, reader io.Reader) (*Diff a := line[beg+2 : middle] b := line[middle+3:] if hasQuote { - a = a[1 : len(a)-1] - b = b[1 : len(b)-1] + a = string(git.UnescapeChars([]byte(a[1 : len(a)-1]))) + b = string(git.UnescapeChars([]byte(b[1 : len(b)-1]))) } curFile = &DiffFile{ diff --git a/modules/git/tree.go b/modules/git/tree.go index 6cfdbf47..1a561cf5 100644 --- a/modules/git/tree.go +++ b/modules/git/tree.go @@ -30,7 +30,7 @@ type Tree struct { var escapeChar = []byte("\\") -func unescapeChars(in []byte) []byte { +func UnescapeChars(in []byte) []byte { if bytes.Index(in, escapeChar) == -1 { return in } @@ -39,12 +39,11 @@ func unescapeChars(in []byte) []byte { isEscape := false out := make([]byte, 0, endIdx+1) for i := range in { - if in[i] == '\\' && i != endIdx { - isEscape = !isEscape - if isEscape { - continue - } + if in[i] == '\\' && !isEscape { + isEscape = true + continue } + isEscape = false out = append(out, in[i]) } return out @@ -92,11 +91,12 @@ func parseTreeData(tree *Tree, data []byte) ([]*TreeEntry, error) { pos += step + 1 // Skip half of sha1. step = bytes.IndexByte(data[pos:], '\n') - entry.name = string(data[pos : pos+step]) // In case entry name is surrounded by double quotes(it happens only in git-shell). - if entry.name[0] == '"' { - entry.name = string(unescapeChars(data[pos+1 : pos+step-1])) + if data[pos] == '"' { + entry.name = string(UnescapeChars(data[pos+1 : pos+step-1])) + } else { + entry.name = string(data[pos : pos+step]) } pos += step + 1 -- cgit v1.2.3