Skip to content

Commit 8bf3d5c

Browse files
authored
Merge pull request #10470 from gitbutlerapp/sc-but-json
JSON stuff
2 parents 7226ad5 + 6ebac1a commit 8bf3d5c

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

crates/but/src/args.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub struct Args {
1616

1717
#[derive(Debug, clap::Subcommand)]
1818
pub enum Subcommands {
19-
/// Provides an overview of the Workspace commit graph.
19+
/// Show commits on active branches in your workspace.
2020
Log,
2121
/// Overview of the uncommitted changes in the repository.
2222
#[clap(alias = "st")]

crates/but/src/log/mod.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::path::Path;
1111

1212
use crate::id::CliId;
1313

14-
pub(crate) fn commit_graph(repo_path: &Path, _json: bool) -> anyhow::Result<()> {
14+
pub(crate) fn commit_graph(repo_path: &Path, json: bool) -> anyhow::Result<()> {
1515
let project = Project::find_by_path(repo_path).expect("Failed to create project from path");
1616
let ctx = &mut CommandContext::open(&project, AppSettings::load_from_default_path_creating()?)?;
1717
but_rules::process_rules(ctx).ok(); // TODO: this is doing double work (dependencies can be reused)
@@ -21,6 +21,10 @@ pub(crate) fn commit_graph(repo_path: &Path, _json: bool) -> anyhow::Result<()>
2121
.filter_map(Result::ok)
2222
.collect::<Vec<_>>();
2323

24+
if json {
25+
return output_json(stacks.into_iter().map(|(_, stack)| stack).collect());
26+
}
27+
2428
let mut nesting = 0;
2529
for (i, (stack_id, stack)) in stacks.iter().enumerate() {
2630
let marked = crate::mark::stack_marked(ctx, *stack_id).unwrap_or_default();
@@ -209,3 +213,9 @@ pub(crate) fn stack_details(
209213
but_workspace::stack_details(&ctx.project().gb_dir(), stack_id, ctx)
210214
}
211215
}
216+
217+
fn output_json(stacks: Vec<but_workspace::ui::StackDetails>) -> anyhow::Result<()> {
218+
let json_output = serde_json::to_string_pretty(&stacks)?;
219+
println!("{json_output}");
220+
Ok(())
221+
}

crates/but/src/status/assignment.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use bstr::BString;
22
use but_hunk_assignment::HunkAssignment;
33

4-
#[derive(Debug, Clone)]
4+
#[derive(Debug, Clone, serde::Serialize)]
55
pub(crate) struct FileAssignment {
66
pub path: BString,
77
pub assignments: Vec<HunkAssignment>,

crates/but/src/status/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub(crate) mod assignment;
1313

1414
use crate::id::CliId;
1515

16-
pub(crate) fn worktree(repo_path: &Path, _json: bool, show_files: bool) -> anyhow::Result<()> {
16+
pub(crate) fn worktree(repo_path: &Path, json: bool, show_files: bool) -> anyhow::Result<()> {
1717
let project = Project::find_by_path(repo_path).expect("Failed to create project from path");
1818
let ctx = &mut CommandContext::open(&project, AppSettings::load_from_default_path_creating()?)?;
1919
but_rules::process_rules(ctx).ok(); // TODO: this is doing double work (dependencies can be reused)
@@ -45,6 +45,12 @@ pub(crate) fn worktree(repo_path: &Path, _json: bool, show_files: bool) -> anyho
4545
stack_details.push((stack.id, (Some(details), assignments)));
4646
}
4747

48+
if json {
49+
let json_output = serde_json::to_string_pretty(&stack_details)?;
50+
println!("{json_output}");
51+
return Ok(());
52+
}
53+
4854
for (stack_id, (details, assignments)) in stack_details {
4955
let mut stack_mark = stack_id.and_then(|stack_id| {
5056
if crate::mark::stack_marked(ctx, stack_id).unwrap_or_default() {

0 commit comments

Comments
 (0)