diff options
Diffstat (limited to 'models/git_diff.go')
-rw-r--r-- | models/git_diff.go | 48 |
1 files changed, 46 insertions, 2 deletions
diff --git a/models/git_diff.go b/models/git_diff.go index 33a63295f0..9cec5f62b6 100644 --- a/models/git_diff.go +++ b/models/git_diff.go @@ -422,6 +422,50 @@ func GetDiffRange(repoPath, beforeCommitID string, afterCommitID string, maxLine return diff, nil } -func GetDiffCommit(repoPath, commitId string, maxLines, maxLineCharacteres, maxFiles int) (*Diff, error) { - return GetDiffRange(repoPath, "", commitId, maxLines, maxLineCharacteres, maxFiles) +func GetRawDiff(repoPath, commitID, diffType string) (string, error) { + repo, err := git.OpenRepository(repoPath) + if err != nil { + return "", err + } + + commit, err := repo.GetCommit(commitID) + if err != nil { + return "", err + } + + var cmd *exec.Cmd + switch diffType { + case "diff": + if commit.ParentCount() == 0 { + cmd = exec.Command("git", "show", commitID) + } else { + c, _ := commit.Parent(0) + cmd = exec.Command("git", "diff", "-M", c.ID.String(), commitID) + } + case "patch": + if commit.ParentCount() == 0 { + cmd = exec.Command("git", "format-patch", "--no-signature", "--stdout", "--root", commitID) + } else { + c, _ := commit.Parent(0) + query := fmt.Sprintf("%s...%s", commitID, c.ID.String()) + cmd = exec.Command("git", "format-patch", "--no-signature", "--stdout", query) + } + default: + return "", fmt.Errorf("Invalid diffType '%s'", diffType) + } + + stderr := new(bytes.Buffer) + + cmd.Dir = repoPath + cmd.Stderr = stderr + + stdout, err := cmd.Output() + if err != nil { + return "", fmt.Errorf("%v - %s", err, stderr) + } + return string(stdout), nil +} + +func GetDiffCommit(repoPath, commitID string, maxLines, maxLineCharacteres, maxFiles int) (*Diff, error) { + return GetDiffRange(repoPath, "", commitID, maxLines, maxLineCharacteres, maxFiles) } |