Skip to content

Commit

Permalink
Added -pull and -push flags
Browse files Browse the repository at this point in the history
  • Loading branch information
muesli committed Aug 5, 2019
1 parent 50018c4 commit 1ea5b7c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 53 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,20 @@ gitomatic -privkey ~/.ssh/id_rsa <path>
gitomatic -username "someone" -password "mypass" <path>
```

Other parameters:
If you want to pull new changes but don't create commits (or vice versa):

```
gitomatic -pull=true -push=false <path>
```

You can control how often gitomatic checks for changes:

```
gitomatic -interval 30m <path>
gitomatic -author "John Doe" <path>
gitomatic -email "[email protected]" <path>
```

Change the commit author's name and email:

```
gitomatic -author "John Doe" -email "[email protected]" <path>
```
106 changes: 56 additions & 50 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
)

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", "[email protected]", "email address for git commits")
interval = flag.String("interval", "1m", "how often to check for changes")
Expand Down Expand Up @@ -137,65 +139,69 @@ func main() {
fatal("cannot access repository: %s\n", err)
}

err = gitPull(r, w, auth)
if err != nil {
fatal("cannot pull from repository: %s\n", err)
if *pull {
err = gitPull(r, w, auth)
if err != nil {
fatal("cannot pull from repository: %s\n", err)
}
}

status, err := w.Status()
if err != nil {
fatal("cannot retrieve git status: %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)
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)
}
}

msg += fmt.Sprintf("Add %s.\n", path)
changes++

case git.Modified:
log.Printf("Modified file detected: %s\n", path)
err := gitAdd(w, path)
if changes == 0 {
log.Println("No changes detected.")
} else {
err = gitCommit(w, msg)
if err != nil {
fatal("cannot add file: %s\n", err)
fatal("cannot commit: %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)
err = gitPush(r, auth)
if err != nil {
fatal("cannot remove file: %s\n", err)
fatal("cannot push: %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)
}
}

Expand Down

0 comments on commit 1ea5b7c

Please sign in to comment.