Checkout a specific git commit

This commit is contained in:
Shihab Hasan
2020-03-23 12:12:52 +06:00
parent 9f4fead7b5
commit 99b08059c4
2 changed files with 24 additions and 2 deletions
+23 -1
View File
@@ -41,6 +41,28 @@ func (g *Git) UnpackTarFromBuildContext() (string, error) {
if len(parts) > 1 {
options.ReferenceName = plumbing.ReferenceName(parts[1])
}
_, err := git.PlainClone(directory, false, &options)
r, err := git.PlainClone(directory, false, &options)
if err == nil && len(parts) > 2 {
// ... retrieving the commit being pointed by HEAD
_, err := r.Head()
if err != nil {
return directory, err
}
w, err := r.Worktree()
if err != nil {
return directory, err
}
// ... checking out to desired commit
err = w.Checkout(&git.CheckoutOptions{
Hash: plumbing.NewHash(parts[2]),
})
if err != nil {
return directory, err
}
}
return directory, err
}