Skip to content

Commit 1f17253

Browse files
author
cobyfrombrooklyn-bot
committed
Fix #5302: Create .git/info directory before writing exclude file
When .git/info directory does not exist (can happen with bare clones or manual deletion), the Exclude function failed with 'no such file or directory'. Added os.MkdirAll to create the directory before opening the exclude file. Added integration test exclude_without_info_dir that removes .git/info before attempting to exclude a file.
1 parent 818ca17 commit 1f17253

3 files changed

Lines changed: 38 additions & 1 deletion

File tree

pkg/commands/git_commands/working_tree.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,11 @@ func (self *WorkingTreeCommands) Ignore(filename string) error {
246246

247247
// Exclude adds a file to the .git/info/exclude for the repo
248248
func (self *WorkingTreeCommands) Exclude(filename string) error {
249-
excludeFile := filepath.Join(self.repoPaths.repoGitDirPath, "info", "exclude")
249+
infoDir := filepath.Join(self.repoPaths.repoGitDirPath, "info")
250+
if err := os.MkdirAll(infoDir, 0o755); err != nil {
251+
return err
252+
}
253+
excludeFile := filepath.Join(infoDir, "exclude")
250254
return self.os.AppendLineToFile(excludeFile, escapeFilename(filename))
251255
}
252256

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package file
2+
3+
import (
4+
"github.com/jesseduffield/lazygit/pkg/config"
5+
. "github.com/jesseduffield/lazygit/pkg/integration/components"
6+
)
7+
8+
var ExcludeWithoutInfoDir = NewIntegrationTest(NewIntegrationTestArgs{
9+
Description: "Exclude a file when .git/info directory does not exist",
10+
ExtraCmdArgs: []string{},
11+
Skip: false,
12+
SetupConfig: func(config *config.AppConfig) {
13+
},
14+
SetupRepo: func(shell *Shell) {
15+
// Remove .git/info directory to reproduce #5302
16+
shell.RunCommand([]string{"rm", "-rf", ".git/info"})
17+
shell.CreateFile("toExclude", "")
18+
},
19+
Run: func(t *TestDriver, keys config.KeybindingConfig) {
20+
t.Views().Files().
21+
IsFocused().
22+
Focus().
23+
NavigateToLine(Contains("toExclude")).
24+
Press(keys.Files.IgnoreFile).
25+
Tap(func() {
26+
t.ExpectPopup().Menu().Title(Equals("Ignore or exclude file")).Select(Contains("Add to .git/info/exclude")).Confirm()
27+
28+
// Should succeed without error, creating .git/info/ directory automatically
29+
t.FileSystem().FileContent(".git/info/exclude", Contains("/toExclude"))
30+
})
31+
},
32+
})

pkg/integration/tests/test_list.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ var tests = []*components.IntegrationTest{
221221
file.DiscardUnstagedRangeSelect,
222222
file.DiscardVariousChanges,
223223
file.DiscardVariousChangesRangeSelect,
224+
file.ExcludeWithoutInfoDir,
224225
file.Gitignore,
225226
file.GitignoreSpecialCharacters,
226227
file.RememberCommitMessageAfterFail,

0 commit comments

Comments
 (0)