aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkolaente <konrad@kola-entertainments.de>2018-10-18 03:57:35 +0200
committerLunny Xiao <xiaolunwen@gmail.com>2018-10-18 09:57:35 +0800
commit49d666f99ab0487d3446f7e515a6e079b6951313 (patch)
tree993f1aa4e513f0b7a1bd2e598be5fa0fea9bf7ea
parent1972383216643dcb01499011f3632a16f3d81611 (diff)
downloadgitea-49d666f99ab0487d3446f7e515a6e079b6951313.tar.gz
gitea-49d666f99ab0487d3446f7e515a6e079b6951313.zip
Fix regex to support optional end line of old section in diff hunk (#5097)
+ Named groups in reges for easier group parsing
-rw-r--r--models/git_diff.go25
1 files changed, 16 insertions, 9 deletions
diff --git a/models/git_diff.go b/models/git_diff.go
index 44e7291c1f..2a8019995d 100644
--- a/models/git_diff.go
+++ b/models/git_diff.go
@@ -273,7 +273,7 @@ func (diff *Diff) NumFiles() int {
}
// Example: @@ -1,8 +1,9 @@ => [..., 1, 8, 1, 9]
-var hunkRegex = regexp.MustCompile(`^@@ -([0-9]+),([0-9]+) \+([0-9]+)(,([0-9]+))? @@`)
+var hunkRegex = regexp.MustCompile(`^@@ -(?P<beginOld>[0-9]+)(,(?P<endOld>[0-9]+))? \+(?P<beginNew>[0-9]+)(,(?P<endNew>[0-9]+))? @@`)
func isHeader(lof string) bool {
return strings.HasPrefix(lof, cmdDiffHead) || strings.HasPrefix(lof, "---") || strings.HasPrefix(lof, "+++")
@@ -311,21 +311,28 @@ func CutDiffAroundLine(originalDiff io.Reader, line int64, old bool, numbersOfLi
if len(hunk) > headerLines {
break
}
- groups := hunkRegex.FindStringSubmatch(lof)
+ // A map with named groups of our regex to recognize them later more easily
+ submatches := hunkRegex.FindStringSubmatch(lof)
+ groups := make(map[string]string)
+ for i, name := range hunkRegex.SubexpNames() {
+ if i != 0 && name != "" {
+ groups[name] = submatches[i]
+ }
+ }
if old {
- begin = com.StrTo(groups[1]).MustInt64()
- end = com.StrTo(groups[2]).MustInt64()
+ begin = com.StrTo(groups["beginOld"]).MustInt64()
+ end = com.StrTo(groups["endOld"]).MustInt64()
// init otherLine with begin of opposite side
- otherLine = com.StrTo(groups[3]).MustInt64()
+ otherLine = com.StrTo(groups["beginNew"]).MustInt64()
} else {
- begin = com.StrTo(groups[3]).MustInt64()
- if groups[5] != "" {
- end = com.StrTo(groups[5]).MustInt64()
+ begin = com.StrTo(groups["beginNew"]).MustInt64()
+ if groups["endNew"] != "" {
+ end = com.StrTo(groups["endNew"]).MustInt64()
} else {
end = 0
}
// init otherLine with begin of opposite side
- otherLine = com.StrTo(groups[1]).MustInt64()
+ otherLine = com.StrTo(groups["beginOld"]).MustInt64()
}
end += begin // end is for real only the number of lines in hunk
// lof is between begin and end