Skip to content

Commit 58c59ff

Browse files
authored
Create new commits even with no diff (#758)
Pushing to `HEAD` when there are no tracked/staged changes should create a new commit, rather than updating statuses on the resolved commit. This removes the `len` check from the snapshot logic so that we always create a commit, even with no changes. It also updates the tests to reflect this new behaviour.
1 parent a628fb0 commit 58c59ff

2 files changed

Lines changed: 29 additions & 22 deletions

File tree

internal/preflight/snapshot.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,8 @@ func WithDebug() SnapshotOption {
5454
}
5555

5656
// Snapshot pushes the current working tree state to a remote preflight ref.
57-
// If there are uncommitted changes, it creates a temporary commit containing
58-
// them without touching the real git index. If the worktree is clean, it
59-
// pushes HEAD directly.
57+
// It always creates a distinct commit on top of HEAD (even when the worktree
58+
// is clean) without touching the real git index.
6059
func Snapshot(dir string, preflightID uuid.UUID, opts ...SnapshotOption) (*SnapshotResult, error) {
6160
cfg := &snapshotConfig{}
6261
for _, opt := range opts {
@@ -101,21 +100,20 @@ func Snapshot(dir string, preflightID uuid.UUID, opts ...SnapshotOption) (*Snaps
101100

102101
branch := fmt.Sprintf("bk/preflight/%s", preflightID.String())
103102
ref := fmt.Sprintf("refs/heads/%s", branch)
104-
commit := head
105103

106-
if len(files) > 0 {
107-
// Write the tree object.
108-
tree, err := gitOutput(dir, env, cfg.debug, "write-tree")
109-
if err != nil {
110-
return nil, err
111-
}
104+
// Always write a tree and create a new commit, even when there are no
105+
// local changes. This ensures the preflight branch always points to a
106+
// distinct commit (not shared with HEAD), which allows commit statuses to
107+
// be attributed to the preflight run rather than the base commit.
108+
tree, err := gitOutput(dir, env, cfg.debug, "write-tree")
109+
if err != nil {
110+
return nil, err
111+
}
112112

113-
// Create a commit on top of HEAD.
114-
msg := fmt.Sprintf("Preflight snapshot\n\nPreflight Run ID: %s\nBase Commit: %s", preflightID, head)
115-
commit, err = gitOutput(dir, env, cfg.debug, "commit-tree", tree, "-p", head, "-m", msg)
116-
if err != nil {
117-
return nil, err
118-
}
113+
msg := fmt.Sprintf("Preflight snapshot\n\nPreflight Run ID: %s\nBase Commit: %s", preflightID, head)
114+
commit, err := gitOutput(dir, env, cfg.debug, "commit-tree", tree, "-p", head, "-m", msg)
115+
if err != nil {
116+
return nil, err
119117
}
120118

121119
// Push the commit to the remote branch.

internal/preflight/snapshot_test.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -373,19 +373,28 @@ func TestSnapshot_CleanWorktree(t *testing.T) {
373373
t.Fatalf("Snapshot() error: %v", err)
374374
}
375375

376-
// Should push HEAD directly with no files changed.
377376
head := runGit(t, worktree, "rev-parse", "HEAD")
378-
if result.Commit != head {
379-
t.Errorf("expected HEAD %s, got %s", head, result.Commit)
377+
378+
// Even with a clean worktree a new commit should always be created so
379+
// that commit statuses are attributed to the preflight run rather than
380+
// the shared HEAD commit.
381+
if result.Commit == head {
382+
t.Errorf("expected a new commit distinct from HEAD %s, but got the same SHA", head)
380383
}
381384

382385
if len(result.Files) != 0 {
383386
t.Errorf("expected no changed files, got %d", len(result.Files))
384387
}
385388

386-
// The remote branch should exist and point to HEAD.
389+
// The new commit should be reachable and its parent should be HEAD.
390+
parent := runGit(t, worktree, "rev-parse", result.Commit+"^")
391+
if parent != head {
392+
t.Errorf("expected parent of snapshot commit to be HEAD %s, got %s", head, parent)
393+
}
394+
395+
// The remote branch should exist and point to the new commit.
387396
remoteRef := runGit(t, worktree, "ls-remote", "origin", result.Ref)
388-
if !strings.Contains(remoteRef, head) {
389-
t.Errorf("remote branch should point to HEAD %s, got %q", head, remoteRef)
397+
if !strings.Contains(remoteRef, result.Commit) {
398+
t.Errorf("remote branch should point to snapshot commit %s, got %q", result.Commit, remoteRef)
390399
}
391400
}

0 commit comments

Comments
 (0)