GetDiff returns the git diff output for the working directory. It shows the diff between the current branch and its merge base with the default branch. If on main/master or if merge-base fails, it shows the last few commits' diff.
(dir string)
| 73 | // It shows the diff between the current branch and its merge base with the default branch. |
| 74 | // If on main/master or if merge-base fails, it shows the last few commits' diff. |
| 75 | func GetDiff(dir string) (string, error) { |
| 76 | branch, err := GetCurrentBranch(dir) |
| 77 | if err != nil { |
| 78 | return "", err |
| 79 | } |
| 80 | |
| 81 | // If on a feature branch, diff against merge-base with main/master |
| 82 | if !IsProtectedBranch(branch) { |
| 83 | baseBranch, err := GetDefaultBranch(dir) |
| 84 | if err == nil && baseBranch != "" { |
| 85 | mergeBase, err := getMergeBase(dir, baseBranch, "HEAD") |
| 86 | if err == nil && mergeBase != "" { |
| 87 | return getDiffOutput(dir, mergeBase, "HEAD") |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // Fallback: show diff of recent commits (last 10) |
| 93 | return getDiffOutput(dir, "HEAD~10", "HEAD") |
| 94 | } |
| 95 | |
| 96 | // GetDiffStats returns a short diffstat summary. |
| 97 | func GetDiffStats(dir string) (string, error) { |
no test coverage detected