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.
This commit is contained in:
brian m. carlson 2021-12-03 15:28:16 +00:00
parent 66d4b17609
commit cd1577af24
No known key found for this signature in database
GPG Key ID: 2D0C9BC12F82B3A1
5 changed files with 11 additions and 4 deletions

@ -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

@ -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

@ -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
}

@ -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

@ -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