From cd1577af245c499fb5d825aa46a5b149728369cb Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Fri, 3 Dec 2021 15:28:16 +0000 Subject: [PATCH] post-checkout: don't modify permissions of untracked files Git doesn't know about untracked files, so we should avoid modifying them when we adjust permissions in the post-checkout hook. Add an option to our invocation of git ls-files that controls the use of the --others flag (which controls listing untracked files), and disable it when we're looking for files to process with the post-checkout hook. --- git/attribs.go | 2 +- git/ls_files.go | 6 ++++-- locking/lockable.go | 2 +- t/t-post-checkout.sh | 4 ++++ t/t-track.sh | 1 + 5 files changed, 11 insertions(+), 4 deletions(-) diff --git a/git/attribs.go b/git/attribs.go index 528dc3ce..ba3955ce 100644 --- a/git/attribs.go +++ b/git/attribs.go @@ -177,7 +177,7 @@ func findAttributeFiles(workingDir, gitDir string) []attrFile { paths = append(paths, attrFile{path: repoAttributes, readMacros: true}) } - lsFiles, err := NewLsFiles(workingDir, true) + lsFiles, err := NewLsFiles(workingDir, true, true) if err != nil { tracerx.Printf("Error finding .gitattributes: %v", err) return paths diff --git a/git/ls_files.go b/git/ls_files.go index e6e3f176..f9f386ea 100644 --- a/git/ls_files.go +++ b/git/ls_files.go @@ -20,18 +20,20 @@ type LsFiles struct { FilesByName map[string][]*lsFileInfo } -func NewLsFiles(workingDir string, standardExclude bool) (*LsFiles, error) { +func NewLsFiles(workingDir string, standardExclude bool, untracked bool) (*LsFiles, error) { args := []string{ "ls-files", "-z", // Use a NUL separator. This also disables the escaping of special characters. - "--others", "--cached", } if standardExclude { args = append(args, "--exclude-standard") } + if untracked { + args = append(args, "--others") + } cmd := gitNoLFS(args...) cmd.Dir = workingDir diff --git a/locking/lockable.go b/locking/lockable.go index 89ff45d4..a7e3f85b 100644 --- a/locking/lockable.go +++ b/locking/lockable.go @@ -113,7 +113,7 @@ func (c *Client) FixFileWriteFlagsInDir(dir string, lockablePatterns, unlockable func (c *Client) fixFileWriteFlags(absPath, workingDir string, lockable, unlockable *filepathfilter.Filter) error { // Build a list of files - lsFiles, err := git.NewLsFiles(workingDir, !c.ModifyIgnoredFiles) + lsFiles, err := git.NewLsFiles(workingDir, !c.ModifyIgnoredFiles, false) if err != nil { return err } diff --git a/t/t-post-checkout.sh b/t/t-post-checkout.sh index bce50649..4a756011 100755 --- a/t/t-post-checkout.sh +++ b/t/t-post-checkout.sh @@ -62,6 +62,8 @@ begin_test "post-checkout" # this will be main + touch untracked.dat + [ "$(cat file1.dat)" == "file 1 updated commit 2" ] [ "$(cat file2.dat)" == "file 2 updated commit 3" ] [ "$(cat file3.big)" == "file 3 creation" ] @@ -71,6 +73,7 @@ begin_test "post-checkout" # without the post-checkout hook, any changed files would now be writeable refute_file_writeable file1.dat refute_file_writeable file2.dat + assert_file_writeable untracked.dat assert_file_writeable file3.big assert_file_writeable file4.big @@ -81,6 +84,7 @@ begin_test "post-checkout" refute_file_writeable file1.dat refute_file_writeable file2.dat refute_file_writeable file5.dat + assert_file_writeable untracked.dat assert_file_writeable file3.big assert_file_writeable file4.big assert_file_writeable file6.big diff --git a/t/t-track.sh b/t/t-track.sh index 921fb284..38ae0e41 100755 --- a/t/t-track.sh +++ b/t/t-track.sh @@ -446,6 +446,7 @@ begin_test "track lockable read-only/read-write" mkdir subfolder echo "sub blah blah" > subfolder/test.bin echo "sub foo bar" > subfolder/test.dat + git add *.bin *.dat subfolder # should start writeable assert_file_writeable test.bin assert_file_writeable test.dat