src-d / go-git

Project has been moved to: https://github.com/go-git/go-git
https://github.com/go-git/go-git
Apache License 2.0
4.91k stars 542 forks source link

fix 'git add' on executable files #1272

Open greenfoo opened 4 years ago

greenfoo commented 4 years ago

Before this change, when adding an "executable" file with worktree.Add(), something really strange happened: the git index file entry associated with that file had its SHA1 updated but not its size; which caused the file to appear both as "staged" and "unstaged" when running git status:

$ git status
On branch xxx
Your branch is ahead of 'origin/r54+x' by 1 commit.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   ci/integrity_checks.py <-------------------------- HERE

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   ci/integrity_checks.py <-------------------------- AND HERE

However, when running git diff, nothing shows up (ie. the fact that this file appears as unstaged is just an artifact of the file length not matching the one registered on the index)

I guess (but I could be wrong) that the original intent of the line I changed was to avoid updating the size when dealing with a folder or a pipe or something like that... but for some reason, IsRegular() was used instead of IsFile().

If there is some hidden reason to use IsRegular() that I'm not aware of, please simply discard this pull request.