brain: Add "drop" action for commits
5c60c517c5d11c1dc559ba8101765d8fcc4999cc
1 parent
64369681
crates/brain/src/backend.rs
+116 -19
| 190 | 190 | ||
| 191 | 191 | /// Authoritative accepted-hunk state for one review. |
|
| 192 | 192 | pub(crate) type Accepted = BTreeSet<AcceptedHunk>; |
|
| 193 | 193 | /// Authoritative rejected-commit state for one review. |
|
| 194 | 194 | type Rejected = BTreeSet<Oid>; |
|
| 195 | + | /// Authoritative dropped-commit state for one review. |
|
| 196 | + | type Dropped = BTreeSet<Oid>; |
|
| 195 | 197 | ||
| 196 | 198 | /// Persisted state for one resolved target of a selector-keyed review. |
|
| 197 | 199 | #[derive(Clone, Debug, Eq, PartialEq)] |
|
| 198 | 200 | struct PersistedState { |
|
| 199 | 201 | /// Target commit whose hunks the accepted identities describe. |
|
| 200 | 202 | target: Oid, |
|
| 201 | 203 | /// Exact hunks accepted by the reviewer. |
|
| 202 | 204 | accepted: Accepted, |
|
| 203 | 205 | /// Commits rejected by the reviewer. |
|
| 204 | 206 | rejected: Rejected, |
|
| 207 | + | /// Commits dropped by the reviewer. |
|
| 208 | + | dropped: Dropped, |
|
| 205 | 209 | } |
|
| 206 | 210 | ||
| 207 | 211 | impl PersistedState { |
|
| 208 | 212 | /// Creates empty decisions for `target`. |
|
| 209 | 213 | fn empty(target: Oid) -> Self { |
|
| 210 | 214 | Self { |
|
| 211 | 215 | target, |
|
| 212 | 216 | accepted: Accepted::new(), |
|
| 213 | 217 | rejected: Rejected::new(), |
|
| 218 | + | dropped: Dropped::new(), |
|
| 214 | 219 | } |
|
| 215 | 220 | } |
|
| 216 | 221 | } |
|
| 217 | 222 | ||
| 218 | 223 | /// Accepted hunks of one file, ready to apply to the projected tree. |
| 277 | 282 | Pending, |
|
| 278 | 283 | /// Every reviewable change in the commit is accepted. |
|
| 279 | 284 | Accepted, |
|
| 280 | 285 | /// The reviewer rejected the commit. |
|
| 281 | 286 | Rejected, |
|
| 287 | + | /// The reviewer dropped the commit. |
|
| 288 | + | Dropped, |
|
| 282 | 289 | } |
|
| 283 | 290 | ||
| 284 | 291 | /// Review state of one commit projected from the accepted set. |
|
| 285 | 292 | pub struct Reconciled { |
|
| 286 | 293 | /// Accepted-line progress for the commit. |
| 294 | 301 | impl Reconciled { |
|
| 295 | 302 | /// Projects persisted decisions onto commit-local hunk ids. |
|
| 296 | 303 | fn for_commits( |
|
| 297 | 304 | accepted_state: &Accepted, |
|
| 298 | 305 | rejected_state: &Rejected, |
|
| 306 | + | dropped_state: &Dropped, |
|
| 299 | 307 | commits: &[CommitHunks<'_>], |
|
| 300 | 308 | ) -> Vec<Self> { |
|
| 301 | 309 | // Accepted and total line counts per commit let undecoded commits |
|
| 302 | 310 | // retain their progress from persisted totals. |
|
| 303 | 311 | let mut totals = BTreeMap::<Oid, (usize, u32)>::new(); |
| 342 | 350 | }, |
|
| 343 | 351 | ); |
|
| 344 | 352 | (progress, BTreeSet::new()) |
|
| 345 | 353 | } |
|
| 346 | 354 | }; |
|
| 347 | - | let state = if rejected_state.contains(&commit.commit) { |
|
| 355 | + | let state = if dropped_state.contains(&commit.commit) { |
|
| 356 | + | CommitReviewState::Dropped |
|
| 357 | + | } else if rejected_state.contains(&commit.commit) { |
|
| 348 | 358 | CommitReviewState::Rejected |
|
| 349 | 359 | } else if progress.is_complete() { |
|
| 350 | 360 | CommitReviewState::Accepted |
|
| 351 | 361 | } else { |
|
| 352 | 362 | CommitReviewState::Pending |
| 535 | 545 | state.target |
|
| 536 | 546 | ); |
|
| 537 | 547 | for commit in &state.rejected { |
|
| 538 | 548 | message.push_str(&format!("reject {commit}\n")); |
|
| 539 | 549 | } |
|
| 550 | + | for commit in &state.dropped { |
|
| 551 | + | message.push_str(&format!("drop {commit}\n")); |
|
| 552 | + | } |
|
| 540 | 553 | for hunk in &state.accepted { |
|
| 541 | 554 | message.push_str(&format!( |
|
| 542 | 555 | "{} {} {} {} {}\n", |
|
| 543 | 556 | hunk.commit, hunk.patch_id, hunk.ordinal, hunk.lines, hunk.commit_lines |
|
| 544 | 557 | )); |
| 558 | 571 | if metadata.next().is_some() { |
|
| 559 | 572 | return None; |
|
| 560 | 573 | } |
|
| 561 | 574 | let mut accepted = BTreeSet::new(); |
|
| 562 | 575 | let mut rejected = BTreeSet::new(); |
|
| 576 | + | let mut dropped = BTreeSet::new(); |
|
| 563 | 577 | let mut commit_totals = BTreeMap::new(); |
|
| 564 | 578 | ||
| 565 | 579 | for line in body.filter(|line| !line.trim().is_empty()) { |
|
| 566 | 580 | if let Some(commit) = line.strip_prefix("reject ") { |
|
| 567 | 581 | let commit = commit.parse().ok()?; |
|
| 568 | - | if !rejected.insert(commit) { |
|
| 582 | + | if !rejected.insert(commit) || dropped.contains(&commit) { |
|
| 583 | + | return None; |
|
| 584 | + | } |
|
| 585 | + | continue; |
|
| 586 | + | } |
|
| 587 | + | if let Some(commit) = line.strip_prefix("drop ") { |
|
| 588 | + | let commit = commit.parse().ok()?; |
|
| 589 | + | if !dropped.insert(commit) || rejected.contains(&commit) { |
|
| 569 | 590 | return None; |
|
| 570 | 591 | } |
|
| 571 | 592 | continue; |
|
| 572 | 593 | } |
|
| 573 | 594 | let mut fields = line.split_whitespace(); |
| 606 | 627 | } |
|
| 607 | 628 | Some(PersistedState { |
|
| 608 | 629 | target, |
|
| 609 | 630 | accepted, |
|
| 610 | 631 | rejected, |
|
| 632 | + | dropped, |
|
| 611 | 633 | }) |
|
| 612 | 634 | } |
|
| 613 | 635 | ||
| 614 | 636 | /// Commit hunk identity data used to summarize stored review progress. |
|
| 615 | 637 | struct SummaryCommit { |
| 753 | 775 | commit: Oid, |
|
| 754 | 776 | /// Exact hunks accepted by the reviewer. |
|
| 755 | 777 | pub(crate) accepted: Accepted, |
|
| 756 | 778 | /// Commits rejected by the reviewer. |
|
| 757 | 779 | rejected: Rejected, |
|
| 780 | + | /// Commits dropped by the reviewer. |
|
| 781 | + | dropped: Dropped, |
|
| 758 | 782 | } |
|
| 759 | 783 | ||
| 760 | 784 | impl Brain { |
|
| 761 | 785 | /// Creates a review state handle. |
|
| 762 | 786 | fn new( |
| 769 | 793 | ) -> Self { |
|
| 770 | 794 | let PersistedState { |
|
| 771 | 795 | target, |
|
| 772 | 796 | accepted, |
|
| 773 | 797 | rejected, |
|
| 798 | + | dropped, |
|
| 774 | 799 | } = state; |
|
| 775 | 800 | Self { |
|
| 776 | 801 | repository, |
|
| 777 | 802 | state_ref, |
|
| 778 | 803 | comments_ref, |
|
| 779 | 804 | base, |
|
| 780 | 805 | target, |
|
| 781 | 806 | commit, |
|
| 782 | 807 | accepted, |
|
| 783 | 808 | rejected, |
|
| 809 | + | dropped, |
|
| 784 | 810 | } |
|
| 785 | 811 | } |
|
| 786 | 812 | ||
| 787 | 813 | /// Returns the hidden Git ref prefix for `namespace`. |
|
| 788 | 814 | fn prefix_for(namespace: &'static str) -> Result<Qualified<'static>, Error> { |
| 1195 | 1221 | comments_ref, |
|
| 1196 | 1222 | base, |
|
| 1197 | 1223 | commit, |
|
| 1198 | 1224 | PersistedState::empty(target), |
|
| 1199 | 1225 | ); |
|
| 1200 | - | brain.remap(commits, documents, &stored.accepted, &stored.rejected)?; |
|
| 1226 | + | brain.remap( |
|
| 1227 | + | commits, |
|
| 1228 | + | documents, |
|
| 1229 | + | &stored.accepted, |
|
| 1230 | + | &stored.rejected, |
|
| 1231 | + | &stored.dropped, |
|
| 1232 | + | )?; |
|
| 1201 | 1233 | ||
| 1202 | 1234 | Ok(brain) |
|
| 1203 | 1235 | } |
|
| 1204 | 1236 | ||
| 1205 | 1237 | /// Creates an empty accepted-state commit for one resolved review range. |
| 1269 | 1301 | accepted.extend(hunks); |
|
| 1270 | 1302 | let mut tree = self.repository.tree_id(self.base)?; |
|
| 1271 | 1303 | for file in files { |
|
| 1272 | 1304 | tree = self.project_file(tree, &file)?; |
|
| 1273 | 1305 | } |
|
| 1274 | - | self.persist(tree, accepted, self.rejected.clone()) |
|
| 1306 | + | self.persist(tree, accepted, self.rejected.clone(), self.dropped.clone()) |
|
| 1275 | 1307 | } |
|
| 1276 | 1308 | ||
| 1277 | 1309 | /// Marks exact hunks unaccepted and rebuilds the derived tree. |
|
| 1278 | 1310 | pub fn reject( |
|
| 1279 | 1311 | &mut self, |
| 1284 | 1316 | accepted.retain(|hunk| !rejected.contains(hunk)); |
|
| 1285 | 1317 | let mut tree = self.repository.tree_id(self.base)?; |
|
| 1286 | 1318 | for file in files { |
|
| 1287 | 1319 | tree = self.project_file(tree, &file)?; |
|
| 1288 | 1320 | } |
|
| 1289 | - | self.persist(tree, accepted, self.rejected.clone()) |
|
| 1321 | + | self.persist(tree, accepted, self.rejected.clone(), self.dropped.clone()) |
|
| 1290 | 1322 | } |
|
| 1291 | 1323 | ||
| 1292 | 1324 | /// Toggles the rejected state of one commit. |
|
| 1293 | 1325 | pub fn toggle_commit_rejected(&mut self, commit: Oid) -> Result<(), Error> { |
|
| 1294 | 1326 | let mut rejected = self.rejected.clone(); |
|
| 1327 | + | let mut dropped = self.dropped.clone(); |
|
| 1295 | 1328 | if !rejected.remove(&commit) { |
|
| 1296 | 1329 | rejected.insert(commit); |
|
| 1330 | + | dropped.remove(&commit); |
|
| 1297 | 1331 | } |
|
| 1298 | 1332 | let tree = self.repository.tree_id(self.commit)?; |
|
| 1299 | - | self.persist(tree, self.accepted.clone(), rejected) |
|
| 1333 | + | self.persist(tree, self.accepted.clone(), rejected, dropped) |
|
| 1334 | + | } |
|
| 1335 | + | ||
| 1336 | + | /// Toggles the dropped state of one commit. |
|
| 1337 | + | pub fn toggle_commit_dropped(&mut self, commit: Oid) -> Result<(), Error> { |
|
| 1338 | + | let mut rejected = self.rejected.clone(); |
|
| 1339 | + | let mut dropped = self.dropped.clone(); |
|
| 1340 | + | if !dropped.remove(&commit) { |
|
| 1341 | + | dropped.insert(commit); |
|
| 1342 | + | rejected.remove(&commit); |
|
| 1343 | + | } |
|
| 1344 | + | let tree = self.repository.tree_id(self.commit)?; |
|
| 1345 | + | self.persist(tree, self.accepted.clone(), rejected, dropped) |
|
| 1300 | 1346 | } |
|
| 1301 | 1347 | ||
| 1302 | 1348 | /// Applies `patch` to `tree`, keeping `tree` when it does not apply. |
|
| 1303 | 1349 | /// |
|
| 1304 | 1350 | /// The tree is only a content projection of the accepted set, so a hunk |
| 1335 | 1381 | &mut self, |
|
| 1336 | 1382 | commits: &[Commit], |
|
| 1337 | 1383 | documents: &[DiffDocument], |
|
| 1338 | 1384 | stored: &Accepted, |
|
| 1339 | 1385 | stored_rejected: &Rejected, |
|
| 1386 | + | stored_dropped: &Dropped, |
|
| 1340 | 1387 | ) -> Result<(), Error> { |
|
| 1341 | 1388 | debug_assert_eq!(commits.len(), documents.len(), "one document per commit"); |
|
| 1342 | 1389 | ||
| 1343 | 1390 | // Hunks of the moved range in range order, grouped by file so the |
|
| 1344 | 1391 | // projection can apply a whole file at once. |
| 1378 | 1425 | .collect::<BTreeSet<_>>(); |
|
| 1379 | 1426 | let rejected = stored_rejected |
|
| 1380 | 1427 | .intersection(¤t_commits) |
|
| 1381 | 1428 | .copied() |
|
| 1382 | 1429 | .collect(); |
|
| 1383 | - | self.persist(tree, accepted, rejected) |
|
| 1430 | + | let dropped = stored_dropped |
|
| 1431 | + | .intersection(¤t_commits) |
|
| 1432 | + | .copied() |
|
| 1433 | + | .collect(); |
|
| 1434 | + | self.persist(tree, accepted, rejected, dropped) |
|
| 1384 | 1435 | } |
|
| 1385 | 1436 | ||
| 1386 | 1437 | /// Persists review decisions and the derived tree in one commit. |
|
| 1387 | - | fn persist(&mut self, tree: Oid, accepted: Accepted, rejected: Rejected) -> Result<(), Error> { |
|
| 1438 | + | fn persist( |
|
| 1439 | + | &mut self, |
|
| 1440 | + | tree: Oid, |
|
| 1441 | + | accepted: Accepted, |
|
| 1442 | + | rejected: Rejected, |
|
| 1443 | + | dropped: Dropped, |
|
| 1444 | + | ) -> Result<(), Error> { |
|
| 1388 | 1445 | let state = PersistedState { |
|
| 1389 | 1446 | target: self.target, |
|
| 1390 | 1447 | accepted, |
|
| 1391 | 1448 | rejected, |
|
| 1449 | + | dropped, |
|
| 1392 | 1450 | }; |
|
| 1393 | 1451 | let message = encode_state(&state); |
|
| 1394 | 1452 | let commit = |
|
| 1395 | 1453 | self.repository |
|
| 1396 | 1454 | .commit(tree, self.base, "brain", "brain@example.invalid", &message)?; |
|
| 1397 | 1455 | self.repository.update_ref(&self.state_ref, commit)?; |
|
| 1398 | 1456 | self.commit = commit; |
|
| 1399 | 1457 | self.accepted = state.accepted; |
|
| 1400 | 1458 | self.rejected = state.rejected; |
|
| 1459 | + | self.dropped = state.dropped; |
|
| 1401 | 1460 | ||
| 1402 | 1461 | Ok(()) |
|
| 1403 | 1462 | } |
|
| 1404 | 1463 | ||
| 1405 | 1464 | /// Projects exact acceptance onto the current commit list. |
|
| 1406 | 1465 | pub fn reconcile(&self, commits: &[CommitHunks<'_>]) -> Vec<Reconciled> { |
|
| 1407 | - | Reconciled::for_commits(&self.accepted, &self.rejected, commits) |
|
| 1466 | + | Reconciled::for_commits(&self.accepted, &self.rejected, &self.dropped, commits) |
|
| 1408 | 1467 | } |
|
| 1409 | 1468 | } |
|
| 1410 | 1469 | ||
| 1411 | 1470 | /// Returns statistics for the unaccepted hunks in `document`. |
|
| 1412 | 1471 | pub fn commit_remaining_stats(document: &DiffDocument, accepted: &BTreeSet<HunkId>) -> DiffStat { |
| 1779 | 1838 | Some(comments) |
|
| 1780 | 1839 | ); |
|
| 1781 | 1840 | } |
|
| 1782 | 1841 | ||
| 1783 | 1842 | #[test] |
|
| 1784 | - | fn commit_rejection_persists_and_toggles() { |
|
| 1843 | + | fn commit_decisions_persist_toggle_and_are_exclusive() { |
|
| 1785 | 1844 | let repository = TestRepository::new(); |
|
| 1786 | 1845 | let reviewed = repository.path.join("reviewed.txt"); |
|
| 1787 | 1846 | fs::write(&reviewed, "base\n").unwrap(); |
|
| 1788 | 1847 | repository.git(&["add", "reviewed.txt"]); |
|
| 1789 | 1848 | repository.git(&["commit", "-q", "-m", "base"]); |
| 1800 | 1859 | brain.toggle_commit_rejected(commit).unwrap(); |
|
| 1801 | 1860 | assert_eq!(backend.brain().unwrap().rejected, Rejected::from([commit])); |
|
| 1802 | 1861 | ||
| 1803 | 1862 | brain.toggle_commit_rejected(commit).unwrap(); |
|
| 1804 | 1863 | assert!(backend.brain().unwrap().rejected.is_empty()); |
|
| 1864 | + | ||
| 1865 | + | brain.toggle_commit_dropped(commit).unwrap(); |
|
| 1866 | + | let stored = backend.brain().unwrap(); |
|
| 1867 | + | assert_eq!(stored.dropped, Dropped::from([commit])); |
|
| 1868 | + | assert!(stored.rejected.is_empty()); |
|
| 1869 | + | ||
| 1870 | + | brain.toggle_commit_rejected(commit).unwrap(); |
|
| 1871 | + | let stored = backend.brain().unwrap(); |
|
| 1872 | + | assert_eq!(stored.rejected, Rejected::from([commit])); |
|
| 1873 | + | assert!(stored.dropped.is_empty()); |
|
| 1874 | + | ||
| 1875 | + | brain.toggle_commit_dropped(commit).unwrap(); |
|
| 1876 | + | brain.toggle_commit_dropped(commit).unwrap(); |
|
| 1877 | + | assert!(backend.brain().unwrap().dropped.is_empty()); |
|
| 1805 | 1878 | } |
|
| 1806 | 1879 | ||
| 1807 | 1880 | #[test] |
|
| 1808 | 1881 | fn rebase_keeps_unchanged_hunk_acceptance_after_amend() { |
|
| 1809 | 1882 | let repository = TestRepository::new(); |
| 2040 | 2113 | accepted: Accepted::from([ |
|
| 2041 | 2114 | accepted_hunk("1111111111111111111111111111111111111111", "alpha", 0, 2), |
|
| 2042 | 2115 | accepted_hunk("1111111111111111111111111111111111111111", "beta", 0, 2), |
|
| 2043 | 2116 | ]), |
|
| 2044 | 2117 | rejected: Rejected::from(["3333333333333333333333333333333333333333".parse().unwrap()]), |
|
| 2118 | + | dropped: Dropped::from(["4444444444444444444444444444444444444444".parse().unwrap()]), |
|
| 2045 | 2119 | }; |
|
| 2046 | 2120 | let message = encode_state(&state); |
|
| 2047 | 2121 | ||
| 2048 | 2122 | assert!(message.starts_with("brain state\n\nbrain-v1\ntarget ")); |
|
| 2049 | 2123 | assert_eq!(decode_state(&message), Some(state)); |
| 2054 | 2128 | fn empty_accepted_state_roundtrips_through_commit_message() { |
|
| 2055 | 2129 | let state = PersistedState { |
|
| 2056 | 2130 | target: "2222222222222222222222222222222222222222".parse().unwrap(), |
|
| 2057 | 2131 | accepted: Accepted::new(), |
|
| 2058 | 2132 | rejected: Rejected::new(), |
|
| 2133 | + | dropped: Dropped::new(), |
|
| 2059 | 2134 | }; |
|
| 2060 | 2135 | let message = encode_state(&state); |
|
| 2061 | 2136 | ||
| 2062 | 2137 | assert_eq!(decode_state(&message), Some(state.clone())); |
|
| 2063 | 2138 | // Git terminates `%B` output with its own newline. |
| 2110 | 2185 | commit: second.commit, |
|
| 2111 | 2186 | hunks: Some(&second_hunks), |
|
| 2112 | 2187 | }, |
|
| 2113 | 2188 | ]; |
|
| 2114 | 2189 | ||
| 2115 | - | let fresh = Reconciled::for_commits(&Accepted::new(), &Rejected::new(), &commits); |
|
| 2190 | + | let fresh = Reconciled::for_commits( |
|
| 2191 | + | &Accepted::new(), |
|
| 2192 | + | &Rejected::new(), |
|
| 2193 | + | &Dropped::new(), |
|
| 2194 | + | &commits, |
|
| 2195 | + | ); |
|
| 2116 | 2196 | assert!(fresh.iter().all(|state| state.accepted.is_empty())); |
|
| 2117 | 2197 | assert!(fresh.iter().all(|state| state.progress |
|
| 2118 | 2198 | == ReviewProgress { |
|
| 2119 | 2199 | accepted: 0, |
|
| 2120 | 2200 | total: 1, |
|
| 2121 | 2201 | })); |
|
| 2122 | 2202 | ||
| 2123 | - | let remaining = |
|
| 2124 | - | Reconciled::for_commits(&Accepted::from([second]), &Rejected::new(), &commits); |
|
| 2203 | + | let remaining = Reconciled::for_commits( |
|
| 2204 | + | &Accepted::from([second]), |
|
| 2205 | + | &Rejected::new(), |
|
| 2206 | + | &Dropped::new(), |
|
| 2207 | + | &commits, |
|
| 2208 | + | ); |
|
| 2125 | 2209 | assert!(remaining[0].accepted.is_empty()); |
|
| 2126 | 2210 | assert_eq!(remaining[1].accepted, BTreeSet::from([HunkId::new(0)])); |
|
| 2127 | 2211 | assert!(remaining[1].progress.is_complete()); |
|
| 2128 | 2212 | ||
| 2129 | - | let rejected = |
|
| 2130 | - | Reconciled::for_commits(&Accepted::new(), &Rejected::from([first.commit]), &commits); |
|
| 2213 | + | let rejected = Reconciled::for_commits( |
|
| 2214 | + | &Accepted::new(), |
|
| 2215 | + | &Rejected::from([first.commit]), |
|
| 2216 | + | &Dropped::new(), |
|
| 2217 | + | &commits, |
|
| 2218 | + | ); |
|
| 2131 | 2219 | assert_eq!(rejected[0].state, CommitReviewState::Rejected); |
|
| 2132 | 2220 | assert_eq!(rejected[1].state, CommitReviewState::Pending); |
|
| 2133 | 2221 | } |
|
| 2134 | 2222 | ||
| 2135 | 2223 | #[test] |
| 2146 | 2234 | let commits = [CommitHunks { |
|
| 2147 | 2235 | commit: first.commit, |
|
| 2148 | 2236 | hunks: None, |
|
| 2149 | 2237 | }]; |
|
| 2150 | 2238 | ||
| 2151 | - | let partial = |
|
| 2152 | - | Reconciled::for_commits(&Accepted::from([first.clone()]), &Rejected::new(), &commits); |
|
| 2239 | + | let partial = Reconciled::for_commits( |
|
| 2240 | + | &Accepted::from([first.clone()]), |
|
| 2241 | + | &Rejected::new(), |
|
| 2242 | + | &Dropped::new(), |
|
| 2243 | + | &commits, |
|
| 2244 | + | ); |
|
| 2153 | 2245 | assert_eq!( |
|
| 2154 | 2246 | partial[0].progress, |
|
| 2155 | 2247 | ReviewProgress { |
|
| 2156 | 2248 | accepted: 3, |
|
| 2157 | 2249 | total: 4, |
|
| 2158 | 2250 | } |
|
| 2159 | 2251 | ); |
|
| 2160 | 2252 | assert!(partial[0].accepted.is_empty()); |
|
| 2161 | 2253 | ||
| 2162 | - | let full = |
|
| 2163 | - | Reconciled::for_commits(&Accepted::from([first, second]), &Rejected::new(), &commits); |
|
| 2254 | + | let full = Reconciled::for_commits( |
|
| 2255 | + | &Accepted::from([first, second]), |
|
| 2256 | + | &Rejected::new(), |
|
| 2257 | + | &Dropped::new(), |
|
| 2258 | + | &commits, |
|
| 2259 | + | ); |
|
| 2164 | 2260 | assert!(full[0].progress.is_complete()); |
|
| 2165 | 2261 | assert!(full[0].accepted.is_empty()); |
|
| 2166 | 2262 | } |
|
| 2167 | 2263 | ||
| 2168 | 2264 | #[test] |
| 2170 | 2266 | let commit = "1111111111111111111111111111111111111111".parse().unwrap(); |
|
| 2171 | 2267 | let identities = BTreeMap::new(); |
|
| 2172 | 2268 | let reconciled = Reconciled::for_commits( |
|
| 2173 | 2269 | &Accepted::new(), |
|
| 2174 | 2270 | &Rejected::new(), |
|
| 2271 | + | &Dropped::new(), |
|
| 2175 | 2272 | &[CommitHunks { |
|
| 2176 | 2273 | commit, |
|
| 2177 | 2274 | hunks: Some(&identities), |
|
| 2178 | 2275 | }], |
|
| 2179 | 2276 | ); |
f
+0 -0
new.rs
+0 -0
image.png
+0 -0
new.txt
+0 -0
crates/brain/src/session.rs
+25 -0
| 469 | 469 | ) { |
|
| 470 | 470 | self.toggle_commit_rejected(self.current)?; |
|
| 471 | 471 | } |
|
| 472 | 472 | Ok(Control::Redraw) |
|
| 473 | 473 | } |
|
| 474 | + | Event::Key(Key::Char('d')) => { |
|
| 475 | + | if matches!( |
|
| 476 | + | self.current()?.viewer.state().focus, |
|
| 477 | + | Some(DiffFocus::Commit) |
|
| 478 | + | ) { |
|
| 479 | + | self.toggle_commit_dropped(self.current)?; |
|
| 480 | + | } |
|
| 481 | + | Ok(Control::Redraw) |
|
| 482 | + | } |
|
| 474 | 483 | Event::Key(Key::Char('v')) => { |
|
| 475 | 484 | self.hunk_visibility.toggle(); |
|
| 476 | 485 | self.refresh_visibility()?; |
|
| 477 | 486 | Ok(Control::Redraw) |
|
| 478 | 487 | } |
| 555 | 564 | }) => -1, |
|
| 556 | 565 | Event::Key(Key::Char('r')) => { |
|
| 557 | 566 | self.toggle_commit_rejected(self.list.selected())?; |
|
| 558 | 567 | return Ok(Control::Redraw); |
|
| 559 | 568 | } |
|
| 569 | + | Event::Key(Key::Char('d')) => { |
|
| 570 | + | self.toggle_commit_dropped(self.list.selected())?; |
|
| 571 | + | return Ok(Control::Redraw); |
|
| 572 | + | } |
|
| 560 | 573 | Event::Key(Key::Enter) => { |
|
| 561 | 574 | self.select_commit(self.list.selected())?; |
|
| 562 | 575 | return Ok(Control::Redraw); |
|
| 563 | 576 | } |
|
| 564 | 577 | Event::Key(Key::Tab) => { |
| 585 | 598 | self.brain.toggle_commit_rejected(commit)?; |
|
| 586 | 599 | self.reconcile_state(); |
|
| 587 | 600 | Ok(()) |
|
| 588 | 601 | } |
|
| 589 | 602 | ||
| 603 | + | /// Toggles the dropped state of one commit after pending persistence settles. |
|
| 604 | + | fn toggle_commit_dropped(&mut self, index: usize) -> Result<(), Error> { |
|
| 605 | + | self.sync_acceptance()?; |
|
| 606 | + | if self.error.is_some() { |
|
| 607 | + | return Ok(()); |
|
| 608 | + | } |
|
| 609 | + | let commit = self.commits[index].commit.id; |
|
| 610 | + | self.brain.toggle_commit_dropped(commit)?; |
|
| 611 | + | self.reconcile_state(); |
|
| 612 | + | Ok(()) |
|
| 613 | + | } |
|
| 614 | + | ||
| 590 | 615 | /// Toggles acceptance of the focused hunk. |
|
| 591 | 616 | fn toggle_focused_hunk(&mut self, area: Rect) -> Result<(), Error> { |
|
| 592 | 617 | let current = self.current; |
|
| 593 | 618 | let before = self.current()?.viewer.state().clone(); |
|
| 594 | 619 | let loaded = self.current()?; |
a.rs
+0 -0
crates/brain/src/session/status.rs
+36 -9
| 51 | 51 | pub(super) const SELECTED_COMMIT_BG: Color = Color::Fixed(235); |
|
| 52 | 52 | /// Marker shown beside a commit whose hunks have all been accepted: ✔. |
|
| 53 | 53 | const REVIEWED_MARK: char = '✔'; |
|
| 54 | 54 | /// Marker shown beside a rejected commit: ✗. |
|
| 55 | 55 | const REJECTED_MARK: char = '✗'; |
|
| 56 | + | /// Marker shown beside a dropped commit: -. |
|
| 57 | + | const DROPPED_MARK: char = '-'; |
|
| 56 | 58 | /// Marker highlighted when all hunks are visible in the footer: ✔. |
|
| 57 | 59 | const ALL_HUNKS_MODE_ICON: &str = "✔"; |
|
| 58 | 60 | /// Marker shown when full-file mode is visible in the footer: ↕. |
|
| 59 | 61 | const FULL_FILE_MODE_ICON: &str = "↕"; |
|
| 60 | 62 | /// Separator shown between footer status segments: ·. |
| 79 | 81 | const COMMITS_ACTIONS: &[(&str, &str)] = &[ |
|
| 80 | 82 | (TAB_KEY, "commit"), |
|
| 81 | 83 | ("j/k", "select"), |
|
| 82 | 84 | (RETURN_KEY, "open"), |
|
| 83 | 85 | ("r", "reject commit"), |
|
| 86 | + | ("d", "drop commit"), |
|
| 84 | 87 | ("q", "quit"), |
|
| 85 | 88 | ]; |
|
| 86 | 89 | /// Action hints shown in commit diff mode. |
|
| 87 | 90 | const COMMIT_ACTIONS: &[(&str, &str)] = &[ |
|
| 88 | 91 | (TAB_KEY, "history"), |
|
| 89 | 92 | ("j/k", "hunk"), |
|
| 90 | 93 | ("n/p", "file"), |
|
| 91 | 94 | ("a", "accept hunk"), |
|
| 92 | 95 | ("r", "reject commit"), |
|
| 96 | + | ("d", "drop commit"), |
|
| 93 | 97 | ("u", "undo"), |
|
| 94 | 98 | ("c", "comment"), |
|
| 95 | 99 | ]; |
|
| 96 | 100 | /// Action hints shown while browsing hunk comments. |
|
| 97 | 101 | const HUNK_BROWSE_ACTIONS: &[(&str, &str)] = &[ |
| 339 | 343 | Chrome::areas(area).0 |
|
| 340 | 344 | } |
|
| 341 | 345 | ||
| 342 | 346 | /// Returns the leading columns for one commit-table row. |
|
| 343 | 347 | pub(super) fn commit_row(slot: &CommitSlot, graph: &str) -> [Line; 3] { |
|
| 348 | + | let dropped = matches!(slot.state, CommitReviewState::Dropped); |
|
| 349 | + | let row_style = if dropped { |
|
| 350 | + | Style::default().dim() |
|
| 351 | + | } else { |
|
| 352 | + | Style::default() |
|
| 353 | + | }; |
|
| 344 | 354 | [ |
|
| 345 | - | Line::new(Label::new(graph).fg(colors::fixed::MUTED)), |
|
| 346 | - | Line::new(Label::new(&slot.commit.id.short()).fg(colors::rgb::ORANGE)), |
|
| 347 | - | Line::new(slot.commit.summary.as_str()), |
|
| 355 | + | Line::new(Label::new(graph).fg(colors::fixed::MUTED)).style(row_style), |
|
| 356 | + | Line::new(Label::new(&slot.commit.id.short()).fg(colors::rgb::ORANGE)).style(row_style), |
|
| 357 | + | Line::new(slot.commit.summary.as_str()).style(if dropped { |
|
| 358 | + | Style::default().dim().strikethrough() |
|
| 359 | + | } else { |
|
| 360 | + | Style::default() |
|
| 361 | + | }), |
|
| 348 | 362 | ] |
|
| 349 | 363 | } |
|
| 350 | 364 | ||
| 351 | 365 | /// Returns the optional right-side columns for one commit-table row. |
|
| 352 | 366 | /// |
| 362 | 376 | let stats = slot.remaining_stats(); |
|
| 363 | 377 | let diffstat = match slot.state { |
|
| 364 | 378 | CommitReviewState::Pending => { |
|
| 365 | 379 | history_diffstat(stats, largest_change_count, max_graph_width, palette) |
|
| 366 | 380 | } |
|
| 367 | - | CommitReviewState::Accepted | CommitReviewState::Rejected => Line::blank(), |
|
| 381 | + | CommitReviewState::Accepted | CommitReviewState::Rejected | CommitReviewState::Dropped => { |
|
| 382 | + | Line::blank() |
|
| 383 | + | } |
|
| 368 | 384 | }; |
|
| 369 | 385 | let (status, progress) = match slot.state { |
|
| 370 | 386 | CommitReviewState::Accepted => ( |
|
| 371 | 387 | Label::from_char(REVIEWED_MARK).fg(palette.positive_fg), |
|
| 372 | 388 | progress_bar(slot.progress, palette, FOOTER_FG), |
|
| 373 | 389 | ), |
|
| 374 | 390 | CommitReviewState::Rejected => ( |
|
| 375 | 391 | Label::from_char(REJECTED_MARK).fg(palette.negative_fg), |
|
| 376 | 392 | full_progress_bar(palette.negative_fg), |
|
| 377 | 393 | ), |
|
| 394 | + | CommitReviewState::Dropped => ( |
|
| 395 | + | Label::from_char(DROPPED_MARK).fg(colors::fixed::DIM), |
|
| 396 | + | full_progress_bar(colors::fixed::DIM), |
|
| 397 | + | ), |
|
| 378 | 398 | CommitReviewState::Pending => ( |
|
| 379 | 399 | Label::space(), |
|
| 380 | 400 | progress_bar(slot.progress, palette, FOOTER_FG), |
|
| 381 | 401 | ), |
|
| 382 | 402 | }; |
|
| 383 | 403 | let progress = Line::new(status).item(Label::space()).extend(progress); |
|
| 384 | 404 | ||
| 405 | + | let row_style = if matches!(slot.state, CommitReviewState::Dropped) { |
|
| 406 | + | Style::default().dim() |
|
| 407 | + | } else { |
|
| 408 | + | Style::default() |
|
| 409 | + | }; |
|
| 385 | 410 | [ |
|
| 386 | - | Line::new(Label::new(slot.commit.author.as_str()).fg(HISTORY_VALUE_FG)), |
|
| 411 | + | Line::new(Label::new(slot.commit.author.as_str()).fg(HISTORY_VALUE_FG)).style(row_style), |
|
| 387 | 412 | Line::new( |
|
| 388 | 413 | Label::from(relative_time( |
|
| 389 | 414 | slot.commit.recency.seconds.into(), |
|
| 390 | 415 | now.into(), |
|
| 391 | 416 | )) |
|
| 392 | 417 | .fg(colors::fixed::MUTED), |
|
| 393 | - | ), |
|
| 394 | - | Line::new(Label::new(&slot.commit.branches.join(", ")).fg(colors::rgb::TEAL)), |
|
| 395 | - | progress, |
|
| 396 | - | diffstat, |
|
| 418 | + | ) |
|
| 419 | + | .style(row_style), |
|
| 420 | + | Line::new(Label::new(&slot.commit.branches.join(", ")).fg(colors::rgb::TEAL)) |
|
| 421 | + | .style(row_style), |
|
| 422 | + | progress.style(row_style), |
|
| 423 | + | diffstat.style(row_style), |
|
| 397 | 424 | ] |
|
| 398 | 425 | } |
|
| 399 | 426 | ||
| 400 | 427 | /// Returns a change count and proportional addition and deletion graph. |
|
| 401 | 428 | fn history_diffstat( |