-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit_utils.go
107 lines (92 loc) · 2.34 KB
/
git_utils.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"errors"
"fmt"
"regexp"
"time"
"github.com/go-git/go-git/v5" // with go modules enabled (GO111MODULE=on or outside GOPATH)
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
"github.com/kevinburke/ssh_config"
)
func CheckIfError(err error) {
if err == nil {
return
}
fmt.Printf("error aaa: %s", err)
}
func GetWorktree(repo *git.Repository) *git.Worktree {
tree, err := repo.Worktree()
CheckIfError(err)
return tree
}
func HasChanges(repo *git.Repository) bool {
tree := GetWorktree(repo)
_, err := tree.Add(".")
CheckIfError(err)
status, err := tree.Status()
CheckIfError(err)
count := 0
for range status {
count++
}
return count > 0
}
func OpenRepo(path string) *git.Repository {
expandedPath := expand(path)
repo, err := git.PlainOpen(expandedPath)
CheckIfError(err)
return repo
}
func CommitAll(repo *git.Repository, message string, signature *object.Signature) {
tree := GetWorktree(repo)
_, err := tree.Add(".")
CheckIfError(err)
commit, err := tree.Commit(message, &git.CommitOptions{
All: true,
Author: &object.Signature{
Name: "git-saver",
When: time.Now(),
},
})
CheckIfError(err)
_, err = repo.CommitObject(commit)
CheckIfError(err)
}
func Pull(repo *git.Repository) {
tree := GetWorktree(repo)
publicKeys := GetAuth(repo)
err := tree.Pull(&git.PullOptions{RemoteName: "origin", Auth: publicKeys})
if err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) {
fmt.Printf("error while pulling: %s", err)
}
}
func Push(repo *git.Repository) (hasPushed bool) {
publicKeys := GetAuth(repo)
err := repo.Push(&git.PushOptions{
Auth: publicKeys,
})
if err != nil {
if !errors.Is(err, git.NoErrAlreadyUpToDate) {
fmt.Printf("error000: %s", err)
}
return false
}
return true
}
func GetAuth(repo *git.Repository) *ssh.PublicKeys {
host := GetHostOfOriginRemote(repo)
privateKeyFilepath := ssh_config.Get(host, "IdentityFile")
user := ssh_config.Get(host, "User")
expandedPath := expand(privateKeyFilepath)
publicKeys, err := ssh.NewPublicKeysFromFile(user, expandedPath, "")
CheckIfError(err)
return publicKeys
}
func GetHostOfOriginRemote(repo *git.Repository) string {
cfg, err := repo.Config()
CheckIfError(err)
r, _ := regexp.Compile("@(.*)+:")
host := r.FindStringSubmatch(cfg.Remotes["origin"].URLs[0])
return host[1]
}