@@ -5,7 +5,7 @@ use gitbutler_oplog::{OplogExt, entry::OperationKind};
5
5
use gitbutler_project:: Project ;
6
6
use std:: path:: Path ;
7
7
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 < ( ) > {
9
9
let project = Project :: from_path ( repo_path) ?;
10
10
let ctx = CommandContext :: open ( & project, AppSettings :: load_from_default_path_creating ( ) ?) ?;
11
11
@@ -37,69 +37,80 @@ pub(crate) fn show_oplog(repo_path: &Path, _json: bool, since: Option<&str>) ->
37
37
} ;
38
38
39
39
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
+ }
41
45
return Ok ( ( ) ) ;
42
46
}
43
47
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( ) ) ;
46
56
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 ( ) ;
52
62
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
+ ) ;
58
68
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 ( ) )
81
95
} ;
82
- ( op_type, details. title . clone ( ) )
83
- } else {
84
- ( "UNKNOWN" , "Unknown operation" . to_string ( ) )
85
- } ;
86
96
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
+ } ;
95
105
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
+ }
103
114
}
104
115
105
116
Ok ( ( ) )
0 commit comments