-
Notifications
You must be signed in to change notification settings - Fork 78
/
util.go
60 lines (49 loc) · 1.25 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package sourceproviders
import (
"fmt"
"github.com/go-git/go-git/v5" // with go modules enabled (GO111MODULE=on or outside GOPATH)
"github.com/go-git/go-git/v5/plumbing"
)
// func switchBranch(repoPath string, branchName string) error {
// // Open an existing repository
// r, err := git.PlainOpen(repoPath)
// if err != nil {
// return err
// }
// // Get the worktree for the repository
// w, err := r.Worktree()
// if err != nil {
// return err
// }
// // Checkout the specified branch
// err = w.Checkout(&git.CheckoutOptions{
// Branch: plumbing.NewBranchReferenceName(branchName),
// })
// if err != nil {
// return err
// }
// fmt.Println("Switched to branch:", branchName)
// return nil
// }
func checkoutRevision(repoPath string, revision string) error {
// Open an existing repository
r, err := git.PlainOpen(repoPath)
if err != nil {
return err
}
// Get the worktree for the repository
w, err := r.Worktree()
if err != nil {
return err
}
// Checkout the specified revision
// For a commit or tag, use `plumbing.Revision` to resolve the hash
err = w.Checkout(&git.CheckoutOptions{
Hash: plumbing.NewHash(revision),
})
if err != nil {
return err
}
fmt.Println("Checked out to revision:", revision)
return nil
}