git-alias 1.2.10

A fast git alias tool with dual-style command support
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
mod alias;
mod config;
mod shell;
mod utils;

use colored::Colorize;
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;

#[cfg(windows)]
fn enable_ansi_support() {
    unsafe {
        use windows::Win32::System::Console::{
            CONSOLE_MODE, GetConsoleMode, GetStdHandle, STD_OUTPUT_HANDLE,
        };
        use windows::Win32::System::LibraryLoader::{GetModuleHandleW, GetProcAddress};
        use windows::core::{s, w};

        let kernel32 = GetModuleHandleW(w!("kernel32")).unwrap();
        let set_console_mode_ptr = GetProcAddress(kernel32, s!("SetConsoleMode")).unwrap();
        let set_console_mode: extern "system" fn(
            windows::Win32::Foundation::HANDLE,
            CONSOLE_MODE,
        ) -> i32 = std::mem::transmute(set_console_mode_ptr);

        let console_handle = GetStdHandle(STD_OUTPUT_HANDLE).unwrap();
        let mut mode = CONSOLE_MODE(0);
        GetConsoleMode(console_handle, &mut mode).ok();
        mode |= CONSOLE_MODE(0x0004); // ENABLE_VIRTUAL_TERMINAL_PROCESSING
        set_console_mode(console_handle, mode);
    }
}

fn main() {
    #[cfg(windows)]
    enable_ansi_support();

    colored::control::set_override(true);

    let args: Vec<String> = std::env::args().collect();
    let exe_name = get_exe_name();

    let aliases = load_aliases();

    // 检查1: 阻止非 g 前缀的直接调用
    if !exe_name.starts_with('g') && aliases.contains_key(&exe_name) {
        eprintln!(
            "{}: '{}' is not a valid command",
            "Error".red().bold(),
            exe_name
        );
        eprintln!();
        eprintln!("Use 'g {}' instead", exe_name.green());
        std::process::exit(1);
    }

    if exe_name == "g" || exe_name == "git-alias" || exe_name == "git-alias.exe" {
        handle_subcommand(&args, &aliases);
    } else {
        execute_alias(&exe_name, &args[1..], &aliases);
    }
}

fn get_exe_name() -> String {
    std::env::args()
        .next()
        .and_then(|p| {
            std::path::Path::new(&p)
                .file_stem()
                .map(|s| s.to_string_lossy().to_string())
        })
        .unwrap_or_else(|| "g".to_string())
}

fn load_aliases() -> HashMap<String, Vec<String>> {
    let mut aliases = alias::get_builtin_aliases();
    let config = config::load_config();
    aliases.extend(config.aliases);
    aliases
}

fn execute_alias(name: &str, args: &[String], aliases: &HashMap<String, Vec<String>>) {
    let config = config::load_config();

    if let Some(git_args) = aliases.get(name) {
        let mut full_args = git_args.clone();
        full_args.extend_from_slice(args);

        if config.verbose {
            eprintln!("{} git {}", "Executing:".cyan(), full_args.join(" "));
        }

        let exit_code = utils::execute_git_command(&full_args);
        std::process::exit(exit_code);
    } else {
        let mut full_args = vec![name.to_string()];
        full_args.extend_from_slice(args);

        if config.verbose {
            eprintln!("{} git {}", "Executing:".cyan(), full_args.join(" "));
        }

        let exit_code = utils::execute_git_command(&full_args);
        std::process::exit(exit_code);
    }
}

