Skip to content

Commit

Permalink
Build a simple daemon
Browse files Browse the repository at this point in the history
This just monitors all the repos
  • Loading branch information
vHanda committed May 5, 2022
1 parent 562ce3f commit b1de9d0
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 19 deletions.
4 changes: 3 additions & 1 deletion common/autosync.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package common

import "github.com/ztrue/tracerr"
import (
"github.com/ztrue/tracerr"
)

func AutoSync(repoPath string) error {
var err error
Expand Down
41 changes: 34 additions & 7 deletions common/ignore.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package common

import (
"os"
"path/filepath"
"strings"

Expand All @@ -9,20 +10,33 @@ import (
"gopkg.in/src-d/go-git.v4/plumbing/format/gitignore"
)

func shouldIgnoreFile(repoPath string, path string) (bool, error) {
fileName := filepath.Base(path)
func shouldIgnoreFile(repoPath string, fullFilePath string) (bool, error) {
fileName := filepath.Base(fullFilePath)
var isTempFile = strings.HasSuffix(fileName, ".swp") || // vim
strings.HasPrefix(path, "~") || // emacs
strings.HasSuffix(path, "~") || // kate
strings.HasPrefix(path, ".") // hidden files
strings.HasPrefix(fileName, "~") || // emacs
strings.HasSuffix(fileName, "~") || // kate
strings.HasPrefix(fileName, ".") // hidden files

// FIXME: Do not automatically ignore all hidden files, make this configurable

if isTempFile {
return true, nil
}

// FIXME: Do not automatically ignore all hidden files, make this configurable
relativePath := fullFilePath[len(repoPath)+1:]
if strings.HasPrefix(relativePath, ".git/") {
return true, nil
}

return isFileIgnoredByGit(repoPath, path)
empty, err := isEmptyFile(fullFilePath)
if err != nil {
return false, tracerr.Wrap(err)
}
if empty {
return true, nil
}

return isFileIgnoredByGit(repoPath, fullFilePath)
}

func isFileIgnoredByGit(repoPath string, filePath string) (bool, error) {
Expand All @@ -46,3 +60,16 @@ func isFileIgnoredByGit(repoPath string, filePath string) (bool, error) {

return m.Match([]string{filePath}, false), err
}

func isEmptyFile(filePath string) (bool, error) {
stat, err := os.Stat(filePath)

if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}

return stat.Size() == 0, nil
}
2 changes: 1 addition & 1 deletion common/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func push(repoPath string) error {
return nil
}

err, _ = GitCommand(repoPath, []string{"push", bi.UpstreamRemote + "/" + bi.UpstreamBranch})
err, _ = GitCommand(repoPath, []string{"push", bi.UpstreamRemote, bi.UpstreamBranch})
if err != nil {
return tracerr.Wrap(err)
}
Expand Down
31 changes: 21 additions & 10 deletions common/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,21 @@ import (
"github.com/ztrue/tracerr"
)

// FIXME: Poll for changes as well?
// FIXME: Watch for suspend / resume
// FIXME: Properly log the errors

func WatchForChanges(repoPath string) error {
var err error
notifyChannel := make(chan notify.EventInfo, 100)

err = notify.Watch(filepath.Join(repoPath, "..."), notifyChannel, notify.Write, notify.Rename, notify.Remove)
err = AutoSync(repoPath)
if err != nil {
return tracerr.Wrap(err)
}
defer notify.Stop(notifyChannel)

notifyFilteredChannel := make(chan notify.EventInfo, 100)
ticker := time.NewTicker(20 * time.Second)

// Filtered events
go func() {
var events []notify.EventInfo
for {
Expand All @@ -34,17 +33,31 @@ func WatchForChanges(repoPath string) error {
events = append(events, ei)
continue

case t := <-ticker.C:
fmt.Println("Tick at", t)
case <-ticker.C:
if len(events) != 0 {
fmt.Println("Committing")
events = []notify.EventInfo{}

fmt.Println("Committing")
err := AutoSync(repoPath)
if err != nil {
log.Fatalln(err)
}
}
}
}
}()

// Block until an event is received.
//
// Watch for FS events
//
notifyChannel := make(chan notify.EventInfo, 100)

err = notify.Watch(filepath.Join(repoPath, "..."), notifyChannel, notify.Write, notify.Rename, notify.Remove, notify.Create)
if err != nil {
return tracerr.Wrap(err)
}
defer notify.Stop(notifyChannel)

for {
ei := <-notifyChannel
ignore, err := shouldIgnoreFile(repoPath, ei.Path())
Expand All @@ -55,8 +68,6 @@ func WatchForChanges(repoPath string) error {
continue
}

// Wait for 'x' seconds
log.Println("Got event:", ei)
notifyFilteredChannel <- ei
}
}
36 changes: 36 additions & 0 deletions daemon/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"fmt"
"log"
"sync"

"github.com/GitJournal/git-auto-sync/common"
)

func main() {
config, err := common.ReadConfig()
if err != nil {
panic(err)
}

var wg sync.WaitGroup

for _, repoPath := range config.Repos {
wg.Add(1)

fmt.Println("Monitoring", repoPath)
go watchForChanges(&wg, repoPath)
}

wg.Wait()
}

func watchForChanges(wg *sync.WaitGroup, repoPath string) {
defer wg.Done()

err := common.WatchForChanges(repoPath)
if err != nil {
log.Println(err)
}
}

0 comments on commit b1de9d0

Please sign in to comment.