This commit is contained in:
Frank Mayer 2025-01-05 07:55:11 +01:00
parent a49f79a06e
commit b2d41bceb1
Signed by: tsukinoko-kun
GPG Key ID: 427B3E61E69C2E51
3 changed files with 70 additions and 15 deletions

View File

@ -10,8 +10,6 @@ import (
"os/exec"
"path/filepath"
"time"
"github.com/go-git/go-git/v5"
)
func (r *Roast) Request(from string, into string) error {
@ -21,11 +19,13 @@ func (r *Roast) Request(from string, into string) error {
if err != nil {
return errors.Join(fmt.Errorf("failed to get source branch %s", from), err)
}
log.Debugf("branch %s found\n", sourceBranch.Name)
targetBranch, err := r.repo.Branch(into)
if err != nil {
return errors.Join(fmt.Errorf("failed to get target branch %s", from), err)
}
log.Debugf("branch %s found\n", targetBranch.Name)
id := url.PathEscape(fmt.Sprintf(
"%016x_from_%s_into_%s",
@ -52,9 +52,9 @@ func (r *Roast) Request(from string, into string) error {
successful := false
defer func() {
if !successful {
clean := exec.Command("git", "clean", "-xfd")
clean.Path = r.dir
_ = clean.Run()
clean := exec.Command("git", "clean", "-xfd")
clean.Path = r.dir
_ = clean.Run()
log.Printf("action not successful, rollback...\n")
reset := exec.Command("git", "reset", "--hard")
reset.Path = r.dir
@ -75,7 +75,13 @@ func (r *Roast) Request(from string, into string) error {
return errors.Join(errors.New("failed to write to file for request"), err)
}
if err := r.repo.Push(&git.PushOptions{Atomic: true}); err != nil {
if err := r.AddAll(); err != nil {
return errors.Join(errors.New("failed to add changes before commit"), err)
}
if err := r.Commit(id); err != nil {
return errors.Join(errors.New("failed to commit changes before push request"), err)
}
if err := r.Push(); err != nil {
return errors.Join(errors.New("failed to push request"), err)
}

View File

@ -169,14 +169,55 @@ func Open() (*Roast, error) {
return nil, errors.Join(fmt.Errorf("failed to open roast repo %s", p), err)
}
pull := exec.Command("git", "pull", "--force")
pull.Dir = p
if err := pull.Run(); err != nil {
return nil, errors.Join(fmt.Errorf("failed to pull roast repo %s", p), err)
}
return &Roast{
r := &Roast{
repo: roastRepo,
dir: p,
}, nil
}
return r, r.Pull()
}
func (r *Roast) Pull() error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
log.Debugf("git pull --force\n")
pullCmd := exec.CommandContext(ctx, "git", "pull", "--force")
pullCmd.Dir = r.dir
pullCmd.Stdin = os.Stdin
pullCmd.Stdout = os.Stdout
pullCmd.Stderr = os.Stderr
return pullCmd.Run()
}
func (r *Roast) AddAll() error {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
log.Debugf("git add .\n")
addCmd := exec.CommandContext(ctx, "git", "add", ".")
addCmd.Dir = r.dir
return addCmd.Run()
}
func (r *Roast) Commit(m string) error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
log.Debugf("git commit\n")
commitCmd := exec.CommandContext(ctx, "git", "commit", "-m", m)
commitCmd.Dir = r.dir
commitCmd.Stdin = os.Stdin
commitCmd.Stdout = os.Stdout
commitCmd.Stderr = os.Stderr
return commitCmd.Run()
}
func (r *Roast) Push() error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
log.Debugf("git push --atomic\n")
pushCmd := exec.CommandContext(ctx, "git", "push", "--atomic")
pushCmd.Dir = r.dir
pushCmd.Stdin = os.Stdin
pushCmd.Stdout = os.Stdout
pushCmd.Stderr = os.Stderr
return pushCmd.Run()
}

View File

@ -3,6 +3,7 @@ package util
import (
"context"
"errors"
"git-roast/internal/log"
"io"
"os"
"os/exec"
@ -17,10 +18,17 @@ func Textedit(text string) (string, error) {
}
fp := f.Name()
defer os.Remove(fp)
_, _ = f.Write([]byte(text))
f.Close()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Minute)
defer cancel()
if err := exec.CommandContext(ctx, editor(), fp).Run(); err != nil {
ed := editor()
log.Debugf("editing message using %s\n", ed)
edCmd := exec.CommandContext(ctx, ed, fp)
edCmd.Stderr = os.Stderr
edCmd.Stdout = os.Stdout
edCmd.Stdin = os.Stdin
if err := edCmd.Run(); err != nil {
return text, errors.Join(errors.New("failed to run text editor"), err)
}