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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
mod cache;
mod commands;
mod formatters;
use clap::{Parser, Subcommand, ValueEnum};
use colored::control;
use colored::Colorize;
use commands::blame::{blame_command, BlameOptions};
use commands::context::{context_command, ContextOptions};
use commands::diff::{diff_command, DiffOptions, OutputFormat};
use commands::entities::{entities_command, EntitiesOptions};
use commands::impact::{impact_command, ImpactMode, ImpactOptions};
use commands::log::{log_command, LogOptions};
#[derive(Parser)]
#[command(name = "sem", version = env!("CARGO_PKG_VERSION"), about = "Semantic version control")]
struct Cli {
#[command(subcommand)]
command: Option<Commands>,
}
#[derive(Clone, Copy, ValueEnum)]
enum ColorMode {
Always,
Auto,
Never,
}
#[derive(Subcommand)]
enum Commands {
/// Show semantic diff of changes (supports git diff syntax)
Diff {
/// Git refs, files, or pathspecs (supports ref1..ref2, ref1...ref2, -- paths)
#[arg(trailing_var_arg = true, allow_hyphen_values = true)]
args: Vec<String>,
/// Show only staged changes (alias: --cached)
#[arg(long)]
staged: bool,
/// Show only staged changes (alias for --staged)
#[arg(long)]
cached: bool,
/// Show changes from a specific commit
#[arg(long)]
commit: Option<String>,
/// Start of commit range
#[arg(long)]
from: Option<String>,
/// End of commit range
#[arg(long)]
to: Option<String>,
/// Read FileChange[] JSON from stdin instead of git
#[arg(long)]
stdin: bool,
/// Output format: terminal, json, or markdown
#[arg(long, default_value = "terminal")]
format: String,
/// Show inline content diffs for each entity
#[arg(long, short = 'v')]
verbose: bool,
/// Show internal timing profile
#[arg(long, hide = true)]
profile: bool,
/// Only include files with these extensions (e.g. --file-exts .py .rs)
#[arg(long)]
file_exts: Vec<String>,
/// When to use colors: always, auto, never
#[arg(long, default_value = "auto")]
color: ColorMode,
},
/// Show impact of changing an entity (deps, dependents, transitive impact, tests)
Impact {
/// Name of the entity to analyze
#[arg()]
entity: String,
/// File containing the entity (disambiguates if multiple matches)
#[arg(long)]
file: Option<String>,
/// Show direct dependencies only
#[arg(long)]
deps: bool,
/// Show direct dependents only
#[arg(long)]
dependents: bool,
/// Show affected test entities only
#[arg(long)]
tests: bool,
/// Output as JSON
#[arg(long)]
json: bool,
/// Only include files with these extensions (e.g. --file-exts .py .rs)
#[arg(long)]
file_exts: Vec<String>,
/// Skip the SQLite entity cache (rebuild from scratch)
#[arg(long)]
no_cache: bool,
},
/// Show semantic blame — who last modified each entity
Blame {
/// File to blame
#[arg()]
file: String,
/// Output as JSON
#[arg(long)]
json: bool,
},
/// Show evolution of an entity through git history
Log {
/// Name of the entity to trace
#[arg()]
entity: String,
/// File containing the entity (auto-detected if omitted)
#[arg(long)]
file: Option<String>,
/// Maximum number of commits to scan
#[arg(long, default_value = "50")]
limit: usize,
/// Output as JSON
#[arg(long)]
json: bool,
/// Show content diff between versions
#[arg(long, short = 'v')]
verbose: bool,
},
/// List entities in a file
Entities {
/// File to extract entities from
#[arg()]
file: String,
/// Output as JSON
#[arg(long)]
json: bool,
},
/// Show token-budgeted context for an entity
Context {
/// Name of the entity
#[arg()]
entity: String,
/// File containing the entity (disambiguates if multiple matches)
#[arg(long)]
file: Option<String>,
/// Token budget
#[arg(long, default_value = "8000")]
budget: usize,
/// Output as JSON
#[arg(long)]
json: bool,
/// Only include files with these extensions (e.g. --file-exts .py .rs)
#[arg(long)]
file_exts: Vec<String>,
/// Skip the SQLite entity cache (rebuild from scratch)
#[arg(long)]
no_cache: bool,
},
/// Replace `git diff` with `sem diff` globally
Setup,
/// Restore default `git diff` behavior
Unsetup,
}
fn apply_color_mode(mode: ColorMode) {
match mode {
ColorMode::Always => control::set_override(true),
ColorMode::Never => control::set_override(false),
ColorMode::Auto => {}
}
}
fn main() {
let cli = Cli::parse();
match cli.command {
Some(Commands::Diff {
args,
staged,
cached,
commit,
from,
to,
stdin,
verbose,
format,
profile,
file_exts,
color,
}) => {
apply_color_mode(color);
let output_format = match format.as_str() {
"json" => OutputFormat::Json,
"markdown" | "md" => OutputFormat::Markdown,
"plain" => OutputFormat::Plain,
_ => OutputFormat::Terminal,
};
diff_command(DiffOptions {
cwd: std::env::current_dir()
.unwrap_or_default()
.to_string_lossy()
.to_string(),
format: output_format,
staged: staged || cached,
commit,
from,
to,
stdin,
verbose,
profile,
file_exts,
args,
});
}
Some(Commands::Blame { file, json }) => {
blame_command(BlameOptions {
cwd: std::env::current_dir()
.unwrap_or_default()
.to_string_lossy()
.to_string(),
file_path: file,
json,
});
}
Some(Commands::Impact {
entity,
file,
deps,
dependents,
tests,
json,
file_exts,
no_cache,
}) => {
let mode = if deps {
ImpactMode::Deps
} else if dependents {
ImpactMode::Dependents
} else if tests {
ImpactMode::Tests
} else {
ImpactMode::All
};
impact_command(ImpactOptions {
cwd: std::env::current_dir()
.unwrap_or_default()
.to_string_lossy()
.to_string(),
entity_name: entity,
file_hint: file,
json,
file_exts,
mode,
no_cache,
});
}
Some(Commands::Log {
entity,
file,
limit,
json,
verbose,
}) => {
log_command(LogOptions {
cwd: std::env::current_dir()
.unwrap_or_default()
.to_string_lossy()
.to_string(),
entity_name: entity,
file_path: file,
limit,
json,
verbose,
});
}
Some(Commands::Entities { file, json }) => {
entities_command(EntitiesOptions {
cwd: std::env::current_dir()
.unwrap_or_default()
.to_string_lossy()
.to_string(),
file_path: file,
json,
});
}
Some(Commands::Context {
entity,
file,
budget,
json,
file_exts,
no_cache,
}) => {
context_command(ContextOptions {
cwd: std::env::current_dir()
.unwrap_or_default()
.to_string_lossy()
.to_string(),
entity_name: entity,
file_path: file,
budget,
json,
file_exts,
no_cache,
});
}
Some(Commands::Setup) => {
if let Err(e) = commands::setup::run() {
eprintln!("{} {}", "error:".red().bold(), e);
std::process::exit(1);
}
}
Some(Commands::Unsetup) => {
if let Err(e) = commands::setup::unsetup() {
eprintln!("{} {}", "error:".red().bold(), e);
std::process::exit(1);
}
}
None => {
// Default to diff when no subcommand is given
diff_command(DiffOptions {
cwd: std::env::current_dir()
.unwrap_or_default()
.to_string_lossy()
.to_string(),
format: OutputFormat::Terminal,
staged: false,
commit: None,
from: None,
to: None,
stdin: false,
verbose: false,
profile: false,
file_exts: vec![],
args: vec![],
});
}
}
}