Skip to content

Commit c14df9c

Browse files
authored
Merge pull request #10486 from gitbutlerapp/add-json-common-merge-base
Include common_merge_base in JSON worktree output
2 parents 240fca0 + 1507d63 commit c14df9c

File tree

1 file changed

+48
-16
lines changed

1 file changed

+48
-16
lines changed

crates/but/src/status/mod.rs

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,29 @@ use gitbutler_command_context::CommandContext;
99
use gitbutler_commit::commit_ext::CommitExt;
1010
use gitbutler_oxidize::OidExt;
1111
use gitbutler_project::Project;
12+
use serde::Serialize;
1213
use std::collections::BTreeMap;
1314
use std::path::Path;
1415
pub(crate) mod assignment;
1516

1617
use crate::id::CliId;
1718

19+
type StackDetail = (Option<StackDetails>, Vec<FileAssignment>);
20+
type StackEntry = (Option<gitbutler_stack::StackId>, StackDetail);
21+
22+
#[derive(Serialize)]
23+
struct CommonMergeBase {
24+
target_name: String,
25+
common_merge_base: String,
26+
message: String,
27+
}
28+
29+
#[derive(Serialize)]
30+
struct WorktreeStatus {
31+
stacks: Vec<StackEntry>,
32+
common_merge_base: CommonMergeBase,
33+
}
34+
1835
pub(crate) fn worktree(repo_path: &Path, json: bool, show_files: bool) -> anyhow::Result<()> {
1936
let project = Project::find_by_path(repo_path).expect("Failed to create project from path");
2037
let ctx = &mut CommandContext::open(&project, AppSettings::load_from_default_path_creating()?)?;
@@ -37,7 +54,7 @@ pub(crate) fn worktree(repo_path: &Path, json: bool, show_files: bool) -> anyhow
3754
FileAssignment::from_assignments(path, assignments),
3855
);
3956
}
40-
let mut stack_details = vec![];
57+
let mut stack_details: Vec<StackEntry> = vec![];
4158

4259
let unassigned = assignment::filter_by_stack_id(assignments_by_file.values(), &None);
4360
stack_details.push((None, (None, unassigned)));
@@ -47,8 +64,31 @@ pub(crate) fn worktree(repo_path: &Path, json: bool, show_files: bool) -> anyhow
4764
stack_details.push((stack.id, (Some(details), assignments)));
4865
}
4966

67+
// Calculate common_merge_base data
68+
let stack = gitbutler_stack::VirtualBranchesHandle::new(ctx.project().gb_dir());
69+
let target = stack.get_default_target()?;
70+
let target_name = format!("[{}/{}]", target.branch.remote(), target.branch.branch());
71+
let repo = ctx.gix_repo()?;
72+
let base_commit = repo.find_commit(target.sha.to_gix())?;
73+
let message = base_commit
74+
.message_bstr()
75+
.to_string()
76+
.replace('\n', " ")
77+
.chars()
78+
.take(50)
79+
.collect::<String>();
80+
let common_merge_base_data = CommonMergeBase {
81+
target_name: target_name.clone(),
82+
common_merge_base: target.sha.to_string()[..7].to_string(),
83+
message: message.clone(),
84+
};
85+
5086
if json {
51-
let json_output = serde_json::to_string_pretty(&stack_details)?;
87+
let worktree_status = WorktreeStatus {
88+
stacks: stack_details,
89+
common_merge_base: common_merge_base_data,
90+
};
91+
let json_output = serde_json::to_string_pretty(&worktree_status)?;
5292
println!("{json_output}");
5393
return Ok(());
5494
}
@@ -72,20 +112,12 @@ pub(crate) fn worktree(repo_path: &Path, json: bool, show_files: bool) -> anyhow
72112
ctx,
73113
)?;
74114
}
75-
let stack = gitbutler_stack::VirtualBranchesHandle::new(ctx.project().gb_dir());
76-
let target = stack.get_default_target()?;
77-
let target_name = format!("[{}/{}]", target.branch.remote(), target.branch.branch());
78-
let repo = ctx.gix_repo()?;
79-
let base_commit = repo.find_commit(target.sha.to_gix())?;
80-
let message = base_commit
81-
.message_bstr()
82-
.to_string()
83-
.replace('\n', " ")
84-
.chars()
85-
.take(50)
86-
.collect::<String>();
87-
let common_merge_base = target.sha.to_string()[..7].to_string();
88-
println!("◉ {common_merge_base} (common base) {target_name} {message}");
115+
println!(
116+
"◉ {} (common base) {} {}",
117+
common_merge_base_data.common_merge_base,
118+
common_merge_base_data.target_name,
119+
common_merge_base_data.message
120+
);
89121
Ok(())
90122
}
91123

0 commit comments

Comments
 (0)