Skip to content

Commit 1ea5b7c

Browse files
committed
Added -pull and -push flags
1 parent 50018c4 commit 1ea5b7c

File tree

2 files changed

+69
-53
lines changed

2 files changed

+69
-53
lines changed

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,20 @@ gitomatic -privkey ~/.ssh/id_rsa <path>
5050
gitomatic -username "someone" -password "mypass" <path>
5151
```
5252

53-
Other parameters:
53+
If you want to pull new changes but don't create commits (or vice versa):
54+
55+
```
56+
gitomatic -pull=true -push=false <path>
57+
```
58+
59+
You can control how often gitomatic checks for changes:
5460

5561
```
5662
gitomatic -interval 30m <path>
57-
gitomatic -author "John Doe" <path>
58-
gitomatic -email "[email protected]" <path>
63+
```
64+
65+
Change the commit author's name and email:
66+
67+
```
68+
gitomatic -author "John Doe" -email "[email protected]" <path>
5969
```

main.go

Lines changed: 56 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
)
1717

1818
var (
19+
pull = flag.Bool("pull", true, "automatically pull changes")
20+
push = flag.Bool("push", true, "automatically push changes")
1921
author = flag.String("author", "gitomatic", "author name for git commits")
2022
email = flag.String("email", "[email protected]", "email address for git commits")
2123
interval = flag.String("interval", "1m", "how often to check for changes")
@@ -137,65 +139,69 @@ func main() {
137139
fatal("cannot access repository: %s\n", err)
138140
}
139141

140-
err = gitPull(r, w, auth)
141-
if err != nil {
142-
fatal("cannot pull from repository: %s\n", err)
142+
if *pull {
143+
err = gitPull(r, w, auth)
144+
if err != nil {
145+
fatal("cannot pull from repository: %s\n", err)
146+
}
143147
}
144148

145-
status, err := w.Status()
146-
if err != nil {
147-
fatal("cannot retrieve git status: %s\n", err)
148-
}
149+
if *push {
150+
status, err := w.Status()
151+
if err != nil {
152+
fatal("cannot retrieve git status: %s\n", err)
153+
}
149154

150-
changes := 0
151-
msg := ""
152-
for path, s := range status {
153-
switch s.Worktree {
154-
case git.Untracked:
155-
log.Printf("New file detected: %s\n", path)
156-
err := gitAdd(w, path)
157-
if err != nil {
158-
fatal("cannot add file: %s\n", err)
155+
changes := 0
156+
msg := ""
157+
for path, s := range status {
158+
switch s.Worktree {
159+
case git.Untracked:
160+
log.Printf("New file detected: %s\n", path)
161+
err := gitAdd(w, path)
162+
if err != nil {
163+
fatal("cannot add file: %s\n", err)
164+
}
165+
166+
msg += fmt.Sprintf("Add %s.\n", path)
167+
changes++
168+
169+
case git.Modified:
170+
log.Printf("Modified file detected: %s\n", path)
171+
err := gitAdd(w, path)
172+
if err != nil {
173+
fatal("cannot add file: %s\n", err)
174+
}
175+
176+
msg += fmt.Sprintf("Update %s.\n", path)
177+
changes++
178+
179+
case git.Deleted:
180+
log.Printf("Deleted file detected: %s\n", path)
181+
err := gitRemove(w, path)
182+
if err != nil {
183+
fatal("cannot remove file: %s\n", err)
184+
}
185+
186+
msg += fmt.Sprintf("Remove %s.\n", path)
187+
changes++
188+
189+
default:
190+
log.Printf("%s %s %s\n", string(s.Worktree), string(s.Staging), path)
159191
}
192+
}
160193

161-
msg += fmt.Sprintf("Add %s.\n", path)
162-
changes++
163-
164-
case git.Modified:
165-
log.Printf("Modified file detected: %s\n", path)
166-
err := gitAdd(w, path)
194+
if changes == 0 {
195+
log.Println("No changes detected.")
196+
} else {
197+
err = gitCommit(w, msg)
167198
if err != nil {
168-
fatal("cannot add file: %s\n", err)
199+
fatal("cannot commit: %s\n", err)
169200
}
170-
171-
msg += fmt.Sprintf("Update %s.\n", path)
172-
changes++
173-
174-
case git.Deleted:
175-
log.Printf("Deleted file detected: %s\n", path)
176-
err := gitRemove(w, path)
201+
err = gitPush(r, auth)
177202
if err != nil {
178-
fatal("cannot remove file: %s\n", err)
203+
fatal("cannot push: %s\n", err)
179204
}
180-
181-
msg += fmt.Sprintf("Remove %s.\n", path)
182-
changes++
183-
184-
default:
185-
log.Printf("%s %s %s\n", string(s.Worktree), string(s.Staging), path)
186-
}
187-
}
188-
189-
if changes == 0 {
190-
log.Println("No changes detected.")
191-
} else {
192-
err = gitCommit(w, msg)
193-
if err != nil {
194-
fatal("cannot commit: %s\n", err)
195-
}
196-
err = gitPush(r, auth)
197-
if err != nil {
198-
fatal("cannot push: %s\n", err)
199205
}
200206
}
201207

0 commit comments

Comments
 (0)