Skip to content

Commit 4dfe5e1

Browse files
authored
Fetch using short commit SHA (#160)
## Description This relates to woodpecker-ci/woodpecker#3965 and closes woodpecker-ci/woodpecker#3932. The Bitbucket Pull Request Hook returns a short commit SHA. The git fetch command requires a full-length SHA. Add a clause to detect when the SHA1 has less than 40 characters. Fetch & switch the branch to reset the expected commit. Raise an error if the branch value is not defined. ## Test Run script: ```shell export GOOS=linux export GOARCH=amd64 export CGO_ENABLED=0 export GO111MODULE=on export CI_REPO_CLONE_URL=https://{User}:{Token}@bitbucket.org/{Workspace}/{Project}.git export CI_WORKSPACE=./woodpecker export CI_BUILD_EVENT=push export CI_COMMIT_SHA=692972aabfec export CI_COMMIT_BRANCH=test ./release/linux/amd64/plugin-git ``` Output: ```shell + git init -b test Initialised empty Git repository in /home/pc415/bit2me/plugin-git/woodpecker/.git/ + git config --global --replace-all safe.directory ./woodpecker + git remote add origin https://{User}:{Token}@bitbucket.org/{Workspace}/{Project}.git + git fetch --no-tags --depth=1 --filter=tree:0 origin +test: warning: filtering not recognized by server, ignoring From https://bitbucket.org/{Workspace}/{Project}.git * branch test -> FETCH_HEAD * [new branch] test -> origin/test + git switch -q test + git reset --hard -q 692972aabfec + git submodule update --init --recursive --depth=1 --recommend-shallow + git lfs fetch ```
1 parent 7ac9615 commit 4dfe5e1

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

plugin.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ func (p Plugin) Exec() error {
8181
fmt.Println("using head checkout")
8282
cmds = append(cmds, fetch(p.Pipeline.Ref, p.Config.Tags, p.Config.Depth, p.Config.filter))
8383
cmds = append(cmds, checkoutHead())
84+
} else if len(p.Pipeline.Commit) != 40 && len(p.Pipeline.Commit) != 64 {
85+
// fetch requires full SHA1 (40 chars) or SHA256 (64 chars) commits (unambiguous reference)
86+
// for short SHA1 or SHA256, fetch and switch the branch before commit reset
87+
if p.Config.Branch == "" {
88+
return fmt.Errorf("short commit SHA1 checkout requires a branch")
89+
}
90+
cmds = append(cmds, fetch(p.Config.Branch, p.Config.Tags, p.Config.Depth, p.Config.filter))
91+
cmds = append(cmds, switchBranch(p.Config.Branch))
92+
cmds = append(cmds, checkoutSha(p.Pipeline.Commit))
8493
} else {
8594
// fetch and checkout by commit sha
8695
cmds = append(cmds, fetch(p.Pipeline.Commit, p.Config.Tags, p.Config.Depth, p.Config.filter))
@@ -256,6 +265,16 @@ func checkoutSha(commit string) *exec.Cmd {
256265
), defaultEnvVars...)
257266
}
258267

268+
// Switch executes a git switch command.
269+
func switchBranch(branch string) *exec.Cmd {
270+
return appendEnv(exec.Command(
271+
"git",
272+
"switch",
273+
"-q",
274+
branch,
275+
), defaultEnvVars...)
276+
}
277+
259278
func fetchLFS() *exec.Cmd {
260279
return appendEnv(exec.Command(
261280
"git", "lfs",

plugin_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,17 @@ var commits = []struct {
9898
file: "README",
9999
data: "Hello World!\n\nsomething is changed!\n",
100100
},
101+
// checkout with short SHA!
102+
{
103+
path: "octocat/Hello-World",
104+
clone: "https://github.com/octocat/Hello-World.git",
105+
event: "pull_request",
106+
branch: "test",
107+
commit: "7629413",
108+
ref: "",
109+
file: "README",
110+
data: "Hello World!\n",
111+
},
101112
// ### test lfs, please do not change order, otherwise TestCloneNonEmpty will fail ###
102113
// checkout with lfs skip
103114
{
@@ -143,6 +154,7 @@ func TestClone(t *testing.T) {
143154
Recursive: c.recursive,
144155
Lfs: c.lfs,
145156
Home: "/tmp",
157+
Branch: c.branch,
146158
},
147159
}
148160

@@ -190,6 +202,7 @@ func TestCloneNonEmpty(t *testing.T) {
190202
Recursive: c.recursive,
191203
Lfs: c.lfs,
192204
Home: "/tmp",
205+
Branch: c.branch,
193206
},
194207
}
195208

@@ -372,6 +385,33 @@ func TestCustomCertFile(t *testing.T) {
372385
}
373386
}
374387

388+
func TestSwitchBranch(t *testing.T) {
389+
testdata := []struct {
390+
exp []string
391+
}{
392+
{
393+
[]string{
394+
"git",
395+
"switch",
396+
"-q",
397+
"test",
398+
},
399+
},
400+
}
401+
402+
for _, td := range testdata {
403+
c := switchBranch("test")
404+
if len(c.Args) != len(td.exp) {
405+
t.Errorf("Expected: %s, got %s", td.exp, c.Args)
406+
}
407+
for i := range c.Args {
408+
if c.Args[i] != td.exp[i] {
409+
t.Errorf("Expected: %s, got %s", td.exp, c.Args)
410+
}
411+
}
412+
}
413+
}
414+
375415
// TestUpdateSubmodules tests if the arguments to `git submodule update`
376416
// are constructed properly.
377417
func TestUpdateSubmodulesRemote(t *testing.T) {

0 commit comments

Comments
 (0)