fix(sync): respect sync.remote config in daemon sync operations (#736)

The daemon sync functions (gitPushFromWorktree, syncBranchPull) were
checking git's branch tracking config and falling back to 'origin',
but ignoring bd's sync.remote config setting.

Now these functions check sync.remote config first, matching the
behavior of sync_check.go and sync_import.go.

Closes #736
This commit is contained in:
Steve Yegge
2025-12-24 12:43:18 -08:00
parent 9d0fc55e0b
commit 7f8f8f69d1

View File

@@ -109,10 +109,12 @@ func syncBranchCommitAndPushWithOptions(ctx context.Context, store storage.Stora
return false, fmt.Errorf("failed to commit in worktree: %w", err)
}
log.log("Committed changes to sync branch %s", syncBranch)
// Push if enabled
if autoPush {
if err := gitPushFromWorktree(ctx, worktreePath, syncBranch); err != nil {
// Get configured remote from bd config (sync.remote), default to empty (will use git config)
configuredRemote, _ := store.GetConfig(ctx, "sync.remote")
if err := gitPushFromWorktree(ctx, worktreePath, syncBranch, configuredRemote); err != nil {
return false, fmt.Errorf("failed to push from worktree: %w", err)
}
log.log("Pushed sync branch %s to remote", syncBranch)
@@ -174,15 +176,19 @@ func gitCommitInWorktree(ctx context.Context, worktreePath, filePath, message st
// gitPushFromWorktree pushes the sync branch from the worktree.
// If push fails due to remote having newer commits, it will fetch, rebase, and retry.
func gitPushFromWorktree(ctx context.Context, worktreePath, branch string) error {
// Get remote name (usually "origin")
remoteCmd := exec.CommandContext(ctx, "git", "-C", worktreePath, "config", "--get", fmt.Sprintf("branch.%s.remote", branch)) // #nosec G204 - worktreePath and branch are from config
remoteOutput, err := remoteCmd.Output()
if err != nil {
// If no remote configured, default to "origin" and set up tracking
remoteOutput = []byte("origin\n")
// The configuredRemote parameter allows passing the bd config sync.remote value.
func gitPushFromWorktree(ctx context.Context, worktreePath, branch, configuredRemote string) error {
// Use configured remote if provided, otherwise check git branch config
remote := configuredRemote
if remote == "" {
remoteCmd := exec.CommandContext(ctx, "git", "-C", worktreePath, "config", "--get", fmt.Sprintf("branch.%s.remote", branch)) // #nosec G204 - worktreePath and branch are from config
remoteOutput, err := remoteCmd.Output()
if err != nil {
// If no remote configured, default to "origin" and set up tracking
remoteOutput = []byte("origin\n")
}
remote = strings.TrimSpace(string(remoteOutput))
}
remote := strings.TrimSpace(string(remoteOutput))
// Push with explicit remote and branch, set upstream if not set
cmd := exec.CommandContext(ctx, "git", "-C", worktreePath, "push", "--set-upstream", remote, branch) // #nosec G204 - worktreePath, remote, and branch are from config
@@ -262,14 +268,19 @@ func syncBranchPull(ctx context.Context, store storage.Storage, log daemonLogger
return false, fmt.Errorf("failed to create worktree: %w", err)
}
// Get remote name
remoteCmd := exec.CommandContext(ctx, "git", "-C", worktreePath, "config", "--get", fmt.Sprintf("branch.%s.remote", syncBranch)) // #nosec G204 - worktreePath and syncBranch are from config
remoteOutput, err := remoteCmd.Output()
if err != nil {
// If no remote configured, default to "origin"
remoteOutput = []byte("origin\n")
// Get remote name - check bd config first, then git branch config, then default to "origin"
remote := ""
if configuredRemote, err := store.GetConfig(ctx, "sync.remote"); err == nil && configuredRemote != "" {
remote = configuredRemote
} else {
remoteCmd := exec.CommandContext(ctx, "git", "-C", worktreePath, "config", "--get", fmt.Sprintf("branch.%s.remote", syncBranch)) // #nosec G204 - worktreePath and syncBranch are from config
remoteOutput, err := remoteCmd.Output()
if err != nil {
// If no remote configured, default to "origin"
remoteOutput = []byte("origin\n")
}
remote = strings.TrimSpace(string(remoteOutput))
}
remote := strings.TrimSpace(string(remoteOutput))
// Pull in worktree
cmd := exec.CommandContext(ctx, "git", "-C", worktreePath, "pull", remote, syncBranch) // #nosec G204 - worktreePath, remote, and syncBranch are from config