fn handle_subcommand(args: &[String], aliases: &HashMap<String, Vec<String>>) {
    if args.len() < 2 {
        print_help();
        return;
    }

    match args[1].as_str() {
        "msg" => {
            let exit_code = handle_msg(args.get(2).map(|s| s.as_str()));
            std::process::exit(exit_code);
        }
        "g" => {
            // g msg 等子命令
            if args.len() < 3 {
                print_help();
                return;
            }
            match args[2].as_str() {
                "list" => {
                    let filter = args.get(3);
                    alias::list_aliases(aliases, filter.map(|s| s.as_str()));
                }
                "completions" => {
                    if let Some(shell) = args.get(3) {
                        shell::print_completions(shell, aliases);
                    } else {
                        print_completions_help();
                    }
                }
                "init" => {
                    if let Err(e) = config::create_default_config() {
                        eprintln!("{}: {}", "Error".red().bold(), e);
                        std::process::exit(1);
                    }
                }
                "install" => {
                    if let Err(e) = install_all() {
                        eprintln!("{}: {}", "Error".red().bold(), e);
                        std::process::exit(1);
                    }
                }
                "msg" => {
                    let exit_code = handle_msg(args.get(3).map(|s| s.as_str()));
                    std::process::exit(exit_code);
                }
                "--help" | "-h" => {
                    print_help();
                }
                "--version" | "-V" => {
                    println!("git-alias 1.2.10");
                }
                sub => {
                    // 其他 g xxx 命令,当作 git xxx 执行
                    let mut full_args = vec![sub.to_string()];
                    full_args.extend_from_slice(&args[3..]);
                    let exit_code = utils::execute_git_command(&full_args);
                    std::process::exit(exit_code);
                }
            }
        }
        "list" => {
            let filter = args.get(2);
            alias::list_aliases(aliases, filter.map(|s| s.as_str()));
        }
        "completions" => {
            if let Some(shell) = args.get(2) {
                shell::print_completions(shell, aliases);
            } else {
                print_completions_help();
            }
        }
        "init" => {
            if let Err(e) = config::create_default_config() {
                eprintln!("{}: {}", "Error".red().bold(), e);
                std::process::exit(1);
            }
        }
        "install" => {
            if let Err(e) = install_all() {
                eprintln!("{}: {}", "Error".red().bold(), e);
                std::process::exit(1);
            }
        }
        "--help" | "-h" => {
            print_help();
        }
        "--version" | "-V" => {
            println!("git-alias 1.2.10");
        }
        alias_name => {
            // 检查2: 阻止 g g* 调用
            if alias_name.starts_with('g') && aliases.contains_key(alias_name) {
                eprintln!(
                    "{}: 'g {}' is not needed",
                    "Hint".yellow().bold(),
                    alias_name
                );
                eprintln!();
                eprintln!("Use '{}' directly instead", alias_name.green());
                std::process::exit(1);
            }

            if aliases.contains_key(alias_name) {
                let remaining_args = &args[2..];
                let git_args = aliases.get(alias_name).unwrap();
                let mut full_args = git_args.clone();
                full_args.extend_from_slice(remaining_args);

                let exit_code = utils::execute_git_command(&full_args);
                std::process::exit(exit_code);
            } else {
                let mut full_args = vec![alias_name.to_string()];
                full_args.extend_from_slice(&args[2..]);

                let exit_code = utils::execute_git_command(&full_args);
                std::process::exit(exit_code);
            }
        }
    }
}

fn install_all() -> Result<(), String> {
    let install_dir = get_install_dir()?;

    fs::create_dir_all(&install_dir).map_err(|e| {
        format!(
            "Failed to create directory {}: {}",
            install_dir.display(),
            e
        )
    })?;

    let current_exe = std::env::current_exe()
        .map_err(|e| format!("Failed to get current executable path: {}", e))?;

    let aliases = load_aliases();

    let mut installed = vec![];

    for alias_name in aliases.keys() {
        // 只安装 g 前缀的命令
        if !alias_name.starts_with('g') {
            continue;
        }

        let target_path = install_dir.join(alias_name);

        #[cfg(windows)]
        {
            fs::copy(&current_exe, &target_path).map_err(|e| {
                format!("Failed to copy binary to {}: {}", target_path.display(), e)
            })?;
        }

        #[cfg(unix)]
        {
            if target_path.exists() || target_path.symlink_metadata().is_ok() {
                fs::remove_file(&target_path)
                    .map_err(|e| format!("Failed to remove existing file: {}", e))?;
            }

            std::os::unix::fs::symlink(&current_exe, &target_path)
                .map_err(|e| format!("Failed to create symlink: {}", e))?;
        }

        installed.push(alias_name.clone());
    }

    // 安装 g 命令本身
    let g_path = install_dir.join("g");
    fs::copy(&current_exe, &g_path)
        .map_err(|e| format!("Failed to copy binary to {}: {}", g_path.display(), e))?;
    installed.push("g".to_string());

    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let permissions = fs::Permissions::from_mode(0o755);
        fs::set_permissions(&g_path, permissions)
            .map_err(|e| format!("Failed to set permissions: {}", e))?;
    }

    println!("{}", "Installation successful!".green().bold());
    println!();
    println!("Installed to: {}", install_dir.display());
    println!("Total: {} commands installed", installed.len());
    println!();
    println!("Usage:");
    println!("  g s         # git status");
    println!("  gs          # git status");
    println!("  g ls        # git log --no-merges");
    println!("  gls         # git log --no-merges");
    println!();
    println!("Run: {} list", "g".green());

    Ok(())
}

