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:
@@ -109,10 +109,12 @@ func syncBranchCommitAndPushWithOptions(ctx context.Context, store storage.Stora
|
|||||||
return false, fmt.Errorf("failed to commit in worktree: %w", err)
|
return false, fmt.Errorf("failed to commit in worktree: %w", err)
|
||||||
}
|
}
|
||||||
log.log("Committed changes to sync branch %s", syncBranch)
|
log.log("Committed changes to sync branch %s", syncBranch)
|
||||||
|
|
||||||
// Push if enabled
|
// Push if enabled
|
||||||
if autoPush {
|
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)
|
return false, fmt.Errorf("failed to push from worktree: %w", err)
|
||||||
}
|
}
|
||||||
log.log("Pushed sync branch %s to remote", syncBranch)
|
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.
|
// gitPushFromWorktree pushes the sync branch from the worktree.
|
||||||
// If push fails due to remote having newer commits, it will fetch, rebase, and retry.
|
// If push fails due to remote having newer commits, it will fetch, rebase, and retry.
|
||||||
func gitPushFromWorktree(ctx context.Context, worktreePath, branch string) error {
|
// The configuredRemote parameter allows passing the bd config sync.remote value.
|
||||||
// Get remote name (usually "origin")
|
func gitPushFromWorktree(ctx context.Context, worktreePath, branch, configuredRemote string) error {
|
||||||
remoteCmd := exec.CommandContext(ctx, "git", "-C", worktreePath, "config", "--get", fmt.Sprintf("branch.%s.remote", branch)) // #nosec G204 - worktreePath and branch are from config
|
// Use configured remote if provided, otherwise check git branch config
|
||||||
remoteOutput, err := remoteCmd.Output()
|
remote := configuredRemote
|
||||||
if err != nil {
|
if remote == "" {
|
||||||
// If no remote configured, default to "origin" and set up tracking
|
remoteCmd := exec.CommandContext(ctx, "git", "-C", worktreePath, "config", "--get", fmt.Sprintf("branch.%s.remote", branch)) // #nosec G204 - worktreePath and branch are from config
|
||||||
remoteOutput = []byte("origin\n")
|
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
|
// 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
|
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)
|
return false, fmt.Errorf("failed to create worktree: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get remote name
|
// Get remote name - check bd config first, then git branch config, then default to "origin"
|
||||||
remoteCmd := exec.CommandContext(ctx, "git", "-C", worktreePath, "config", "--get", fmt.Sprintf("branch.%s.remote", syncBranch)) // #nosec G204 - worktreePath and syncBranch are from config
|
remote := ""
|
||||||
remoteOutput, err := remoteCmd.Output()
|
if configuredRemote, err := store.GetConfig(ctx, "sync.remote"); err == nil && configuredRemote != "" {
|
||||||
if err != nil {
|
remote = configuredRemote
|
||||||
// If no remote configured, default to "origin"
|
} else {
|
||||||
remoteOutput = []byte("origin\n")
|
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
|
// Pull in worktree
|
||||||
cmd := exec.CommandContext(ctx, "git", "-C", worktreePath, "pull", remote, syncBranch) // #nosec G204 - worktreePath, remote, and syncBranch are from config
|
cmd := exec.CommandContext(ctx, "git", "-C", worktreePath, "pull", remote, syncBranch) // #nosec G204 - worktreePath, remote, and syncBranch are from config
|
||||||
|
|||||||
Reference in New Issue
Block a user