1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
use clap::{Args, Subcommand};
use serde::Serialize;
use homeboy::git::{self, GitOutput};
use homeboy::BulkResult;
use crate::commands::version;
use super::CmdResult;
#[derive(Args)]
pub struct GitArgs {
#[command(subcommand)]
command: GitCommand,
}
#[derive(Subcommand)]
enum GitCommand {
/// Show git status for a component
Status {
/// JSON input spec for bulk operations.
/// Use "-" for stdin, "@file.json" for file, or inline JSON string.
#[arg(long)]
json: Option<String>,
/// Component ID (non-JSON mode)
component_id: Option<String>,
},
/// Commit changes (by default stages all, use flags for granular control)
Commit {
/// Component ID (optional if provided in JSON body)
component_id: Option<String>,
/// Commit message or JSON spec (auto-detected).
/// Plain text: treated as commit message.
/// JSON (starts with { or [): parsed as commit spec.
/// @file.json: reads JSON from file.
/// "-": reads JSON from stdin.
spec: Option<String>,
/// Explicit JSON spec (takes precedence over positional)
#[arg(long, value_name = "JSON")]
json: Option<String>,
/// Commit message (CLI mode)
#[arg(short, long)]
message: Option<String>,
/// Commit only staged changes (skip automatic git add)
#[arg(long)]
staged_only: bool,
/// Stage and commit only these specific files
#[arg(long, num_args = 1.., conflicts_with = "exclude")]
files: Option<Vec<String>>,
/// Stage all files except these (mutually exclusive with --files)
#[arg(long, num_args = 1.., conflicts_with = "files")]
exclude: Option<Vec<String>>,
/// Explicit include list (repeatable)
#[arg(long, num_args = 1.., conflicts_with = "exclude", conflicts_with = "files")]
include: Option<Vec<String>>,
},
/// Push local commits to remote
Push {
/// JSON input spec for bulk operations.
/// Use "-" for stdin, "@file.json" for file, or inline JSON string.
#[arg(long)]
json: Option<String>,
/// Component ID (non-JSON mode)
component_id: Option<String>,
/// Push tags as well
#[arg(long)]
tags: bool,
},
/// Pull remote changes
Pull {
/// JSON input spec for bulk operations.
/// Use "-" for stdin, "@file.json" for file, or inline JSON string.
#[arg(long)]
json: Option<String>,
/// Component ID (non-JSON mode)
component_id: Option<String>,
},
/// Create a git tag
Tag {
/// Component ID
component_id: Option<String>,
/// Tag name (e.g., v0.1.2)
///
/// Defaults to v<component version> if not provided.
tag_name: Option<String>,
/// Tag message (creates annotated tag)
#[arg(short, long)]
message: Option<String>,
},
}
#[derive(Serialize)]
#[serde(untagged)]
pub enum GitCommandOutput {
Single(GitOutput),
Bulk(BulkResult<GitOutput>),
}
pub fn run(args: GitArgs, _global: &crate::commands::GlobalArgs) -> CmdResult<GitCommandOutput> {
match args.command {
GitCommand::Status { json, component_id } => {
if let Some(spec) = json {
let output = git::status_bulk(&spec)?;
let exit_code = if output.summary.failed > 0 { 1 } else { 0 };
return Ok((GitCommandOutput::Bulk(output), exit_code));
}
let output = git::status(component_id.as_deref())?;
let exit_code = output.exit_code;
Ok((GitCommandOutput::Single(output), exit_code))
}
GitCommand::Commit {
component_id,
spec,
json,
message,
staged_only,
files,
exclude,
include,
} => {
// Explicit --json flag always uses JSON mode
if let Some(json_spec) = json {
let output = git::commit_from_json(component_id.as_deref(), &json_spec)?;
return match output {
git::CommitJsonOutput::Single(o) => {
let exit_code = o.exit_code;
Ok((GitCommandOutput::Single(o), exit_code))
}
git::CommitJsonOutput::Bulk(b) => {
let exit_code = if b.summary.failed > 0 { 1 } else { 0 };
Ok((GitCommandOutput::Bulk(b), exit_code))
}
};
}
// Auto-detect: check if positional spec looks like JSON or is a plain message
let (inferred_message, json_spec) = match &spec {
Some(s) => {
let trimmed = s.trim();
// JSON indicators: starts with { or [, uses @file, or - for stdin
let is_json = trimmed.starts_with('{')
|| trimmed.starts_with('[')
|| trimmed.starts_with('@')
|| trimmed == "-";
if is_json {
(None, Some(s.clone()))
} else {
// Treat as plain commit message
(Some(s.clone()), None)
}
}
None => (None, None),
};
// JSON mode if auto-detected
if let Some(json_str) = json_spec {
let output = git::commit_from_json(component_id.as_deref(), &json_str)?;
return match output {
git::CommitJsonOutput::Single(o) => {
let exit_code = o.exit_code;
Ok((GitCommandOutput::Single(o), exit_code))
}
git::CommitJsonOutput::Bulk(b) => {
let exit_code = if b.summary.failed > 0 { 1 } else { 0 };
Ok((GitCommandOutput::Bulk(b), exit_code))
}
};
}
// CLI flag mode - use inferred message or explicit -m flag
let final_message = inferred_message.or(message);
let mut resolved_files = files;
if resolved_files.is_none() {
resolved_files = include;
}
let options = git::CommitOptions {
staged_only,
files: resolved_files,
exclude,
amend: false,
};
let output = git::commit(component_id.as_deref(), final_message.as_deref(), options)?;
let exit_code = output.exit_code;
Ok((GitCommandOutput::Single(output), exit_code))
}
GitCommand::Push {
json,
component_id,
tags,
} => {
if let Some(spec) = json {
let output = git::push_bulk(&spec)?;
let exit_code = if output.summary.failed > 0 { 1 } else { 0 };
return Ok((GitCommandOutput::Bulk(output), exit_code));
}
let output = git::push(component_id.as_deref(), tags)?;
let exit_code = output.exit_code;
Ok((GitCommandOutput::Single(output), exit_code))
}
GitCommand::Pull { json, component_id } => {
if let Some(spec) = json {
let output = git::pull_bulk(&spec)?;
let exit_code = if output.summary.failed > 0 { 1 } else { 0 };
return Ok((GitCommandOutput::Bulk(output), exit_code));
}
let output = git::pull(component_id.as_deref())?;
let exit_code = output.exit_code;
Ok((GitCommandOutput::Single(output), exit_code))
}
GitCommand::Tag {
component_id,
tag_name,
message,
} => {
// Derive tag from version if not provided
let final_tag = match tag_name {
Some(name) => name,
None => {
// Need component_id to look up version
let id = component_id.as_ref().ok_or_else(|| {
homeboy::Error::validation_invalid_argument(
"componentId",
"Missing componentId",
None,
Some(vec![
"Provide a component ID: homeboy git tag <component-id>"
.to_string(),
"Or specify a tag name: homeboy git tag <component-id> <tag-name>"
.to_string(),
]),
)
})?;
let (out, _) = version::show_version_output(id)?;
format!("v{}", out.version)
}
};
let output = git::tag(
component_id.as_deref(),
Some(&final_tag),
message.as_deref(),
)?;
let exit_code = output.exit_code;
Ok((GitCommandOutput::Single(output), exit_code))
}
}
}