fn get_install_dir() -> Result<PathBuf, String> {
    let home = dirs::home_dir().ok_or_else(|| "Failed to get home directory".to_string())?;

    let install_dir = home.join(".local").join("bin");

    Ok(install_dir)
}

fn print_help() {
    println!("{}", "Git-Alias - Fast Git Alias Tool".cyan().bold());
    println!();
    println!("{}", "USAGE:".yellow().bold());
    println!("  {} [args...]          Run git command directly", "gs".green());
    println!("  g {} [args...]        Run git command via g prefix", "s".green());
    println!();
    println!("{}", "SUBCOMMANDS:".yellow().bold());
    println!("  g list [filter]       List all aliases");
    println!("  g completions <sh>    Generate shell completions (bash/zsh/fish)");
    println!("  g init                Create config file at ~/.git-alias.toml");
    println!("  g install             Install all alias commands to ~/.local/bin");
    println!("  g msg <range>         AI summarize commits (claude/opencode)");
    println!();
    println!("{}", "NOTES:".yellow().bold());
    println!("  - Direct call must start with 'g' (e.g., gs, gls)");
    println!("  - g prefix works with any alias (e.g., g s, g ls)");
    println!("  - g gs is not supported, use gs instead");
    println!("  - Run 'g list' to see all available aliases");
}

fn print_completions_help() {
    println!("{}", "Shell Completions".cyan().bold());
    println!();
    println!("{}", "Usage:".yellow().bold());
    println!("  {} <shell>", "g completions".green());
    println!();
    println!("{}", "Supported shells:".yellow().bold());
    println!("  {}    Bash shell", "bash".cyan());
    println!("  {}     Zsh shell", "zsh".cyan());
    println!("  {}    Fish shell", "fish".cyan());
    println!();
    println!("{}", "Examples:".yellow().bold());
    println!("  {} {}", "#".dimmed(), "Bash - 临时启用".white());
    println!("  {}", "source <(g completions bash)".green());
    println!();
    println!("  {} {}", "#".dimmed(), "Bash - 持久化 (添加到 ~/.bashrc)".white());
    println!("  {}", "echo 'source <(g completions bash)' >> ~/.bashrc".green());
    println!();
    println!("  {} {}", "#".dimmed(), "Zsh - 临时启用".white());
    println!("  {}", "source <(g completions zsh)".green());
    println!();
    println!("  {} {}", "#".dimmed(), "Zsh - 持久化 (添加到 ~/.zshrc)".white());
    println!("  {}", "echo 'source <(g completions zsh)' >> ~/.zshrc".green());
    println!();
    println!("  {} {}", "#".dimmed(), "Fish - 持久化".white());
    println!("  {}", "g completions fish > ~/.config/fish/completions/g.fish".green());
}

fn print_msg_help() {
    println!("{}", "Git Msg - AI Commit Summarizer".cyan().bold());
    println!();
    println!("{}", "USAGE:".yellow().bold());
    println!("  {} <commit_range>    Generate AI-powered changelog summary", "g msg".green());
    println!();
    println!("{}", "EXAMPLES:".yellow().bold());
    println!("  {} release/v0.3.4..release/v0.3.5  # Compare branches", "g msg".green());
    println!("  {} db8201ed..8a61f32a           # Compare commits", "g msg".green());
    println!("  {} HEAD~10..HEAD                 # Last 10 commits", "g msg".green());
    println!();
    println!("{}", "REQUIREMENTS:".yellow().bold());
    println!("  - Requires AI tool installed: claude or opencode");
    println!("  - AI tool must be in PATH or WinGet Links");
}

