Skip to content

Commit 59b7382

Browse files
committed
Support JSON output for but -j oplog
Allow the oplog command to return JSON when the json flag is set. Previously the json parameter was unused; this change wires the flag into show_oplog, prints an empty JSON array when no snapshots are found, and emits pretty-printed JSON for the snapshots. When not in JSON mode, the existing human-readable, colorized output is preserved. Additionally, minor refactoring fixes variable indentation and ensures snapshot details are handled correctly for both output modes.
1 parent 68c8950 commit 59b7382

File tree

1 file changed

+66
-55
lines changed

1 file changed

+66
-55
lines changed

crates/but/src/oplog/mod.rs

Lines changed: 66 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use gitbutler_oplog::{OplogExt, entry::OperationKind};
55
use gitbutler_project::Project;
66
use std::path::Path;
77

8-
pub(crate) fn show_oplog(repo_path: &Path, _json: bool, since: Option<&str>) -> anyhow::Result<()> {
8+
pub(crate) fn show_oplog(repo_path: &Path, json: bool, since: Option<&str>) -> anyhow::Result<()> {
99
let project = Project::from_path(repo_path)?;
1010
let ctx = CommandContext::open(&project, AppSettings::load_from_default_path_creating()?)?;
1111

@@ -37,69 +37,80 @@ pub(crate) fn show_oplog(repo_path: &Path, _json: bool, since: Option<&str>) ->
3737
};
3838

3939
if snapshots.is_empty() {
40-
println!("No operations found in history.");
40+
if json {
41+
println!("[]");
42+
} else {
43+
println!("No operations found in history.");
44+
}
4145
return Ok(());
4246
}
4347

44-
println!("{}", "Operations History".blue().bold());
45-
println!("{}", "─".repeat(50).dimmed());
48+
if json {
49+
// Output JSON format
50+
let json_output = serde_json::to_string_pretty(&snapshots)?;
51+
println!("{}", json_output);
52+
} else {
53+
// Output human-readable format
54+
println!("{}", "Operations History".blue().bold());
55+
println!("{}", "─".repeat(50).dimmed());
4656

47-
for snapshot in snapshots {
48-
let time_string = chrono::DateTime::from_timestamp(snapshot.created_at.seconds(), 0)
49-
.ok_or(anyhow::anyhow!("Could not parse timestamp"))?
50-
.format("%Y-%m-%d %H:%M:%S")
51-
.to_string();
57+
for snapshot in snapshots {
58+
let time_string = chrono::DateTime::from_timestamp(snapshot.created_at.seconds(), 0)
59+
.ok_or(anyhow::anyhow!("Could not parse timestamp"))?
60+
.format("%Y-%m-%d %H:%M:%S")
61+
.to_string();
5262

53-
let commit_id = format!(
54-
"{}{}",
55-
&snapshot.commit_id.to_string()[..7].blue().underline(),
56-
&snapshot.commit_id.to_string()[7..12].blue().dimmed()
57-
);
63+
let commit_id = format!(
64+
"{}{}",
65+
&snapshot.commit_id.to_string()[..7].blue().underline(),
66+
&snapshot.commit_id.to_string()[7..12].blue().dimmed()
67+
);
5868

59-
let (operation_type, title) = if let Some(details) = &snapshot.details {
60-
let op_type = match details.operation {
61-
OperationKind::CreateCommit => "CREATE",
62-
OperationKind::CreateBranch => "BRANCH",
63-
OperationKind::AmendCommit => "AMEND",
64-
OperationKind::UndoCommit => "UNDO",
65-
OperationKind::SquashCommit => "SQUASH",
66-
OperationKind::UpdateCommitMessage => "REWORD",
67-
OperationKind::MoveCommit => "MOVE",
68-
OperationKind::RestoreFromSnapshot => "RESTORE",
69-
OperationKind::ReorderCommit => "REORDER",
70-
OperationKind::InsertBlankCommit => "INSERT",
71-
OperationKind::MoveHunk => "MOVE_HUNK",
72-
OperationKind::ReorderBranches => "REORDER_BRANCH",
73-
OperationKind::UpdateWorkspaceBase => "UPDATE_BASE",
74-
OperationKind::UpdateBranchName => "RENAME",
75-
OperationKind::GenericBranchUpdate => "BRANCH_UPDATE",
76-
OperationKind::ApplyBranch => "APPLY",
77-
OperationKind::UnapplyBranch => "UNAPPLY",
78-
OperationKind::DeleteBranch => "DELETE",
79-
OperationKind::DiscardChanges => "DISCARD",
80-
_ => "OTHER",
69+
let (operation_type, title) = if let Some(details) = &snapshot.details {
70+
let op_type = match details.operation {
71+
OperationKind::CreateCommit => "CREATE",
72+
OperationKind::CreateBranch => "BRANCH",
73+
OperationKind::AmendCommit => "AMEND",
74+
OperationKind::UndoCommit => "UNDO",
75+
OperationKind::SquashCommit => "SQUASH",
76+
OperationKind::UpdateCommitMessage => "REWORD",
77+
OperationKind::MoveCommit => "MOVE",
78+
OperationKind::RestoreFromSnapshot => "RESTORE",
79+
OperationKind::ReorderCommit => "REORDER",
80+
OperationKind::InsertBlankCommit => "INSERT",
81+
OperationKind::MoveHunk => "MOVE_HUNK",
82+
OperationKind::ReorderBranches => "REORDER_BRANCH",
83+
OperationKind::UpdateWorkspaceBase => "UPDATE_BASE",
84+
OperationKind::UpdateBranchName => "RENAME",
85+
OperationKind::GenericBranchUpdate => "BRANCH_UPDATE",
86+
OperationKind::ApplyBranch => "APPLY",
87+
OperationKind::UnapplyBranch => "UNAPPLY",
88+
OperationKind::DeleteBranch => "DELETE",
89+
OperationKind::DiscardChanges => "DISCARD",
90+
_ => "OTHER",
91+
};
92+
(op_type, details.title.clone())
93+
} else {
94+
("UNKNOWN", "Unknown operation".to_string())
8195
};
82-
(op_type, details.title.clone())
83-
} else {
84-
("UNKNOWN", "Unknown operation".to_string())
85-
};
8696

87-
let operation_colored = match operation_type {
88-
"CREATE" => operation_type.green(),
89-
"AMEND" | "REWORD" => operation_type.yellow(),
90-
"UNDO" | "RESTORE" => operation_type.red(),
91-
"BRANCH" | "CHECKOUT" => operation_type.purple(),
92-
"MOVE" | "REORDER" | "MOVE_HUNK" => operation_type.cyan(),
93-
_ => operation_type.normal(),
94-
};
97+
let operation_colored = match operation_type {
98+
"CREATE" => operation_type.green(),
99+
"AMEND" | "REWORD" => operation_type.yellow(),
100+
"UNDO" | "RESTORE" => operation_type.red(),
101+
"BRANCH" | "CHECKOUT" => operation_type.purple(),
102+
"MOVE" | "REORDER" | "MOVE_HUNK" => operation_type.cyan(),
103+
_ => operation_type.normal(),
104+
};
95105

96-
println!(
97-
"{} {} {} {}",
98-
commit_id,
99-
time_string.dimmed(),
100-
format!("[{}]", operation_colored),
101-
title
102-
);
106+
println!(
107+
"{} {} {} {}",
108+
commit_id,
109+
time_string.dimmed(),
110+
format!("[{}]", operation_colored),
111+
title
112+
);
113+
}
103114
}
104115

105116
Ok(())

0 commit comments

Comments
 (0)