Skip to content

Commit

Permalink
Add storage.TryAdd and TryCommit methods (#2723)
Browse files Browse the repository at this point in the history
* Add storage.TryAdd and TryCommit methods

Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>

* Fix lint errors and replace Git with Fossil in the Fossil backend.

Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>

---------

Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>
  • Loading branch information
dominikschulz authored Nov 25, 2023
1 parent 5f18942 commit 8dfd7bd
Show file tree
Hide file tree
Showing 16 changed files with 197 additions and 205 deletions.
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 @@ -184,9 +184,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
57 changes: 57 additions & 0 deletions internal/backend/storage/fossilfs/fossil.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fossilfs
import (
"bytes"
"context"
"errors"
"fmt"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -207,6 +208,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 errors.Is(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 +267,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 errors.Is(err, store.ErrGitNothingToCommit) {
debug.Log("Nothing to commit. Ignoring.")

return nil
}
if errors.Is(err, store.ErrGitNotInit) {
debug.Log("Fossil not initialized. Ignoring.")

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 +329,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 {
case errors.Is(err, store.ErrGitNotInit):
debug.Log("Fossil not initialized. Ignoring.")

return nil
case errors.Is(err, store.ErrGitNoRemote):
debug.Log("Fossil has no remote. Ignoring.")

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
57 changes: 57 additions & 0 deletions internal/backend/storage/gitfs/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package gitfs
import (
"bytes"
"context"
"errors"
"fmt"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -233,6 +234,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 errors.Is(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 +288,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 errors.Is(err, store.ErrGitNothingToCommit) {
debug.Log("Nothing to commit. Ignoring.")

return nil
}
if errors.Is(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 +386,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 {
case errors.Is(err, store.ErrGitNotInit):
debug.Log("Git not initialized. Ignoring.")

return nil
case errors.Is(err, 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 @@ -206,15 +206,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
Loading

0 comments on commit 8dfd7bd

Please sign in to comment.