fn handle_msg(range: Option<&str>) -> i32 {
    let range = match range {
        Some("-h") | Some("--help") | None => {
            print_msg_help();
            return 0;
        }
        Some(r) => r,
    };

    // 解析 range 获取起始和结束 commit,用于生成标题
    let range_parts: Vec<&str> = range.split("..").collect();
    let range_title = if range_parts.len() == 2 {
        format!("{}{}", range_parts[0], range_parts[1])
    } else {
        range.to_string()
    };

    // 1. 获取提交列表 (简洁格式)
    let log_output = utils::exec_git_output(&["log", "--no-merges", "--format=%h %s", range]);
    // 2. 获取详细 diff
    let diff_output = utils::exec_git_output(&["log", "-p", "--no-merges", range]);

    if log_output.trim().is_empty() {
        eprintln!("{}", "错误: 未找到提交记录或范围无效".red().bold());
        return 1;
    }

    // 3. 调用 AI 工具生成摘要
    let prompt = format!(
        r#"请分析以下 Git 提交记录,生成一份版本变更摘要。

输出格式要求:
- 标题: ## 版本变更摘要 ({range})
- 按以下分类输出,每类用对应 emoji 标记:
  - ### 🐛 Bug 修复
  - ### ✨ 新功能
  - ### 🔧 优化与重构
  - ### 📝 其他
- 每条变更用 **粗体标题**:描述内容
- 简洁明了,适合作为 changelog
- 如果某类没有内容则写"无"

## 提交列表
{log}

## 详细变更
{diff}"#,
        range = range_title,
        log = log_output,
        diff = diff_output
    );

    match call_ai_tool(&prompt) {
        Ok(summary) => {
            println!("{}", summary);
            0
        }
        Err(e) => {
            eprintln!("{}", e);
            1
        }
    }
}

fn call_ai_tool(prompt: &str) -> Result<String, String> {
    // Windows 命令行长度限制约 32KB,截断 prompt 避免超过限制
    // 使用字符边界截断,避免中文字符被切断
    const MAX_PROMPT_LEN: usize = 30000;
    let truncated_prompt: String = if prompt.len() > MAX_PROMPT_LEN {
        prompt.chars().take(MAX_PROMPT_LEN).collect()
    } else {
        prompt.to_string()
    };

    // 1. 尝试 claude 实际 exe 路径
    let claude_exe = r"C:\Users\liuzhifeng\AppData\Local\Microsoft\WinGet\Packages\Anthropic.ClaudeCode_Microsoft.Winget.Source_8wekyb3d8bbwe\claude.exe";
    if let Ok(output) = try_command_direct(claude_exe, &["-p", &truncated_prompt]) {
        if !output.trim().is_empty() {
            return Ok(output);
        }
    }

    // 2. 尝试用最短的 PATH 搜索
    let minimal_path = r"C:\Users\liuzhifeng\AppData\Local\Microsoft\WinGet\Links;C:\Users\liuzhifeng\AppData\Roaming\npm";
    for tool in ["claude", "opencode", "openspec"] {
        if let Ok(output) = try_command(tool, &["-p", &truncated_prompt], minimal_path) {
            if !output.trim().is_empty() {
                return Ok(output);
            }
        }
    }

    Err("错误: 未找到可用的 AI 工具 (claude/opencode/openspec),请确保已安装并配置在 PATH 中".to_string())
}

fn try_command_direct(cmd_path: &str, args: &[&str]) -> Result<String, String> {
    std::process::Command::new(cmd_path)
        .args(args)
        .output()
        .map(|o| String::from_utf8_lossy(&o.stdout).to_string())
        .map_err(|e| e.to_string())
}

fn try_command(cmd: &str, args: &[&str], path: &str) -> Result<String, String> {
    std::process::Command::new(cmd)
        .args(args)
        .env("PATH", path)
        .output()
        .map(|o| String::from_utf8_lossy(&o.stdout).to_string())
        .map_err(|e| e.to_string())
}