|
| 1 | +//! These tests exercise the replace operation. |
| 2 | +use anyhow::{Context, Result, bail}; |
| 3 | +use but_graph::Graph; |
| 4 | +use but_rebase::graph_rebase::{GraphExt, Step, rebase::RebaseOutcome}; |
| 5 | +use but_testsupport::{git_status, visualize_commit_graph_all, visualize_tree}; |
| 6 | + |
| 7 | +use crate::{ |
| 8 | + graph_rebase::set_var, |
| 9 | + utils::{fixture_writable, standard_options}, |
| 10 | +}; |
| 11 | + |
| 12 | +#[test] |
| 13 | +fn reword_a_commit() -> Result<()> { |
| 14 | + set_var("GITBUTLER_CHANGE_ID", "1"); |
| 15 | + let (repo, _tmpdir, meta) = fixture_writable("merge-in-the-middle")?; |
| 16 | + |
| 17 | + insta::assert_snapshot!(visualize_commit_graph_all(&repo)?, @r" |
| 18 | + * e8ee978 (HEAD -> with-inner-merge) on top of inner merge |
| 19 | + * 2fc288c Merge branch 'B' into with-inner-merge |
| 20 | + |\ |
| 21 | + | * 984fd1c (B) C: new file with 10 lines |
| 22 | + * | add59d2 (A) A: 10 lines on top |
| 23 | + |/ |
| 24 | + * 8f0d338 (tag: base, main) base |
| 25 | + "); |
| 26 | + insta::assert_snapshot!(git_status(&repo)?, @r" |
| 27 | + "); |
| 28 | + |
| 29 | + let head_tree = repo.head_tree()?.id; |
| 30 | + |
| 31 | + let graph = Graph::from_head(&repo, &*meta, standard_options())?.validated()?; |
| 32 | + |
| 33 | + let mut editor = graph.to_editor()?; |
| 34 | + |
| 35 | + // get the origional a |
| 36 | + let a = repo.rev_parse_single("A")?.detach(); |
| 37 | + |
| 38 | + // reword commit a |
| 39 | + let a_obj = repo.find_commit(a)?; |
| 40 | + let mut a_obj = a_obj.decode()?; |
| 41 | + a_obj.message = "A: a second coming".into(); |
| 42 | + let a_new = repo.write_object(a_obj)?.detach(); |
| 43 | + |
| 44 | + // select the origional a out of the graph |
| 45 | + let a_selector = editor |
| 46 | + .select_commit(a) |
| 47 | + .context("Failed to find commit a in editor graph")?; |
| 48 | + // replace it with the new one |
| 49 | + editor.replace(&a_selector, Step::Pick { id: a_new }); |
| 50 | + |
| 51 | + let outcome = editor.rebase(&repo)?; |
| 52 | + let RebaseOutcome::Success(outcome) = outcome else { |
| 53 | + bail!("Rebase failed"); |
| 54 | + }; |
| 55 | + outcome.materialize(&repo)?; |
| 56 | + |
| 57 | + assert_eq!(head_tree, repo.head_tree()?.id); |
| 58 | + |
| 59 | + insta::assert_snapshot!(visualize_commit_graph_all(&repo)?, @r" |
| 60 | + * 4894b95 (HEAD -> with-inner-merge) on top of inner merge |
| 61 | + * af38519 Merge branch 'B' into with-inner-merge |
| 62 | + |\ |
| 63 | + | * 984fd1c (B) C: new file with 10 lines |
| 64 | + * | 6de6b92 (A) A: a second coming |
| 65 | + |/ |
| 66 | + * 8f0d338 (tag: base, main) base |
| 67 | + "); |
| 68 | + insta::assert_snapshot!(git_status(&repo)?, @r" |
| 69 | + "); |
| 70 | + |
| 71 | + Ok(()) |
| 72 | +} |
| 73 | + |
| 74 | +#[test] |
| 75 | +fn ammend_a_commit() -> Result<()> { |
| 76 | + set_var("GITBUTLER_CHANGE_ID", "1"); |
| 77 | + let (repo, _tmpdir, meta) = fixture_writable("merge-in-the-middle")?; |
| 78 | + |
| 79 | + insta::assert_snapshot!(visualize_commit_graph_all(&repo)?, @r" |
| 80 | + * e8ee978 (HEAD -> with-inner-merge) on top of inner merge |
| 81 | + * 2fc288c Merge branch 'B' into with-inner-merge |
| 82 | + |\ |
| 83 | + | * 984fd1c (B) C: new file with 10 lines |
| 84 | + * | add59d2 (A) A: 10 lines on top |
| 85 | + |/ |
| 86 | + * 8f0d338 (tag: base, main) base |
| 87 | + "); |
| 88 | + insta::assert_snapshot!(git_status(&repo)?, @r" |
| 89 | + "); |
| 90 | + |
| 91 | + let head_tree = repo.head_tree()?.id(); |
| 92 | + insta::assert_snapshot!(visualize_tree(head_tree), @r#" |
| 93 | + f766d1f |
| 94 | + ├── added-after-with-inner-merge:100644:861be1b "seq 10\n" |
| 95 | + ├── file:100644:d78dd4f "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n50\n51\n52\n53\n54\n55\n56\n57\n58\n59\n60\n" |
| 96 | + └── new-file:100644:f00c965 "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" |
| 97 | + "#); |
| 98 | + |
| 99 | + let graph = Graph::from_head(&repo, &*meta, standard_options())?.validated()?; |
| 100 | + |
| 101 | + let mut editor = graph.to_editor()?; |
| 102 | + |
| 103 | + // get the origional a |
| 104 | + let a = repo.rev_parse_single("A")?; |
| 105 | + insta::assert_snapshot!(visualize_tree(a), @r#" |
| 106 | + 0cc630c |
| 107 | + └── file:100644:d78dd4f "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n50\n51\n52\n53\n54\n55\n56\n57\n58\n59\n60\n" |
| 108 | + "#); |
| 109 | + |
| 110 | + // reword commit a |
| 111 | + let mut a_obj = but_core::Commit::from_id(a)?; |
| 112 | + |
| 113 | + let mut builder = repo.edit_tree(a_obj.tree)?; |
| 114 | + let new_blob = repo.write_blob("I'm a new file :D\n")?; |
| 115 | + builder.upsert("new-file.txt", gix::objs::tree::EntryKind::Blob, new_blob)?; |
| 116 | + let tree = builder.write()?; |
| 117 | + |
| 118 | + a_obj.tree = tree.detach(); |
| 119 | + a_obj.message = "A: a second coming".into(); |
| 120 | + let a_new = repo.write_object(a_obj.inner)?.detach(); |
| 121 | + |
| 122 | + // select the origional a out of the graph |
| 123 | + let a_selector = editor |
| 124 | + .select_commit(a.detach()) |
| 125 | + .context("Failed to find commit a in editor graph")?; |
| 126 | + // replace it with the new one |
| 127 | + editor.replace(&a_selector, Step::Pick { id: a_new }); |
| 128 | + |
| 129 | + let outcome = editor.rebase(&repo)?; |
| 130 | + let RebaseOutcome::Success(outcome) = outcome else { |
| 131 | + bail!("Rebase failed"); |
| 132 | + }; |
| 133 | + outcome.materialize(&repo)?; |
| 134 | + |
| 135 | + insta::assert_snapshot!(visualize_commit_graph_all(&repo)?, @r" |
| 136 | + * 4a8642c (HEAD -> with-inner-merge) on top of inner merge |
| 137 | + * fa3d6b8 Merge branch 'B' into with-inner-merge |
| 138 | + |\ |
| 139 | + | * 984fd1c (B) C: new file with 10 lines |
| 140 | + * | f1905a8 (A) A: a second coming |
| 141 | + |/ |
| 142 | + * 8f0d338 (tag: base, main) base |
| 143 | + "); |
| 144 | + insta::assert_snapshot!(git_status(&repo)?, @r" |
| 145 | + "); |
| 146 | + |
| 147 | + // A should include our extra blob |
| 148 | + let a = repo.rev_parse_single("A")?; |
| 149 | + insta::assert_snapshot!(visualize_tree(a), @r#" |
| 150 | + 0c482d4 |
| 151 | + ├── file:100644:d78dd4f "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n50\n51\n52\n53\n54\n55\n56\n57\n58\n59\n60\n" |
| 152 | + └── new-file.txt:100644:715faaf "I\'m a new file :D\n" |
| 153 | + "#); |
| 154 | + |
| 155 | + // New head tree should also include our extra blob |
| 156 | + let new_head_tree = repo.head_tree()?.id(); |
| 157 | + insta::assert_snapshot!(visualize_tree(new_head_tree), @r#" |
| 158 | + 89042ca |
| 159 | + ├── added-after-with-inner-merge:100644:861be1b "seq 10\n" |
| 160 | + ├── file:100644:d78dd4f "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n50\n51\n52\n53\n54\n55\n56\n57\n58\n59\n60\n" |
| 161 | + ├── new-file:100644:f00c965 "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" |
| 162 | + └── new-file.txt:100644:715faaf "I\'m a new file :D\n" |
| 163 | + "#); |
| 164 | + |
| 165 | + Ok(()) |
| 166 | +} |
0 commit comments