diff --git a/.gitignore b/.gitignore index 450fd229..7e61ed8f 100644 --- a/.gitignore +++ b/.gitignore @@ -8,7 +8,9 @@ *.so *.dylib /uncloud +/uncloud-* /uncloudd +/uncloudd-* # OS X .DS_Store diff --git a/internal/gitutil/state.go b/internal/gitutil/state.go index 9cda871f..0b4ab1d0 100644 --- a/internal/gitutil/state.go +++ b/internal/gitutil/state.go @@ -36,10 +36,12 @@ func InspectGitState(dir string) (GitState, error) { return state, nil } - // Get the current commit SHA. - sha, err := gitCommand(dir, "rev-parse", "HEAD") + // Get the current commit SHA. An initialised but empty repo has no HEAD yet, + // so treat it as a non-repo so callers can fall back to non-git logic. + sha, err := gitCommand(dir, "rev-parse", "--verify", "HEAD") if err != nil { - return state, fmt.Errorf("get current commit SHA: %w", err) + state.IsRepo = false + return state, nil } state.SHA = strings.TrimSpace(sha) diff --git a/internal/gitutil/state_test.go b/internal/gitutil/state_test.go index 1533a1e3..5b09df0a 100644 --- a/internal/gitutil/state_test.go +++ b/internal/gitutil/state_test.go @@ -46,6 +46,22 @@ func TestInspectGitState_NotARepo(t *testing.T) { assert.False(t, state.IsDirty) } +func TestInspectGitState_EmptyRepo(t *testing.T) { + // Create a temporary git repo with no commits. + tmpDir := t.TempDir() + initGitRepo(t, tmpDir) + + state, err := InspectGitState(tmpDir) + require.NoError(t, err) + + // An empty repo should be treated as a non-repo so that callers fall back to non-git logic. + assert.False(t, state.IsRepo) + assert.Empty(t, state.SHA) + assert.Empty(t, state.ShortSHA(7)) + assert.True(t, state.Date.IsZero()) + assert.False(t, state.IsDirty) +} + func TestInspectGitState_CleanRepo(t *testing.T) { // Create a temporary git repo. tmpDir := t.TempDir()