Skip to content

Commit

Permalink
Merge pull request #5698 from bk2204/checkout-ignore-missing
Browse files Browse the repository at this point in the history
checkout: gracefully handle files deleted from the index
  • Loading branch information
bk2204 committed Apr 16, 2024
2 parents 2502e92 + d329917 commit 3d1ca20
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
26 changes: 20 additions & 6 deletions commands/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"io"
"os"
"strings"
"sync"

"github.com/git-lfs/git-lfs/v3/config"
Expand Down Expand Up @@ -69,14 +70,27 @@ func (c *singleCheckout) Run(p *lfs.WrappedPointer) {

// Check the content - either missing or still this pointer (not exist is ok)
filepointer, err := lfs.DecodePointerFromFile(cwdfilepath)
if err != nil && !os.IsNotExist(err) {
if errors.IsNotAPointerError(err) || errors.IsBadPointerKeyError(err) {
// File has non-pointer content, leave it alone
if err != nil {
if os.IsNotExist(err) {
output, err := git.DiffIndexWithPaths("HEAD", true, []string{p.Name})
if err != nil {
LoggedError(err, tr.Tr.Get("Checkout error trying to run diff-index: %s", err))
return
}
if strings.HasPrefix(output, ":100644 000000 ") || strings.HasPrefix(output, ":100755 000000 ") {
// This file is deleted in the index. Don't try
// to check it out.
return
}
} else {
if errors.IsNotAPointerError(err) || errors.IsBadPointerKeyError(err) {
// File has non-pointer content, leave it alone
return
}

LoggedError(err, tr.Tr.Get("Checkout error: %s", err))
return
}

LoggedError(err, tr.Tr.Get("Checkout error: %s", err))
return
}

if filepointer != nil && filepointer.Oid != p.Oid {
Expand Down
17 changes: 17 additions & 0 deletions git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,23 @@ func DiffIndex(ref string, cached bool, refresh bool, workingDir string) (*bufio
return bufio.NewScanner(cmd.Stdout), nil
}

func DiffIndexWithPaths(ref string, cached bool, paths []string) (string, error) {
args := []string{"diff-index"}
if cached {
args = append(args, "--cached")
}
args = append(args, ref)
args = append(args, "--")
args = append(args, paths...)

output, err := gitSimple(args...)
if err != nil {
return "", err
}

return output, nil
}

func HashObject(r io.Reader) (string, error) {
cmd, err := gitNoLFS("hash-object", "--stdin")
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions t/t-checkout.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ begin_test "checkout"
grep 'accepting "file1.dat"' checkout.log
grep 'rejecting "file1.dat"' checkout.log && exit 1

git rm file1.dat

echo "checkout should skip replacing files deleted in index"
git lfs checkout
[ ! -f file1.dat ]

git reset --hard

# Remove the working directory
rm -rf file1.dat file2.dat file3.dat folder1/nested.dat folder2/nested.dat

Expand Down

0 comments on commit 3d1ca20

Please sign in to comment.