| 1 | package gitrepo |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "os/exec" |
| 9 | "strings" |
| 10 | ) |
| 11 | |
| 12 | // IsAncestor zegt of ancestor een voorouder is van descendant, inclusief |
| 13 | // het geval dat beide dezelfde commit zijn. |
| 14 | func IsAncestor(ctx context.Context, gitBin, dir, ancestor, descendant string) bool { |
| 15 | if ancestor == "" || descendant == "" { |
| 16 | return false |
| 17 | } |
| 18 | cmd := exec.CommandContext(ctx, gitBin, "-C", dir, "merge-base", "--is-ancestor", ancestor, descendant) |
| 19 | return cmd.Run() == nil |
| 20 | } |
| 21 | |
| 22 | // Conflicts zegt of ours en theirs niet schoon samen te voegen zijn. |
| 23 | // ours is de doelbranch, theirs de bron. Een ontbrekende revisie is een fout. |
| 24 | func Conflicts(ctx context.Context, gitBin, dir, ours, theirs string) (bool, error) { |
| 25 | cmd := exec.CommandContext(ctx, gitBin, "-C", dir, "merge-tree", "--write-tree", ours, theirs) |
| 26 | out, err := cmd.CombinedOutput() |
| 27 | if err == nil { |
| 28 | return false, nil |
| 29 | } |
| 30 | var exit *exec.ExitError |
| 31 | if errors.As(err, &exit) && exit.ExitCode() == 1 { |
| 32 | return true, nil |
| 33 | } |
| 34 | if errors.As(err, &exit) && exit.ExitCode() == 129 || bytes.Contains(out, []byte("unknown option")) { |
| 35 | return conflictsLegacy(ctx, gitBin, dir, ours, theirs) |
| 36 | } |
| 37 | return false, fmt.Errorf("merge-tree: %w (%s)", err, strings.TrimSpace(string(out))) |
| 38 | } |
| 39 | |
| 40 | func conflictsLegacy(ctx context.Context, gitBin, dir, ours, theirs string) (bool, error) { |
| 41 | base, err := run(ctx, gitBin, nil, "-C", dir, "merge-base", ours, theirs) |
| 42 | if err != nil { |
| 43 | return false, err |
| 44 | } |
| 45 | out, err := run(ctx, gitBin, nil, "-C", dir, "merge-tree", strings.TrimSpace(base), ours, theirs) |
| 46 | if err != nil { |
| 47 | return false, err |
| 48 | } |
| 49 | return strings.Contains(out, "changed in both") || strings.Contains(out, "CONFLICT"), nil |
| 50 | } |