Skip to content

Commit 6a2287e

Browse files
committed
fix support for empty git ref with subdir
Signed-off-by: CrazyMax <[email protected]>
1 parent b5c9183 commit 6a2287e

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

util/gitutil/git_ref.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,12 @@ func ParseGitRef(ref string) (*GitRef, error) {
7373
}
7474
}
7575

76-
refSplitBySharp := strings.SplitN(ref, "#", 2)
77-
res.Remote = refSplitBySharp[0]
76+
var fragment string
77+
res.Remote, fragment, _ = strings.Cut(ref, "#")
7878
if len(res.Remote) == 0 {
7979
return res, errdefs.ErrInvalidArgument
8080
}
81-
82-
if len(refSplitBySharp) > 1 {
83-
refSplitBySharpSplitByColon := strings.SplitN(refSplitBySharp[1], ":", 2)
84-
res.Commit = refSplitBySharpSplitByColon[0]
85-
if len(res.Commit) == 0 {
86-
return res, errdefs.ErrInvalidArgument
87-
}
88-
if len(refSplitBySharpSplitByColon) > 1 {
89-
res.SubDir = refSplitBySharpSplitByColon[1]
90-
}
91-
}
81+
res.Commit, res.SubDir, _ = strings.Cut(fragment, ":")
9282
repoSplitBySlash := strings.Split(res.Remote, "/")
9383
res.ShortName = strings.TrimSuffix(repoSplitBySlash[len(repoSplitBySlash)-1], ".git")
9484
return res, nil

util/gitutil/git_ref_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ func TestParseGitRef(t *testing.T) {
110110
ref: "http://github.com/docker/docker.git:#branch",
111111
expected: nil,
112112
},
113+
{
114+
ref: "https://github.com/docker/docker.git#:myfolder",
115+
expected: &GitRef{
116+
Remote: "https://github.com/docker/docker.git",
117+
ShortName: "docker",
118+
SubDir: "myfolder",
119+
},
120+
},
113121
}
114122
for _, tt := range cases {
115123
tt := tt

0 commit comments

Comments
 (0)