diff options
author | 无闻 <joe2010xtmf@163.com> | 2014-09-01 20:02:34 -0700 |
---|---|---|
committer | 无闻 <joe2010xtmf@163.com> | 2014-09-01 20:02:34 -0700 |
commit | 85f375433dfb2114477b84350c67e80363747e21 (patch) | |
tree | 87ca44e3ed68fe111d16be285f17ff2e52f846b4 /models/git_diff.go | |
parent | d55c5b9e289c0c97aa51ffe5cb5b77f703cc2a47 (diff) | |
parent | af0741da07ec190804fff2a84c3813fc62a1c3ba (diff) |
Merge pull request #416 from compressed/compare_commits
Add commit compare functionality
Diffstat (limited to 'models/git_diff.go')
-rw-r--r-- | models/git_diff.go | 25 |
1 files changed, 17 insertions, 8 deletions
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) +} |