feat(sync): replace hub with an internal branch engine
Fetch and prune the preferred remote, then reconcile every local branch independently. Fast-forward branches that are behind, preserve divergent work, and delete a branch only when its configured upstream is gone and its commits are already contained by the remote default branch. Keep dirty repositories eligible while protecting the active branch and refs checked out in linked worktrees. Preserve the previous all-clean policy behind --strict, expose structured per-branch results, add real bare-remote integration tests, and retain the upstream MIT notice for the adapted hub sync behavior.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
# Stack Sync
|
||||
|
||||
`stack-sync` safely runs [`hub sync`](https://hub.github.com/hub-sync.1.html) across every Git repository in a workspace. It is designed for directory trees such as JezzWTF where the root may not be a repository and a parent repository may contain deliberately untracked, nested repositories.
|
||||
`stack-sync` safely fetches and synchronizes local branches across every Git repository in a workspace. It is designed for directory trees such as JezzWTF where the root may not be a repository and a parent repository may contain deliberately untracked, nested repositories.
|
||||
|
||||
It provides both a full-screen [Bubble Tea](https://github.com/charmbracelet/bubbletea) control deck for interactive work and a conventional CLI/JSON interface for SSH sessions and automation. Both interfaces use the same discovery and safety engine.
|
||||
|
||||
@@ -10,24 +10,24 @@ Before a repository is eligible, Stack Sync verifies that it:
|
||||
|
||||
- is on a branch (not a detached `HEAD`);
|
||||
- has at least one remote;
|
||||
- has no merge, rebase, cherry-pick, revert, or bisect in progress;
|
||||
- has no modified, staged, deleted, conflicted, or untracked files.
|
||||
- has no merge, rebase, cherry-pick, revert, or bisect in progress.
|
||||
|
||||
Untracked paths that are themselves discovered nested Git repositories are excluded from the parent repository's dirty check. All other untracked files still block it.
|
||||
Modified, staged, deleted, conflicted, and untracked files are reported but do not block the repository by default. If the checked-out branch would otherwise be fast-forwarded or deleted, Stack Sync protects that branch and continues synchronizing safe inactive branches. Branches checked out in another linked worktree are protected too. Use `--strict` when every repository must be completely clean before anything is synchronized.
|
||||
|
||||
Stack Sync never runs `git stash`, `git reset`, `git clean`, `git commit`, or any other command that saves or discards work. A repository is checked again immediately before `hub sync` to reduce the chance of a change slipping in between the plan and apply phases. Blocked repositories are skipped; use `--strict` to abort the whole run if even one is blocked.
|
||||
Untracked paths that are themselves discovered nested Git repositories are excluded from the parent repository's dirty report. All other worktree changes remain visible in the CLI, JSON, and TUI.
|
||||
|
||||
Stack Sync intentionally preserves `hub sync` semantics. That means `hub` may delete a local branch when its upstream branch has been deleted and it considers the local branch merged. It warns instead when it finds unpushed or apparently unmerged commits. The interactive confirmation calls this out; review `hub help sync` before using `--yes` in automation.
|
||||
Stack Sync never runs `git stash`, `git reset`, `git clean`, `git commit`, or any other command that saves or discards work. A repository is checked again immediately before synchronization, and the worktree is checked again before changing its checked-out branch.
|
||||
|
||||
The branch behavior is derived from the MIT-licensed `hub sync` implementation and is now built directly into Stack Sync. It fetches and prunes the main remote, fast-forwards outdated branches, warns about divergent/unpushed branches, and removes a branch only when its configured upstream was deleted and the branch is already merged into the remote default branch. It prefers a remote named `upstream`, `github`, or `origin`; a sole differently named remote is also accepted. See [third-party notices](THIRD_PARTY_NOTICES.md) for attribution.
|
||||
|
||||
## Requirements
|
||||
|
||||
Stack Sync supports Linux and Windows. It requires:
|
||||
|
||||
- Go 1.24 or newer to build;
|
||||
- Git on `PATH`;
|
||||
- [hub](https://hub.github.com/) on `PATH` for `sync` and `tui` sync operations.
|
||||
- Git on `PATH`.
|
||||
|
||||
The read-only `scan` command does not require `hub`.
|
||||
There is no separate `hub` dependency.
|
||||
|
||||
## Build and install
|
||||
|
||||
@@ -78,7 +78,7 @@ PowerShell accepts the same options with a Windows path:
|
||||
stack-sync tui --root C:\Users\you\Coding\jwtf
|
||||
```
|
||||
|
||||
Every eligible repository starts selected. Move with the arrow keys or `j`/`k`, toggle the focused repository with Space, select all with `a`, clear the selection with `n`, and refresh with `r`. Press `s` or Enter to review the destructive-branch warning, then `y` to begin. Blocked repositories cannot be selected and show their first dirty paths in the detail panel. After a run, focusing a repository shows its latest `hub` output.
|
||||
Every eligible repository starts selected. Dirty repositories remain selectable and are labelled `ready (dirty)`; their checked-out branches are protected if an update would affect them. Move with the arrow keys or `j`/`k`, toggle the focused repository with Space, select all with `a`, clear the selection with `n`, and refresh with `r`. Press `s` or Enter to review the branch-deletion warning, then `y` to begin. Blocked repositories cannot be selected. After a run, focusing a repository shows its latest per-branch results.
|
||||
|
||||
### CLI and automation
|
||||
|
||||
@@ -100,7 +100,7 @@ Review the same plan, confirm it, and sync every eligible repository:
|
||||
stack-sync sync --root ~/Coding/jwtf
|
||||
```
|
||||
|
||||
For automation, suppress the prompt and optionally require the entire workspace to be clean:
|
||||
For automation, suppress the prompt and optionally require the entire workspace to be clean. In strict mode, either a blocked repository or any worktree change aborts the whole run:
|
||||
|
||||
```bash
|
||||
stack-sync sync --root ~/Coding/jwtf --yes --strict
|
||||
@@ -110,7 +110,7 @@ Useful options:
|
||||
|
||||
```text
|
||||
--jobs 4 maximum concurrent inspections or syncs
|
||||
--timeout 5m per-repository hub sync timeout
|
||||
--timeout 5m per-repository fetch and sync timeout
|
||||
--exclude temp skip a directory name anywhere in the tree
|
||||
--exclude Org/old skip a root-relative path
|
||||
--json emit structured output
|
||||
@@ -120,4 +120,4 @@ Both `/` and `\` are accepted in Windows paths. Quote a root or exclusion contai
|
||||
|
||||
Dependency caches, build outputs, and tool-managed directories such as `node_modules`, `target`, `.claude`, and `.codex` are skipped during discovery by default. These exclusions only affect repository discovery; they never make real changes inside a discovered repository disappear from its dirty-worktree check.
|
||||
|
||||
Exit codes are `0` for a successful scan or sync (planned dirty repositories may be safely skipped), `1` for an operational/sync failure, `2` for invalid or unconfirmed non-interactive use, and `3` when strict mode refuses the plan or a previously eligible repository fails its immediate pre-sync safety check.
|
||||
Exit codes are `0` for a successful scan or sync (including protected branches and divergence warnings), `1` for an operational/sync failure, `2` for invalid or unconfirmed non-interactive use, and `3` when strict mode refuses the plan or a previously eligible repository fails its immediate pre-sync safety check.
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
# Third-party notices
|
||||
|
||||
The branch synchronization behavior in `sync.go` is adapted from the `hub sync` command in [hub](https://github.com/mislav/hub), version 2.14.2.
|
||||
|
||||
## hub
|
||||
|
||||
Copyright (c) 2009 Chris Wanstrath
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
@@ -3,7 +3,6 @@ package main
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
@@ -19,7 +18,7 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
const version = "0.2.0"
|
||||
const version = "0.3.0"
|
||||
|
||||
var defaultSkippedDirs = map[string]bool{
|
||||
".cache": true, ".claude": true, ".codex": true, ".git": true, ".next": true, ".pnpm-store": true,
|
||||
@@ -62,6 +61,7 @@ type syncResult struct {
|
||||
Skipped bool `json:"skipped"`
|
||||
Message string `json:"message,omitempty"`
|
||||
Duration string `json:"duration,omitempty"`
|
||||
Branches []branchSyncResult `json:"branches,omitempty"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
@@ -130,21 +130,17 @@ func run(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
|
||||
return 0
|
||||
}
|
||||
|
||||
if _, err := exec.LookPath("hub"); err != nil {
|
||||
fmt.Fprintln(stderr, "hub is required for sync but was not found on PATH")
|
||||
return 1
|
||||
}
|
||||
if !opts.json {
|
||||
printScan(stdout, root, repos)
|
||||
}
|
||||
eligible := eligibleCount(repos)
|
||||
blocked := blockedCount(repos)
|
||||
if eligible == 0 {
|
||||
fmt.Fprintln(stderr, "nothing to sync: every repository is blocked")
|
||||
return 3
|
||||
}
|
||||
if opts.strict && blocked > 0 {
|
||||
fmt.Fprintf(stderr, "strict mode: refusing to sync because %d repositories are blocked\n", blocked)
|
||||
strictUnsafe := strictUnsafeCount(repos)
|
||||
if opts.strict && strictUnsafe > 0 {
|
||||
fmt.Fprintf(stderr, "strict mode: refusing to sync because %d repositories are blocked or dirty\n", strictUnsafe)
|
||||
return 3
|
||||
}
|
||||
if !opts.yes {
|
||||
@@ -152,7 +148,7 @@ func run(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
|
||||
fmt.Fprintln(stderr, "refusing to sync without confirmation; pass --yes in non-interactive use")
|
||||
return 2
|
||||
}
|
||||
fmt.Fprintf(stdout, "\nRun hub sync in %d clean repositories? hub may delete local branches it considers merged. [y/N] ", eligible)
|
||||
fmt.Fprintf(stdout, "\nSynchronize branches in %d repositories? Merged branches whose upstream was deleted may be removed. [y/N] ", eligible)
|
||||
answer, _ := bufio.NewReader(stdin).ReadString('\n')
|
||||
answer = strings.ToLower(strings.TrimSpace(answer))
|
||||
if answer != "y" && answer != "yes" {
|
||||
@@ -161,7 +157,7 @@ func run(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
|
||||
}
|
||||
}
|
||||
|
||||
results := syncAll(root, repos, opts.jobs, opts.timeout, stdout, opts.json)
|
||||
results := syncAll(root, repos, opts.jobs, opts.timeout, stdout, opts.json, opts.strict)
|
||||
if opts.json {
|
||||
writeJSON(stdout, results)
|
||||
} else {
|
||||
@@ -190,10 +186,10 @@ func parseFlags(command string, args []string, stderr io.Writer) (options, error
|
||||
fs.Var(&opts.exclusions, "exclude", "directory name or root-relative path to skip (repeatable)")
|
||||
if command == "sync" {
|
||||
fs.BoolVar(&opts.yes, "yes", false, "apply without an interactive confirmation")
|
||||
fs.BoolVar(&opts.strict, "strict", false, "abort the entire run if any repository is blocked")
|
||||
fs.BoolVar(&opts.strict, "strict", false, "abort the entire run if any repository is blocked or dirty")
|
||||
}
|
||||
if command == "sync" || command == "tui" {
|
||||
fs.DurationVar(&opts.timeout, "timeout", opts.timeout, "timeout for each hub sync")
|
||||
fs.DurationVar(&opts.timeout, "timeout", opts.timeout, "timeout for each repository sync")
|
||||
}
|
||||
fs.Usage = func() { usage(stderr) }
|
||||
if err := fs.Parse(args); err != nil {
|
||||
@@ -218,7 +214,7 @@ func parseFlags(command string, args []string, stderr io.Writer) (options, error
|
||||
}
|
||||
|
||||
func usage(w io.Writer) {
|
||||
fmt.Fprintln(w, `stack-sync safely runs hub sync across a directory tree.
|
||||
fmt.Fprintln(w, `stack-sync safely synchronizes Git branches across a directory tree.
|
||||
|
||||
Usage:
|
||||
stack-sync [scan] [options]
|
||||
@@ -228,13 +224,14 @@ Usage:
|
||||
|
||||
Commands:
|
||||
scan Discover repositories and report whether each is safe to sync (default)
|
||||
sync Scan, confirm, re-check safety, then run hub sync in eligible repositories
|
||||
sync Scan, confirm, re-check safety, then sync branches in eligible repositories
|
||||
tui Interactively review, select, and sync repositories
|
||||
|
||||
Safety:
|
||||
Repositories with edits, untracked files, detached HEADs, no remotes, or an
|
||||
in-progress Git operation are never synced. Untracked nested repositories do
|
||||
not falsely dirty their parent. stack-sync never stashes, resets, or commits.
|
||||
Dirty worktrees are reported but remain eligible: their checked-out branch is
|
||||
protected while safe inactive branches are synchronized. Detached HEADs,
|
||||
repositories without remotes, and in-progress Git operations remain blocked.
|
||||
stack-sync never stashes, resets, cleans, or commits.
|
||||
|
||||
Use "stack-sync <command> -h" for command options.`)
|
||||
}
|
||||
@@ -336,9 +333,6 @@ func inspect(root, path string, allRepos []string) repo {
|
||||
}
|
||||
r.Dirty = append(r.Dirty, change.code+" "+change.path)
|
||||
}
|
||||
if len(r.Dirty) > 0 {
|
||||
setBlock(&r, fmt.Sprintf("dirty worktree (%d changes)", len(r.Dirty)))
|
||||
}
|
||||
r.Eligible = r.BlockReason == ""
|
||||
return r
|
||||
}
|
||||
@@ -422,11 +416,11 @@ func gitBytes(path string, args ...string) ([]byte, error) {
|
||||
return cmd.Output()
|
||||
}
|
||||
|
||||
func syncAll(root string, repos []repo, jobs int, timeout time.Duration, stdout io.Writer, quiet bool) []syncResult {
|
||||
return syncSelected(root, repos, repos, jobs, timeout, stdout, quiet)
|
||||
func syncAll(root string, repos []repo, jobs int, timeout time.Duration, stdout io.Writer, quiet, strict bool) []syncResult {
|
||||
return syncSelected(root, repos, repos, jobs, timeout, stdout, quiet, strict)
|
||||
}
|
||||
|
||||
func syncSelected(root string, repos, workspaceRepos []repo, jobs int, timeout time.Duration, stdout io.Writer, quiet bool) []syncResult {
|
||||
func syncSelected(root string, repos, workspaceRepos []repo, jobs int, timeout time.Duration, stdout io.Writer, quiet, strict bool) []syncResult {
|
||||
type item struct {
|
||||
index int
|
||||
repo repo
|
||||
@@ -456,22 +450,18 @@ func syncSelected(root string, repos, workspaceRepos []repo, jobs int, timeout t
|
||||
results[next.index] = syncResult{Path: r.RelativePath, Skipped: true, Message: "pre-sync recheck: " + fresh.BlockReason}
|
||||
continue
|
||||
}
|
||||
if strict && len(fresh.Dirty) > 0 {
|
||||
results[next.index] = syncResult{Path: r.RelativePath, Skipped: true, Message: fmt.Sprintf("pre-sync recheck: dirty worktree (%d changes)", len(fresh.Dirty))}
|
||||
continue
|
||||
}
|
||||
if !quiet {
|
||||
outputMu.Lock()
|
||||
fmt.Fprintf(stdout, "\nSTART %s (%s)\n", r.RelativePath, r.Branch)
|
||||
outputMu.Unlock()
|
||||
}
|
||||
started := time.Now()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
cmd := exec.CommandContext(ctx, "hub", "sync", "--color=never")
|
||||
cmd.Dir = r.Path
|
||||
combined, err := cmd.CombinedOutput()
|
||||
cancel()
|
||||
message := strings.TrimSpace(string(combined))
|
||||
if ctx.Err() == context.DeadlineExceeded {
|
||||
err = ctx.Err()
|
||||
message = "timed out after " + timeout.String()
|
||||
}
|
||||
report, err := syncRepository(r.Path, timeout, len(fresh.Dirty) > 0, fresh.NestedRepoEntries)
|
||||
message := report.Message
|
||||
duration := time.Since(started).Round(time.Millisecond).String()
|
||||
if !quiet {
|
||||
outputMu.Lock()
|
||||
@@ -488,7 +478,7 @@ func syncSelected(root string, repos, workspaceRepos []repo, jobs int, timeout t
|
||||
if err != nil && message == "" {
|
||||
message = err.Error()
|
||||
}
|
||||
results[next.index] = syncResult{Path: r.RelativePath, Success: err == nil, Message: message, Duration: duration}
|
||||
results[next.index] = syncResult{Path: r.RelativePath, Success: err == nil, Message: message, Duration: duration, Branches: report.Branches}
|
||||
}
|
||||
}()
|
||||
}
|
||||
@@ -513,6 +503,8 @@ func printScan(w io.Writer, root string, repos []repo) {
|
||||
status := "ready"
|
||||
if !r.Eligible {
|
||||
status = "BLOCKED: " + r.BlockReason
|
||||
} else if len(r.Dirty) > 0 {
|
||||
status = fmt.Sprintf("ready; dirty worktree (%d changes), checked-out branch protected", len(r.Dirty))
|
||||
}
|
||||
if len(r.NestedRepoEntries) > 0 {
|
||||
status += fmt.Sprintf("; %d nested repo path(s) ignored", len(r.NestedRepoEntries))
|
||||
@@ -555,6 +547,15 @@ func eligibleCount(repos []repo) int {
|
||||
return n
|
||||
}
|
||||
func blockedCount(repos []repo) int { return len(repos) - eligibleCount(repos) }
|
||||
func strictUnsafeCount(repos []repo) int {
|
||||
n := 0
|
||||
for _, r := range repos {
|
||||
if !r.Eligible || len(r.Dirty) > 0 {
|
||||
n++
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
func writeJSON(w io.Writer, value any) {
|
||||
enc := json.NewEncoder(w)
|
||||
enc.SetIndent("", " ")
|
||||
|
||||
+11
-102
@@ -2,63 +2,14 @@ package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
if os.Getenv("STACK_SYNC_TEST_HELPER") == "hub" {
|
||||
marker := os.Getenv("STACK_SYNC_TEST_MARKER")
|
||||
if err := os.WriteFile(marker, []byte(strings.Join(os.Args[1:], " ")), 0o644); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
os.Exit(0)
|
||||
}
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
|
||||
func installFakeHub(t *testing.T, dir string) string {
|
||||
t.Helper()
|
||||
source, err := os.Executable()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
name := "hub"
|
||||
if runtime.GOOS == "windows" {
|
||||
name += ".exe"
|
||||
}
|
||||
destination := filepath.Join(dir, name)
|
||||
if err := os.Link(source, destination); err == nil {
|
||||
return destination
|
||||
}
|
||||
|
||||
input, err := os.Open(source)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer input.Close()
|
||||
output, err := os.OpenFile(destination, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o755)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := io.Copy(output, input); err != nil {
|
||||
_ = output.Close()
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := output.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return destination
|
||||
}
|
||||
|
||||
func git(t *testing.T, dir string, args ...string) {
|
||||
t.Helper()
|
||||
cmd := exec.Command("git", append([]string{"-C", dir}, args...)...)
|
||||
@@ -116,7 +67,7 @@ func TestNestedRepoDoesNotDirtyParent(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDirtyFileBlocksRepository(t *testing.T) {
|
||||
func TestDirtyFileIsReportedButRepositoryRemainsEligible(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
path := filepath.Join(root, "repo")
|
||||
makeRepo(t, path)
|
||||
@@ -125,12 +76,12 @@ func TestDirtyFileBlocksRepository(t *testing.T) {
|
||||
}
|
||||
|
||||
got := inspect(root, path, []string{path})
|
||||
if got.Eligible || len(got.Dirty) != 1 {
|
||||
t.Fatalf("dirty repository was not blocked: %+v", got)
|
||||
if !got.Eligible || len(got.Dirty) != 1 {
|
||||
t.Fatalf("dirty repository was not eligible with its changes reported: %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUntrackedFileAlongsideNestedRepoStillBlocksParent(t *testing.T) {
|
||||
func TestUntrackedFileAlongsideNestedRepoIsStillReported(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
parent := filepath.Join(root, "parent")
|
||||
child := filepath.Join(parent, "child")
|
||||
@@ -142,8 +93,8 @@ func TestUntrackedFileAlongsideNestedRepoStillBlocksParent(t *testing.T) {
|
||||
|
||||
paths, _ := discover(root, nil)
|
||||
got := inspect(root, parent, paths)
|
||||
if got.Eligible || len(got.Dirty) != 1 {
|
||||
t.Fatalf("untracked file should block parent: %+v", got)
|
||||
if !got.Eligible || len(got.Dirty) != 1 {
|
||||
t.Fatalf("untracked file should be reported without blocking parent: %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -160,7 +111,7 @@ func TestSkippedDirectoriesAreNotSearched(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSyncRechecksAndSkipsNewlyDirtyRepository(t *testing.T) {
|
||||
func TestStrictSyncRechecksAndSkipsNewlyDirtyRepository(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
path := filepath.Join(root, "repo")
|
||||
makeRepo(t, path)
|
||||
@@ -172,55 +123,13 @@ func TestSyncRechecksAndSkipsNewlyDirtyRepository(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
bin := filepath.Join(root, "bin")
|
||||
if err := os.Mkdir(bin, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
marker := filepath.Join(root, "hub-was-called")
|
||||
installFakeHub(t, bin)
|
||||
t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
|
||||
t.Setenv("STACK_SYNC_TEST_HELPER", "hub")
|
||||
t.Setenv("STACK_SYNC_TEST_MARKER", marker)
|
||||
|
||||
results := syncAll(root, []repo{planned}, 1, time.Minute, io.Discard, true)
|
||||
results := syncAll(root, []repo{planned}, 1, time.Minute, io.Discard, true, true)
|
||||
if len(results) != 1 || !results[0].Skipped || results[0].Success {
|
||||
t.Fatalf("syncAll() = %+v", results)
|
||||
}
|
||||
if _, err := os.Stat(marker); !os.IsNotExist(err) {
|
||||
t.Fatalf("hub ran despite the newly dirty worktree")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSyncInvokesHubForCleanRepository(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
path := filepath.Join(root, "repo")
|
||||
makeRepo(t, path)
|
||||
planned := inspect(root, path, []string{path})
|
||||
|
||||
bin := filepath.Join(root, "bin")
|
||||
if err := os.Mkdir(bin, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
marker := filepath.Join(root, "hub-was-called")
|
||||
installFakeHub(t, bin)
|
||||
t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
|
||||
t.Setenv("STACK_SYNC_TEST_HELPER", "hub")
|
||||
t.Setenv("STACK_SYNC_TEST_MARKER", marker)
|
||||
|
||||
results := syncAll(root, []repo{planned}, 1, time.Minute, io.Discard, true)
|
||||
if len(results) != 1 || !results[0].Success || results[0].Skipped {
|
||||
t.Fatalf("syncAll() = %+v", results)
|
||||
}
|
||||
args, err := os.ReadFile(marker)
|
||||
if err != nil {
|
||||
t.Fatalf("hub was not called: %v", err)
|
||||
}
|
||||
if string(args) != "sync --color=never" {
|
||||
t.Fatalf("hub arguments = %q", args)
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanWithBlockedRepositoryIsAValidReport(t *testing.T) {
|
||||
func TestScanReportsDirtyEligibleRepository(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
path := filepath.Join(root, "repo")
|
||||
makeRepo(t, path)
|
||||
@@ -233,8 +142,8 @@ func TestScanWithBlockedRepositoryIsAValidReport(t *testing.T) {
|
||||
if code != 0 {
|
||||
t.Fatalf("run() code = %d, stderr = %q", code, stderr.String())
|
||||
}
|
||||
if !bytes.Contains(stdout.Bytes(), []byte("BLOCKED: dirty worktree")) {
|
||||
t.Fatalf("scan did not report blocked repository:\n%s", stdout.String())
|
||||
if !bytes.Contains(stdout.Bytes(), []byte("dirty worktree (1 changes), checked-out branch protected")) {
|
||||
t.Fatalf("scan did not report dirty eligible repository:\n%s", stdout.String())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,383 @@
|
||||
package main
|
||||
|
||||
// The branch synchronization behavior in this file is adapted from hub's
|
||||
// `hub sync` command. See THIRD_PARTY_NOTICES.md for attribution and license.
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type branchSyncResult struct {
|
||||
Branch string `json:"branch"`
|
||||
Action string `json:"action"`
|
||||
Message string `json:"message,omitempty"`
|
||||
}
|
||||
|
||||
type repositorySyncReport struct {
|
||||
Message string
|
||||
Branches []branchSyncResult
|
||||
}
|
||||
|
||||
type localBranch struct {
|
||||
name string
|
||||
ref string
|
||||
oid string
|
||||
}
|
||||
|
||||
func syncRepository(path string, timeout time.Duration, initiallyDirty bool, ignoredNested []string) (repositorySyncReport, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
|
||||
var report repositorySyncReport
|
||||
remote, err := mainRemote(ctx, path)
|
||||
if err != nil {
|
||||
return failedReport(report, ctx, timeout, err)
|
||||
}
|
||||
fetchOutput, err := gitCombinedContext(ctx, path, "fetch", "--prune", "--quiet", "--progress", remote)
|
||||
if err != nil {
|
||||
return failedReport(report, ctx, timeout, gitCommandError("fetch "+remote, fetchOutput, err))
|
||||
}
|
||||
if operation := gitOperation(path); operation != "" {
|
||||
return failedReport(report, ctx, timeout, fmt.Errorf("%s started during sync", operation))
|
||||
}
|
||||
|
||||
current, checkedOut, err := worktreeState(ctx, path)
|
||||
if err != nil {
|
||||
return failedReport(report, ctx, timeout, err)
|
||||
}
|
||||
branches, err := localBranches(ctx, path)
|
||||
if err != nil {
|
||||
return failedReport(report, ctx, timeout, err)
|
||||
}
|
||||
defaultRef, defaultBranch, err := remoteDefaultBranch(ctx, path, remote)
|
||||
if err != nil {
|
||||
return failedReport(report, ctx, timeout, err)
|
||||
}
|
||||
|
||||
for _, branch := range branches {
|
||||
target, gone, err := upstreamForBranch(ctx, path, remote, branch.name)
|
||||
if err != nil {
|
||||
return failedReport(report, ctx, timeout, err)
|
||||
}
|
||||
if target != "" {
|
||||
targetOID, err := resolveCommit(ctx, path, target)
|
||||
if err != nil {
|
||||
return failedReport(report, ctx, timeout, fmt.Errorf("resolve upstream for %s: %w", branch.name, err))
|
||||
}
|
||||
if targetOID == branch.oid {
|
||||
continue
|
||||
}
|
||||
behind, err := isAncestor(ctx, path, branch.oid, targetOID)
|
||||
if err != nil {
|
||||
return failedReport(report, ctx, timeout, fmt.Errorf("compare branch %s with its upstream: %w", branch.name, err))
|
||||
}
|
||||
if !behind {
|
||||
report.add(branch.name, "warning", "contains commits that are not in its upstream")
|
||||
continue
|
||||
}
|
||||
current, checkedOut, err = worktreeState(ctx, path)
|
||||
if err != nil {
|
||||
return failedReport(report, ctx, timeout, err)
|
||||
}
|
||||
|
||||
if branch.name == current {
|
||||
dirty, err := worktreeDirty(ctx, path, ignoredNested)
|
||||
if err != nil {
|
||||
return failedReport(report, ctx, timeout, err)
|
||||
}
|
||||
if initiallyDirty || dirty {
|
||||
report.add(branch.name, "protected", "checked-out branch has worktree changes")
|
||||
continue
|
||||
}
|
||||
output, err := gitCombinedContext(ctx, path, "merge", "--ff-only", "--quiet", target)
|
||||
if err != nil {
|
||||
return failedReport(report, ctx, timeout, gitCommandError("fast-forward branch "+branch.name, output, err))
|
||||
}
|
||||
} else if checkedOut[branch.ref] {
|
||||
report.add(branch.name, "protected", "checked out in another worktree")
|
||||
continue
|
||||
} else {
|
||||
output, err := gitCombinedContext(ctx, path, "update-ref", branch.ref, targetOID, branch.oid)
|
||||
if err != nil {
|
||||
return failedReport(report, ctx, timeout, gitCommandError("fast-forward branch "+branch.name, output, err))
|
||||
}
|
||||
}
|
||||
report.add(branch.name, "updated", "fast-forwarded from "+abbreviateOID(branch.oid))
|
||||
continue
|
||||
}
|
||||
|
||||
if !gone {
|
||||
continue
|
||||
}
|
||||
if defaultRef == "" {
|
||||
report.add(branch.name, "warning", "upstream was deleted; kept because the remote default branch is unknown")
|
||||
continue
|
||||
}
|
||||
merged, err := isAncestor(ctx, path, branch.oid, defaultRef)
|
||||
if err != nil {
|
||||
return failedReport(report, ctx, timeout, fmt.Errorf("check whether branch %s is merged: %w", branch.name, err))
|
||||
}
|
||||
if !merged {
|
||||
report.add(branch.name, "warning", "upstream was deleted, but the branch is not merged into "+defaultBranch)
|
||||
continue
|
||||
}
|
||||
current, checkedOut, err = worktreeState(ctx, path)
|
||||
if err != nil {
|
||||
return failedReport(report, ctx, timeout, err)
|
||||
}
|
||||
if checkedOut[branch.ref] {
|
||||
if branch.name != current {
|
||||
report.add(branch.name, "protected", "upstream was deleted, but the branch is checked out in another worktree")
|
||||
continue
|
||||
}
|
||||
dirty, err := worktreeDirty(ctx, path, ignoredNested)
|
||||
if err != nil {
|
||||
return failedReport(report, ctx, timeout, err)
|
||||
}
|
||||
if initiallyDirty || dirty {
|
||||
report.add(branch.name, "protected", "upstream was deleted, but the checked-out branch has worktree changes")
|
||||
continue
|
||||
}
|
||||
if !hasLocalBranch(branches, defaultBranch) {
|
||||
report.add(branch.name, "protected", "upstream was deleted, but the local default branch does not exist")
|
||||
continue
|
||||
}
|
||||
output, err := gitCombinedContext(ctx, path, "checkout", "--quiet", defaultBranch)
|
||||
if err != nil {
|
||||
return failedReport(report, ctx, timeout, gitCommandError("check out default branch "+defaultBranch, output, err))
|
||||
}
|
||||
current = defaultBranch
|
||||
}
|
||||
output, err := gitCombinedContext(ctx, path, "branch", "-D", "--", branch.name)
|
||||
if err != nil {
|
||||
return failedReport(report, ctx, timeout, gitCommandError("delete merged branch "+branch.name, output, err))
|
||||
}
|
||||
report.add(branch.name, "deleted", "upstream was deleted and the branch was merged into "+defaultBranch)
|
||||
}
|
||||
|
||||
if len(report.Branches) == 0 {
|
||||
report.Message = "Already up to date."
|
||||
} else {
|
||||
report.Message = formatBranchResults(report.Branches)
|
||||
}
|
||||
return report, nil
|
||||
}
|
||||
|
||||
func (r *repositorySyncReport) add(branch, action, message string) {
|
||||
r.Branches = append(r.Branches, branchSyncResult{Branch: branch, Action: action, Message: message})
|
||||
}
|
||||
|
||||
func failedReport(report repositorySyncReport, ctx context.Context, timeout time.Duration, err error) (repositorySyncReport, error) {
|
||||
if ctx.Err() == context.DeadlineExceeded {
|
||||
err = fmt.Errorf("timed out after %s", timeout)
|
||||
}
|
||||
report.Message = err.Error()
|
||||
return report, err
|
||||
}
|
||||
|
||||
func mainRemote(ctx context.Context, path string) (string, error) {
|
||||
output, err := gitOutputContext(ctx, path, "remote")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("list remotes: %w", err)
|
||||
}
|
||||
remotes := strings.Fields(output)
|
||||
if len(remotes) == 0 {
|
||||
return "", errors.New("no remotes")
|
||||
}
|
||||
for _, preferred := range []string{"upstream", "github", "origin"} {
|
||||
for _, remote := range remotes {
|
||||
if remote == preferred {
|
||||
return remote, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(remotes) == 1 {
|
||||
return remotes[0], nil
|
||||
}
|
||||
return "", fmt.Errorf("cannot choose a main remote from %s; name one upstream, github, or origin", strings.Join(remotes, ", "))
|
||||
}
|
||||
|
||||
func localBranches(ctx context.Context, path string) ([]localBranch, error) {
|
||||
output, err := gitBytesContext(ctx, path, "for-each-ref", "--format=%(refname)%00%(refname:short)%00%(objectname)", "refs/heads")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list local branches: %w", err)
|
||||
}
|
||||
var branches []localBranch
|
||||
for _, line := range bytes.Split(bytes.TrimSpace(output), []byte{'\n'}) {
|
||||
parts := bytes.Split(line, []byte{0})
|
||||
if len(parts) != 3 {
|
||||
continue
|
||||
}
|
||||
branches = append(branches, localBranch{ref: string(parts[0]), name: string(parts[1]), oid: string(parts[2])})
|
||||
}
|
||||
sort.Slice(branches, func(i, j int) bool { return branches[i].name < branches[j].name })
|
||||
return branches, nil
|
||||
}
|
||||
|
||||
func upstreamForBranch(ctx context.Context, path, remote, branch string) (target string, gone bool, err error) {
|
||||
configuredRemote, _ := gitOutputContext(ctx, path, "config", "--get", "branch."+branch+".remote")
|
||||
configuredRemote = strings.TrimSpace(configuredRemote)
|
||||
mergeRef, _ := gitOutputContext(ctx, path, "config", "--get", "branch."+branch+".merge")
|
||||
mergeRef = strings.TrimSpace(mergeRef)
|
||||
if configuredRemote == remote && mergeRef != "" {
|
||||
target = "refs/remotes/" + remote + "/" + strings.TrimPrefix(mergeRef, "refs/heads/")
|
||||
exists, err := refExists(ctx, path, target)
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
if !exists {
|
||||
return "", true, nil
|
||||
}
|
||||
return target, false, nil
|
||||
}
|
||||
target = "refs/remotes/" + remote + "/" + branch
|
||||
exists, err := refExists(ctx, path, target)
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
if !exists {
|
||||
return "", false, nil
|
||||
}
|
||||
return target, false, nil
|
||||
}
|
||||
|
||||
func remoteDefaultBranch(ctx context.Context, path, remote string) (ref, branch string, err error) {
|
||||
head := "refs/remotes/" + remote + "/HEAD"
|
||||
if output, symbolicErr := gitOutputContext(ctx, path, "symbolic-ref", "--quiet", head); symbolicErr == nil {
|
||||
ref = strings.TrimSpace(output)
|
||||
exists, existsErr := refExists(ctx, path, ref)
|
||||
if existsErr != nil {
|
||||
return "", "", existsErr
|
||||
}
|
||||
if exists {
|
||||
return ref, strings.TrimPrefix(ref, "refs/remotes/"+remote+"/"), nil
|
||||
}
|
||||
} else if ctx.Err() != nil {
|
||||
return "", "", ctx.Err()
|
||||
}
|
||||
return "", "", nil
|
||||
}
|
||||
|
||||
func checkedOutBranches(ctx context.Context, path string) (map[string]bool, error) {
|
||||
output, err := gitOutputContext(ctx, path, "worktree", "list", "--porcelain")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list worktrees: %w", err)
|
||||
}
|
||||
branches := make(map[string]bool)
|
||||
for _, line := range strings.Split(output, "\n") {
|
||||
if strings.HasPrefix(line, "branch ") {
|
||||
branches[strings.TrimSpace(strings.TrimPrefix(line, "branch "))] = true
|
||||
}
|
||||
}
|
||||
return branches, nil
|
||||
}
|
||||
|
||||
func worktreeState(ctx context.Context, path string) (string, map[string]bool, error) {
|
||||
current, err := gitOutputContext(ctx, path, "symbolic-ref", "--quiet", "--short", "HEAD")
|
||||
if err != nil {
|
||||
return "", nil, errors.New("cannot determine the checked-out branch after fetch")
|
||||
}
|
||||
checkedOut, err := checkedOutBranches(ctx, path)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
return strings.TrimSpace(current), checkedOut, nil
|
||||
}
|
||||
|
||||
func worktreeDirty(ctx context.Context, path string, ignoredNested []string) (bool, error) {
|
||||
output, err := gitBytesContext(ctx, path, "status", "--porcelain=v1", "-z", "--untracked-files=all", "--ignore-submodules=none")
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("read worktree status: %w", err)
|
||||
}
|
||||
for _, change := range parsePorcelain(output) {
|
||||
if change.code != "??" || !belongsToNestedRepo(change.path, ignoredNested) {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func resolveCommit(ctx context.Context, path, ref string) (string, error) {
|
||||
output, err := gitOutputContext(ctx, path, "rev-parse", "--verify", ref+"^{commit}")
|
||||
return strings.TrimSpace(output), err
|
||||
}
|
||||
|
||||
func refExists(ctx context.Context, path, ref string) (bool, error) {
|
||||
cmd := exec.CommandContext(ctx, "git", "-C", path, "show-ref", "--verify", "--quiet", ref)
|
||||
err := cmd.Run()
|
||||
if err == nil {
|
||||
return true, nil
|
||||
}
|
||||
var exitErr *exec.ExitError
|
||||
if errors.As(err, &exitErr) && exitErr.ExitCode() == 1 {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
|
||||
func isAncestor(ctx context.Context, path, older, newer string) (bool, error) {
|
||||
cmd := exec.CommandContext(ctx, "git", "-C", path, "merge-base", "--is-ancestor", older, newer)
|
||||
err := cmd.Run()
|
||||
if err == nil {
|
||||
return true, nil
|
||||
}
|
||||
var exitErr *exec.ExitError
|
||||
if errors.As(err, &exitErr) && exitErr.ExitCode() == 1 {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
|
||||
func hasLocalBranch(branches []localBranch, name string) bool {
|
||||
for _, branch := range branches {
|
||||
if branch.name == name {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func gitOutputContext(ctx context.Context, path string, args ...string) (string, error) {
|
||||
output, err := gitBytesContext(ctx, path, args...)
|
||||
return string(output), err
|
||||
}
|
||||
|
||||
func gitBytesContext(ctx context.Context, path string, args ...string) ([]byte, error) {
|
||||
cmd := exec.CommandContext(ctx, "git", append([]string{"-C", path}, args...)...)
|
||||
return cmd.Output()
|
||||
}
|
||||
|
||||
func gitCombinedContext(ctx context.Context, path string, args ...string) ([]byte, error) {
|
||||
cmd := exec.CommandContext(ctx, "git", append([]string{"-C", path}, args...)...)
|
||||
return cmd.CombinedOutput()
|
||||
}
|
||||
|
||||
func gitCommandError(action string, output []byte, err error) error {
|
||||
detail := strings.TrimSpace(string(output))
|
||||
if detail == "" {
|
||||
return fmt.Errorf("%s: %w", action, err)
|
||||
}
|
||||
return fmt.Errorf("%s: %s", action, firstLine(detail))
|
||||
}
|
||||
|
||||
func formatBranchResults(results []branchSyncResult) string {
|
||||
lines := make([]string, 0, len(results))
|
||||
for _, result := range results {
|
||||
lines = append(lines, fmt.Sprintf("%-9s %s: %s", strings.ToUpper(result.Action), result.Branch, result.Message))
|
||||
}
|
||||
return strings.Join(lines, "\n")
|
||||
}
|
||||
|
||||
func abbreviateOID(oid string) string {
|
||||
if len(oid) <= 7 {
|
||||
return oid
|
||||
}
|
||||
return oid[:7]
|
||||
}
|
||||
+212
@@ -0,0 +1,212 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type syncFixture struct {
|
||||
root string
|
||||
remote string
|
||||
work string
|
||||
other string
|
||||
}
|
||||
|
||||
func makeSyncFixture(t *testing.T) syncFixture {
|
||||
t.Helper()
|
||||
root := t.TempDir()
|
||||
remote := filepath.Join(root, "remote.git")
|
||||
seed := filepath.Join(root, "seed")
|
||||
work := filepath.Join(root, "work")
|
||||
other := filepath.Join(root, "other")
|
||||
|
||||
if err := os.MkdirAll(seed, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
git(t, root, "init", "--bare", "-q", "--initial-branch=main", remote)
|
||||
git(t, seed, "init", "-q", "--initial-branch=main")
|
||||
configureTestIdentity(t, seed)
|
||||
writeTestFile(t, filepath.Join(seed, "tracked.txt"), "initial\n")
|
||||
git(t, seed, "add", "tracked.txt")
|
||||
git(t, seed, "commit", "-qm", "initial")
|
||||
git(t, seed, "remote", "add", "origin", remote)
|
||||
git(t, seed, "push", "-qu", "origin", "main")
|
||||
|
||||
git(t, root, "clone", "-q", remote, work)
|
||||
git(t, root, "clone", "-q", remote, other)
|
||||
configureTestIdentity(t, work)
|
||||
configureTestIdentity(t, other)
|
||||
|
||||
git(t, work, "checkout", "-qb", "feature")
|
||||
writeTestFile(t, filepath.Join(work, "feature.txt"), "feature\n")
|
||||
git(t, work, "add", "feature.txt")
|
||||
git(t, work, "commit", "-qm", "feature")
|
||||
git(t, work, "push", "-qu", "origin", "feature")
|
||||
|
||||
return syncFixture{root: root, remote: remote, work: work, other: other}
|
||||
}
|
||||
|
||||
func configureTestIdentity(t *testing.T, path string) {
|
||||
t.Helper()
|
||||
git(t, path, "config", "user.name", "Test")
|
||||
git(t, path, "config", "user.email", "test@example.com")
|
||||
}
|
||||
|
||||
func writeTestFile(t *testing.T, path, contents string) {
|
||||
t.Helper()
|
||||
if err := os.WriteFile(path, []byte(contents), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func advanceRemoteBranch(t *testing.T, fixture syncFixture, branch, filename string) {
|
||||
t.Helper()
|
||||
git(t, fixture.other, "fetch", "-q", "origin")
|
||||
git(t, fixture.other, "checkout", "-qB", branch, "origin/"+branch)
|
||||
writeTestFile(t, filepath.Join(fixture.other, filename), branch+" update\n")
|
||||
git(t, fixture.other, "add", filename)
|
||||
git(t, fixture.other, "commit", "-qm", "advance "+branch)
|
||||
git(t, fixture.other, "push", "-q", "origin", branch)
|
||||
}
|
||||
|
||||
func gitText(t *testing.T, path string, args ...string) string {
|
||||
t.Helper()
|
||||
cmd := append([]string{"-C", path}, args...)
|
||||
output, err := exec.Command("git", cmd...).CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("git %v: %v\n%s", args, err, output)
|
||||
}
|
||||
return strings.TrimSpace(string(output))
|
||||
}
|
||||
|
||||
func TestDirtyFeatureBranchStillAllowsInactiveMainUpdate(t *testing.T) {
|
||||
fixture := makeSyncFixture(t)
|
||||
writeTestFile(t, filepath.Join(fixture.work, "local-notes.txt"), "unfinished work\n")
|
||||
advanceRemoteBranch(t, fixture, "main", "main-remote.txt")
|
||||
|
||||
beforeFeature := gitText(t, fixture.work, "rev-parse", "feature")
|
||||
report, err := syncRepository(fixture.work, time.Minute, true, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("syncRepository(): %v\n%s", err, report.Message)
|
||||
}
|
||||
|
||||
if got, want := gitText(t, fixture.work, "rev-parse", "main"), gitText(t, fixture.work, "rev-parse", "origin/main"); got != want {
|
||||
t.Fatalf("inactive main was not updated: got %s, want %s", got, want)
|
||||
}
|
||||
if got := gitText(t, fixture.work, "rev-parse", "feature"); got != beforeFeature {
|
||||
t.Fatalf("dirty checked-out feature branch moved: got %s, want %s", got, beforeFeature)
|
||||
}
|
||||
if got := gitText(t, fixture.work, "branch", "--show-current"); got != "feature" {
|
||||
t.Fatalf("checked-out branch = %q, want feature", got)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(fixture.work, "local-notes.txt")); err != nil {
|
||||
t.Fatalf("dirty file was lost: %v", err)
|
||||
}
|
||||
assertBranchAction(t, report.Branches, "main", "updated")
|
||||
}
|
||||
|
||||
func TestDirtyCheckedOutBranchIsProtectedWhenRemoteAdvances(t *testing.T) {
|
||||
fixture := makeSyncFixture(t)
|
||||
writeTestFile(t, filepath.Join(fixture.work, "feature.txt"), "unfinished feature edit\n")
|
||||
advanceRemoteBranch(t, fixture, "feature", "feature-remote.txt")
|
||||
before := gitText(t, fixture.work, "rev-parse", "feature")
|
||||
|
||||
report, err := syncRepository(fixture.work, time.Minute, true, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("syncRepository(): %v\n%s", err, report.Message)
|
||||
}
|
||||
|
||||
if got := gitText(t, fixture.work, "rev-parse", "feature"); got != before {
|
||||
t.Fatalf("dirty checked-out branch moved: got %s, want %s", got, before)
|
||||
}
|
||||
if upstream := gitText(t, fixture.work, "rev-parse", "origin/feature"); upstream == before {
|
||||
t.Fatal("fixture did not fetch the advanced remote feature branch")
|
||||
}
|
||||
contents, err := os.ReadFile(filepath.Join(fixture.work, "feature.txt"))
|
||||
if err != nil || string(contents) != "unfinished feature edit\n" {
|
||||
t.Fatalf("dirty edit changed: contents=%q err=%v", contents, err)
|
||||
}
|
||||
assertBranchAction(t, report.Branches, "feature", "protected")
|
||||
}
|
||||
|
||||
func TestCleanCheckedOutBranchFastForwards(t *testing.T) {
|
||||
fixture := makeSyncFixture(t)
|
||||
advanceRemoteBranch(t, fixture, "feature", "feature-remote.txt")
|
||||
|
||||
report, err := syncRepository(fixture.work, time.Minute, false, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("syncRepository(): %v\n%s", err, report.Message)
|
||||
}
|
||||
if got, want := gitText(t, fixture.work, "rev-parse", "feature"), gitText(t, fixture.work, "rev-parse", "origin/feature"); got != want {
|
||||
t.Fatalf("checked-out feature was not fast-forwarded: got %s, want %s", got, want)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(fixture.work, "feature-remote.txt")); err != nil {
|
||||
t.Fatalf("fast-forwarded file is missing from worktree: %v", err)
|
||||
}
|
||||
assertBranchAction(t, report.Branches, "feature", "updated")
|
||||
}
|
||||
|
||||
func TestDivergentBranchIsWarnedAndLeftUnchanged(t *testing.T) {
|
||||
fixture := makeSyncFixture(t)
|
||||
writeTestFile(t, filepath.Join(fixture.work, "local-only.txt"), "local commit\n")
|
||||
git(t, fixture.work, "add", "local-only.txt")
|
||||
git(t, fixture.work, "commit", "-qm", "local feature work")
|
||||
localOID := gitText(t, fixture.work, "rev-parse", "feature")
|
||||
advanceRemoteBranch(t, fixture, "feature", "remote-only.txt")
|
||||
|
||||
report, err := syncRepository(fixture.work, time.Minute, false, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("syncRepository(): %v\n%s", err, report.Message)
|
||||
}
|
||||
if got := gitText(t, fixture.work, "rev-parse", "feature"); got != localOID {
|
||||
t.Fatalf("divergent feature branch moved: got %s, want %s", got, localOID)
|
||||
}
|
||||
assertBranchAction(t, report.Branches, "feature", "warning")
|
||||
}
|
||||
|
||||
func TestMergedInactiveBranchWithDeletedUpstreamIsDeleted(t *testing.T) {
|
||||
fixture := makeSyncFixture(t)
|
||||
git(t, fixture.work, "branch", "old", "main")
|
||||
git(t, fixture.work, "push", "-qu", "origin", "old")
|
||||
git(t, fixture.work, "push", "-q", "origin", "--delete", "old")
|
||||
|
||||
report, err := syncRepository(fixture.work, time.Minute, false, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("syncRepository(): %v\n%s", err, report.Message)
|
||||
}
|
||||
if err := exec.Command("git", "-C", fixture.work, "show-ref", "--verify", "--quiet", "refs/heads/old").Run(); err == nil {
|
||||
t.Fatal("merged branch with deleted configured upstream still exists")
|
||||
}
|
||||
assertBranchAction(t, report.Branches, "old", "deleted")
|
||||
}
|
||||
|
||||
func TestBranchCheckedOutInAnotherWorktreeIsProtected(t *testing.T) {
|
||||
fixture := makeSyncFixture(t)
|
||||
mainWorktree := filepath.Join(fixture.root, "main-worktree")
|
||||
git(t, fixture.work, "worktree", "add", "-q", mainWorktree, "main")
|
||||
before := gitText(t, fixture.work, "rev-parse", "main")
|
||||
advanceRemoteBranch(t, fixture, "main", "main-remote.txt")
|
||||
|
||||
report, err := syncRepository(fixture.work, time.Minute, false, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("syncRepository(): %v\n%s", err, report.Message)
|
||||
}
|
||||
if got := gitText(t, fixture.work, "rev-parse", "main"); got != before {
|
||||
t.Fatalf("branch checked out in another worktree moved: got %s, want %s", got, before)
|
||||
}
|
||||
assertBranchAction(t, report.Branches, "main", "protected")
|
||||
}
|
||||
|
||||
func assertBranchAction(t *testing.T, results []branchSyncResult, branch, action string) {
|
||||
t.Helper()
|
||||
for _, result := range results {
|
||||
if result.Branch == branch && result.Action == action {
|
||||
return
|
||||
}
|
||||
}
|
||||
t.Fatalf("missing %s action for branch %s in %+v", action, branch, results)
|
||||
}
|
||||
@@ -3,7 +3,6 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -181,8 +180,6 @@ func (m tuiModel) handleKey(key tea.KeyMsg) (tea.Model, tea.Cmd) {
|
||||
count := len(m.selectedRepos())
|
||||
if count == 0 {
|
||||
m.status = "Select at least one eligible repository"
|
||||
} else if _, err := exec.LookPath("hub"); err != nil {
|
||||
m.status = "hub is required for sync but was not found on PATH"
|
||||
} else {
|
||||
m.mode = modeConfirm
|
||||
}
|
||||
@@ -231,6 +228,8 @@ func (m tuiModel) View() string {
|
||||
state := readyStyle.Render("ready")
|
||||
if !r.Eligible {
|
||||
state = blockStyle.Render("blocked")
|
||||
} else if len(r.Dirty) > 0 {
|
||||
state = titleStyle.Render("ready (dirty)")
|
||||
}
|
||||
line := fmt.Sprintf("%s %-3s %-*s %-16s %s", pointer, check, nameWidth, truncateMiddle(r.RelativePath, nameWidth), truncateMiddle(r.Branch, 16), state)
|
||||
if i == m.cursor {
|
||||
@@ -250,9 +249,9 @@ func (m tuiModel) View() string {
|
||||
|
||||
switch m.mode {
|
||||
case modeConfirm:
|
||||
b.WriteString(blockStyle.Bold(true).Render(fmt.Sprintf("Run hub sync in %d selected repositories?", selected)))
|
||||
b.WriteString(blockStyle.Bold(true).Render(fmt.Sprintf("Synchronize branches in %d selected repositories?", selected)))
|
||||
b.WriteString("\n")
|
||||
b.WriteString(mutedStyle.Render("hub may delete local branches it considers merged. y confirm • n/esc cancel"))
|
||||
b.WriteString(mutedStyle.Render("Dirty checked-out branches are protected; merged branches with deleted upstreams may be removed. y confirm • n/esc cancel"))
|
||||
case modeSyncing:
|
||||
spinner := []string{"◐", "◓", "◑", "◒"}[m.frame%4]
|
||||
b.WriteString(titleStyle.Render(fmt.Sprintf("%s Syncing %d repositories…", spinner, selected)))
|
||||
@@ -301,7 +300,11 @@ func (m tuiModel) detailView(width int) string {
|
||||
r := m.repos[m.cursor]
|
||||
var lines []string
|
||||
if r.Eligible {
|
||||
lines = append(lines, readyStyle.Render("READY")+" "+r.RelativePath+" "+mutedStyle.Render(r.Branch))
|
||||
state := readyStyle.Render("READY")
|
||||
if len(r.Dirty) > 0 {
|
||||
state = titleStyle.Render("READY · CHECKED-OUT BRANCH PROTECTED")
|
||||
}
|
||||
lines = append(lines, state+" "+r.RelativePath+" "+mutedStyle.Render(r.Branch))
|
||||
} else {
|
||||
lines = append(lines, blockStyle.Render("BLOCKED")+" "+r.RelativePath+" "+r.BlockReason)
|
||||
}
|
||||
@@ -372,7 +375,7 @@ func (m tuiModel) scanCmd() tea.Cmd {
|
||||
|
||||
func (m tuiModel) syncCmd(selected []repo) tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
results := syncSelected(m.root, selected, m.repos, m.opts.jobs, m.opts.timeout, io.Discard, true)
|
||||
results := syncSelected(m.root, selected, m.repos, m.opts.jobs, m.opts.timeout, io.Discard, true, false)
|
||||
paths, err := discover(m.root, m.opts.exclusions)
|
||||
if err != nil {
|
||||
return syncDoneMsg{results: results, err: err}
|
||||
|
||||
+20
-8
@@ -10,20 +10,21 @@ import (
|
||||
func testTUIRepos() []repo {
|
||||
return []repo{
|
||||
{Path: "/workspace/clean", RelativePath: "clean", Branch: "main", Eligible: true},
|
||||
{Path: "/workspace/dirty", RelativePath: "dirty", Branch: "develop", BlockReason: "dirty worktree (1 changes)", Dirty: []string{" M file.txt"}},
|
||||
{Path: "/workspace/dirty", RelativePath: "dirty", Branch: "develop", Dirty: []string{" M file.txt"}, Eligible: true},
|
||||
{Path: "/workspace/blocked", RelativePath: "blocked", BlockReason: "detached HEAD"},
|
||||
}
|
||||
}
|
||||
|
||||
func TestTUIOnlySelectsEligibleRepositories(t *testing.T) {
|
||||
m := newTUIModel("/workspace", testTUIRepos(), options{})
|
||||
if got := len(m.selectedRepos()); got != 1 {
|
||||
t.Fatalf("selected repositories = %d, want 1", got)
|
||||
if got := len(m.selectedRepos()); got != 2 {
|
||||
t.Fatalf("selected repositories = %d, want 2", got)
|
||||
}
|
||||
|
||||
m.cursor = 1
|
||||
m.cursor = 2
|
||||
updated, _ := m.Update(tea.KeyMsg{Type: tea.KeySpace})
|
||||
m = updated.(tuiModel)
|
||||
if got := len(m.selectedRepos()); got != 1 {
|
||||
if got := len(m.selectedRepos()); got != 2 {
|
||||
t.Fatalf("blocked repository became selectable; selected = %d", got)
|
||||
}
|
||||
}
|
||||
@@ -45,16 +46,27 @@ func TestTUIRequiresExplicitConfirmation(t *testing.T) {
|
||||
|
||||
func TestTUIViewExplainsBlockedRepository(t *testing.T) {
|
||||
m := newTUIModel("/workspace", testTUIRepos(), options{})
|
||||
m.width, m.height, m.cursor = 100, 30, 1
|
||||
m.width, m.height, m.cursor = 100, 30, 2
|
||||
view := m.View()
|
||||
for _, want := range []string{"STACK SYNC", "dirty worktree", "file.txt", "1 blocked"} {
|
||||
for _, want := range []string{"STACK SYNC", "detached HEAD", "1 blocked"} {
|
||||
if !strings.Contains(view, want) {
|
||||
t.Fatalf("view missing %q:\n%s", want, view)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTUIViewShowsLastHubOutputForFocusedRepository(t *testing.T) {
|
||||
func TestTUIViewExplainsDirtyBranchProtection(t *testing.T) {
|
||||
m := newTUIModel("/workspace", testTUIRepos(), options{})
|
||||
m.width, m.height, m.cursor = 100, 30, 1
|
||||
view := m.View()
|
||||
for _, want := range []string{"ready (dirty)", "CHECKED-OUT BRANCH PROTECTED", "file.txt"} {
|
||||
if !strings.Contains(view, want) {
|
||||
t.Fatalf("view missing %q:\n%s", want, view)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTUIViewShowsLastSyncOutputForFocusedRepository(t *testing.T) {
|
||||
m := newTUIModel("/workspace", testTUIRepos(), options{})
|
||||
m.width, m.height = 100, 30
|
||||
m.lastResult = []syncResult{{Path: "clean", Success: true, Message: "Updated branch main", Duration: "120ms"}}
|
||||
|
||||
Reference in New Issue
Block a user