Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add storage.TryAdd and TryCommit methods #2723

Merged
merged 2 commits into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 4 additions & 14 deletions internal/action/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package action

import (
"context"
"errors"
"fmt"

"github.com/gopasspw/gopass/internal/action/exit"
"github.com/gopasspw/gopass/internal/config"
"github.com/gopasspw/gopass/internal/out"
"github.com/gopasspw/gopass/internal/set"
istore "github.com/gopasspw/gopass/internal/store"
"github.com/gopasspw/gopass/pkg/ctxutil"
"github.com/gopasspw/gopass/pkg/debug"
"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -109,23 +107,15 @@ func (s *Action) setConfigValue(ctx context.Context, store, key, value string) e
return fmt.Errorf("local config file %s didn't exist in store, this is unexpected", configFile)
}

switch err := st.Add(ctx, configFile); {
case err == nil:
debug.Log("Added local config for commit")
case errors.Is(err, istore.ErrGitNotInit):
debug.Log("Skipping staging of local config %q: %v", configFile, err)
default:
if err := st.TryAdd(ctx, configFile); err != nil {
return fmt.Errorf("failed to Add local config %q: %w", configFile, err)
}
debug.Log("Added local config for commit")

switch err := st.Commit(ctx, "Update config"); {
case err == nil:
debug.Log("Committed local config")
case errors.Is(err, istore.ErrGitNotInit), errors.Is(err, istore.ErrGitNothingToCommit):
debug.Log("Skipping Commit of local config: %v", err)
default:
if err := st.TryCommit(ctx, "Update config"); err != nil {
return fmt.Errorf("failed to commit local config: %w", err)
}
debug.Log("Committed local config")

s.printConfigValues(ctx, store, key)

Expand Down
3 changes: 1 addition & 2 deletions internal/action/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,8 @@ func (s *Action) syncMount(ctx context.Context, mp string) error {
}

out.Printf(ctxno, "\n "+color.GreenString("%s pull and push ... ", sub.Storage().Name()))
err = sub.Storage().Push(ctx, "", "")

switch {
switch err := sub.Storage().Push(ctx, "", ""); {
case err == nil:
debug.Log("Push succeeded")
out.Printf(ctxno, color.GreenString("OK"))
Expand Down
4 changes: 4 additions & 0 deletions internal/backend/rcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ type rcs interface {
Push(ctx context.Context, remote, location string) error
Pull(ctx context.Context, remote, location string) error

TryAdd(ctx context.Context, args ...string) error
TryCommit(ctx context.Context, msg string) error
TryPush(ctx context.Context, remote, location string) error

InitConfig(ctx context.Context, name, email string) error
AddRemote(ctx context.Context, remote, location string) error
RemoveRemote(ctx context.Context, remote string) error
Expand Down
56 changes: 56 additions & 0 deletions internal/backend/storage/fossilfs/fossil.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,21 @@ func (f *Fossil) Add(ctx context.Context, files ...string) error {
return f.Cmd(ctx, "fossilAdd", args...)
}

// TryAdd adds the listed files to the fossil index.
func (f *Fossil) TryAdd(ctx context.Context, files ...string) error {
err := f.Add(ctx, files...)
if err == nil {
return nil
}
if err == store.ErrGitNotInit {
debug.Log("Fossil not initialized. Ignoring.")

return nil
}

return err
}

// HasStagedChanges returns true if there are any staged changes which can be committed.
func (f *Fossil) HasStagedChanges(ctx context.Context) bool {
s, err := f.getStatus(ctx)
Expand Down Expand Up @@ -251,6 +266,26 @@ func (f *Fossil) Commit(ctx context.Context, msg string) error {
)
}

// TryCommit calls commit and returns nil if there was nothing to commit or if the Fossil repo was not initialized.
func (f *Fossil) TryCommit(ctx context.Context, msg string) error {
err := f.Commit(ctx, msg)
if err == nil {
return nil
}
if err == store.ErrGitNothingToCommit {
debug.Log("Nothing to commit. Ignoring.")

return nil
}
if err == store.ErrGitNotInit {
debug.Log("Git not initialized. Ignoring.")
dominikschulz marked this conversation as resolved.
Show resolved Hide resolved

return nil
}

return err
}

// PushPull pushes the repo to it's origin.
// optional arguments: remote and branch.
func (f *Fossil) PushPull(ctx context.Context, op, remote, branch string) error {
Expand Down Expand Up @@ -293,6 +328,27 @@ func (f *Fossil) Push(ctx context.Context, remote, branch string) error {
return f.PushPull(ctx, "push", remote, branch)
}

// TryPush calls Push and returns nil if the Fossil repo was not initialized.
func (f *Fossil) TryPush(ctx context.Context, remote, branch string) error {
err := f.Push(ctx, remote, branch)
if err == nil {
return nil
}

switch err {
case store.ErrGitNotInit:
debug.Log("Git not initialized. Ignoring.")

return nil
case store.ErrGitNoRemote:
debug.Log("Git has no remote. Ignoring.")
dominikschulz marked this conversation as resolved.
Show resolved Hide resolved

return nil
default:
return err
}
}

// Pull pulls from the fossil remote.
func (f *Fossil) Pull(ctx context.Context, remote, branch string) error {
if ctxutil.IsNoNetwork(ctx) {
Expand Down
15 changes: 15 additions & 0 deletions internal/backend/storage/fs/rcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,31 @@ func (s *Store) Add(ctx context.Context, args ...string) error {
return store.ErrGitNotInit
}

// TryAdd does nothing.
func (s *Store) TryAdd(ctx context.Context, args ...string) error {
return nil
}

// Commit does nothing.
func (s *Store) Commit(ctx context.Context, msg string) error {
return store.ErrGitNotInit
}

// TryCommit does nothing.
func (s *Store) TryCommit(ctx context.Context, msg string) error {
return nil
}

// Push does nothing.
func (s *Store) Push(ctx context.Context, origin, branch string) error {
return store.ErrGitNotInit
}

// TryPush does nothing.
func (s *Store) TryPush(ctx context.Context, origin, branch string) error {
return nil
}

// Pull does nothing.
func (s *Store) Pull(ctx context.Context, origin, branch string) error {
return store.ErrGitNotInit
Expand Down
56 changes: 56 additions & 0 deletions internal/backend/storage/gitfs/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,21 @@ func (g *Git) Add(ctx context.Context, files ...string) error {
return g.Cmd(ctx, "gitAdd", args...)
}

// TryAdd calls Add and returns nil if the git repo was not initialized.
func (g *Git) TryAdd(ctx context.Context, files ...string) error {
err := g.Add(ctx, files...)
if err == nil {
return nil
}
if err == store.ErrGitNotInit {
debug.Log("Git not initialized. Ignoring.")

return nil
}

return err
}

// HasStagedChanges returns true if there are any staged changes which can be committed.
func (g *Git) HasStagedChanges(ctx context.Context) bool {
if err := g.Cmd(ctx, "gitDiffIndex", "diff-index", "--quiet", "HEAD"); err != nil {
Expand Down Expand Up @@ -272,6 +287,26 @@ func (g *Git) Commit(ctx context.Context, msg string) error {
return g.Cmd(ctx, "gitCommit", "commit", fmt.Sprintf("--date=%d +00:00", ctxutil.GetCommitTimestamp(ctx).UTC().Unix()), "-m", msg)
}

// TryCommit calls commit and returns nil if there was nothing to commit or if the git repo was not initialized.
func (g *Git) TryCommit(ctx context.Context, msg string) error {
err := g.Commit(ctx, msg)
if err == nil {
return nil
}
if err == store.ErrGitNothingToCommit {
debug.Log("Nothing to commit. Ignoring.")

return nil
}
if err == store.ErrGitNotInit {
debug.Log("Git not initialized. Ignoring.")

return nil
}

return err
}

func (g *Git) defaultRemote(ctx context.Context, branch string) string {
opts, err := g.ConfigList(ctx)
if err != nil {
Expand Down Expand Up @@ -350,6 +385,27 @@ func (g *Git) PushPull(ctx context.Context, op, remote, branch string) error {
return g.Cmd(ctx, "gitPush", "push", remote, branch)
}

// TryPush calls Push and returns nil if the git repo was not initialized.
func (g *Git) TryPush(ctx context.Context, remote, branch string) error {
err := g.Push(ctx, remote, branch)
if err == nil {
return nil
}

switch err {
case store.ErrGitNotInit:
debug.Log("Git not initialized. Ignoring.")

return nil
case store.ErrGitNoRemote:
debug.Log("Git has no remote. Ignoring.")

return nil
default:
return err
}
}

// Push pushes to the git remote.
func (g *Git) Push(ctx context.Context, remote, branch string) error {
if ctxutil.IsNoNetwork(ctx) {
Expand Down
8 changes: 4 additions & 4 deletions internal/create/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ attributes:

type storageSetter interface {
Set(context.Context, string, []byte) error
Add(context.Context, ...string) error
Commit(context.Context, string) error
TryAdd(context.Context, ...string) error
TryCommit(context.Context, string) error
}

func (w *Wizard) writeTemplates(ctx context.Context, s storageSetter) error {
Expand All @@ -86,14 +86,14 @@ func (w *Wizard) writeTemplates(ctx context.Context, s storageSetter) error {
return fmt.Errorf("failed to write default template %s: %w", path, err)
}

if err := s.Add(ctx, path); err != nil && !errors.Is(err, store.ErrGitNotInit) {
if err := s.TryAdd(ctx, path); err != nil {
return fmt.Errorf("failed to stage changes %s: %w", path, err)
}

debug.Log("wrote default template to %s", path)
}

if err := s.Commit(ctx, "Added default wizard templates"); err != nil && !errors.Is(err, store.ErrGitNotInit) {
if err := s.TryCommit(ctx, "Added default wizard templates"); err != nil {
return fmt.Errorf("failed to commit changes: %w", err)
}

Expand Down
8 changes: 8 additions & 0 deletions internal/create/wizard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,18 @@ func (f *fakeSetter) Add(ctx context.Context, args ...string) error {
return nil
}

func (f *fakeSetter) TryAdd(ctx context.Context, args ...string) error {
return nil
}

func (f *fakeSetter) Commit(ctx context.Context, msg string) error {
return nil
}

func (f *fakeSetter) TryCommit(ctx context.Context, msg string) error {
return nil
}

func TestWrite(t *testing.T) {
t.Parallel()

Expand Down
11 changes: 2 additions & 9 deletions internal/store/leaf/fsck.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,8 @@ func (s *Store) fsckLoop(ctx context.Context, path string) error {
out.Warningf(ctx, "Nothing to commit: all secrets up to date")
}

if err := s.storage.Commit(ctx, ctxutil.GetCommitMessageFull(ctx)); err != nil {
switch {
case errors.Is(err, store.ErrGitNotInit):
out.Warning(ctx, "Cannot commit: git not initialized\nplease run `gopass git init` (and note that manual intervention might be needed)")
case errors.Is(err, store.ErrGitNothingToCommit):
debug.Log("commitAndPush - skipping git commit - nothing to commit")
default:
return fmt.Errorf("failed to commit changes to git: %w", err)
}
if err := s.storage.TryCommit(ctx, ctxutil.GetCommitMessageFull(ctx)); err != nil {
return fmt.Errorf("failed to commit changes to git: %w", err)
}

return nil
Expand Down
35 changes: 6 additions & 29 deletions internal/store/leaf/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,7 @@ func (s *Store) directMove(ctx context.Context, from, to string, del bool) error
return nil
}

if err := s.storage.Add(ctx, pFrom, pTo); err != nil {
if errors.Is(err, store.ErrGitNotInit) {
return nil
}

if err := s.storage.TryAdd(ctx, pFrom, pTo); err != nil {
return fmt.Errorf("failed to add %q and %q to git: %w", pFrom, pTo, err)
}

Expand Down Expand Up @@ -164,15 +160,8 @@ func (s *Store) delete(ctx context.Context, name string, recurse bool) error {
return nil
}

if err := s.storage.Commit(ctx, fmt.Sprintf("Remove %s from store.", name)); err != nil {
switch {
case errors.Is(err, store.ErrGitNotInit):
debug.Log("skipping git commit - git not initialized")
case errors.Is(err, store.ErrGitNothingToCommit):
debug.Log("skipping git commit - nothing to commit")
default:
return fmt.Errorf("failed to commit changes to git: %w", err)
}
if err := s.storage.TryCommit(ctx, fmt.Sprintf("Remove %s from store.", name)); err != nil {
return fmt.Errorf("failed to commit changes to git: %w", err)
}

if !config.Bool(ctx, "core.autopush") {
Expand All @@ -181,11 +170,7 @@ func (s *Store) delete(ctx context.Context, name string, recurse bool) error {
return nil
}

if err := s.storage.Push(ctx, "", ""); err != nil {
if errors.Is(err, store.ErrGitNotInit) || errors.Is(err, store.ErrGitNoRemote) {
return nil
}

if err := s.storage.TryPush(ctx, "", ""); err != nil {
return fmt.Errorf("failed to push change to git remote: %w", err)
}

Expand All @@ -206,11 +191,7 @@ func (s *Store) deleteRecurse(ctx context.Context, name, path string) error {
return err
}

if err := s.storage.Add(ctx, name); err != nil {
if errors.Is(err, store.ErrGitNotInit) {
return nil
}

if err := s.storage.TryAdd(ctx, name); err != nil {
return fmt.Errorf("failed to add %q to git: %w", path, err)
}
debug.Log("pruned")
Expand All @@ -228,11 +209,7 @@ func (s *Store) deleteSingle(ctx context.Context, path string) error {
return err
}

if err := s.storage.Add(ctx, path); err != nil {
if errors.Is(err, store.ErrGitNotInit) {
return nil
}

if err := s.storage.TryAdd(ctx, path); err != nil {
return fmt.Errorf("failed to add %q to git: %w", path, err)
}

Expand Down
Loading