github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/git/diff.go (about) 1 // Copyright 2023 The GitBundle Inc. All rights reserved. 2 // Copyright 2017 The Gitea Authors. All rights reserved. 3 // Use of this source code is governed by a MIT-style 4 // license that can be found in the LICENSE file. 5 6 package git 7 8 import ( 9 "bufio" 10 "bytes" 11 "context" 12 "fmt" 13 "io" 14 "os" 15 "regexp" 16 "strconv" 17 "strings" 18 19 "github.com/gitbundle/modules/log" 20 ) 21 22 // RawDiffType type of a raw diff. 23 type RawDiffType string 24 25 // RawDiffType possible values. 26 const ( 27 RawDiffNormal RawDiffType = "diff" 28 RawDiffPatch RawDiffType = "patch" 29 ) 30 31 // GetRawDiff dumps diff results of repository in given commit ID to io.Writer. 32 func GetRawDiff(repo *Repository, commitID string, diffType RawDiffType, writer io.Writer) error { 33 return GetRepoRawDiffForFile(repo, "", commitID, diffType, "", writer) 34 } 35 36 // GetReverseRawDiff dumps the reverse diff results of repository in given commit ID to io.Writer. 37 func GetReverseRawDiff(ctx context.Context, repoPath, commitID string, writer io.Writer) error { 38 stderr := new(bytes.Buffer) 39 cmd := NewCommand(ctx, "show", "--pretty=format:revert %H%n", "-R", commitID) 40 if err := cmd.Run(&RunOpts{ 41 Dir: repoPath, 42 Stdout: writer, 43 Stderr: stderr, 44 }); err != nil { 45 return fmt.Errorf("Run: %v - %s", err, stderr) 46 } 47 return nil 48 } 49 50 // GetRepoRawDiffForFile dumps diff results of file in given commit ID to io.Writer according given repository 51 func GetRepoRawDiffForFile(repo *Repository, startCommit, endCommit string, diffType RawDiffType, file string, writer io.Writer) error { 52 commit, err := repo.GetCommit(endCommit) 53 if err != nil { 54 return err 55 } 56 fileArgs := make([]string, 0) 57 if len(file) > 0 { 58 fileArgs = append(fileArgs, "--", file) 59 } 60 61 var args []string 62 switch diffType { 63 case RawDiffNormal: 64 if len(startCommit) != 0 { 65 args = append([]string{"diff", "-M", startCommit, endCommit}, fileArgs...) 66 } else if commit.ParentCount() == 0 { 67 args = append([]string{"show", endCommit}, fileArgs...) 68 } else { 69 c, _ := commit.Parent(0) 70 args = append([]string{"diff", "-M", c.ID.String(), endCommit}, fileArgs...) 71 } 72 case RawDiffPatch: 73 if len(startCommit) != 0 { 74 query := fmt.Sprintf("%s...%s", endCommit, startCommit) 75 args = append([]string{"format-patch", "--no-signature", "--stdout", "--root", query}, fileArgs...) 76 } else if commit.ParentCount() == 0 { 77 args = append([]string{"format-patch", "--no-signature", "--stdout", "--root", endCommit}, fileArgs...) 78 } else { 79 c, _ := commit.Parent(0) 80 query := fmt.Sprintf("%s...%s", endCommit, c.ID.String()) 81 args = append([]string{"format-patch", "--no-signature", "--stdout", query}, fileArgs...) 82 } 83 default: 84 return fmt.Errorf("invalid diffType: %s", diffType) 85 } 86 87 stderr := new(bytes.Buffer) 88 cmd := NewCommand(repo.Ctx, args...) 89 if err = cmd.Run(&RunOpts{ 90 Dir: repo.Path, 91 Stdout: writer, 92 Stderr: stderr, 93 }); err != nil { 94 return fmt.Errorf("Run: %v - %s", err, stderr) 95 } 96 return nil 97 } 98 99 // ParseDiffHunkString parse the diffhunk content and return 100 func ParseDiffHunkString(diffhunk string) (leftLine, leftHunk, rightLine, righHunk int) { 101 ss := strings.Split(diffhunk, "@@") 102 ranges := strings.Split(ss[1][1:], " ") 103 leftRange := strings.Split(ranges[0], ",") 104 leftLine, _ = strconv.Atoi(leftRange[0][1:]) 105 if len(leftRange) > 1 { 106 leftHunk, _ = strconv.Atoi(leftRange[1]) 107 } 108 if len(ranges) > 1 { 109 rightRange := strings.Split(ranges[1], ",") 110 rightLine, _ = strconv.Atoi(rightRange[0]) 111 if len(rightRange) > 1 { 112 righHunk, _ = strconv.Atoi(rightRange[1]) 113 } 114 } else { 115 log.Debug("Parse line number failed: %v", diffhunk) 116 rightLine = leftLine 117 righHunk = leftHunk 118 } 119 return 120 } 121 122 // Example: @@ -1,8 +1,9 @@ => [..., 1, 8, 1, 9] 123 var hunkRegex = regexp.MustCompile(`^@@ -(?P<beginOld>[0-9]+)(,(?P<endOld>[0-9]+))? \+(?P<beginNew>[0-9]+)(,(?P<endNew>[0-9]+))? @@`) 124 125 const cmdDiffHead = "diff --git " 126 127 func isHeader(lof string, inHunk bool) bool { 128 return strings.HasPrefix(lof, cmdDiffHead) || (!inHunk && (strings.HasPrefix(lof, "---") || strings.HasPrefix(lof, "+++"))) 129 } 130 131 // CutDiffAroundLine cuts a diff of a file in way that only the given line + numberOfLine above it will be shown 132 // it also recalculates hunks and adds the appropriate headers to the new diff. 133 // Warning: Only one-file diffs are allowed. 134 func CutDiffAroundLine(originalDiff io.Reader, line int64, old bool, numbersOfLine int) (string, error) { 135 if line == 0 || numbersOfLine == 0 { 136 // no line or num of lines => no diff 137 return "", nil 138 } 139 140 scanner := bufio.NewScanner(originalDiff) 141 hunk := make([]string, 0) 142 143 // begin is the start of the hunk containing searched line 144 // end is the end of the hunk ... 145 // currentLine is the line number on the side of the searched line (differentiated by old) 146 // otherLine is the line number on the opposite side of the searched line (differentiated by old) 147 var begin, end, currentLine, otherLine int64 148 var headerLines int 149 150 inHunk := false 151 152 for scanner.Scan() { 153 lof := scanner.Text() 154 // Add header to enable parsing 155 156 if isHeader(lof, inHunk) { 157 if strings.HasPrefix(lof, cmdDiffHead) { 158 inHunk = false 159 } 160 hunk = append(hunk, lof) 161 headerLines++ 162 } 163 if currentLine > line { 164 break 165 } 166 // Detect "hunk" with contains commented lof 167 if strings.HasPrefix(lof, "@@") { 168 inHunk = true 169 // Already got our hunk. End of hunk detected! 170 if len(hunk) > headerLines { 171 break 172 } 173 // A map with named groups of our regex to recognize them later more easily 174 submatches := hunkRegex.FindStringSubmatch(lof) 175 groups := make(map[string]string) 176 for i, name := range hunkRegex.SubexpNames() { 177 if i != 0 && name != "" { 178 groups[name] = submatches[i] 179 } 180 } 181 if old { 182 begin, _ = strconv.ParseInt(groups["beginOld"], 10, 64) 183 end, _ = strconv.ParseInt(groups["endOld"], 10, 64) 184 // init otherLine with begin of opposite side 185 otherLine, _ = strconv.ParseInt(groups["beginNew"], 10, 64) 186 } else { 187 begin, _ = strconv.ParseInt(groups["beginNew"], 10, 64) 188 if groups["endNew"] != "" { 189 end, _ = strconv.ParseInt(groups["endNew"], 10, 64) 190 } else { 191 end = 0 192 } 193 // init otherLine with begin of opposite side 194 otherLine, _ = strconv.ParseInt(groups["beginOld"], 10, 64) 195 } 196 end += begin // end is for real only the number of lines in hunk 197 // lof is between begin and end 198 if begin <= line && end >= line { 199 hunk = append(hunk, lof) 200 currentLine = begin 201 continue 202 } 203 } else if len(hunk) > headerLines { 204 hunk = append(hunk, lof) 205 // Count lines in context 206 switch lof[0] { 207 case '+': 208 if !old { 209 currentLine++ 210 } else { 211 otherLine++ 212 } 213 case '-': 214 if old { 215 currentLine++ 216 } else { 217 otherLine++ 218 } 219 case '\\': 220 // FIXME: handle `\ No newline at end of file` 221 default: 222 currentLine++ 223 otherLine++ 224 } 225 } 226 } 227 if err := scanner.Err(); err != nil { 228 return "", err 229 } 230 231 // No hunk found 232 if currentLine == 0 { 233 return "", nil 234 } 235 // headerLines + hunkLine (1) = totalNonCodeLines 236 if len(hunk)-headerLines-1 <= numbersOfLine { 237 // No need to cut the hunk => return existing hunk 238 return strings.Join(hunk, "\n"), nil 239 } 240 var oldBegin, oldNumOfLines, newBegin, newNumOfLines int64 241 if old { 242 oldBegin = currentLine 243 newBegin = otherLine 244 } else { 245 oldBegin = otherLine 246 newBegin = currentLine 247 } 248 // headers + hunk header 249 newHunk := make([]string, headerLines) 250 // transfer existing headers 251 copy(newHunk, hunk[:headerLines]) 252 // transfer last n lines 253 newHunk = append(newHunk, hunk[len(hunk)-numbersOfLine-1:]...) 254 // calculate newBegin, ... by counting lines 255 for i := len(hunk) - 1; i >= len(hunk)-numbersOfLine; i-- { 256 switch hunk[i][0] { 257 case '+': 258 newBegin-- 259 newNumOfLines++ 260 case '-': 261 oldBegin-- 262 oldNumOfLines++ 263 default: 264 oldBegin-- 265 newBegin-- 266 newNumOfLines++ 267 oldNumOfLines++ 268 } 269 } 270 // construct the new hunk header 271 newHunk[headerLines] = fmt.Sprintf("@@ -%d,%d +%d,%d @@", 272 oldBegin, oldNumOfLines, newBegin, newNumOfLines) 273 return strings.Join(newHunk, "\n"), nil 274 } 275 276 // GetAffectedFiles returns the affected files between two commits 277 func GetAffectedFiles(repo *Repository, oldCommitID, newCommitID string, env []string) ([]string, error) { 278 stdoutReader, stdoutWriter, err := os.Pipe() 279 if err != nil { 280 log.Error("Unable to create os.Pipe for %s", repo.Path) 281 return nil, err 282 } 283 defer func() { 284 _ = stdoutReader.Close() 285 _ = stdoutWriter.Close() 286 }() 287 288 affectedFiles := make([]string, 0, 32) 289 290 // Run `git diff --name-only` to get the names of the changed files 291 err = NewCommand(repo.Ctx, "diff", "--name-only", oldCommitID, newCommitID). 292 Run(&RunOpts{ 293 Env: env, 294 Dir: repo.Path, 295 Stdout: stdoutWriter, 296 PipelineFunc: func(ctx context.Context, cancel context.CancelFunc) error { 297 // Close the writer end of the pipe to begin processing 298 _ = stdoutWriter.Close() 299 defer func() { 300 // Close the reader on return to terminate the git command if necessary 301 _ = stdoutReader.Close() 302 }() 303 // Now scan the output from the command 304 scanner := bufio.NewScanner(stdoutReader) 305 for scanner.Scan() { 306 path := strings.TrimSpace(scanner.Text()) 307 if len(path) == 0 { 308 continue 309 } 310 affectedFiles = append(affectedFiles, path) 311 } 312 return scanner.Err() 313 }, 314 }) 315 if err != nil { 316 log.Error("Unable to get affected files for commits from %s to %s in %s: %v", oldCommitID, newCommitID, repo.Path, err) 317 } 318 319 return affectedFiles, err 320 }