diff --git a/pkg/commands/git_commands/working_tree.go b/pkg/commands/git_commands/working_tree.go index 664436066..7aafe3655 100644 --- a/pkg/commands/git_commands/working_tree.go +++ b/pkg/commands/git_commands/working_tree.go @@ -246,7 +246,11 @@ func (self *WorkingTreeCommands) Ignore(filename string) error { // Exclude adds a file to the .git/info/exclude for the repo func (self *WorkingTreeCommands) Exclude(filename string) error { - excludeFile := filepath.Join(self.repoPaths.repoGitDirPath, "info", "exclude") + infoDir := filepath.Join(self.repoPaths.repoGitDirPath, "info") + if err := os.MkdirAll(infoDir, 0o755); err != nil { + return err + } + excludeFile := filepath.Join(infoDir, "exclude") return self.os.AppendLineToFile(excludeFile, escapeFilename(filename)) } diff --git a/pkg/integration/tests/file/exclude_without_info_dir.go b/pkg/integration/tests/file/exclude_without_info_dir.go new file mode 100644 index 000000000..6e88de747 --- /dev/null +++ b/pkg/integration/tests/file/exclude_without_info_dir.go @@ -0,0 +1,32 @@ +package file + +import ( + "github.com/jesseduffield/lazygit/pkg/config" + . "github.com/jesseduffield/lazygit/pkg/integration/components" +) + +var ExcludeWithoutInfoDir = NewIntegrationTest(NewIntegrationTestArgs{ + Description: "Exclude a file when .git/info directory does not exist", + ExtraCmdArgs: []string{}, + Skip: false, + SetupConfig: func(config *config.AppConfig) { + }, + SetupRepo: func(shell *Shell) { + // Remove .git/info directory to reproduce #5302 + shell.RunCommand([]string{"rm", "-rf", ".git/info"}) + shell.CreateFile("toExclude", "") + }, + Run: func(t *TestDriver, keys config.KeybindingConfig) { + t.Views().Files(). + IsFocused(). + Focus(). + NavigateToLine(Contains("toExclude")). + Press(keys.Files.IgnoreFile). + Tap(func() { + t.ExpectPopup().Menu().Title(Equals("Ignore or exclude file")).Select(Contains("Add to .git/info/exclude")).Confirm() + + // Should succeed without error, creating .git/info/ directory automatically + t.FileSystem().FileContent(".git/info/exclude", Contains("/toExclude")) + }) + }, +}) diff --git a/pkg/integration/tests/test_list.go b/pkg/integration/tests/test_list.go index 1fdd4c161..c336cce1f 100644 --- a/pkg/integration/tests/test_list.go +++ b/pkg/integration/tests/test_list.go @@ -221,6 +221,7 @@ var tests = []*components.IntegrationTest{ file.DiscardUnstagedRangeSelect, file.DiscardVariousChanges, file.DiscardVariousChangesRangeSelect, + file.ExcludeWithoutInfoDir, file.Gitignore, file.GitignoreSpecialCharacters, file.RememberCommitMessageAfterFail,