fix errors from missed changes
This commit is contained in:
parent
bd0ba1bf5e
commit
0d015ae410
1 changed files with 45 additions and 24 deletions
69
main.go
69
main.go
|
@ -13,6 +13,7 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
"errors"
|
||||||
|
|
||||||
"github.com/go-git/go-git/v5"
|
"github.com/go-git/go-git/v5"
|
||||||
"github.com/go-git/go-git/v5/config"
|
"github.com/go-git/go-git/v5/config"
|
||||||
|
@ -23,23 +24,40 @@ import (
|
||||||
|
|
||||||
const DefaultRemoteName = "pad-archiver"
|
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,
|
tree *git.Worktree,
|
||||||
padfile string,
|
padfile string,
|
||||||
url string,
|
url string,
|
||||||
) (plumbing.Hash, error) {
|
) (plumbing.Hash, error) {
|
||||||
commitmu.Lock()
|
Commitmu.Lock()
|
||||||
defer commitmu.Unlock()
|
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 {
|
if _, err := tree.Add(padfile); err != nil {
|
||||||
return plumbing.ZeroHash, fmt.Errorf("Failed to stage %s: %w", padfile, err)
|
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(
|
commit, err := tree.Commit(
|
||||||
fmt.Sprintf("Updated %s from %s", padfile, url),
|
fmt.Sprintf("Updated %s from %s", padfile, url),
|
||||||
&git.CommitOptions{
|
&git.CommitOptions{
|
||||||
|
All: false,
|
||||||
Author: &object.Signature {
|
Author: &object.Signature {
|
||||||
Name: "Pad Archiver",
|
Name: "Pad Archiver",
|
||||||
Email: "pad-archiver@dadada.li",
|
Email: "pad-archiver@dadada.li",
|
||||||
|
@ -55,41 +73,32 @@ func commit(
|
||||||
return commit, nil
|
return commit, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func update(
|
func Download(
|
||||||
tree *git.Worktree,
|
gitdir string,
|
||||||
url string,
|
url string,
|
||||||
) (plumbing.Hash, error) {
|
) (string, error) {
|
||||||
res, err := http.Get(url + "/export/txt")
|
res, err := http.Get(url + "/export/txt")
|
||||||
if err != nil {
|
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()
|
defer res.Body.Close()
|
||||||
|
|
||||||
padfile := path.Base(url) + ".txt"
|
padfile := path.Base(url) + ".txt"
|
||||||
|
|
||||||
padpath := filepath.Join(tree.Filesystem.Root(), padfile)
|
padpath := filepath.Join(gitdir, padfile)
|
||||||
out, err := os.Create(padpath)
|
out, err := os.Create(padpath)
|
||||||
|
|
||||||
written, err := io.Copy(out, res.Body)
|
written, err := io.Copy(out, res.Body)
|
||||||
if err != nil {
|
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 {
|
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()
|
return padfile, nil
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -161,6 +170,7 @@ func main() {
|
||||||
log.Fatalf("Failed to open git worktree %s", err)
|
log.Fatalf("Failed to open git worktree %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
filesystemRoot := tree.Filesystem.Root()
|
||||||
scanner := bufio.NewScanner(os.Stdin)
|
scanner := bufio.NewScanner(os.Stdin)
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
@ -170,16 +180,27 @@ func main() {
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
if _, err := update(tree, padurl); err != nil {
|
padfile, err := Download(filesystemRoot, padurl)
|
||||||
|
if err != nil {
|
||||||
log.Printf("%s", err)
|
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 {
|
} else {
|
||||||
log.Printf("Updated %s", padurl)
|
log.Printf("Committed %s", padfile)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
tree.Clean(&git.CleanOptions{})
|
tree.Clean(&git.CleanOptions{Dir: true})
|
||||||
|
|
||||||
if *push == true {
|
if *push == true {
|
||||||
auth := &githttp.BasicAuth{
|
auth := &githttp.BasicAuth{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue