catch new untracked and unignored files (#3875)

This commit is contained in:
Avi Deitcher 2022-11-20 10:29:05 -05:00 committed by GitHub
parent dee4c37648
commit 7c5b1f1b30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -88,11 +88,17 @@ func (g git) isWorkTree(pkg string) (bool, error) {
func (g git) contentHash() (string, error) { func (g git) contentHash() (string, error) {
hash := sha256.New() hash := sha256.New()
out, err := g.commandStdout(nil, "ls-files") // list of files tracked by git that might have changed
trackedFiles, err := g.commandStdout(nil, "ls-files")
if err != nil { if err != nil {
return "", err return "", err
} }
scanner := bufio.NewScanner(strings.NewReader(strings.TrimSpace(out))) untrackedFiles, err := g.commandStdout(nil, "ls-files", "--exclude-standard", "--others")
if err != nil {
return "", err
}
allFiles := strings.Join([]string{trackedFiles, untrackedFiles}, "\n")
scanner := bufio.NewScanner(strings.NewReader(strings.TrimSpace(allFiles)))
for scanner.Scan() { for scanner.Scan() {
filename := filepath.Join(g.dir, scanner.Text()) filename := filepath.Join(g.dir, scanner.Text())
info, err := os.Lstat(filename) info, err := os.Lstat(filename)
@ -188,10 +194,15 @@ func (g git) isDirty(pkg, commit string) (bool, error) {
return false, err return false, err
} }
// diff-index works pretty well, except that
err := g.command("diff-index", "--quiet", commit, "--", pkg) err := g.command("diff-index", "--quiet", commit, "--", pkg)
if err == nil { if err == nil {
// this returns an error if there are *no* untracked files, which is strange, but we can work with it
if _, err := g.commandStdout(nil, "ls-files", "--exclude-standard", "--others", "--error-unmatch", "--", pkg); err != nil {
return false, nil return false, nil
} }
return true, nil
}
switch err.(type) { switch err.(type) {
case *exec.ExitError: case *exec.ExitError:
// diff-index exits with an error if there are differences // diff-index exits with an error if there are differences