repo: Fall back from missing upstreams
3157f06787f0640095ad0be6c496c5e7c156740c
Resolve configured branch upstreams before selecting a merge base. This lets branch-base discovery skip deleted remote-tracking refs and fall back to the local trunk. Assisted-by: Codex:gpt-5.6-sol
1 parent
8a10dfc9
crates/repo/src/git.rs
+200 -10
| 449 | 449 | let contains = format!("--contains={target_id}"); |
|
| 450 | 450 | let bytes = self.git( |
|
| 451 | 451 | "for-each-ref", |
|
| 452 | 452 | &[ |
|
| 453 | 453 | &contains, |
|
| 454 | - | "--format=%(objectname)%00%(upstream:short)", |
|
| 454 | + | "--format=%(objectname)%00%(upstream)", |
|
| 455 | 455 | "refs/heads", |
|
| 456 | 456 | ], |
|
| 457 | 457 | )?; |
|
| 458 | 458 | let text = utf8(bytes)?; |
|
| 459 | 459 | let target_id = target_id.to_string(); |
|
| 460 | - | let upstream = text |
|
| 461 | - | .lines() |
|
| 462 | - | .filter_map(|line| line.split_once('\0')) |
|
| 463 | - | .min_by_key(|(head, upstream)| (*head != target_id, upstream.is_empty())) |
|
| 464 | - | .map(|(_, upstream)| upstream) |
|
| 465 | - | .filter(|upstream| !upstream.is_empty()); |
|
| 466 | - | let base = if let Some(upstream) = upstream { |
|
| 467 | - | Rev::try_from(upstream) |
|
| 468 | - | .map_err(|error| RepoError::Git(format!("invalid branch upstream: {error}")))? |
|
| 460 | + | let refs = || text.lines().filter_map(|line| line.split_once('\0')); |
|
| 461 | + | let has_exact_head = refs().any(|(head, _)| *head == target_id); |
|
| 462 | + | let mut base = None; |
|
| 463 | + | for (_, upstream) in refs() |
|
| 464 | + | .filter(|(_, upstream)| !upstream.is_empty()) |
|
| 465 | + | .filter(|(head, _)| !has_exact_head || *head == target_id) |
|
| 466 | + | { |
|
| 467 | + | let revision = format!("{upstream}^{{commit}}"); |
|
| 468 | + | let output = self.run( |
|
| 469 | + | "rev-parse", |
|
| 470 | + | &["--verify", "--quiet", &revision], |
|
| 471 | + | Opts::default(), |
|
| 472 | + | )?; |
|
| 473 | + | if !output.status.success() { |
|
| 474 | + | continue; |
|
| 475 | + | } |
|
| 476 | + | let upstream = Oid::try_from(output.stdout.as_slice()) |
|
| 477 | + | .map_err(|_| RepoError::Git(format!("invalid oid from rev-parse {upstream}")))?; |
|
| 478 | + | base = Some(Rev::from(upstream)); |
|
| 479 | + | break; |
|
| 480 | + | } |
|
| 481 | + | let base = if let Some(base) = base { |
|
| 482 | + | base |
|
| 469 | 483 | } else { |
|
| 470 | 484 | let branches = self.branches()?; |
|
| 471 | 485 | let Some(trunk) = self.trunk(&branches)? else { |
|
| 472 | 486 | return Ok(None); |
|
| 473 | 487 | }; |
| 1101 | 1115 | repository.branch_base(&target).unwrap(), |
|
| 1102 | 1116 | Some(repository.commit_id(&integration).unwrap()) |
|
| 1103 | 1117 | ); |
|
| 1104 | 1118 | } |
|
| 1105 | 1119 | ||
| 1120 | + | #[test] |
|
| 1121 | + | fn branch_base_ignores_missing_upstream() { |
|
| 1122 | + | let directory = TempIndex::new().unwrap(); |
|
| 1123 | + | let repository_path = directory.path().parent().unwrap(); |
|
| 1124 | + | let status = Command::new("git") |
|
| 1125 | + | .args(["init", "-q", "-b", "main"]) |
|
| 1126 | + | .arg(repository_path) |
|
| 1127 | + | .status() |
|
| 1128 | + | .unwrap(); |
|
| 1129 | + | assert!(status.success()); |
|
| 1130 | + | let repository = Repository::discover(repository_path.to_owned()).unwrap(); |
|
| 1131 | + | git( |
|
| 1132 | + | &repository, |
|
| 1133 | + | "commit", |
|
| 1134 | + | &["-q", "--allow-empty", "-m", "main"], |
|
| 1135 | + | ); |
|
| 1136 | + | git(&repository, "switch", &["-q", "-c", "feature"]); |
|
| 1137 | + | git( |
|
| 1138 | + | &repository, |
|
| 1139 | + | "commit", |
|
| 1140 | + | &["-q", "--allow-empty", "-m", "feature"], |
|
| 1141 | + | ); |
|
| 1142 | + | git(&repository, "remote", &["add", "origin", "."]); |
|
| 1143 | + | git( |
|
| 1144 | + | &repository, |
|
| 1145 | + | "update-ref", |
|
| 1146 | + | &["refs/remotes/origin/feature", "feature"], |
|
| 1147 | + | ); |
|
| 1148 | + | git( |
|
| 1149 | + | &repository, |
|
| 1150 | + | "branch", |
|
| 1151 | + | &["--set-upstream-to=origin/feature", "feature"], |
|
| 1152 | + | ); |
|
| 1153 | + | git( |
|
| 1154 | + | &repository, |
|
| 1155 | + | "update-ref", |
|
| 1156 | + | &["-d", "refs/remotes/origin/feature"], |
|
| 1157 | + | ); |
|
| 1158 | + | assert_eq!( |
|
| 1159 | + | repository.config("branch.feature.remote").unwrap(), |
|
| 1160 | + | Some("origin".to_owned()) |
|
| 1161 | + | ); |
|
| 1162 | + | assert_eq!( |
|
| 1163 | + | repository.config("branch.feature.merge").unwrap(), |
|
| 1164 | + | Some("refs/heads/feature".to_owned()) |
|
| 1165 | + | ); |
|
| 1166 | + | let feature = repository |
|
| 1167 | + | .commit_id(&Rev::try_from("feature").unwrap()) |
|
| 1168 | + | .unwrap(); |
|
| 1169 | + | let target = Rev::from(feature); |
|
| 1170 | + | let main = Rev::try_from("main").unwrap(); |
|
| 1171 | + | ||
| 1172 | + | assert_eq!( |
|
| 1173 | + | repository.branch_base(&target).unwrap(), |
|
| 1174 | + | Some(repository.commit_id(&main).unwrap()) |
|
| 1175 | + | ); |
|
| 1176 | + | } |
|
| 1177 | + | ||
| 1178 | + | #[test] |
|
| 1179 | + | fn branch_base_missing_exact_upstream_falls_back_to_trunk() { |
|
| 1180 | + | let directory = TempIndex::new().unwrap(); |
|
| 1181 | + | let repository_path = directory.path().parent().unwrap(); |
|
| 1182 | + | let status = Command::new("git") |
|
| 1183 | + | .args(["init", "-q", "-b", "main"]) |
|
| 1184 | + | .arg(repository_path) |
|
| 1185 | + | .status() |
|
| 1186 | + | .unwrap(); |
|
| 1187 | + | assert!(status.success()); |
|
| 1188 | + | let repository = Repository::discover(repository_path.to_owned()).unwrap(); |
|
| 1189 | + | git( |
|
| 1190 | + | &repository, |
|
| 1191 | + | "commit", |
|
| 1192 | + | &["-q", "--allow-empty", "-m", "main"], |
|
| 1193 | + | ); |
|
| 1194 | + | git(&repository, "switch", &["-q", "-c", "integration"]); |
|
| 1195 | + | git( |
|
| 1196 | + | &repository, |
|
| 1197 | + | "commit", |
|
| 1198 | + | &["-q", "--allow-empty", "-m", "integration"], |
|
| 1199 | + | ); |
|
| 1200 | + | git(&repository, "switch", &["-q", "-c", "feature"]); |
|
| 1201 | + | git( |
|
| 1202 | + | &repository, |
|
| 1203 | + | "commit", |
|
| 1204 | + | &["-q", "--allow-empty", "-m", "feature"], |
|
| 1205 | + | ); |
|
| 1206 | + | git(&repository, "remote", &["add", "origin", "."]); |
|
| 1207 | + | git( |
|
| 1208 | + | &repository, |
|
| 1209 | + | "update-ref", |
|
| 1210 | + | &["refs/remotes/origin/feature", "feature"], |
|
| 1211 | + | ); |
|
| 1212 | + | git( |
|
| 1213 | + | &repository, |
|
| 1214 | + | "branch", |
|
| 1215 | + | &["--set-upstream-to=origin/feature", "feature"], |
|
| 1216 | + | ); |
|
| 1217 | + | git( |
|
| 1218 | + | &repository, |
|
| 1219 | + | "update-ref", |
|
| 1220 | + | &["-d", "refs/remotes/origin/feature"], |
|
| 1221 | + | ); |
|
| 1222 | + | git(&repository, "switch", &["-q", "-c", "container"]); |
|
| 1223 | + | git( |
|
| 1224 | + | &repository, |
|
| 1225 | + | "commit", |
|
| 1226 | + | &["-q", "--allow-empty", "-m", "container"], |
|
| 1227 | + | ); |
|
| 1228 | + | git( |
|
| 1229 | + | &repository, |
|
| 1230 | + | "branch", |
|
| 1231 | + | &["--set-upstream-to=integration", "container"], |
|
| 1232 | + | ); |
|
| 1233 | + | let feature = repository |
|
| 1234 | + | .commit_id(&Rev::try_from("feature").unwrap()) |
|
| 1235 | + | .unwrap(); |
|
| 1236 | + | let target = Rev::from(feature); |
|
| 1237 | + | let main = Rev::try_from("main").unwrap(); |
|
| 1238 | + | ||
| 1239 | + | assert_eq!( |
|
| 1240 | + | repository.branch_base(&target).unwrap(), |
|
| 1241 | + | Some(repository.commit_id(&main).unwrap()) |
|
| 1242 | + | ); |
|
| 1243 | + | } |
|
| 1244 | + | ||
| 1245 | + | #[test] |
|
| 1246 | + | fn branch_base_exact_branch_without_upstream_falls_back_to_trunk() { |
|
| 1247 | + | let directory = TempIndex::new().unwrap(); |
|
| 1248 | + | let repository_path = directory.path().parent().unwrap(); |
|
| 1249 | + | let status = Command::new("git") |
|
| 1250 | + | .args(["init", "-q", "-b", "main"]) |
|
| 1251 | + | .arg(repository_path) |
|
| 1252 | + | .status() |
|
| 1253 | + | .unwrap(); |
|
| 1254 | + | assert!(status.success()); |
|
| 1255 | + | let repository = Repository::discover(repository_path.to_owned()).unwrap(); |
|
| 1256 | + | git( |
|
| 1257 | + | &repository, |
|
| 1258 | + | "commit", |
|
| 1259 | + | &["-q", "--allow-empty", "-m", "main"], |
|
| 1260 | + | ); |
|
| 1261 | + | git(&repository, "switch", &["-q", "-c", "feature"]); |
|
| 1262 | + | git( |
|
| 1263 | + | &repository, |
|
| 1264 | + | "commit", |
|
| 1265 | + | &["-q", "--allow-empty", "-m", "feature"], |
|
| 1266 | + | ); |
|
| 1267 | + | git(&repository, "switch", &["-q", "-c", "container"]); |
|
| 1268 | + | git( |
|
| 1269 | + | &repository, |
|
| 1270 | + | "commit", |
|
| 1271 | + | &["-q", "--allow-empty", "-m", "container"], |
|
| 1272 | + | ); |
|
| 1273 | + | git(&repository, "remote", &["add", "origin", "."]); |
|
| 1274 | + | git( |
|
| 1275 | + | &repository, |
|
| 1276 | + | "update-ref", |
|
| 1277 | + | &["refs/remotes/origin/container", "container"], |
|
| 1278 | + | ); |
|
| 1279 | + | git( |
|
| 1280 | + | &repository, |
|
| 1281 | + | "branch", |
|
| 1282 | + | &["--set-upstream-to=origin/container", "container"], |
|
| 1283 | + | ); |
|
| 1284 | + | let feature = repository |
|
| 1285 | + | .commit_id(&Rev::try_from("feature").unwrap()) |
|
| 1286 | + | .unwrap(); |
|
| 1287 | + | let target = Rev::from(feature); |
|
| 1288 | + | let main = Rev::try_from("main").unwrap(); |
|
| 1289 | + | ||
| 1290 | + | assert_eq!( |
|
| 1291 | + | repository.branch_base(&target).unwrap(), |
|
| 1292 | + | Some(repository.commit_id(&main).unwrap()) |
|
| 1293 | + | ); |
|
| 1294 | + | } |
|
| 1295 | + | ||
| 1106 | 1296 | #[test] |
|
| 1107 | 1297 | fn commit_stats_uses_merge_first_parent() { |
|
| 1108 | 1298 | let directory = TempIndex::new().unwrap(); |
|
| 1109 | 1299 | let repository_path = directory.path().parent().unwrap(); |
|
| 1110 | 1300 | let status = Command::new("git") |
root\nnew file mode"));
+0 -0
file\n"));
+0 -0
file\n"),
+0 -0
file\n"),
+0 -0
a\n--- a/a\n+++ b/a\n@@ -1 +1 @@\n-old\n+new\n".to_owned(),
+0 -0
b\n--- a/b\n+++ b/b\n@@ -0,0 +1 @@\n+line\n".to_owned(),
+0 -0