twin-cli 0.2.0

Git worktree wrapper with side effects (symlinks and hooks)
Documentation
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
#![allow(dead_code)]
/// Git操作モジュール
///
/// このモジュールの役割:
/// - git worktree add/remove/list コマンドのラッパー
/// - ブランチの作成と管理
/// - 自動コミット機能の実装
/// - Gitリポジトリの状態確認
use crate::core::{TwinError, TwinResult};
use chrono::{DateTime, Local};
use log::{debug, info, warn};
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use std::process::{Command, Output};

/// Worktreeの情報を表す構造体
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorktreeInfo {
    /// Worktreeのパス
    pub path: PathBuf,
    /// チェックアウトされているブランチ名
    pub branch: String,
    /// コミットハッシュ
    pub commit: String,
    /// エージェント名(ブランチ名から抽出)
    pub agent_name: Option<String>,
    /// 作成日時
    pub created_at: Option<DateTime<Local>>,
    /// 最終更新日時
    pub last_updated: Option<DateTime<Local>>,
    /// ロック状態
    pub locked: bool,
    /// プルーニング可能かどうか
    pub prunable: bool,
}

/// ブランチの情報を表す構造体
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BranchInfo {
    /// ブランチ名
    pub name: String,
    /// リモートブランチ名
    pub remote: Option<String>,
    /// 現在のブランチかどうか
    pub current: bool,
    /// コミットハッシュ
    pub commit: String,
    /// 上流ブランチとの差分
    pub ahead: usize,
    pub behind: usize,
}

/// Git操作を管理する構造体
pub struct GitManager {
    /// リポジトリのルートパス
    repo_path: PathBuf,
    /// git2ライブラリのリポジトリインスタンス(オプション)
    repository: Option<git2::Repository>,
    /// 実行履歴の記録
    command_history: Vec<String>,
    /// ドライラン モード
    dry_run: bool,
}

impl GitManager {
    /// 新しいGitManagerインスタンスを作成
    pub fn new(repo_path: &Path) -> TwinResult<Self> {
        let repo_path = repo_path.to_path_buf();

        // git2ライブラリを使用してリポジトリを開く
        let repository = match git2::Repository::open(&repo_path) {
            Ok(repo) => {
                info!("Opened Git repository at: {repo_path:?}");
                Some(repo)
            }
            Err(e) => {
                warn!("Failed to open repository with git2: {e}");
                // git2で開けない場合でも、gitコマンドは使える可能性があるので続行
                None
            }
        };

        // gitコマンドが使用可能か確認
        Self::verify_git_available()?;

        Ok(Self {
            repo_path,
            repository,
            command_history: Vec::new(),
            dry_run: false,
        })
    }

    /// ドライランモードを設定
    pub fn set_dry_run(&mut self, dry_run: bool) {
        self.dry_run = dry_run;
    }

    /// gitコマンドが使用可能か確認
    fn verify_git_available() -> TwinResult<()> {
        let output = Command::new("git")
            .arg("--version")
            .output()
            .map_err(|e| TwinError::git(format!("Git command not found: {e}")))?;

        if !output.status.success() {
            return Err(TwinError::git("Git command failed to execute"));
        }

        Ok(())
    }

    /// gitコマンドを実行する共通メソッド
    fn execute_git_command(&mut self, args: &[&str]) -> TwinResult<Output> {
        let command_str = format!("git {}", args.join(" "));
        info!("Executing: {command_str}");
        self.command_history.push(command_str.clone());

        // 透明性のあるコマンド実行ログ
        if std::env::var("TWIN_VERBOSE").is_ok() || std::env::var("TWIN_DEBUG").is_ok() {
            eprintln!("🔧 実行中: {command_str}");
        }

        if self.dry_run {
            info!("[DRY RUN] Would execute: {command_str}");
            if std::env::var("TWIN_VERBOSE").is_ok() || std::env::var("TWIN_DEBUG").is_ok() {
                eprintln!("📝 ドライラン: {command_str}");
            }
            return Ok(Output {
                #[cfg(unix)]
                status: std::os::unix::process::ExitStatusExt::from_raw(0),
                #[cfg(windows)]
                status: std::os::windows::process::ExitStatusExt::from_raw(0),
                stdout: b"[DRY RUN]".to_vec(),
                stderr: Vec::new(),
            });
        }

        let output = Command::new("git")
            .current_dir(&self.repo_path)
            .args(args)
            .output()
            .map_err(|e| TwinError::git(format!("Failed to execute git command: {e}")))?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            return Err(TwinError::git(format!("Git command failed: {stderr}")));
        }

