1
0
Fork 0
forked from forgejo/forgejo

add commit compare functionality

This commit is contained in:
Christopher Brickley 2014-08-26 08:20:18 -04:00
parent d55c5b9e28
commit 00a864e693
11 changed files with 169 additions and 66 deletions

View file

@ -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)
}