summaryrefslogtreecommitdiffstats
path: root/models/git_diff.go
diff options
context:
space:
mode:
authornemoinho <felix@nehrke.info>2018-08-14 19:49:33 +0200
committertechknowlogick <techknowlogick@users.noreply.github.com>2018-08-14 13:49:33 -0400
commitca112f0a04ea7f4fdb8e6dc1e83e293a598abc50 (patch)
tree419042b6b41dccc68030df68f8112d08f3a6b1c0 /models/git_diff.go
parent03e558c29b55c522c52608065d2b688f8a9a4bc3 (diff)
downloadgitea-ca112f0a04ea7f4fdb8e6dc1e83e293a598abc50.tar.gz
gitea-ca112f0a04ea7f4fdb8e6dc1e83e293a598abc50.zip
Add whitespace handling to PR-comparsion (#4683)
* Add whitespace handling to PR-comparsion In a PR we have to keep an eye on a lot of different things. But sometimes the bare code is the key-thing we want to care about and just don't want to care about fixed indention on some places. Especially if we follow the pathfinder rule we face a lot of these situations because these changes don't break the code in many languages but improve the readability a lot. So this change introduce a fine graned button to adjust the way how the reviewer want to see whitespace-changes within the code. The possibilities reflect the possibilities from git itself except of the `--ignore-blank-lines` flag because that one is also handled by `-b` and is really rare. Signed-off-by: Felix Nehrke <felix@nehrke.info>
Diffstat (limited to 'models/git_diff.go')
-rw-r--r--models/git_diff.go31
1 files changed, 21 insertions, 10 deletions
diff --git a/models/git_diff.go b/models/git_diff.go
index 006238cd06..d288ea50b1 100644
--- a/models/git_diff.go
+++ b/models/git_diff.go
@@ -633,6 +633,13 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D
// passing the empty string as beforeCommitID returns a diff from the
// parent commit.
func GetDiffRange(repoPath, beforeCommitID, afterCommitID string, maxLines, maxLineCharacters, maxFiles int) (*Diff, error) {
+ return GetDiffRangeWithWhitespaceBehavior(repoPath, beforeCommitID, afterCommitID, maxLines, maxLineCharacters, maxFiles, "")
+}
+
+// GetDiffRangeWithWhitespaceBehavior builds a Diff between two commits of a repository.
+// Passing the empty string as beforeCommitID returns a diff from the parent commit.
+// The whitespaceBehavior is either an empty string or a git flag
+func GetDiffRangeWithWhitespaceBehavior(repoPath, beforeCommitID, afterCommitID string, maxLines, maxLineCharacters, maxFiles int, whitespaceBehavior string) (*Diff, error) {
gitRepo, err := git.OpenRepository(repoPath)
if err != nil {
return nil, err
@@ -644,17 +651,21 @@ func GetDiffRange(repoPath, beforeCommitID, afterCommitID string, maxLines, maxL
}
var cmd *exec.Cmd
- // if "after" commit given
- if len(beforeCommitID) == 0 {
- // First commit of repository.
- if commit.ParentCount() == 0 {
- cmd = exec.Command("git", "show", afterCommitID)
- } else {
- c, _ := commit.Parent(0)
- cmd = exec.Command("git", "diff", "-M", c.ID.String(), afterCommitID)
- }
+ if len(beforeCommitID) == 0 && commit.ParentCount() == 0 {
+ cmd = exec.Command("git", "show", afterCommitID)
} else {
- cmd = exec.Command("git", "diff", "-M", beforeCommitID, afterCommitID)
+ actualBeforeCommitID := beforeCommitID
+ if len(actualBeforeCommitID) == 0 {
+ parentCommit, _ := commit.Parent(0)
+ actualBeforeCommitID = parentCommit.ID.String()
+ }
+ diffArgs := []string{"diff", "-M"}
+ if len(whitespaceBehavior) != 0 {
+ diffArgs = append(diffArgs, whitespaceBehavior)
+ }
+ diffArgs = append(diffArgs, actualBeforeCommitID)
+ diffArgs = append(diffArgs, afterCommitID)
+ cmd = exec.Command("git", diffArgs...)
}
cmd.Dir = repoPath
cmd.Stderr = os.Stderr