diff --git a/internal/diff/compare.go b/internal/diff/compare.go index 57c292f..c7f414c 100644 --- a/internal/diff/compare.go +++ b/internal/diff/compare.go @@ -171,6 +171,12 @@ func diffDotfiles(systemURL, referenceURL string) *DotfilesDiff { dd.RepoChanged = &ValueChange{System: systemURL, Reference: referenceURL} } + // Only check local dotfiles repo state if dotfiles are actually configured + // If both URLs are empty, there's no dotfiles setup to check + if sysNorm == "" && refNorm == "" { + return dd + } + // Check local dotfiles repo for dirty state home, err := os.UserHomeDir() if err != nil { diff --git a/internal/system/system.go b/internal/system/system.go index f22be6e..a920f12 100644 --- a/internal/system/system.go +++ b/internal/system/system.go @@ -70,11 +70,18 @@ func InstallHomebrew() error { } func GetGitConfig(key string) string { + // Try global first (most common) output, err := RunCommandSilent("git", "config", "--global", key) - if err != nil { - return "" + if err == nil && output != "" { + return output + } + // Fall back to any available config (local, system, etc.) + output, err = RunCommandSilent("git", "config", key) + if err == nil { + return output } - return output + + return "" } func GetExistingGitConfig() (name, email string) { diff --git a/internal/system/system_test.go b/internal/system/system_test.go index 84ca188..8c82f20 100644 --- a/internal/system/system_test.go +++ b/internal/system/system_test.go @@ -302,3 +302,38 @@ func TestRunCommandSilent_MultilineOutput(t *testing.T) { assert.Contains(t, output, "line2") assert.Contains(t, output, "line3") } + +// TestGetGitConfig_FallsBackToAnyScope verifies that GetGitConfig checks all git config scopes, +// not just --global. This handles cases where user.name/user.email are set in local or system config. +// Regression test for: git config detection issue +func TestGetGitConfig_FallsBackToAnyScope(t *testing.T) { + tmpDir := t.TempDir() + t.Setenv("HOME", tmpDir) + + // Create a git repo in tmpDir + cmd := exec.Command("git", "init") + cmd.Dir = tmpDir + if err := cmd.Run(); err != nil { + t.Skip("git not installed, skipping") + } + + // Set a local config value (not global) + cmd = exec.Command("git", "config", "user.localtest", "local-value") + cmd.Dir = tmpDir + require.NoError(t, cmd.Run()) + + // Change to the tmpDir so git can find the local config + oldWd, _ := os.Getwd() + os.Chdir(tmpDir) + defer os.Chdir(oldWd) + + // Set XDG_CONFIG_HOME to avoid reading global git config + oldXdg := os.Getenv("XDG_CONFIG_HOME") + os.Setenv("XDG_CONFIG_HOME", tmpDir+"/nonexistent") + defer os.Setenv("XDG_CONFIG_HOME", oldXdg) + + // The global config should be empty since we changed HOME + // but the local config should be found via fallback + value := GetGitConfig("user.localtest") + assert.Equal(t, "local-value", value, "GetGitConfig should fall back to local config when global is not set") +}