brain: Preserve commit decisions across rebases
38de92fc3339df1e0e334ea4c6116350600cd96e
Rebase a reviewed range after Git rewrites unchanged commits: rejected and dropped decisions previously disappeared because they were matched only by commit ID. Persist each decision's stable patch ID and duplicate occurrence, reserve commits whose exact IDs remain, and remap only rewritten decisions by that identity. This keeps duplicate patches distinct without reopening stale commits. Assisted-by: Codex:gpt-5.6
1 parent
9e33f884
crates/brain/src/backend.rs
+261 -41
| 186 | 186 | keep |
|
| 187 | 187 | }); |
|
| 188 | 188 | } |
|
| 189 | 189 | } |
|
| 190 | 190 | ||
| 191 | + | /// Stable identity of a commit-level review decision. |
|
| 192 | + | #[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)] |
|
| 193 | + | struct CommitDecision { |
|
| 194 | + | /// Stable identity of the commit's complete change. |
|
| 195 | + | patch_id: PatchId, |
|
| 196 | + | /// Occurrence of this patch id within the reviewed range. |
|
| 197 | + | ordinal: u32, |
|
| 198 | + | } |
|
| 199 | + | ||
| 200 | + | impl CommitDecision { |
|
| 201 | + | /// Decodes a commit id and decision identity from one state row. |
|
| 202 | + | fn decode(row: &str) -> Option<(Oid, Self)> { |
|
| 203 | + | let mut fields = row.split_whitespace(); |
|
| 204 | + | let commit = fields.next()?.parse().ok()?; |
|
| 205 | + | let patch_id = PatchId::from_str(fields.next()?).ok()?; |
|
| 206 | + | let ordinal = fields.next()?.parse().ok()?; |
|
| 207 | + | if fields.next().is_some() { |
|
| 208 | + | return None; |
|
| 209 | + | } |
|
| 210 | + | Some((commit, Self { patch_id, ordinal })) |
|
| 211 | + | } |
|
| 212 | + | } |
|
| 213 | + | ||
| 191 | 214 | /// Authoritative accepted-hunk state for one review. |
|
| 192 | 215 | pub(crate) type Accepted = BTreeSet<AcceptedHunk>; |
|
| 193 | - | /// Authoritative rejected-commit state for one review. |
|
| 194 | - | type Rejected = BTreeSet<Oid>; |
|
| 195 | - | /// Authoritative dropped-commit state for one review. |
|
| 196 | - | type Dropped = BTreeSet<Oid>; |
|
| 216 | + | /// Authoritative rejected-commit state keyed by commit id. |
|
| 217 | + | type Rejected = BTreeMap<Oid, CommitDecision>; |
|
| 218 | + | /// Authoritative dropped-commit state keyed by commit id. |
|
| 219 | + | type Dropped = BTreeMap<Oid, CommitDecision>; |
|
| 197 | 220 | ||
| 198 | 221 | /// Persisted state for one resolved target of a selector-keyed review. |
|
| 199 | 222 | #[derive(Clone, Debug, Eq, PartialEq)] |
|
| 200 | 223 | struct PersistedState { |
|
| 201 | 224 | /// Target commit whose hunks the accepted identities describe. |
| 357 | 380 | }, |
|
| 358 | 381 | ); |
|
| 359 | 382 | (progress, BTreeSet::new()) |
|
| 360 | 383 | } |
|
| 361 | 384 | }; |
|
| 362 | - | let state = if dropped_state.contains(&commit.commit) { |
|
| 385 | + | let state = if dropped_state.contains_key(&commit.commit) { |
|
| 363 | 386 | CommitReviewState::Dropped |
|
| 364 | - | } else if rejected_state.contains(&commit.commit) { |
|
| 387 | + | } else if rejected_state.contains_key(&commit.commit) { |
|
| 365 | 388 | CommitReviewState::Rejected |
|
| 366 | 389 | } else if progress.is_complete() { |
|
| 367 | 390 | CommitReviewState::Accepted |
|
| 368 | 391 | } else { |
|
| 369 | 392 | CommitReviewState::Pending |
| 549 | 572 | fn encode_state(state: &PersistedState) -> String { |
|
| 550 | 573 | let mut message = format!( |
|
| 551 | 574 | "{SYNTHETIC_BRAIN_MESSAGE}\n\n{STATE_SECTION}\ntarget {}\n", |
|
| 552 | 575 | state.target |
|
| 553 | 576 | ); |
|
| 554 | - | for commit in &state.rejected { |
|
| 555 | - | message.push_str(&format!("reject {commit}\n")); |
|
| 577 | + | for (commit, decision) in &state.rejected { |
|
| 578 | + | message.push_str(&format!( |
|
| 579 | + | "reject {commit} {} {}\n", |
|
| 580 | + | decision.patch_id, decision.ordinal |
|
| 581 | + | )); |
|
| 556 | 582 | } |
|
| 557 | - | for commit in &state.dropped { |
|
| 558 | - | message.push_str(&format!("drop {commit}\n")); |
|
| 583 | + | for (commit, decision) in &state.dropped { |
|
| 584 | + | message.push_str(&format!( |
|
| 585 | + | "drop {commit} {} {}\n", |
|
| 586 | + | decision.patch_id, decision.ordinal |
|
| 587 | + | )); |
|
| 559 | 588 | } |
|
| 560 | 589 | for hunk in &state.accepted { |
|
| 561 | 590 | message.push_str(&format!( |
|
| 562 | 591 | "{} {} {} {} {}\n", |
|
| 563 | 592 | hunk.commit, hunk.patch_id, hunk.ordinal, hunk.lines, hunk.commit_lines |
| 577 | 606 | let target = metadata.next()?.parse().ok()?; |
|
| 578 | 607 | if metadata.next().is_some() { |
|
| 579 | 608 | return None; |
|
| 580 | 609 | } |
|
| 581 | 610 | let mut accepted = BTreeSet::new(); |
|
| 582 | - | let mut rejected = BTreeSet::new(); |
|
| 583 | - | let mut dropped = BTreeSet::new(); |
|
| 611 | + | let mut rejected = BTreeMap::new(); |
|
| 612 | + | let mut dropped = BTreeMap::new(); |
|
| 584 | 613 | let mut commit_totals = BTreeMap::new(); |
|
| 585 | 614 | ||
| 586 | 615 | for line in body.filter(|line| !line.trim().is_empty()) { |
|
| 587 | - | if let Some(commit) = line.strip_prefix("reject ") { |
|
| 588 | - | let commit = commit.parse().ok()?; |
|
| 589 | - | if !rejected.insert(commit) || dropped.contains(&commit) { |
|
| 616 | + | if let Some(decision) = line.strip_prefix("reject ") { |
|
| 617 | + | let (commit, decision) = CommitDecision::decode(decision)?; |
|
| 618 | + | if rejected.insert(commit, decision).is_some() || dropped.contains_key(&commit) { |
|
| 590 | 619 | return None; |
|
| 591 | 620 | } |
|
| 592 | 621 | continue; |
|
| 593 | 622 | } |
|
| 594 | - | if let Some(commit) = line.strip_prefix("drop ") { |
|
| 595 | - | let commit = commit.parse().ok()?; |
|
| 596 | - | if !dropped.insert(commit) || rejected.contains(&commit) { |
|
| 623 | + | if let Some(decision) = line.strip_prefix("drop ") { |
|
| 624 | + | let (commit, decision) = CommitDecision::decode(decision)?; |
|
| 625 | + | if dropped.insert(commit, decision).is_some() || rejected.contains_key(&commit) { |
|
| 597 | 626 | return None; |
|
| 598 | 627 | } |
|
| 599 | 628 | continue; |
|
| 600 | 629 | } |
|
| 601 | 630 | let mut fields = line.split_whitespace(); |
| 1331 | 1360 | ||
| 1332 | 1361 | /// Toggles the rejected state of one commit. |
|
| 1333 | 1362 | pub fn toggle_commit_rejected(&mut self, commit: Oid) -> Result<(), Error> { |
|
| 1334 | 1363 | let mut rejected = self.rejected.clone(); |
|
| 1335 | 1364 | let mut dropped = self.dropped.clone(); |
|
| 1336 | - | if !rejected.remove(&commit) { |
|
| 1337 | - | rejected.insert(commit); |
|
| 1365 | + | if rejected.remove(&commit).is_none() { |
|
| 1366 | + | let decision = self.commit_decision(commit)?; |
|
| 1367 | + | rejected.insert(commit, decision); |
|
| 1338 | 1368 | dropped.remove(&commit); |
|
| 1339 | 1369 | } |
|
| 1340 | 1370 | let tree = self.repository.tree_id(self.commit)?; |
|
| 1341 | 1371 | self.persist(tree, self.accepted.clone(), rejected, dropped) |
|
| 1342 | 1372 | } |
|
| 1343 | 1373 | ||
| 1344 | 1374 | /// Toggles the dropped state of one commit. |
|
| 1345 | 1375 | pub fn toggle_commit_dropped(&mut self, commit: Oid) -> Result<(), Error> { |
|
| 1346 | 1376 | let mut rejected = self.rejected.clone(); |
|
| 1347 | 1377 | let mut dropped = self.dropped.clone(); |
|
| 1348 | - | if !dropped.remove(&commit) { |
|
| 1349 | - | dropped.insert(commit); |
|
| 1378 | + | if dropped.remove(&commit).is_none() { |
|
| 1379 | + | let decision = self.commit_decision(commit)?; |
|
| 1380 | + | dropped.insert(commit, decision); |
|
| 1350 | 1381 | rejected.remove(&commit); |
|
| 1351 | 1382 | } |
|
| 1352 | 1383 | let tree = self.repository.tree_id(self.commit)?; |
|
| 1353 | 1384 | self.persist(tree, self.accepted.clone(), rejected, dropped) |
|
| 1354 | 1385 | } |
| 1364 | 1395 | Err(rustik_repo::RepoError::PatchApply(_)) => Ok(tree), |
|
| 1365 | 1396 | Err(error) => Err(error.into()), |
|
| 1366 | 1397 | } |
|
| 1367 | 1398 | } |
|
| 1368 | 1399 | ||
| 1400 | + | /// Computes the stable patch id for a commit's complete change. |
|
| 1401 | + | fn commit_patch_id(&self, commit: Oid) -> Result<PatchId, Error> { |
|
| 1402 | + | let diff = self.repository.commit_diff(commit)?; |
|
| 1403 | + | let document = DiffDocument::decode_bytes(&diff)?; |
|
| 1404 | + | self.repository |
|
| 1405 | + | .patch_id(&document.to_unified_string()?) |
|
| 1406 | + | .map_err(Error::from) |
|
| 1407 | + | } |
|
| 1408 | + | ||
| 1409 | + | /// Computes the stable decision identity for one commit in this range. |
|
| 1410 | + | fn commit_decision(&self, commit: Oid) -> Result<CommitDecision, Error> { |
|
| 1411 | + | let patch_id = self.commit_patch_id(commit)?; |
|
| 1412 | + | let commits = self |
|
| 1413 | + | .repository |
|
| 1414 | + | .commits(&Rev::from(self.base), &Rev::from(self.target))?; |
|
| 1415 | + | let mut ordinal = 0; |
|
| 1416 | + | for (candidate, _) in commits { |
|
| 1417 | + | if candidate == commit { |
|
| 1418 | + | return Ok(CommitDecision { patch_id, ordinal }); |
|
| 1419 | + | } |
|
| 1420 | + | if self.commit_patch_id(candidate)? == patch_id { |
|
| 1421 | + | ordinal += 1; |
|
| 1422 | + | } |
|
| 1423 | + | } |
|
| 1424 | + | Err(Error::Git(format!( |
|
| 1425 | + | "commit {commit} is outside the reviewed range" |
|
| 1426 | + | ))) |
|
| 1427 | + | } |
|
| 1428 | + | ||
| 1369 | 1429 | /// Applies one file's accepted hunks to `tree`. |
|
| 1370 | 1430 | /// |
|
| 1371 | 1431 | /// The combined patch is tried first so a file costs one `git apply`; only |
|
| 1372 | 1432 | /// when it fails are the hunks replayed individually, so one hunk that no |
|
| 1373 | 1433 | /// longer applies cannot drop the rest of its file from the projection. |
| 1382 | 1442 | tree = self.project(tree, patch)?; |
|
| 1383 | 1443 | } |
|
| 1384 | 1444 | Ok(tree) |
|
| 1385 | 1445 | } |
|
| 1386 | 1446 | ||
| 1447 | + | /// Remaps commit-level decisions by exact id, then stable decision identity. |
|
| 1448 | + | fn remap_commit_decisions( |
|
| 1449 | + | &self, |
|
| 1450 | + | commits: &[Commit], |
|
| 1451 | + | documents: &[DiffDocument], |
|
| 1452 | + | stored_rejected: &Rejected, |
|
| 1453 | + | stored_dropped: &Dropped, |
|
| 1454 | + | ) -> Result<(Rejected, Dropped), Error> { |
|
| 1455 | + | let current = commits |
|
| 1456 | + | .iter() |
|
| 1457 | + | .map(|commit| commit.id) |
|
| 1458 | + | .collect::<BTreeSet<_>>(); |
|
| 1459 | + | let rejected_identities = stored_rejected |
|
| 1460 | + | .iter() |
|
| 1461 | + | .filter(|(commit, _)| !current.contains(commit)) |
|
| 1462 | + | .map(|(_, decision)| decision.clone()) |
|
| 1463 | + | .collect::<BTreeSet<_>>(); |
|
| 1464 | + | let dropped_identities = stored_dropped |
|
| 1465 | + | .iter() |
|
| 1466 | + | .filter(|(commit, _)| !current.contains(commit)) |
|
| 1467 | + | .map(|(_, decision)| decision.clone()) |
|
| 1468 | + | .collect::<BTreeSet<_>>(); |
|
| 1469 | + | let mut rejected = Rejected::new(); |
|
| 1470 | + | let mut dropped = Dropped::new(); |
|
| 1471 | + | let mut seen = BTreeMap::<PatchId, u32>::new(); |
|
| 1472 | + | for (commit, document) in commits.iter().zip(documents) { |
|
| 1473 | + | let patch_id = self.repository.patch_id(&document.to_unified_string()?)?; |
|
| 1474 | + | let ordinal = seen.entry(patch_id.clone()).or_default(); |
|
| 1475 | + | let identity = CommitDecision { |
|
| 1476 | + | patch_id, |
|
| 1477 | + | ordinal: *ordinal, |
|
| 1478 | + | }; |
|
| 1479 | + | *ordinal += 1; |
|
| 1480 | + | if stored_rejected.contains_key(&commit.id) || rejected_identities.contains(&identity) { |
|
| 1481 | + | rejected.insert(commit.id, identity); |
|
| 1482 | + | } else if stored_dropped.contains_key(&commit.id) |
|
| 1483 | + | || dropped_identities.contains(&identity) |
|
| 1484 | + | { |
|
| 1485 | + | dropped.insert(commit.id, identity); |
|
| 1486 | + | } |
|
| 1487 | + | } |
|
| 1488 | + | Ok((rejected, dropped)) |
|
| 1489 | + | } |
|
| 1490 | + | ||
| 1387 | 1491 | /// Remaps accepted identities and exact rejected commits onto a moved range. |
|
| 1388 | 1492 | fn remap( |
|
| 1389 | 1493 | &mut self, |
|
| 1390 | 1494 | commits: &[Commit], |
|
| 1391 | 1495 | documents: &[DiffDocument], |
| 1425 | 1529 | if hunks.is_empty() { |
|
| 1426 | 1530 | continue; |
|
| 1427 | 1531 | } |
|
| 1428 | 1532 | tree = self.project_file(tree, &FilePatches::new(file, hunks)?)?; |
|
| 1429 | 1533 | } |
|
| 1430 | - | let current_commits = commits |
|
| 1431 | - | .iter() |
|
| 1432 | - | .map(|commit| commit.id) |
|
| 1433 | - | .collect::<BTreeSet<_>>(); |
|
| 1434 | - | let rejected = stored_rejected |
|
| 1435 | - | .intersection(¤t_commits) |
|
| 1436 | - | .copied() |
|
| 1437 | - | .collect(); |
|
| 1438 | - | let dropped = stored_dropped |
|
| 1439 | - | .intersection(¤t_commits) |
|
| 1440 | - | .copied() |
|
| 1441 | - | .collect(); |
|
| 1534 | + | let (rejected, dropped) = |
|
| 1535 | + | self.remap_commit_decisions(commits, documents, stored_rejected, stored_dropped)?; |
|
| 1442 | 1536 | self.persist(tree, accepted, rejected, dropped) |
|
| 1443 | 1537 | } |
|
| 1444 | 1538 | ||
| 1445 | 1539 | /// Persists review decisions and the derived tree in one commit. |
|
| 1446 | 1540 | fn persist( |
| 1753 | 1847 | repository.git(&["config", "user.name", "Rustik Test"]); |
|
| 1754 | 1848 | repository.git(&["config", "user.email", "rustik@example.invalid"]); |
|
| 1755 | 1849 | repository |
|
| 1756 | 1850 | } |
|
| 1757 | 1851 | ||
| 1852 | + | /// Creates a topic containing the same patch before and after a revert. |
|
| 1853 | + | fn with_duplicate_patch_commits() -> Self { |
|
| 1854 | + | let repository = Self::new(); |
|
| 1855 | + | let reviewed = repository.path.join("reviewed.txt"); |
|
| 1856 | + | fs::write(&reviewed, "base\n").unwrap(); |
|
| 1857 | + | repository.git(&["add", "reviewed.txt"]); |
|
| 1858 | + | repository.git(&["commit", "-q", "-m", "base"]); |
|
| 1859 | + | repository.git(&["switch", "-q", "-c", "topic"]); |
|
| 1860 | + | fs::write(&reviewed, "decision\n").unwrap(); |
|
| 1861 | + | repository.git(&["commit", "-q", "-am", "first"]); |
|
| 1862 | + | repository.git(&["revert", "--no-edit", "HEAD"]); |
|
| 1863 | + | fs::write(&reviewed, "decision\n").unwrap(); |
|
| 1864 | + | repository.git(&["commit", "-q", "-am", "second"]); |
|
| 1865 | + | repository |
|
| 1866 | + | } |
|
| 1867 | + | ||
| 1758 | 1868 | /// Runs a Git command and requires success. |
|
| 1759 | 1869 | fn git(&self, args: &[&str]) { |
|
| 1760 | 1870 | let status = Command::new("git") |
|
| 1761 | 1871 | .args(args) |
|
| 1762 | 1872 | .current_dir(&self.path) |
| 1874 | 1984 | let backend = GitBackend::new(&repository.path, base, target).unwrap(); |
|
| 1875 | 1985 | let commit = backend.commits().unwrap().pop().unwrap().id; |
|
| 1876 | 1986 | let mut brain = backend.reset_brain().unwrap(); |
|
| 1877 | 1987 | ||
| 1878 | 1988 | brain.toggle_commit_rejected(commit).unwrap(); |
|
| 1879 | - | assert_eq!(backend.brain().unwrap().rejected, Rejected::from([commit])); |
|
| 1989 | + | assert!(backend.brain().unwrap().rejected.contains_key(&commit)); |
|
| 1880 | 1990 | ||
| 1881 | 1991 | brain.toggle_commit_rejected(commit).unwrap(); |
|
| 1882 | 1992 | assert!(backend.brain().unwrap().rejected.is_empty()); |
|
| 1883 | 1993 | ||
| 1884 | 1994 | brain.toggle_commit_dropped(commit).unwrap(); |
|
| 1885 | 1995 | let stored = backend.brain().unwrap(); |
|
| 1886 | - | assert_eq!(stored.dropped, Dropped::from([commit])); |
|
| 1996 | + | assert!(stored.dropped.contains_key(&commit)); |
|
| 1887 | 1997 | assert!(stored.rejected.is_empty()); |
|
| 1888 | 1998 | ||
| 1889 | 1999 | brain.toggle_commit_rejected(commit).unwrap(); |
|
| 1890 | 2000 | let stored = backend.brain().unwrap(); |
|
| 1891 | - | assert_eq!(stored.rejected, Rejected::from([commit])); |
|
| 2001 | + | assert!(stored.rejected.contains_key(&commit)); |
|
| 1892 | 2002 | assert!(stored.dropped.is_empty()); |
|
| 1893 | 2003 | ||
| 1894 | 2004 | brain.toggle_commit_dropped(commit).unwrap(); |
|
| 1895 | 2005 | brain.toggle_commit_dropped(commit).unwrap(); |
|
| 1896 | 2006 | assert!(backend.brain().unwrap().dropped.is_empty()); |
| 1953 | 2063 | assert_eq!(rebased.state_ref, brain.state_ref); |
|
| 1954 | 2064 | assert_eq!(rebased.target, second.id); |
|
| 1955 | 2065 | assert_eq!(rebased.accepted, Accepted::from([second_identity])); |
|
| 1956 | 2066 | } |
|
| 1957 | 2067 | ||
| 2068 | + | #[test] |
|
| 2069 | + | fn rebase_keeps_rejection_for_an_unchanged_commit() { |
|
| 2070 | + | let repository = TestRepository::new(); |
|
| 2071 | + | let reviewed = repository.path.join("reviewed.txt"); |
|
| 2072 | + | let unrelated = repository.path.join("unrelated.txt"); |
|
| 2073 | + | fs::write(&reviewed, "base\n").unwrap(); |
|
| 2074 | + | repository.git(&["add", "reviewed.txt"]); |
|
| 2075 | + | repository.git(&["commit", "-q", "-m", "base"]); |
|
| 2076 | + | repository.git(&["switch", "-q", "-c", "topic"]); |
|
| 2077 | + | fs::write(&reviewed, "rejected\n").unwrap(); |
|
| 2078 | + | repository.git(&["commit", "-q", "-am", "topic"]); |
|
| 2079 | + | ||
| 2080 | + | let base = Rev::try_from("main").unwrap(); |
|
| 2081 | + | let target = Rev::try_from("topic").unwrap(); |
|
| 2082 | + | let backend = GitBackend::new(&repository.path, base, target).unwrap(); |
|
| 2083 | + | let rejected = backend.commits().unwrap().pop().unwrap().id; |
|
| 2084 | + | let mut brain = backend.reset_brain().unwrap(); |
|
| 2085 | + | brain.toggle_commit_rejected(rejected).unwrap(); |
|
| 2086 | + | ||
| 2087 | + | repository.git(&["switch", "-q", "main"]); |
|
| 2088 | + | fs::write(&unrelated, "new base\n").unwrap(); |
|
| 2089 | + | repository.git(&["add", "unrelated.txt"]); |
|
| 2090 | + | repository.git(&["commit", "-q", "-m", "advance base"]); |
|
| 2091 | + | repository.git(&["switch", "-q", "topic"]); |
|
| 2092 | + | repository.git(&["rebase", "-q", "main"]); |
|
| 2093 | + | let rewritten = backend.commits().unwrap().pop().unwrap().id; |
|
| 2094 | + | ||
| 2095 | + | assert_ne!(rewritten, rejected); |
|
| 2096 | + | assert!(matches!(backend.brain(), Err(Error::StaleReviewState))); |
|
| 2097 | + | ||
| 2098 | + | let rebased = backend.rebase_brain().unwrap(); |
|
| 2099 | + | ||
| 2100 | + | assert_eq!( |
|
| 2101 | + | rebased.rejected.keys().copied().collect::<BTreeSet<_>>(), |
|
| 2102 | + | BTreeSet::from([rewritten]) |
|
| 2103 | + | ); |
|
| 2104 | + | } |
|
| 2105 | + | ||
| 2106 | + | #[test] |
|
| 2107 | + | fn rebase_keeps_distinct_decisions_for_duplicate_commit_patches() { |
|
| 2108 | + | let repository = TestRepository::with_duplicate_patch_commits(); |
|
| 2109 | + | let unrelated = repository.path.join("unrelated.txt"); |
|
| 2110 | + | ||
| 2111 | + | let base = Rev::try_from("main").unwrap(); |
|
| 2112 | + | let target = Rev::try_from("topic").unwrap(); |
|
| 2113 | + | let backend = GitBackend::new(&repository.path, base, target).unwrap(); |
|
| 2114 | + | let commits = backend.commits().unwrap(); |
|
| 2115 | + | let mut brain = backend.reset_brain().unwrap(); |
|
| 2116 | + | brain.toggle_commit_dropped(commits[0].id).unwrap(); |
|
| 2117 | + | brain.toggle_commit_rejected(commits[2].id).unwrap(); |
|
| 2118 | + | ||
| 2119 | + | repository.git(&["switch", "-q", "main"]); |
|
| 2120 | + | fs::write(&unrelated, "new base\n").unwrap(); |
|
| 2121 | + | repository.git(&["add", "unrelated.txt"]); |
|
| 2122 | + | repository.git(&["commit", "-q", "-m", "advance base"]); |
|
| 2123 | + | repository.git(&["switch", "-q", "topic"]); |
|
| 2124 | + | repository.git(&["rebase", "-q", "main"]); |
|
| 2125 | + | let rewritten = backend.commits().unwrap(); |
|
| 2126 | + | let rebased = backend.rebase_brain().unwrap(); |
|
| 2127 | + | ||
| 2128 | + | assert_eq!( |
|
| 2129 | + | rebased.dropped.keys().copied().collect::<BTreeSet<_>>(), |
|
| 2130 | + | BTreeSet::from([rewritten[0].id]) |
|
| 2131 | + | ); |
|
| 2132 | + | assert_eq!( |
|
| 2133 | + | rebased.rejected.keys().copied().collect::<BTreeSet<_>>(), |
|
| 2134 | + | BTreeSet::from([rewritten[2].id]) |
|
| 2135 | + | ); |
|
| 2136 | + | } |
|
| 2137 | + | ||
| 2138 | + | #[test] |
|
| 2139 | + | fn rebase_reserves_an_unchanged_decision_before_matching_duplicates() { |
|
| 2140 | + | let repository = TestRepository::with_duplicate_patch_commits(); |
|
| 2141 | + | repository.git(&["branch", "-f", "main", "HEAD~1"]); |
|
| 2142 | + | ||
| 2143 | + | let base = Rev::try_from("main").unwrap(); |
|
| 2144 | + | let target = Rev::try_from("topic").unwrap(); |
|
| 2145 | + | let backend = GitBackend::new(&repository.path, base, target).unwrap(); |
|
| 2146 | + | let unchanged = backend.commits().unwrap().pop().unwrap().id; |
|
| 2147 | + | let mut brain = backend.reset_brain().unwrap(); |
|
| 2148 | + | brain.toggle_commit_rejected(unchanged).unwrap(); |
|
| 2149 | + | ||
| 2150 | + | repository.git(&["branch", "-f", "main", "HEAD~3"]); |
|
| 2151 | + | let commits = backend.commits().unwrap(); |
|
| 2152 | + | let rebased = backend.rebase_brain().unwrap(); |
|
| 2153 | + | ||
| 2154 | + | assert_eq!(commits[2].id, unchanged); |
|
| 2155 | + | assert_eq!( |
|
| 2156 | + | rebased.rejected.keys().copied().collect::<BTreeSet<_>>(), |
|
| 2157 | + | BTreeSet::from([unchanged]) |
|
| 2158 | + | ); |
|
| 2159 | + | } |
|
| 2160 | + | ||
| 1958 | 2161 | #[test] |
|
| 1959 | 2162 | fn statuses_are_empty_in_repository_without_commits() { |
|
| 1960 | 2163 | let fixture = TestRepository::new(); |
|
| 1961 | 2164 | ||
| 1962 | 2165 | let statuses = statuses(fixture.path()).unwrap(); |
|
| 1963 | 2166 | ||
| 1964 | 2167 | assert!(statuses.is_empty()); |
|
| 1965 | 2168 | } |
|
| 1966 | - | ||
| 1967 | 2169 | #[test] |
|
| 1968 | 2170 | fn statuses_include_native_jujutsu_bookmarks() { |
|
| 1969 | 2171 | let fixture = TestRepository::new(); |
|
| 1970 | 2172 | fs::remove_dir_all(fixture.path().join(".git")).unwrap(); |
|
| 1971 | 2173 | fixture.jj(&["git", "init", "--no-colocate", "."]); |
| 2131 | 2333 | target: "2222222222222222222222222222222222222222".parse().unwrap(), |
|
| 2132 | 2334 | accepted: Accepted::from([ |
|
| 2133 | 2335 | accepted_hunk("1111111111111111111111111111111111111111", "alpha", 0, 2), |
|
| 2134 | 2336 | accepted_hunk("1111111111111111111111111111111111111111", "beta", 0, 2), |
|
| 2135 | 2337 | ]), |
|
| 2136 | - | rejected: Rejected::from(["3333333333333333333333333333333333333333".parse().unwrap()]), |
|
| 2137 | - | dropped: Dropped::from(["4444444444444444444444444444444444444444".parse().unwrap()]), |
|
| 2338 | + | rejected: Rejected::from([( |
|
| 2339 | + | "3333333333333333333333333333333333333333".parse().unwrap(), |
|
| 2340 | + | CommitDecision { |
|
| 2341 | + | patch_id: PatchId::from("rejected".to_owned()), |
|
| 2342 | + | ordinal: 0, |
|
| 2343 | + | }, |
|
| 2344 | + | )]), |
|
| 2345 | + | dropped: Dropped::from([( |
|
| 2346 | + | "4444444444444444444444444444444444444444".parse().unwrap(), |
|
| 2347 | + | CommitDecision { |
|
| 2348 | + | patch_id: PatchId::from("dropped".to_owned()), |
|
| 2349 | + | ordinal: 0, |
|
| 2350 | + | }, |
|
| 2351 | + | )]), |
|
| 2138 | 2352 | }; |
|
| 2139 | 2353 | let message = encode_state(&state); |
|
| 2140 | 2354 | ||
| 2141 | 2355 | assert!(message.starts_with("brain state\n\nbrain-v1\ntarget ")); |
|
| 2142 | 2356 | assert_eq!(decode_state(&message), Some(state)); |
| 2229 | 2443 | assert_eq!(remaining[1].accepted, BTreeSet::from([HunkId::new(0)])); |
|
| 2230 | 2444 | assert!(remaining[1].progress.is_complete()); |
|
| 2231 | 2445 | ||
| 2232 | 2446 | let rejected = Reconciled::for_commits( |
|
| 2233 | 2447 | &Accepted::new(), |
|
| 2234 | - | &Rejected::from([first.commit]), |
|
| 2448 | + | &Rejected::from([( |
|
| 2449 | + | first.commit, |
|
| 2450 | + | CommitDecision { |
|
| 2451 | + | patch_id: PatchId::from("change".to_owned()), |
|
| 2452 | + | ordinal: 0, |
|
| 2453 | + | }, |
|
| 2454 | + | )]), |
|
| 2235 | 2455 | &Dropped::new(), |
|
| 2236 | 2456 | &commits, |
|
| 2237 | 2457 | ); |
|
| 2238 | 2458 | assert_eq!(rejected[0].state, CommitReviewState::Rejected); |
|
| 2239 | 2459 | assert_eq!(rejected[1].state, CommitReviewState::Pending); |
f
+0 -0
new.rs
+0 -0
image.png
+0 -0
new.txt
+0 -0