Merge pull request 'agentws: check out --branch from origin when it exists' (#17) from benvin/agentws-branch-checkout into main

Reviewed-on: #17
This commit was merged in pull request #17.
This commit is contained in:
2026-09-19 16:36:45 +10:00
5 changed files with 319 additions and 13 deletions
+31
View File
@@ -109,6 +109,37 @@ func GitRemoteBranchExists(repoDir, remote, branch string) bool {
return err == nil
}
// GitRevParse resolves ref to a full object id in repoDir.
func GitRevParse(repoDir, ref string) (string, error) {
return runGit(repoDir, "rev-parse", ref)
}
// GitAheadCount counts commits reachable from head that upstream does not hold.
func GitAheadCount(repoDir, upstream, head string) (int, error) {
out, err := runGit(repoDir, "rev-list", "--count", upstream+".."+head)
if err != nil {
return 0, err
}
n, err := strconv.Atoi(strings.TrimSpace(out))
if err != nil {
return 0, fmt.Errorf("parse rev-list count %q: %w", out, err)
}
return n, nil
}
// GitMergeFFOnly advances the branch checked out at dir to ref, failing rather
// than writing a merge commit when the move is not a fast-forward.
func GitMergeFFOnly(dir, ref string) error {
_, err := runGit(dir, "merge", "--ff-only", ref)
return err
}
// GitSetUpstream points branch at the remote-tracking ref upstream.
func GitSetUpstream(repoDir, branch, upstream string) error {
_, err := runGit(repoDir, "branch", "--set-upstream-to="+upstream, branch)
return err
}
// GitIsDirty reports whether the checkout at dir has uncommitted or untracked
// changes.
func GitIsDirty(dir string) (bool, error) {