GetDiffShortStat counts number of changed files, number of additions and deletions.
( ctx context.Context, repoPath string, ignoreWhitespace bool, args ...string, )
| 666 | |
| 667 | // GetDiffShortStat counts number of changed files, number of additions and deletions. |
| 668 | func GetDiffShortStat( |
| 669 | ctx context.Context, |
| 670 | repoPath string, |
| 671 | ignoreWhitespace bool, |
| 672 | args ...string, |
| 673 | ) (DiffShortStat, error) { |
| 674 | // Now if we call: |
| 675 | // $ git diff --shortstat 1ebb35b98889ff77299f24d82da426b434b0cca0...788b8b1440462d477f45b0088875 |
| 676 | // we get: |
| 677 | // " 9902 files changed, 2034198 insertions(+), 298800 deletions(-)\n" |
| 678 | |
| 679 | cmd := command.New("diff", |
| 680 | command.WithFlag("--shortstat"), |
| 681 | command.WithArg(args...), |
| 682 | ) |
| 683 | |
| 684 | if ignoreWhitespace { |
| 685 | // Ignore whitespace when comparing lines. |
| 686 | cmd.Add(command.WithFlag("-w")) |
| 687 | } |
| 688 | |
| 689 | stdout := &bytes.Buffer{} |
| 690 | if err := cmd.Run(ctx, |
| 691 | command.WithDir(repoPath), |
| 692 | command.WithStdout(stdout), |
| 693 | ); err != nil { |
| 694 | return DiffShortStat{}, err |
| 695 | } |
| 696 | |
| 697 | return parseDiffStat(stdout.String()) |
| 698 | } |
| 699 | |
| 700 | var shortStatFormat = regexp.MustCompile( |
| 701 | `\s*(\d+) files? changed(?:, (\d+) insertions?\(\+\))?(?:, (\d+) deletions?\(-\))?`) |
no test coverage detected
searching dependent graphs…