Update Go module and dependencies; refactor Git operations to use go-git library
This commit is contained in:
136
main.go
136
main.go
@@ -6,10 +6,12 @@ import (
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/go-git/go-git/v5"
|
||||
"github.com/go-git/go-git/v5/config"
|
||||
)
|
||||
|
||||
var Error *log.Logger
|
||||
@@ -55,13 +57,6 @@ func main() {
|
||||
log.Printf("ROOT: %s", ROOT)
|
||||
log.Printf("SCAN_INTERVAL: %.0fs", scanInterval.Seconds())
|
||||
|
||||
// This shit DOES NOT run correctly on windows
|
||||
// It absolutely butchers CRLF -> LF
|
||||
// And shits the bed
|
||||
// Thinking there are changed files
|
||||
// Making empty commits
|
||||
// And other stupid shit
|
||||
// It does run on unix though
|
||||
for {
|
||||
log.Printf("Running at %s", time.Now().Format(time.RFC3339))
|
||||
doRun()
|
||||
@@ -70,105 +65,70 @@ func main() {
|
||||
}
|
||||
|
||||
func doRun() {
|
||||
file, err := os.Open(filepath.Join(ROOT, ".git/index.lock"))
|
||||
if err == nil {
|
||||
log.Printf("Index lock found, removing")
|
||||
file.Close()
|
||||
err = os.Remove(filepath.Join(ROOT, ".git/index.lock"))
|
||||
if err != nil {
|
||||
Error.Printf("Error removing index.lock: %v", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
status, err := doStatus()
|
||||
// Open the repository
|
||||
r, err := git.PlainOpen(ROOT)
|
||||
if err != nil {
|
||||
Error.Printf("Error executing doStatus code %v with out: %v", err, status)
|
||||
Error.Printf("Error opening repository: %v", err)
|
||||
return
|
||||
}
|
||||
if status == "" {
|
||||
|
||||
// Get the worktree
|
||||
w, err := r.Worktree()
|
||||
if err != nil {
|
||||
Error.Printf("Error getting worktree: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Check status
|
||||
status, err := w.Status()
|
||||
if err != nil {
|
||||
Error.Printf("Error getting status: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if status.IsClean() {
|
||||
log.Printf("Nothing to commit")
|
||||
return
|
||||
}
|
||||
log.Printf("Changes detected")
|
||||
|
||||
res, err := doAddAll()
|
||||
// Add all changes
|
||||
err = w.AddWithOptions(&git.AddOptions{All: true})
|
||||
if err != nil {
|
||||
Error.Printf("Error executing doAddAll with code %v and out: %v", err, res)
|
||||
Error.Printf("Error adding changes: %v", err)
|
||||
return
|
||||
}
|
||||
log.Printf("Changes added to index: %v", res)
|
||||
log.Printf("Changes added to index")
|
||||
|
||||
res, err = setIdentity()
|
||||
// Set identity
|
||||
err = setIdentity(r)
|
||||
if err != nil {
|
||||
Error.Printf("Error setting identity with code %v and out: %v", err, res)
|
||||
}
|
||||
|
||||
res, err = doCommit()
|
||||
if err != nil {
|
||||
Error.Printf("Error executing doCommitAll with code %v and out: %v", err, res)
|
||||
Error.Printf("Error setting identity: %v", err)
|
||||
return
|
||||
}
|
||||
log.Printf("Changes committed: %v", res)
|
||||
|
||||
// Commit changes
|
||||
_, err = w.Commit("Update", &git.CommitOptions{
|
||||
Author: &config.Signature{
|
||||
Name: "system",
|
||||
Email: "system@localhost",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
Error.Printf("Error committing changes: %v", err)
|
||||
return
|
||||
}
|
||||
log.Printf("Changes committed")
|
||||
}
|
||||
|
||||
func doStatus() (string, error) {
|
||||
gitStatus := exec.Command("git", "status", "-s")
|
||||
gitStatus.Dir = ROOT
|
||||
out, err := gitStatus.CombinedOutput()
|
||||
func setIdentity(r *git.Repository) error {
|
||||
cfg, err := r.Config()
|
||||
if err != nil {
|
||||
return string(out), err
|
||||
return err
|
||||
}
|
||||
return string(out), nil
|
||||
}
|
||||
func doAddAll() (string, error) {
|
||||
gitAdd := exec.Command("git", "add", ".")
|
||||
gitAdd.Dir = ROOT
|
||||
out, err := gitAdd.CombinedOutput()
|
||||
if err != nil {
|
||||
return string(out), err
|
||||
}
|
||||
return string(out), nil
|
||||
}
|
||||
|
||||
func doCommit() (string, error) {
|
||||
gitCommit := exec.Command("git", "commit", "-am", "Update")
|
||||
gitCommit.Dir = ROOT
|
||||
out, err := gitCommit.CombinedOutput()
|
||||
if err != nil {
|
||||
return string(out), err
|
||||
}
|
||||
return string(out), nil
|
||||
}
|
||||
cfg.User.Name = "system"
|
||||
cfg.User.Email = "system@localhost"
|
||||
|
||||
func setIdentity() (string, error) {
|
||||
res, err := setUsername()
|
||||
if err != nil {
|
||||
log.Println("Error executing setUsername:", err)
|
||||
return string(res), err
|
||||
}
|
||||
res, err = setEmail()
|
||||
if err != nil {
|
||||
log.Println("Error executing setEmail:", err)
|
||||
return string(res), err
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
func setUsername() (string, error) {
|
||||
gitConfig := exec.Command("git", "config", "user.name", "system")
|
||||
gitConfig.Dir = ROOT
|
||||
out, err := gitConfig.CombinedOutput()
|
||||
if err != nil {
|
||||
return string(out), err
|
||||
}
|
||||
return string(out), nil
|
||||
}
|
||||
func setEmail() (string, error) {
|
||||
gitConfig := exec.Command("git", "config", "user.email", "system@localhost")
|
||||
gitConfig.Dir = ROOT
|
||||
out, err := gitConfig.CombinedOutput()
|
||||
if err != nil {
|
||||
return string(out), err
|
||||
}
|
||||
return string(out), nil
|
||||
return r.SetConfig(cfg)
|
||||
}
|
||||
|
Reference in New Issue
Block a user