Skip to content

Commit 49c75c7

Browse files
krlvischacon
authored andcommitted
Amending rule now uses change ID
Fix a thing
1 parent 48701ee commit 49c75c7

File tree

6 files changed

+56
-14
lines changed

6 files changed

+56
-14
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/but-rules/src/handler.rs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ pub fn process_workspace_rules(
6363
handle_assign(ctx, assignments, dependencies.as_ref()).unwrap_or_default();
6464
}
6565
}
66-
super::Action::Explicit(super::Operation::Amend { commit_id }) => {
66+
super::Action::Explicit(super::Operation::Amend { change_id }) => {
6767
let assignments = matching(assignments, rule.filters.clone());
68-
handle_amend(ctx, assignments, commit_id).unwrap_or_default();
68+
handle_amend(ctx, assignments, change_id).unwrap_or_default();
6969
}
7070
_ => continue,
7171
};
@@ -147,18 +147,46 @@ fn handle_assign(
147147
fn handle_amend(
148148
ctx: &mut CommandContext,
149149
assignments: Vec<HunkAssignment>,
150-
commit_id: String,
150+
change_id: String,
151151
) -> anyhow::Result<()> {
152152
let changes: Vec<DiffSpec> = assignments.into_iter().map(|a| a.into()).collect();
153153
let project = ctx.project();
154154
let mut guard = project.exclusive_worktree_access();
155155
let repo = but_core::open_repo_for_merging(project.worktree_path())?;
156+
157+
let meta = VirtualBranchesTomlMetadata::from_path(
158+
ctx.project().gb_dir().join("virtual_branches.toml"),
159+
)?;
160+
let ref_info_options = but_workspace::ref_info::Options {
161+
expensive_commit_info: true,
162+
traversal: meta.graph_options(),
163+
};
164+
let info = but_workspace::head_info(&repo, &meta, ref_info_options)?;
165+
let mut commit_id: Option<gix::ObjectId> = None;
166+
'outer: for stack in info.stacks {
167+
for segment in stack.segments {
168+
for commit in segment.commits {
169+
if Some(change_id.clone()) == commit.change_id.map(|c| c.to_string()) {
170+
commit_id = Some(commit.id);
171+
break 'outer;
172+
}
173+
}
174+
}
175+
}
176+
177+
let commit_id = commit_id.ok_or_else(|| {
178+
anyhow::anyhow!(
179+
"No commit with Change-Id {} found in the current workspace",
180+
change_id
181+
)
182+
})?;
183+
156184
commit_engine::create_commit_and_update_refs_with_project(
157185
&repo,
158186
project,
159187
None,
160188
commit_engine::Destination::AmendCommit {
161-
commit_id: gix::ObjectId::from_str(&commit_id)?,
189+
commit_id,
162190
// TODO: Expose this in the UI for 'edit message' functionality.
163191
new_message: None,
164192
},

crates/but-rules/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ impl WorkspaceRule {
4747
}
4848

4949
pub fn target_commit_id(&self) -> Option<String> {
50-
if let Action::Explicit(Operation::Amend { commit_id }) = &self.action {
51-
Some(commit_id.clone())
50+
if let Action::Explicit(Operation::Amend { change_id }) = &self.action {
51+
Some(change_id.clone())
5252
} else {
5353
None
5454
}
@@ -147,7 +147,7 @@ pub enum Operation {
147147
/// Assign the matched changes to a specific stack ID.
148148
Assign { target: StackTarget },
149149
/// Amend the matched changes into a specific commit.
150-
Amend { commit_id: String },
150+
Amend { change_id: String },
151151
/// Create a new commit with the matched changes on a specific branch.
152152
NewCommit { branch_name: String },
153153
}

crates/but/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ but-rules.workspace = true
4747
gitbutler-command-context.workspace = true
4848
gitbutler-serde.workspace = true
4949
gitbutler-stack.workspace = true
50+
gitbutler-commit.workspace = true
5051
gitbutler-branch-actions.workspace = true
5152
gitbutler-branch.workspace = true
5253
gitbutler-reference.workspace = true

crates/but/src/log/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ pub(crate) fn commit_graph(repo_path: &Path, json: bool, short: bool) -> anyhow:
2222
.collect::<Vec<_>>();
2323

2424
if json {
25-
return output_json(stacks);
25+
return output_json(stacks.iter().map(|(_, d)| d).cloned().collect());
2626
}
2727

2828
if short {
29-
return commit_graph_short(stacks);
29+
return commit_graph_short(stacks.iter().map(|(_, d)| d).cloned().collect());
3030
}
3131

3232
let mut nesting = 0;

crates/but/src/mark/mod.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
use std::path::Path;
1+
use std::{path::Path, str::FromStr};
22

33
use crate::rub::branch_name_to_stack_id;
44
use anyhow::bail;
55
use but_rules::Operation;
66
use but_settings::AppSettings;
77
use but_workspace::StackId;
88
use gitbutler_command_context::CommandContext;
9+
use gitbutler_commit::commit_ext::CommitExt;
910
use gitbutler_project::Project;
1011
pub(crate) fn handle(
1112
repo_path: &Path,
@@ -45,9 +46,12 @@ fn mark_commit(ctx: &mut CommandContext, oid: gix::ObjectId, delete: bool) -> an
4546
println!("Mark was removed");
4647
return Ok(());
4748
}
48-
let action = but_rules::Action::Explicit(Operation::Amend {
49-
commit_id: oid.to_string(),
50-
});
49+
let repo = ctx.gix_repo()?;
50+
let commit = repo.find_commit(oid)?;
51+
let change_id = commit.change_id().ok_or_else(|| {
52+
anyhow::anyhow!("Commit {} does not have a Change-Id, cannot mark it", oid)
53+
})?;
54+
let action = but_rules::Action::Explicit(Operation::Amend { change_id });
5155
let req = but_rules::CreateRuleRequest {
5256
trigger: but_rules::Trigger::FileSytemChange,
5357
filters: vec![but_rules::Filter::PathMatchesRegex(regex::Regex::new(
@@ -97,8 +101,16 @@ pub(crate) fn stack_marked(ctx: &mut CommandContext, stack_id: StackId) -> anyho
97101
}
98102

99103
pub(crate) fn commit_marked(ctx: &mut CommandContext, commit_id: String) -> anyhow::Result<bool> {
104+
let repo = ctx.gix_repo()?;
105+
let commit = repo.find_commit(gix::ObjectId::from_str(&commit_id)?)?;
106+
let change_id = commit.change_id().ok_or_else(|| {
107+
anyhow::anyhow!(
108+
"Commit {} does not have a Change-Id, cannot mark it",
109+
commit_id
110+
)
111+
})?;
100112
let rules = but_rules::list_rules(ctx)?
101113
.iter()
102-
.any(|r| r.target_commit_id() == Some(commit_id.clone()));
114+
.any(|r| r.target_commit_id() == Some(change_id.clone()));
103115
Ok(rules)
104116
}

0 commit comments

Comments
 (0)