Skip to content

Commit f86997f

Browse files
author
Olivier Monnier
committed
Fix 'git lfs fetch' with a sha1 ref
The commit 3967619 introduced a regression in the capability to fetch a sha1 ref. The command 'git lfs fetch origin <sha1>' was returning following error: 'Invalid ref argument: [<sha1>]'. This is due to the fact that 'git rev-parse --symbolic-full-name <ref>' returns an empty content if the ref is a sha1. Signed-off-by: Olivier Monnier <olivier.monnier@intel.com>
1 parent 94fc09e commit f86997f

4 files changed

Lines changed: 25 additions & 2 deletions

File tree

git/git.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,21 @@ func ResolveRef(ref string) (*Ref, error) {
6767
if err != nil {
6868
return nil, err
6969
}
70-
lines := strings.Split(outp, "\n")
71-
if len(lines) <= 1 {
70+
if outp == "" {
7271
return nil, fmt.Errorf("Git can't resolve ref: %q", ref)
7372
}
7473

74+
lines := strings.Split(outp, "\n")
7575
fullref := &Ref{Sha: lines[0]}
76+
77+
if len(lines) == 1 {
78+
// ref is a sha1 and has no symbolic-full-name
79+
fullref.Name = lines[0] // fullref.Sha
80+
fullref.Type = RefTypeOther
81+
return fullref, nil
82+
}
83+
84+
// parse the symbolic-full-name
7685
fullref.Type, fullref.Name = ParseRefToTypeAndName(lines[1])
7786
return fullref, nil
7887
}

git/git_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ func TestCurrentRefAndCurrentRemoteRef(t *testing.T) {
7171
remote, err := RemoteForCurrentBranch()
7272
assert.Nil(t, err)
7373
assert.Equal(t, "origin", remote)
74+
75+
ref, err = ResolveRef(outputs[2].Sha)
76+
assert.Nil(t, err)
77+
assert.Equal(t, &Ref{outputs[2].Sha, RefTypeOther, outputs[2].Sha}, ref)
7478
}
7579

7680
func TestRecentBranches(t *testing.T) {

test/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ and the remote repository data in `test/remote`.
7777
tests when you're running the same test script multiple times without changing
7878
any Go code.
7979

80+
Also ensure that your `noproxy` environment variable contains `127.0.0.1` host,
81+
to allow git commands to reach the local Git server `lfstest-gitserver`.
82+
8083
### Test Suite
8184

8285
The `testenv.sh` script includes some global variables used in tests. This

test/test-fetch.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ begin_test "fetch"
8080
assert_local_object "$contents_oid" 1
8181
assert_local_object "$b_oid" 1
8282

83+
# test with commit sha1 specified
84+
rm -rf .git/lfs/objects
85+
newbranch_sha1=$(git rev-parse newbranch)
86+
git lfs fetch origin "$newbranch_sha1"
87+
assert_local_object "$contents_oid" 1
88+
assert_local_object "$b_oid" 1
89+
8390
# Test include / exclude filters supplied in gitconfig
8491
rm -rf .git/lfs/objects
8592
git config "lfs.fetchinclude" "a*"

0 commit comments

Comments
 (0)