DiffCut parses full file git diff output and returns lines specified with the parameters. The purpose of this function is to get diff data with which code comments could be generated. nolint:gocognit
( ctx context.Context, repoPath string, targetRef string, sourceRef string, path string, ignoreWhitespace bool, params parser.DiffCutParams, )
| 389 | // |
| 390 | //nolint:gocognit |
| 391 | func (g *Git) DiffCut( |
| 392 | ctx context.Context, |
| 393 | repoPath string, |
| 394 | targetRef string, |
| 395 | sourceRef string, |
| 396 | path string, |
| 397 | ignoreWhitespace bool, |
| 398 | params parser.DiffCutParams, |
| 399 | ) (parser.HunkHeader, parser.Hunk, error) { |
| 400 | if repoPath == "" { |
| 401 | return parser.HunkHeader{}, parser.Hunk{}, ErrRepositoryPathEmpty |
| 402 | } |
| 403 | |
| 404 | // first fetch the list of the changed files |
| 405 | |
| 406 | pipeRead, pipeWrite := io.Pipe() |
| 407 | go func() { |
| 408 | var err error |
| 409 | |
| 410 | defer func() { |
| 411 | // If running of the command below fails, make the pipe reader also fail with the same error. |
| 412 | _ = pipeWrite.CloseWithError(err) |
| 413 | }() |
| 414 | |
| 415 | cmd := command.New("diff", |
| 416 | command.WithFlag("--raw"), |
| 417 | command.WithFlag("--merge-base"), |
| 418 | command.WithFlag("-z"), |
| 419 | command.WithFlag("--find-renames"), |
| 420 | command.WithArg(targetRef), |
| 421 | command.WithArg(sourceRef)) |
| 422 | if ignoreWhitespace { |
| 423 | // Ignore whitespace when comparing lines. |
| 424 | cmd.Add(command.WithFlag("-w")) |
| 425 | } |
| 426 | err = cmd.Run(ctx, command.WithDir(repoPath), command.WithStdout(pipeWrite)) |
| 427 | }() |
| 428 | |
| 429 | diffEntries, err := parser.DiffRaw(pipeRead) |
| 430 | if err != nil { |
| 431 | return parser.HunkHeader{}, parser.Hunk{}, fmt.Errorf("failed to find the list of changed files: %w", err) |
| 432 | } |
| 433 | |
| 434 | var oldSHA, newSHA string |
| 435 | |
| 436 | for _, entry := range diffEntries { |
| 437 | if entry.Status == parser.DiffStatusRenamed || entry.Status == parser.DiffStatusCopied { |
| 438 | // Entries with the status 'R' and 'C' output two paths: the old path and the new path. |
| 439 | // Using the params.LineStartNew flag to match the path with the entry's old or new path. |
| 440 | |
| 441 | if entry.Path != path && entry.OldPath != path { |
| 442 | continue |
| 443 | } |
| 444 | |
| 445 | if params.LineStartNew && path == entry.OldPath { |
| 446 | msg := "for renamed files provide the new file name if commenting the changed lines" |
| 447 | return parser.HunkHeader{}, parser.Hunk{}, errors.InvalidArgument(msg) |
| 448 | } |
nothing calls this directly
no test coverage detected