From b2d41bceb174f9e6e2e997973ed4f13faa3c5e11 Mon Sep 17 00:00:00 2001 From: Frank Mayer Date: Sun, 5 Jan 2025 07:55:11 +0100 Subject: [PATCH] req --- internal/roast/request.go | 18 ++++++++----- internal/roast/roast.go | 57 +++++++++++++++++++++++++++++++++------ internal/util/textedit.go | 10 ++++++- 3 files changed, 70 insertions(+), 15 deletions(-) diff --git a/internal/roast/request.go b/internal/roast/request.go index f30cc0f..4bea938 100644 --- a/internal/roast/request.go +++ b/internal/roast/request.go @@ -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) } diff --git a/internal/roast/roast.go b/internal/roast/roast.go index 61218d3..e98ad91 100644 --- a/internal/roast/roast.go +++ b/internal/roast/roast.go @@ -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() } diff --git a/internal/util/textedit.go b/internal/util/textedit.go index 476132d..6df5df5 100644 --- a/internal/util/textedit.go +++ b/internal/util/textedit.go @@ -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) }