Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion cmd/preflight/preflight.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ func (c *PreflightCmd) Run(kongCtx *kong.Context, globals cli.GlobalFlags) error
snapshotDetail += fmt.Sprintf("\n %s %s", file.StatusSymbol(), file.Path)
}
}
if result.PushSkipped {
snapshotDetail += "\nWarning: no remote \"origin\" found; push skipped"
}
_ = renderer.Render(Event{Type: EventOperation, Time: time.Now(), PreflightID: preflightID.String(), Title: "Pushed snapshot of working tree...", Detail: snapshotDetail})

_ = renderer.Render(Event{Type: EventOperation, Time: time.Now(), PreflightID: preflightID.String(), Title: fmt.Sprintf("Creating build on %s/%s...", resolvedPipeline.Org, resolvedPipeline.Name)})
Expand Down Expand Up @@ -168,7 +171,7 @@ func (c *PreflightCmd) Run(kongCtx *kong.Context, globals cli.GlobalFlags) error
buildResult := NewResult(finalBuild)
finalErr := buildResult.Error()

if !c.NoCleanup {
if !c.NoCleanup && !result.PushSkipped {
_ = renderer.Render(Event{Type: EventOperation, Time: time.Now(), PreflightID: preflightID.String(), Title: fmt.Sprintf("Cleaning up remote branch %s...", result.Branch)})
if cleanupErr := preflight.Cleanup(repoRoot, result.Ref, globals.EnableDebug()); cleanupErr != nil {
_ = renderer.Render(Event{Type: EventOperation, Time: time.Now(), PreflightID: preflightID.String(), Title: fmt.Sprintf("Warning: failed to delete remote branch %s: %v", result.Ref, cleanupErr)})
Expand Down
31 changes: 19 additions & 12 deletions internal/preflight/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ type FileChange struct {

// SnapshotResult holds the output of a successful snapshot operation.
type SnapshotResult struct {
Commit string
Ref string
Branch string
Files []FileChange
Commit string
Ref string
Branch string
Files []FileChange
PushSkipped bool
}

func (r SnapshotResult) ShortCommit() string {
Expand Down Expand Up @@ -110,17 +111,23 @@ func Snapshot(dir string, preflightID uuid.UUID, opts ...SnapshotOption) (*Snaps
return nil, err
}

// Push the commit to the remote branch.
refspec := fmt.Sprintf("%s:%s", commit, ref)
if err := gitRun(dir, env, cfg.debug, "push", "origin", refspec); err != nil {
return nil, err
var pushSkipped bool
if _, err := gitOutput(dir, env, cfg.debug, "remote", "get-url", "origin"); err != nil {
pushSkipped = true
} else {
// Push the commit to the remote branch.
refspec := fmt.Sprintf("%s:%s", commit, ref)
if err := gitRun(dir, env, cfg.debug, "push", "origin", refspec); err != nil {
return nil, err
}
}

return &SnapshotResult{
Commit: commit,
Ref: ref,
Branch: branch,
Files: files,
Commit: commit,
Ref: ref,
Branch: branch,
Files: files,
PushSkipped: pushSkipped,
}, nil
}

Expand Down
27 changes: 27 additions & 0 deletions internal/preflight/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,33 @@ func TestDiffFiles(t *testing.T) {
}
}

func TestSnapshot_NoRemote(t *testing.T) {
worktree := initTestRepo(t)

// Remove the origin remote.
runGit(t, worktree, "remote", "remove", "origin")

// Add a change so we exercise the commit path too.
if err := os.WriteFile(filepath.Join(worktree, "README.md"), []byte("# changed\n"), 0o644); err != nil {
t.Fatal(err)
}

preflightID := uuid.MustParse("00000000-0000-0000-0000-000000000010")
result, err := Snapshot(worktree, preflightID)
if err != nil {
t.Fatalf("Snapshot() error: %v", err)
}

if !result.PushSkipped {
t.Error("expected PushSkipped to be true when no remote exists")
}

// The commit should still be valid.
if len(result.Commit) != 40 {
t.Errorf("expected 40-char SHA, got %q", result.Commit)
}
}

func TestSnapshot_CleanWorktree(t *testing.T) {
worktree := initTestRepo(t)

Expand Down