diff --git a/go/libraries/doltcore/sqle/dprocedures/dolt_remote.go b/go/libraries/doltcore/sqle/dprocedures/dolt_remote.go index 4aff66eeb7..b9c8dc1d53 100644 --- a/go/libraries/doltcore/sqle/dprocedures/dolt_remote.go +++ b/go/libraries/doltcore/sqle/dprocedures/dolt_remote.go @@ -141,5 +141,32 @@ func removeRemote(ctx *sql.Context, dbd env.DbData[*sql.Context], apr *argparser } } + // Remove branch configurations that reference the removed remote + branches, err := dbd.Rsr.GetBranches() + if err != nil { + return fmt.Errorf("failed to get branches: %w", err) + } + + var branchesToUpdate []string + branches.Iter(func(branchName string, config env.BranchConfig) bool { + if config.Remote == remote.Name { + branchesToUpdate = append(branchesToUpdate, branchName) + } + return true + }) + + // Clear the remote tracking for these branches by updating their configs + for _, branchName := range branchesToUpdate { + currentConfig, _ := branches.Get(branchName) + updatedConfig := env.BranchConfig{ + Merge: currentConfig.Merge, + Remote: "", // Clear the remote reference + } + err = dbd.Rsw.UpdateBranch(branchName, updatedConfig) + if err != nil { + return fmt.Errorf("failed to update branch config for %s: %w", branchName, err) + } + } + return dbd.Rsw.RemoveRemote(ctx, remote.Name) } diff --git a/integration-tests/bats/remote-cmd.bats b/integration-tests/bats/remote-cmd.bats index 58d0099be8..04061b67a6 100755 --- a/integration-tests/bats/remote-cmd.bats +++ b/integration-tests/bats/remote-cmd.bats @@ -75,3 +75,36 @@ teardown() { [[ "$output" =~ "only valid for aws remotes" ]] || false fi } + +@test "remote-cmd: remove origin and verify tracking is gone" { + mkdir remote_repo + mkdir initter + cd initter + dolt init + dolt remote add origin file://../remote_repo + dolt push origin main + cd ../ + rm -rf initter + + dolt clone file://remote_repo cloned_repo + cd cloned_repo + + # Verify we are tracking origin + run dolt status + [ "$status" -eq 0 ] + [[ "$output" =~ "Your branch is up to date with 'origin/main'" ]] || false + + grep 'origin' .dolt/repo_state.json + + # Remove the remote + dolt remote remove origin + + # Verify that the current branch is not tracking origin (because it doesn't exist) + run dolt status + [ "$status" -eq 0 ] + [[ ! "$output" =~ "origin" ]] || false + [[ ! "$output" =~ "Your branch is up to date with 'origin/main'" ]] || false + + run grep -q 'origin' .dolt/repo_state.json + [ "$status" -eq 1 ] +}