diff --git a/CHANGELOG.md b/CHANGELOG.md index 57b161b0d..48ddf8fc7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## 0.47.2 - Unreleased +- Backup: allow a locally initialized backup to acquire its first configured remote without replacing an existing origin. (#1287) — thanks @NasAndNora. + - Gmail: give every outgoing attachment a unique, stable `X-Attachment-Id`, including duplicate files, while preserving inline Content-ID references. (#1299, #1300) — thanks @zupolgec. - Auth: retain the command runtime and OAuth client dependencies during automatic reauthorization, preserving cancellation and the stored service/scope grant. (#1277, #1278) — thanks @TheAngryPit. diff --git a/docs/backup.md b/docs/backup.md index 2d15adb94..336efaa25 100644 --- a/docs/backup.md +++ b/docs/backup.md @@ -30,7 +30,9 @@ gog backup init --remote https://github.com/your-account/your-private-backup.git `init` and `push` commit locally and explain that no upload happened when no remote is configured. Explicitly saved remotes and existing Git `origin` URLs -continue to work without rewriting them. Checkpoint pushes follow the same rule. +continue to work without rewriting them. Configuring a remote after local +initialization adds `origin` only when it is missing. Checkpoint pushes follow +the same rule. Back up all supported services: diff --git a/internal/backup/backup_test.go b/internal/backup/backup_test.go index 63e75b07d..806ee20d0 100644 --- a/internal/backup/backup_test.go +++ b/internal/backup/backup_test.go @@ -1071,3 +1071,39 @@ func TestBackupExistingOriginPreservedAndPushed(t *testing.T) { t.Fatalf("snapshot not pushed: %q %v", remoteHead, err) } } + +func TestInitAddsConfiguredRemoteToLocalRepository(t *testing.T) { + ctx, repo, config, _ := initTestBackup(t) + remote := filepath.Join(t.TempDir(), "remote.git") + if err := git(ctx, "", "init", "--bare", remote); err != nil { + t.Fatal(err) + } + if _, _, initErr := Init(ctx, testOptions(t, Options{ConfigPath: config, Remote: remote, Push: true})); initErr != nil { + t.Fatal(initErr) + } + origin, err := gitOutput(ctx, repo, "remote", "get-url", "origin") + if err != nil || strings.TrimSpace(origin) != remote { + t.Fatalf("configured origin missing: %q %v", origin, err) + } + if _, pushErr := PushSnapshot(ctx, Snapshot{Services: []string{"gmail"}}, testOptions(t, Options{ConfigPath: config, Push: true})); pushErr != nil { + t.Fatal(pushErr) + } + localHead, err := gitOutput(ctx, repo, "rev-parse", "HEAD") + if err != nil { + t.Fatal(err) + } + if _, hasErr := gitOutput(ctx, remote, "cat-file", "-e", strings.TrimSpace(localHead)); hasErr != nil { + t.Fatalf("configured backup was not pushed: %v", hasErr) + } + otherRemote := filepath.Join(t.TempDir(), "other.git") + if remoteErr := git(ctx, "", "init", "--bare", otherRemote); remoteErr != nil { + t.Fatal(remoteErr) + } + if _, _, initErr := Init(ctx, testOptions(t, Options{ConfigPath: config, Remote: otherRemote})); initErr != nil { + t.Fatal(initErr) + } + origin, err = gitOutput(ctx, repo, "remote", "get-url", "origin") + if err != nil || strings.TrimSpace(origin) != remote { + t.Fatalf("existing origin was rewritten: %q %v", origin, err) + } +} diff --git a/internal/backup/git.go b/internal/backup/git.go index 1d5034206..68f440bc6 100644 --- a/internal/backup/git.go +++ b/internal/backup/git.go @@ -28,6 +28,11 @@ func ensureRepo(ctx context.Context, cfg Config) error { return fmt.Errorf("backup repo path is required") } if _, err := os.Stat(filepath.Join(cfg.Repo, ".git")); err == nil { + if strings.TrimSpace(cfg.Remote) != "" && !HasRemote(ctx, cfg.Repo) { + if addErr := git(ctx, cfg.Repo, "remote", "add", "origin", cfg.Remote); addErr != nil { + return addErr + } + } return pullRepo(ctx, cfg.Repo) } if strings.TrimSpace(cfg.Remote) != "" {