Bold questions

This commit is contained in:
Ian Gulliver
2026-10-14 19:35:34 -07:00
parent ab4c3d2c6f
commit 1ea055682b
54 changed files with 3696 additions and 762 deletions
+189 -4
View File
@@ -1,9 +1,12 @@
package git
import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
)
@@ -75,12 +78,194 @@ func RemoveWorktree(repo, path, branch string) error {
return err
}
type Unmerged struct {
Commits int
Files int
Insertions int
Deletions int
Uncommitted int
}
type ConflictError struct {
Files []string
}
func (e *ConflictError) Error() string {
return "merge conflicts in " + strings.Join(e.Files, ", ")
}
func UnmergedChanges(worktree, target string) (Unmerged, error) {
u := Unmerged{}
count, err := run(worktree, "rev-list", "--count", target+"..HEAD")
if err != nil {
return u, err
}
u.Commits, err = strconv.Atoi(count)
if err != nil {
return u, err
}
status, err := run(worktree, "status", "--porcelain", "--untracked-files=all")
if err != nil {
return u, err
}
if status != "" {
u.Uncommitted = len(strings.Split(status, "\n"))
}
tree, err := Snapshot(worktree)
if err != nil {
return u, err
}
base, err := run(worktree, "merge-base", target, "HEAD")
if err != nil {
return u, err
}
numstat, err := run(worktree, "diff", "--numstat", base, tree)
if err != nil {
return u, err
}
for _, line := range strings.Split(numstat, "\n") {
if line == "" {
continue
}
fields := strings.SplitN(line, "\t", 3)
u.Files++
adds, err := strconv.Atoi(fields[0])
if err != nil {
continue
}
dels, err := strconv.Atoi(fields[1])
if err != nil {
continue
}
u.Insertions += adds
u.Deletions += dels
}
return u, nil
}
func SquashMerge(repo, worktree, target, message string, env []string) (string, error) {
checkedOut, err := run(repo, "symbolic-ref", "--short", "HEAD")
if err != nil {
return "", err
}
if checkedOut != target {
return "", fmt.Errorf("%s has %s checked out, not %s", repo, checkedOut, target)
}
tree, err := Snapshot(worktree)
if err != nil {
return "", err
}
base, err := run(worktree, "merge-base", target, "HEAD")
if err != nil {
return "", err
}
head, err := run(repo, "rev-parse", target)
if err != nil {
return "", err
}
out, stderr, code, err := runCode(repo, nil, "merge-tree", "--write-tree", "--name-only", "--merge-base="+base, head, tree)
if err != nil {
return "", err
}
lines := strings.Split(out, "\n")
if code == 1 {
conflict := &ConflictError{}
for _, l := range lines[1:] {
if l == "" {
break
}
conflict.Files = append(conflict.Files, l)
}
return "", conflict
}
if code != 0 {
return "", fmt.Errorf("git merge-tree exited %d: %s", code, stderr)
}
headTree, err := run(repo, "rev-parse", head+"^{tree}")
if err != nil {
return "", err
}
if lines[0] == headTree {
return "", nil
}
commit, err := runEnv(repo, env, "commit-tree", lines[0], "-p", head, "-m", message)
if err != nil {
return "", err
}
_, err = run(repo, "merge", "--ff-only", commit)
if err != nil {
return "", err
}
return commit, nil
}
func Head(dir string) (string, error) {
return run(dir, "rev-parse", "HEAD")
}
func CommitAll(dir, message, name, email string) (string, error) {
status, err := run(dir, "status", "--porcelain", "--untracked-files=all")
if err != nil {
return "", err
}
if status == "" {
return "", nil
}
env := []string{
"GIT_AUTHOR_NAME=" + name,
"GIT_AUTHOR_EMAIL=" + email,
"GIT_COMMITTER_NAME=" + name,
"GIT_COMMITTER_EMAIL=" + email,
}
_, err = runEnv(dir, env, "add", "--all")
if err != nil {
return "", err
}
_, err = runEnv(dir, env, "commit", "--quiet", "--message", message)
if err != nil {
return "", err
}
return Head(dir)
}
func run(dir string, args ...string) (string, error) {
return runEnv(dir, nil, args...)
}
func runEnv(dir string, env []string, args ...string) (string, error) {
out, stderr, code, err := runCode(dir, env, args...)
if err != nil {
return "", err
}
if code != 0 {
return "", fmt.Errorf("git %s: exit status %d: %s", strings.Join(args, " "), code, stderr)
}
return out, nil
}
func runCode(dir string, env []string, args ...string) (string, string, int, error) {
cmd := exec.Command("git", args...)
cmd.Dir = dir
out, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("git %s: %w: %s", strings.Join(args, " "), err, strings.TrimSpace(string(out)))
cmd.Env = append(os.Environ(), env...)
stderr := &strings.Builder{}
cmd.Stderr = stderr
out, err := cmd.Output()
exit := &exec.ExitError{}
if errors.As(err, &exit) {
return strings.TrimSpace(string(out)), strings.TrimSpace(stderr.String()), exit.ExitCode(), nil
}
return strings.TrimSpace(string(out)), nil
if err != nil {
return "", "", 0, fmt.Errorf("git %s: %w", strings.Join(args, " "), err)
}
return strings.TrimSpace(string(out)), strings.TrimSpace(stderr.String()), 0, nil
}