fix errors from missed changes

This commit is contained in:
Tim Schubert 2022-05-23 19:30:54 +02:00
parent bd0ba1bf5e
commit 0d015ae410
Signed by: dadada
GPG key ID: EEB8D1CE62C4DFEA

69
main.go
View file

@ -13,6 +13,7 @@ import (
"path/filepath"
"sync"
"time"
"errors"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
@ -23,23 +24,40 @@ import (
const DefaultRemoteName = "pad-archiver"
var commitmu sync.Mutex
var (
NothingToDo = errors.New("Nothing to do for unmodified file")
)
func commit(
var Commitmu sync.Mutex
func Commit(
tree *git.Worktree,
padfile string,
url string,
) (plumbing.Hash, error) {
commitmu.Lock()
defer commitmu.Unlock()
Commitmu.Lock()
defer Commitmu.Unlock()
status, err := tree.Status()
if err != nil {
return plumbing.ZeroHash, fmt.Errorf("Failed to get status of %s", padfile)
}
if _, err := tree.Add(padfile); err != nil {
return plumbing.ZeroHash, fmt.Errorf("Failed to stage %s: %w", padfile, err)
}
fileStatus := status.File(padfile)
if fileStatus.Staging == git.Unmodified ||
fileStatus.Staging == git.Untracked {
return plumbing.ZeroHash, NothingToDo
}
commit, err := tree.Commit(
fmt.Sprintf("Updated %s from %s", padfile, url),
&git.CommitOptions{
All: false,
Author: &object.Signature {
Name: "Pad Archiver",
Email: "pad-archiver@dadada.li",
@ -55,41 +73,32 @@ func commit(
return commit, nil
}
func update(
tree *git.Worktree,
func Download(
gitdir string,
url string,
) (plumbing.Hash, error) {
) (string, error) {
res, err := http.Get(url + "/export/txt")
if err != nil {
return plumbing.ZeroHash, fmt.Errorf("Failed to get pad at %s: %w", url, err)
return "", fmt.Errorf("Failed to get pad at %s: %w", url, err)
}
defer res.Body.Close()
padfile := path.Base(url) + ".txt"
padpath := filepath.Join(tree.Filesystem.Root(), padfile)
padpath := filepath.Join(gitdir, padfile)
out, err := os.Create(padpath)
written, err := io.Copy(out, res.Body)
if err != nil {
return plumbing.ZeroHash, fmt.Errorf("Failed to write pad to file at %s: %w", padfile, err)
return "", fmt.Errorf("Failed to write pad to file at %s: %w", padfile, err)
}
if written < 100 {
return plumbing.ZeroHash, fmt.Errorf("Skipping update of %s, because pad has likely been removed from %s", padfile, url)
return "", fmt.Errorf("Skipping update of %s, because pad has likely been removed from %s", padfile, url)
}
status, err := tree.Status()
if err != nil {
return plumbing.ZeroHash, fmt.Errorf("Failed to get status of %s: %w", padfile, err)
}
if status.IsClean() {
return plumbing.ZeroHash, fmt.Errorf("No changes recorded for %s", url)
}
return commit(tree, padfile, url)
return padfile, nil
}
@ -161,6 +170,7 @@ func main() {
log.Fatalf("Failed to open git worktree %s", err)
}
filesystemRoot := tree.Filesystem.Root()
scanner := bufio.NewScanner(os.Stdin)
var wg sync.WaitGroup
@ -170,16 +180,27 @@ func main() {
go func() {
defer wg.Done()
if _, err := update(tree, padurl); err != nil {
padfile, err := Download(filesystemRoot, padurl)
if err != nil {
log.Printf("%s", err)
return
}
log.Printf("Downloaded %s", padurl)
if _, err := Commit(tree, padfile, *remote); err != nil {
if err == NothingToDo {
log.Printf("Nothing to do for %s", padfile)
} else {
log.Fatalf("%s", err)
}
} else {
log.Printf("Updated %s", padurl)
log.Printf("Committed %s", padfile)
}
}()
}
wg.Wait()
tree.Clean(&git.CleanOptions{})
tree.Clean(&git.CleanOptions{Dir: true})
if *push == true {
auth := &githttp.BasicAuth{