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
//! repos: A tool for managing and synchronizing multiple git repositories
//!
//! This tool scans for git repositories and provides commands to:
//! - Push any unpushed commits to their upstream remotes
//! - Sync user configuration (name/email) across repositories
use anyhow::Result;
use clap::{Parser, Subcommand};
mod audit;
mod commands;
mod core;
mod git;
mod package;
mod subrepo;
mod utils;
use commands::audit::handle_audit_command;
use commands::config::{handle_config_command, parse_config_command};
use commands::publish::handle_publish_command;
use commands::staging::{
handle_commit_command, handle_stage_command, handle_staging_status_command,
handle_unstage_command,
};
use commands::sync::{handle_pull_command, handle_push_command};
use git::ConfigArgs;
#[derive(Subcommand, Clone)]
enum Commands {
/// Push unpushed commits to remotes across all repositories
Push {
/// Automatically push branches with no upstream tracking
#[arg(long)]
force: bool,
/// Show detailed progress for all repositories
#[arg(long, short)]
verbose: bool,
/// Show file changes in repos with uncommitted changes
#[arg(long, short = 'c')]
show_changes: bool,
/// Skip subrepo drift check (faster but less complete health check)
#[arg(long)]
no_drift_check: bool,
/// Number of concurrent operations (default: CPU cores + 2)
#[arg(long, short = 'j', conflicts_with = "sequential")]
jobs: Option<usize>,
/// Run one operation at a time (useful for debugging or very slow connections)
#[arg(long)]
sequential: bool,
},
/// Pull changes from remotes across all repositories
Pull {
/// Use rebase instead of merge (git pull --rebase)
#[arg(long)]
rebase: bool,
/// Show detailed progress for all repositories
#[arg(long, short)]
verbose: bool,
/// Show file changes in repos with uncommitted changes
#[arg(long, short = 'c')]
show_changes: bool,
/// Skip subrepo drift check (faster but less complete health check)
#[arg(long)]
no_drift_check: bool,
/// Number of concurrent operations (default: CPU cores + 2, capped at 12)
#[arg(long, short = 'j', conflicts_with = "sequential")]
jobs: Option<usize>,
/// Run one operation at a time (useful for debugging or very slow connections)
#[arg(long)]
sequential: bool,
},
/// Manage git configuration across repositories
Config {
/// User name to set across all repositories
#[arg(long)]
name: Option<String>,
/// User email to set across all repositories
#[arg(long)]
email: Option<String>,
/// Use global git config as source
#[arg(long, conflicts_with_all = ["name", "email", "from_current"])]
from_global: bool,
/// Use current repository's config as source
#[arg(long, conflicts_with_all = ["name", "email", "from_global"])]
from_current: bool,
/// Force overwrite all configs without prompting
#[arg(long)]
force: bool,
/// Show what would be changed without making changes
#[arg(long)]
dry_run: bool,
},
/// Stage files matching pattern across all repositories
Stage {
/// Pattern to match files (e.g., "*.md", "README.md")
pattern: String,
},
/// Unstage files matching pattern across all repositories
Unstage {
/// Pattern to match files (e.g., "*.md", "README.md", "*")
pattern: String,
},
/// Show staging status across all repositories
Status,
/// Commit staged changes across all repositories
Commit {
/// Commit message
message: String,
/// Include repositories with no staged changes (create empty commits)
#[arg(long)]
include_empty: bool,
},
/// Publish packages to their registries (npm, cargo, PyPI)
Publish {
/// Specific repositories to publish (by name)
repos: Vec<String>,
/// Show what would be published without actually publishing
#[arg(long)]
dry_run: bool,
/// Create and push git tags after successful publish (e.g., v1.2.3)
#[arg(long)]
tag: bool,
/// Allow publishing with uncommitted changes (not recommended)
#[arg(long)]
allow_dirty: bool,
/// Publish all repositories regardless of visibility
#[arg(long, conflicts_with_all = ["public_only", "private_only"])]
all: bool,
/// Only publish public repositories (default behavior)
#[arg(long, conflicts_with_all = ["all", "private_only"])]
public_only: bool,
/// Only publish private repositories
#[arg(long, conflicts_with_all = ["all", "public_only"])]
private_only: bool,
},
/// Audit repositories for security vulnerabilities and secrets
Audit {
/// Install required tools (TruffleHog) without prompting
#[arg(long)]
install_tools: bool,
/// Verify discovered secrets are active and fail on findings
#[arg(long)]
verify: bool,
/// Output results in JSON format
#[arg(long)]
json: bool,
/// Interactive mode - choose fixes interactively
#[arg(long)]
interactive: bool,
/// Fix .gitignore violations by adding entries
#[arg(long)]
fix_gitignore: bool,
/// Remove large files from Git history (requires git filter-repo)
#[arg(long)]
fix_large: bool,
/// Remove secrets from Git history
#[arg(long)]
fix_secrets: bool,
/// Apply all available fixes automatically
#[arg(long)]
fix_all: bool,
/// Preview changes without applying them
#[arg(long)]
dry_run: bool,
/// Only fix specific repositories (comma-separated)
#[arg(long, value_delimiter = ',')]
repos: Option<Vec<String>>,
},
/// Manage nested repository synchronization
Subrepo {
#[command(subcommand)]
subcommand: SubrepoCommand,
},
}
#[derive(Subcommand, Clone)]
enum SubrepoCommand {
/// Validate subrepo setup and show all nested repos
Validate,
/// Show subrepo sync status (drift detection)
Status {
/// Show all subrepos, not just drifted ones
#[arg(long)]
all: bool,
},
/// Sync a subrepo to specific commit across all parents
Sync {
/// Subrepo name
name: String,
/// Target commit hash
#[arg(long)]
to: String,
/// Stash uncommitted changes before syncing (safe, reversible)
#[arg(long)]
stash: bool,
/// Force sync even with uncommitted changes (discards changes)
#[arg(long)]
force: bool,
},
/// Update a subrepo to latest commit across all parents
Update {
/// Subrepo name
name: String,
/// Force update even with uncommitted changes
#[arg(long)]
force: bool,
},
}
#[derive(Parser)]
#[command(name = "repos")]
#[command(about = "A tool for managing and synchronizing multiple git repositories")]
#[command(version = env!("CARGO_PKG_VERSION"))]
struct Cli {
/// Automatically push branches with no upstream tracking (for push)
#[arg(long, global = true)]
force: bool,
#[command(subcommand)]
command: Option<Commands>,
}
/// Handles subrepo subcommands
fn handle_subrepo_command(subcommand: SubrepoCommand) -> Result<()> {
match subcommand {
SubrepoCommand::Validate => {
let report = subrepo::validation::validate_subrepos()?;
subrepo::validation::display_report(&report);
Ok(())
}
SubrepoCommand::Status { all } => {
let statuses = subrepo::status::analyze_subrepos()?;
subrepo::status::display_status(&statuses, all);
Ok(())
}
SubrepoCommand::Sync { name, to, stash, force } => {
subrepo::sync::sync_subrepo(&name, &to, stash, force)
}
SubrepoCommand::Update { name, force } => {
subrepo::sync::update_subrepo(&name, force)
}
}
}
#[tokio::main]
async fn main() -> Result<()> {
let cli = Cli::parse();
// Determine the operation mode and handle commands
match &cli.command {
Some(Commands::Push { force, verbose, show_changes, no_drift_check, jobs, sequential }) => {
let force_push = *force || cli.force;
handle_push_command(force_push, *verbose, *show_changes, *no_drift_check, *jobs, *sequential).await
}
Some(Commands::Pull { rebase, verbose, show_changes, no_drift_check, jobs, sequential }) => {
handle_pull_command(*rebase, *verbose, *show_changes, *no_drift_check, *jobs, *sequential).await
}
Some(Commands::Stage { pattern }) => handle_stage_command(pattern.clone()).await,
Some(Commands::Unstage { pattern }) => handle_unstage_command(pattern.clone()).await,
Some(Commands::Status) => handle_staging_status_command().await,
Some(Commands::Commit {
message,
include_empty,
}) => handle_commit_command(message.clone(), *include_empty).await,
Some(Commands::Publish { repos, dry_run, tag, allow_dirty, all, public_only, private_only }) => {
handle_publish_command(repos.clone(), *dry_run, *tag, *allow_dirty, *all, *public_only, *private_only).await
}
Some(Commands::Config {
name,
email,
from_global,
from_current,
force,
dry_run,
}) => {
let config_args = ConfigArgs {
command: parse_config_command(
name.clone(),
email.clone(),
*from_global,
*from_current,
*force,
*dry_run,
)?,
};
handle_config_command(config_args).await
}
Some(Commands::Audit {
install_tools,
verify,
json,
interactive,
fix_gitignore,
fix_large,
fix_secrets,
fix_all,
dry_run,
repos,
}) => {
handle_audit_command(
*install_tools,
*verify,
*json,
*interactive,
*fix_gitignore,
*fix_large,
*fix_secrets,
*fix_all,
*dry_run,
repos.clone(),
)
.await
}
Some(Commands::Subrepo { subcommand }) => {
handle_subrepo_command(subcommand.clone())
}
None => {
// Default behavior - show help
use clap::CommandFactory;
let mut cmd = Cli::command();
cmd.print_help()?;
Ok(())
}
}
}