From 6d133fd61123cf1ced09aaa1eb5016e233e66d09 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Tue, 5 Jul 2022 18:35:24 +1000 Subject: better handling of gitignore files --- pkg/integration/integration.go | 38 +++++++++++++--------- .../excludeGitIgnore/expected/repo/.gitignore | 1 - .../excludeGitIgnore/expected/repo/lg_ignore_file | 1 + test/integration/excludeGitIgnore/test.json | 4 +-- .../expected/repo/.git_keep/info/exclude | 8 ----- .../expected/repo/.git_keep/info/lg_exclude_file | 8 +++++ .../gitignoreMenu/expected/repo/.gitignore | 2 -- .../gitignoreMenu/expected/repo/lg_ignore_file | 2 ++ .../gitignoreMenu/expected/repo/myfile1 | 1 + 9 files changed, 37 insertions(+), 28 deletions(-) delete mode 100644 test/integration/excludeGitIgnore/expected/repo/.gitignore create mode 100644 test/integration/excludeGitIgnore/expected/repo/lg_ignore_file delete mode 100644 test/integration/excludeMenu/expected/repo/.git_keep/info/exclude create mode 100644 test/integration/excludeMenu/expected/repo/.git_keep/info/lg_exclude_file delete mode 100644 test/integration/gitignoreMenu/expected/repo/.gitignore create mode 100644 test/integration/gitignoreMenu/expected/repo/lg_ignore_file create mode 100644 test/integration/gitignoreMenu/expected/repo/myfile1 diff --git a/pkg/integration/integration.go b/pkg/integration/integration.go index 89c906c51..c56134a4a 100644 --- a/pkg/integration/integration.go +++ b/pkg/integration/integration.go @@ -137,7 +137,7 @@ func RunTests( return err } - if err := renameGitDirs(expectedDir); err != nil { + if err := renameSpecialPaths(expectedDir); err != nil { return err } @@ -441,7 +441,7 @@ func generateSnapshots(actualDir string, expectedDir string) (string, string, er return "", "", err } - if err := restoreGitDirs(expectedDirCopyDir); err != nil { + if err := restoreSpecialPaths(expectedDirCopyDir); err != nil { return "", "", err } @@ -458,7 +458,7 @@ func generateSnapshots(actualDir string, expectedDir string) (string, string, er return actual, expected, nil } -func getPathsToRename(dir string, needle string) []string { +func getPathsToRename(dir string, needle string, contains string) []string { pathsToRename := []string{} err := filepath.Walk(dir, func(path string, f os.FileInfo, err error) error { @@ -466,7 +466,7 @@ func getPathsToRename(dir string, needle string) []string { return err } - if f.Name() == needle { + if f.Name() == needle && (contains == "" || strings.Contains(path, contains)) { pathsToRename = append(pathsToRename, path) } @@ -479,14 +479,22 @@ func getPathsToRename(dir string, needle string) []string { return pathsToRename } -// Git refuses to track .git and .gitmodules folders in subdirectories so we need to rename it -// to git_keep after running a test, and then change it back again -var untrackedGitDirs []string = []string{".git", ".gitmodules"} +var specialPathMappings = []struct{ original, new, contains string }{ + // git refuses to track .git or .gitmodules in subdirectories so we need to rename them + {".git", ".git_keep", ""}, + {".gitmodules", ".gitmodules_keep", ""}, + // we also need git to ignore the contents of our test gitignore files so that + // we actually commit files that are ignored within the test. + {".gitignore", "lg_ignore_file", ""}, + // this is the .git/info/exclude file. We're being a little more specific here + // so that we don't accidentally mess with some other file named 'exclude' in the test. + {"exclude", "lg_exclude_file", ".git/info/exclude"}, +} -func renameGitDirs(dir string) error { - for _, untrackedGitDir := range untrackedGitDirs { - for _, path := range getPathsToRename(dir, untrackedGitDir) { - err := os.Rename(path, path+"_keep") +func renameSpecialPaths(dir string) error { + for _, specialPath := range specialPathMappings { + for _, path := range getPathsToRename(dir, specialPath.original, specialPath.contains) { + err := os.Rename(path, filepath.Join(filepath.Dir(path), specialPath.new)) if err != nil { return err } @@ -496,10 +504,10 @@ func renameGitDirs(dir string) error { return nil } -func restoreGitDirs(dir string) error { - for _, untrackedGitDir := range untrackedGitDirs { - for _, path := range getPathsToRename(dir, untrackedGitDir+"_keep") { - err := os.Rename(path, strings.TrimSuffix(path, "_keep")) +func restoreSpecialPaths(dir string) error { + for _, specialPath := range specialPathMappings { + for _, path := range getPathsToRename(dir, specialPath.new, specialPath.contains) { + err := os.Rename(path, filepath.Join(filepath.Dir(path), specialPath.original)) if err != nil { return err } diff --git a/test/integration/excludeGitIgnore/expected/repo/.gitignore b/test/integration/excludeGitIgnore/expected/repo/.gitignore deleted file mode 100644 index a5bce3fd2..000000000 --- a/test/integration/excludeGitIgnore/expected/repo/.gitignore +++ /dev/null @@ -1 +0,0 @@ -test1 diff --git a/test/integration/excludeGitIgnore/expected/repo/lg_ignore_file b/test/integration/excludeGitIgnore/expected/repo/lg_ignore_file new file mode 100644 index 000000000..a5bce3fd2 --- /dev/null +++ b/test/integration/excludeGitIgnore/expected/repo/lg_ignore_file @@ -0,0 +1 @@ +test1 diff --git a/test/integration/excludeGitIgnore/test.json b/test/integration/excludeGitIgnore/test.json index a8ca45bb0..9c466ba11 100644 --- a/test/integration/excludeGitIgnore/test.json +++ b/test/integration/excludeGitIgnore/test.json @@ -1,4 +1,4 @@ { - "description": "In this test .gitignore is added to .git/info/exclude using the ignore or exclude menu to check that this operation is to allowed", + "description": "In this test we attempt to add .gitignore to .git/info/exclude to ensure lazygit rejects the action", "speed": 5 -} \ No newline at end of file +} diff --git a/test/integration/excludeMenu/expected/repo/.git_keep/info/exclude b/test/integration/excludeMenu/expected/repo/.git_keep/info/exclude deleted file mode 100644 index ee02e5c09..000000000 --- a/test/integration/excludeMenu/expected/repo/.git_keep/info/exclude +++ /dev/null @@ -1,8 +0,0 @@ -# git ls-files --others --exclude-from=.git/info/exclude -# Lines that start with '#' are comments. -# For a project mostly in C, the following would be a good set of -# exclude patterns (uncomment them if you want to use them): -# *.[oa] -# *~ - -myfile1 \ No newline at end of file diff --git a/test/integration/excludeMenu/expected/repo/.git_keep/info/lg_exclude_file b/test/integration/excludeMenu/expected/repo/.git_keep/info/lg_exclude_file new file mode 100644 index 000000000..ee02e5c09 --- /dev/null +++ b/test/integration/excludeMenu/expected/repo/.git_keep/info/lg_exclude_file @@ -0,0 +1,8 @@ +# git ls-files --others --exclude-from=.git/info/exclude +# Lines that start with '#' are comments. +# For a project mostly in C, the following would be a good set of +# exclude patterns (uncomment them if you want to use them): +# *.[oa] +# *~ + +myfile1 \ No newline at end of file diff --git a/test/integration/gitignoreMenu/expected/repo/.gitignore b/test/integration/gitignoreMenu/expected/repo/.gitignore deleted file mode 100644 index 959aad479..000000000 --- a/test/integration/gitignoreMenu/expected/repo/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ - -myfile1 \ No newline at end of file diff --git a/test/integration/gitignoreMenu/expected/repo/lg_ignore_file b/test/integration/gitignoreMenu/expected/repo/lg_ignore_file new file mode 100644 index 000000000..959aad479 --- /dev/null +++ b/test/integration/gitignoreMenu/expected/repo/lg_ignore_file @@ -0,0 +1,2 @@ + +myfile1 \ No newline at end of file diff --git a/test/integration/gitignoreMenu/expected/repo/myfile1 b/test/integration/gitignoreMenu/expected/repo/myfile1 new file mode 100644 index 000000000..a5bce3fd2 --- /dev/null +++ b/test/integration/gitignoreMenu/expected/repo/myfile1 @@ -0,0 +1 @@ +test1 -- cgit v1.2.3