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
mod config;
mod diff;
mod ai;
mod git;
mod git_status;
mod git_remote;
mod ssh_setup;
mod staging;
use clap::{Parser, Subcommand};
use std::io::{self, Write};
use config::Config;
#[derive(Parser)]
#[command(author, version, about="gitbit - AI Git Assistant")]
struct Args {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
/// Save your Gemini API key
Setup,
/// Automatically fix "origin" to SSH and configure GitHub auth
FixRemote,
/// Stage files smartly, generate AI commit, push to remote
Push {
/// Exclude files by pattern: gitbit push -x file1 file2 "*.log"
#[arg(short='x', long="exclude", num_args = 1.., value_delimiter = ' ')]
exclude: Vec<String>,
},
}
fn main() {
let args = Args::parse();
match args.command {
/* ────────────────────────────────────────────────
gitbit setup
───────────────────────────────────────────────── */
Commands::Setup => {
print!("Enter your Gemini API key: ");
io::stdout().flush().unwrap();
let mut key = String::new();
io::stdin().read_line(&mut key).unwrap();
Config::save(key.trim());
println!("🔑 API key saved!");
}
/* ────────────────────────────────────────────────
gitbit fix-remote
───────────────────────────────────────────────── */
Commands::FixRemote => {
use git_remote::*;
use ssh_setup::*;
if !git_status::is_git_repo() {
println!("❌ Not a Git repository.");
return;
}
let origin = match get_remote_origin() {
Some(o) => o,
None => {
println!("❌ No remote 'origin' found.");
return;
}
};
println!("📦 Current remote: {}", origin);
if origin.starts_with("https://") {
println!("🔄 Switching remote HTTPS → SSH...");
if let Some((user, repo)) = get_repo_parts(&origin) {
set_ssh_remote(&user, &repo);
println!("✔ SSH remote set: git@github.com:{}/{}.git", user, repo);
} else {
println!("❌ Could not parse remote URL.");
return;
}
} else {
println!("✔ Remote already using SSH.");
}
ensure_ssh_key();
show_public_key();
test_github_connection();
println!("🎉 fix-remote completed!");
}
/* ────────────────────────────────────────────────
gitbit push (with -x exclusions)
───────────────────────────────────────────────── */
Commands::Push { exclude } => {
// 1. Must be a Git repo
if !git_status::is_git_repo() {
println!("❌ Not a Git repository.");
return;
}
// 2. Must have at least one commit
if !git_status::has_commits() {
println!("❌ No commits yet. Create the first commit manually.");
return;
}
// 3. Show remote + branch
if let Some(remote) = git_remote::get_remote_origin() {
println!("📦 Remote: {}", remote);
} else {
println!("❌ No remote 'origin' found.");
println!("Run: git remote add origin <url>");
return;
}
if let Some(branch) = git_remote::get_current_branch() {
println!("🌿 Branch: {}", branch);
}
// 4. Get file changes
let changes = staging::get_changes();
if changes.is_empty() {
println!("✔ No changes to commit.");
return;
}
// 5. Smart filtering
let (to_stage, ignored_default, ignored_user) =
staging::filter_changes(&changes, &exclude);
println!("\n📄 Detected changes:");
for c in &changes {
println!(" {} {}", c.status, c.path);
}
println!("\n🛑 Excluded by DEFAULT rules:");
for f in ignored_default {
println!(" - {}", f);
}
println!("\n🛑 Excluded by -x patterns:");
for f in ignored_user {
println!(" - {}", f);
}
println!("\n📦 Final files to stage:");
for f in &to_stage {
println!(" - {}", f);
}
if to_stage.is_empty() {
println!("❌ Nothing to stage after exclusions.");
return;
}
// 6. Stage only selected files
git::git_add_specific(&to_stage);
// 🟡 ASK FOR CONFIRMATION BEFORE COMMIT & PUSH
use std::io::{self, Write};
print!("\nProceed with commit and push? (y/n): ");
io::stdout().flush().unwrap();
let mut confirm = String::new();
io::stdin().read_line(&mut confirm).unwrap();
let confirm = confirm.trim().to_lowercase();
if confirm != "y" && confirm != "yes" {
println!("❌ Aborted by user.");
return;
}
// 7. AI commit message
let cfg = Config::load().expect("Run gitbit setup first.");
let diff = diff::get_diff();
if diff.trim().is_empty() {
println!("⚠️ No staged changes detected. Using default commit message.");
}
let msg = ai::generate_message(&diff, &cfg);
println!("\n🧠 Commit message:\n{}\n", msg);
git::git_commit(&msg);
git::git_push();
println!("🚀 gitbit push complete!");
}
}
}