-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathmain.go
211 lines (181 loc) · 4.67 KB
/
main.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
import (
"flag"
"fmt"
"log"
"os"
"time"
"github.com/mitchellh/go-homedir"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/object"
"gopkg.in/src-d/go-git.v4/plumbing/transport"
"gopkg.in/src-d/go-git.v4/plumbing/transport/http"
"gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
)
var (
pull = flag.Bool("pull", true, "automatically pull changes")
push = flag.Bool("push", true, "automatically push changes")
author = flag.String("author", "gitomatic", "author name for git commits")
email = flag.String("email", "gitomatic@fribbledom.com", "email address for git commits")
interval = flag.String("interval", "1m", "how often to check for changes")
privkey = flag.String("privkey", "~/.ssh/id_rsa", "location of private key used for auth")
username = flag.String("username", "", "username used for auth")
password = flag.String("password", "", "password used for auth")
)
func fatal(format string, a ...interface{}) {
fmt.Printf(format, a...)
os.Exit(1)
}
func gitAdd(w *git.Worktree, path string) error {
log.Printf("Adding file to work-tree: %s\n", path)
_, err := w.Add(path)
return err
}
func gitRemove(w *git.Worktree, path string) error {
log.Printf("Removing file from work-tree: %s\n", path)
_, err := w.Remove(path)
return err
}
func gitCommit(w *git.Worktree, message string) error {
log.Printf("Creating commit: %s", message)
_, err := w.Commit(message, &git.CommitOptions{
Author: &object.Signature{
Name: *author,
Email: *email,
When: time.Now(),
},
})
return err
}
func gitPush(r *git.Repository, auth transport.AuthMethod) error {
if !gitHasRemote(r) {
log.Println("Not pushing: no remotes configured.")
return nil
}
log.Println("Pushing changes...")
return r.Push(&git.PushOptions{
Auth: auth,
})
}
func gitPull(r *git.Repository, w *git.Worktree, auth transport.AuthMethod) error {
if !gitHasRemote(r) {
log.Println("Not pulling: no remotes configured.")
return nil
}
log.Println("Pulling changes...")
err := w.Pull(&git.PullOptions{
RemoteName: "origin",
Auth: auth,
})
if err == transport.ErrEmptyRemoteRepository {
return nil
}
if err == git.NoErrAlreadyUpToDate {
return nil
}
return err
}
func gitHasRemote(r *git.Repository) bool {
remotes, _ := r.Remotes()
return len(remotes) > 0
}
func parseAuthArgs() (transport.AuthMethod, error) {
if len(*username) > 0 {
return &http.BasicAuth{
Username: *username,
Password: *password,
}, nil
}
*privkey, _ = homedir.Expand(*privkey)
auth, err := ssh.NewPublicKeysFromFile("git", *privkey, "")
if err != nil {
return nil, err
}
return auth, nil
}
func main() {
fmt.Println("git-o-matic")
flag.Parse()
if len(flag.Args()) < 1 {
fmt.Println("usage: gitomatic <path>")
os.Exit(1)
}
timeout, err := time.ParseDuration(*interval)
if err != nil {
fatal("cannot parse interval: %s\n", err)
}
auth, err := parseAuthArgs()
if err != nil {
fatal("cannot parse key: %s\n", err)
}
path := flag.Args()[0]
for {
log.Println("Checking repository:", path)
r, err := git.PlainOpen(path)
if err != nil {
fatal("cannot open repository: %s\n", err)
}
w, err := r.Worktree()
if err != nil {
fatal("cannot access repository: %s\n", err)
}
if *pull {
err = gitPull(r, w, auth)
if err != nil {
fatal("cannot pull from repository: %s\n", err)
}
}
if *push {
status, err := w.Status()
if err != nil {
fatal("cannot retrieve git status: %s\n", err)
}
changes := 0
msg := ""
for path, s := range status {
switch s.Worktree {
case git.Untracked:
log.Printf("New file detected: %s\n", path)
err := gitAdd(w, path)
if err != nil {
fatal("cannot add file: %s\n", err)
}
msg += fmt.Sprintf("Add %s.\n", path)
changes++
case git.Modified:
log.Printf("Modified file detected: %s\n", path)
err := gitAdd(w, path)
if err != nil {
fatal("cannot add file: %s\n", err)
}
msg += fmt.Sprintf("Update %s.\n", path)
changes++
case git.Deleted:
log.Printf("Deleted file detected: %s\n", path)
err := gitRemove(w, path)
if err != nil {
fatal("cannot remove file: %s\n", err)
}
msg += fmt.Sprintf("Remove %s.\n", path)
changes++
default:
log.Printf("%s %s %s\n", string(s.Worktree), string(s.Staging), path)
}
}
if changes == 0 {
log.Println("No changes detected.")
} else {
err = gitCommit(w, msg)
if err != nil {
fatal("cannot commit: %s\n", err)
}
err = gitPush(r, auth)
if err != nil {
fatal("cannot push: %s\n", err)
}
}
}
log.Printf("Sleeping until next check in %s...\n", timeout)
time.Sleep(timeout)
}
}