        Ok(output)
    }

    /// Worktreeを追加
    pub fn add_worktree(
        &mut self,
        path: &Path,
        branch: Option<&str>,
        create_branch: bool,
    ) -> TwinResult<WorktreeInfo> {
        let mut args = vec!["worktree", "add"];

        // 新しいブランチを作成する場合
        if create_branch {
            if let Some(b) = branch {
                args.push("-b");
                args.push(b);
            }
        }

        // パスを追加
        let path_str = path.to_string_lossy();
        args.push(&path_str);

        // 既存のブランチを指定する場合
        if !create_branch {
            if let Some(b) = branch {
                args.push(b);
            }
        }

        let output = self.execute_git_command(&args)?;
        debug!(
            "Worktree added: {:?}",
            String::from_utf8_lossy(&output.stdout)
        );

        // 作成されたWorktreeの情報を取得
        self.get_worktree_info(path)
    }

    /// Worktreeを追加(オプションを直接渡す)
    pub fn add_worktree_with_options(&mut self, args: &[&str]) -> TwinResult<Output> {
        let mut full_args = vec!["worktree", "add"];
        full_args.extend_from_slice(args);

        self.execute_git_command_raw(&full_args)
    }

    /// Gitコマンドを実行し、エラーメッセージをそのまま表示
    pub fn execute_git_command_raw(&mut self, args: &[&str]) -> TwinResult<Output> {
        let command_str = format!("git {}", args.join(" "));
        info!("Executing: {command_str}");

        if self.dry_run {
            info!("[DRY RUN] Would execute: {command_str}");
            return Ok(Output {
                #[cfg(unix)]
                status: std::os::unix::process::ExitStatusExt::from_raw(0),
                #[cfg(windows)]
                status: std::os::windows::process::ExitStatusExt::from_raw(0),
                stdout: b"[DRY RUN]".to_vec(),
                stderr: Vec::new(),
            });
        }

        let output = Command::new("git")
            .args(args)
            .current_dir(&self.repo_path)
            .output()
            .map_err(|e| TwinError::git(format!("{e}")))?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            // git worktreeのエラーメッセージをそのまま返す
            return Err(TwinError::git(stderr.trim().to_string()));
        }

        Ok(output)
    }

    /// Worktreeを削除
    pub fn remove_worktree(&mut self, path: &Path, force: bool) -> TwinResult<()> {
        let mut args = vec!["worktree", "remove"];

        if force {
            args.push("--force");
        }

        let path_str = path.to_string_lossy();
        args.push(&path_str);

        self.execute_git_command(&args)?;
        info!("Worktree removed: {path:?}");

        Ok(())
    }

    /// Worktreeの一覧を取得
    pub fn list_worktrees(&mut self) -> TwinResult<Vec<WorktreeInfo>> {
        let output = self.execute_git_command(&["worktree", "list", "--porcelain"])?;
        let stdout = String::from_utf8_lossy(&output.stdout);

        self.parse_worktree_list(&stdout)
    }

    /// Worktreeリストの出力をパース
    fn parse_worktree_list(&self, output: &str) -> TwinResult<Vec<WorktreeInfo>> {
        let mut worktrees = Vec::new();
        let mut current_worktree: Option<WorktreeInfo> = None;

        for line in output.lines() {
            if line.starts_with("worktree ") {
                // 前のworktree情報を保存
                if let Some(wt) = current_worktree.take() {
                    worktrees.push(wt);
                }

                // 新しいworktree情報を開始
                let path = PathBuf::from(line.strip_prefix("worktree ").unwrap());
                current_worktree = Some(WorktreeInfo {
                    path,
                    branch: String::new(),
                    commit: String::new(),
                    agent_name: None,
                    created_at: None,
                    last_updated: None,
                    locked: false,
                    prunable: false,
                });
            } else if let Some(ref mut wt) = current_worktree {
                if line.starts_with("HEAD ") {
                    wt.commit = line.strip_prefix("HEAD ").unwrap().to_string();
                } else if line.starts_with("branch ") {
                    wt.branch = line.strip_prefix("branch ").unwrap().to_string();
                    // エージェント名を抽出(例: agent/claude -> claude)
                    if wt.branch.starts_with("agent/") {
                        wt.agent_name = Some(wt.branch[6..].to_string());
                    }
                } else if line == "locked" {
                    wt.locked = true;
                } else if line == "prunable" {
                    wt.prunable = true;
                }
            }
        }

        // 最後のworktree情報を保存
        if let Some(wt) = current_worktree {
            worktrees.push(wt);
        }

        Ok(worktrees)
    }

    /// 特定のWorktreeの情報を取得
    pub fn get_worktree_info(&mut self, path: &Path) -> TwinResult<WorktreeInfo> {
        let worktrees = self.list_worktrees()?;

        // パスを絶対パスに変換して比較
        let abs_path = if path.is_absolute() {
            path.to_path_buf()
        } else {
            std::env::current_dir()
                .map_err(|e| TwinError::io(format!("Failed to get current dir: {e}"), None))?
                .join(path)
        };

        // 正規化されたパスでも検索を試みる
        let canonical_path = abs_path.canonicalize().ok();

        worktrees
            .into_iter()
            .find(|wt| {
                // 直接比較
                wt.path == path ||
                wt.path == abs_path ||
                // 正規化されたパスとの比較
                canonical_path.as_ref().is_some_and(|cp| {
                    wt.path.canonicalize().ok().is_some_and(|wtp| wtp == *cp)
                }) ||
                // ファイル名だけでも一致を確認(最後の手段)
                wt.path.file_name() == path.file_name() && path.file_name().is_some()
            })
            .ok_or_else(|| TwinError::not_found("Worktree", path.to_string_lossy().to_string()))
    }

    /// プルーニング可能なWorktreeをクリーンアップ
    pub fn prune_worktrees(&mut self, dry_run: bool) -> TwinResult<Vec<PathBuf>> {
        let mut args = vec!["worktree", "prune"];

        if dry_run {
            args.push("--dry-run");
        }

        let output = self.execute_git_command(&args)?;
        let stdout = String::from_utf8_lossy(&output.stdout);

        // プルーニングされたWorktreeのパスを抽出
        let pruned: Vec<PathBuf> = stdout
            .lines()
            .filter_map(|line| {
                if line.contains("Removing worktrees") {
                    Some(PathBuf::from(line.rsplit(":").next()?.trim()))
                } else {
                    None
                }
            })
            .collect();

        Ok(pruned)
    }

    /// ブランチを作成
    pub fn create_branch(
        &mut self,
        branch_name: &str,
        start_point: Option<&str>,
    ) -> TwinResult<()> {
        let mut args = vec!["branch", branch_name];

        if let Some(start) = start_point {
            args.push(start);
        }

        self.execute_git_command(&args)?;
        info!("Branch created: {branch_name}");

        Ok(())
    }

    /// ブランチを削除
    pub fn delete_branch(&mut self, branch_name: &str, force: bool) -> TwinResult<()> {
        let mut args = vec!["branch"];

        if force {
            args.push("-D");
        } else {
            args.push("-d");
        }

        args.push(branch_name);

        self.execute_git_command(&args)?;
        info!("Branch deleted: {branch_name}");

        Ok(())
    }

    /// ブランチの一覧を取得
    pub fn list_branches(&mut self, remote: bool) -> TwinResult<Vec<BranchInfo>> {
        let mut args = vec!["branch", "-v"];

        if remote {
            args.push("-r");
        } else {
            args.push("-a");
        }

        let output = self.execute_git_command(&args)?;
        let stdout = String::from_utf8_lossy(&output.stdout);

        self.parse_branch_list(&stdout)
    }

    /// ブランチリストの出力をパース
    fn parse_branch_list(&self, output: &str) -> TwinResult<Vec<BranchInfo>> {
        let mut branches = Vec::new();

        for line in output.lines() {
            let line = line.trim();
            if line.is_empty() {
                continue;
            }

            let current = line.starts_with('*');
            let line = if current { &line[2..] } else { line };

            let parts: Vec<&str> = line.split_whitespace().collect();
            if parts.len() < 2 {
                continue;
            }

            let name = parts[0].to_string();
            let commit = parts[1].to_string();

            branches.push(BranchInfo {
                name,
                remote: None,
                current,
                commit,
                ahead: 0,
                behind: 0,
            });
        }

        Ok(branches)
    }

    /// ブランチが存在するか確認
    pub fn branch_exists(&mut self, branch_name: &str) -> TwinResult<bool> {
        let branches = self.list_branches(false)?;
        Ok(branches.iter().any(|b| b.name == branch_name))
    }

    /// ユニークなブランチ名を生成(既存のブランチと重複しないように)
    pub fn generate_unique_branch_name(
        &mut self,
        base_name: &str,
        max_attempts: usize,
    ) -> TwinResult<String> {
        // まず基本名を試す
        if !self.branch_exists(base_name)? {
            return Ok(base_name.to_string());
        }

        // 番号付きの名前を試す
        for i in 1..=max_attempts {
            let name = format!("{base_name}-{i}");
            if !self.branch_exists(&name)? {
                return Ok(name);
            }
        }

        // タイムスタンプ付きの名前を生成
        let timestamp = chrono::Local::now().format("%Y%m%d-%H%M%S");
        let name = format!("{base_name}-{timestamp}");

        if !self.branch_exists(&name)? {
            Ok(name)
        } else {
            Err(TwinError::git(format!(
                "Failed to generate unique branch name for: {base_name}"
            )))
        }
    }

    /// コマンド実行履歴を取得
    pub fn get_command_history(&self) -> &[String] {
        &self.command_history
    }

    /// コマンド実行履歴をクリア
    pub fn clear_command_history(&mut self) {
        self.command_history.clear();
    }

    /// リポジトリのルートパスを取得
    pub fn get_repo_path(&self) -> &Path {
        &self.repo_path
    }

    /// 現在のブランチ名を取得
    pub fn get_current_branch(&mut self) -> TwinResult<String> {
        let output = self.execute_git_command(&["rev-parse", "--abbrev-ref", "HEAD"])?;
        let branch = String::from_utf8_lossy(&output.stdout).trim().to_string();
        Ok(branch)
    }

    /// cdコマンド文字列を生成
    pub fn generate_cd_command(&self, path: &Path) -> String {
        format!("cd \"{}\"", path.display())
    }

    /// シェル関数用のヘルパースクリプトを生成
    pub fn generate_shell_helper(&self, shell_type: ShellType) -> String {
        match shell_type {
            ShellType::Bash | ShellType::Zsh => r#"
# Twin worktree helper function
twin-switch() {
    if [ -z "$1" ]; then
        echo "Usage: twin-switch <agent-name>"
        return 1
    fi
    
    local path=$(twin switch "$1" --print-path)
    if [ $? -eq 0 ] && [ -n "$path" ]; then
        cd "$path"
        echo "Switched to agent: $1"
    else
        echo "Failed to switch to agent: $1"
        return 1
    fi
}

# Twin create and switch function
twin-create() {
    if [ -z "$1" ]; then
        echo "Usage: twin-create <agent-name>"
        return 1
    fi
    
    local path=$(twin create "$1" --print-path)
    if [ $? -eq 0 ] && [ -n "$path" ]; then
        cd "$path"
        echo "Created and switched to agent: $1"
    else
        echo "Failed to create agent: $1"
        return 1
    fi
}
"#
            .to_string(),
            ShellType::PowerShell => r#"
# Twin worktree helper function
function Twin-Switch {
    param(
        [Parameter(Mandatory=$true)]
        [string]$AgentName
    )
    
    $path = twin switch $AgentName --print-path
    if ($LASTEXITCODE -eq 0 -and $path) {
        Set-Location $path
        Write-Host "Switched to agent: $AgentName"
    } else {
        Write-Error "Failed to switch to agent: $AgentName"
    }
}

# Twin create and switch function
function Twin-Create {
    param(
        [Parameter(Mandatory=$true)]
        [string]$AgentName
    )
    
    $path = twin create $AgentName --print-path
    if ($LASTEXITCODE -eq 0 -and $path) {
        Set-Location $path
        Write-Host "Created and switched to agent: $AgentName"
    } else {
        Write-Error "Failed to create agent: $AgentName"
    }
}
"#
            .to_string(),
            ShellType::Fish => r#"
# Twin worktree helper function
function twin-switch
    if test -z "$argv[1]"
        echo "Usage: twin-switch <agent-name>"
        return 1
    end
    
    set -l path (twin switch $argv[1] --print-path)
    if test $status -eq 0; and test -n "$path"
        cd $path
        echo "Switched to agent: $argv[1]"
    else
        echo "Failed to switch to agent: $argv[1]"
        return 1
    end
end

# Twin create and switch function
function twin-create
    if test -z "$argv[1]"
        echo "Usage: twin-create <agent-name>"
        return 1
    end
    
    set -l path (twin create $argv[1] --print-path)
    if test $status -eq 0; and test -n "$path"
        cd $path
        echo "Created and switched to agent: $argv[1]"
    else
        echo "Failed to create agent: $argv[1]"
        return 1
    end
end
"#
            .to_string(),
        }
    }

    /// エイリアス設定を生成
    pub fn generate_aliases(&self, shell_type: ShellType) -> String {
        match shell_type {
            ShellType::Bash | ShellType::Zsh => r#"
# Twin aliases
alias tw='twin'
alias tws='twin-switch'
alias twc='twin-create'
alias twl='twin list'
alias twr='twin remove'
"#
            .to_string(),
            ShellType::PowerShell => r#"
# Twin aliases
Set-Alias -Name tw -Value twin
Set-Alias -Name tws -Value Twin-Switch
Set-Alias -Name twc -Value Twin-Create
Set-Alias -Name twl -Value 'twin list'
Set-Alias -Name twr -Value 'twin remove'
"#
            .to_string(),
            ShellType::Fish => r#"
# Twin aliases
alias tw='twin'
alias tws='twin-switch'
alias twc='twin-create'
alias twl='twin list'
alias twr='twin remove'
"#
            .to_string(),
        }
    }
}

/// サポートされているシェルタイプ
#[allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ShellType {
    Bash,
    Zsh,
    Fish,
    PowerShell,
}

impl ShellType {
    /// 現在のシェルを検出
    pub fn detect() -> Option<Self> {
        if cfg!(target_os = "windows") {
            // Windows環境ではPowerShellをデフォルトとする
            return Some(ShellType::PowerShell);
        }

        // Unix系環境では$SHELL環境変数を確認
        if let Ok(shell) = std::env::var("SHELL") {
            if shell.contains("bash") {
                Some(ShellType::Bash)
            } else if shell.contains("zsh") {
                Some(ShellType::Zsh)
            } else if shell.contains("fish") {
                Some(ShellType::Fish)
            } else {
                // デフォルトはBash
                Some(ShellType::Bash)
            }
        } else {
            None
        }
    }

    /// シェルタイプの文字列表現
    pub fn as_str(&self) -> &str {
        match self {
            ShellType::Bash => "bash",
            ShellType::Zsh => "zsh",
            ShellType::Fish => "fish",
            ShellType::PowerShell => "powershell",
        }
    }
}