git-worktree-manager 0.0.31

CLI tool integrating git worktree with AI coding assistants
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
/// Auto-update check and self-upgrade via GitHub Releases.
///
use std::io::IsTerminal;
use std::path::PathBuf;
use std::process::Command;

use console::style;
use serde::{Deserialize, Serialize};

use crate::constants::home_dir_or_fallback;

const CURRENT_VERSION: &str = env!("CARGO_PKG_VERSION");
const REPO_OWNER: &str = "DaveDev42";
const REPO_NAME: &str = "git-worktree-manager";

/// How often to check for updates (in seconds). Default: 6 hours.
const CHECK_INTERVAL_SECS: u64 = 6 * 60 * 60;

/// Cache for update check results.
#[derive(Debug, Serialize, Deserialize, Default)]
struct UpdateCache {
    /// Unix timestamp of last check.
    #[serde(default)]
    last_check_ts: u64,
    /// Legacy date string (for backward compat).
    #[serde(default)]
    last_check: String,
    latest_version: Option<String>,
}

fn get_cache_path() -> PathBuf {
    dirs::cache_dir()
        .unwrap_or_else(home_dir_or_fallback)
        .join("git-worktree-manager")
        .join("update_check.json")
}

fn load_cache() -> UpdateCache {
    let path = get_cache_path();
    if !path.exists() {
        return UpdateCache::default();
    }
    std::fs::read_to_string(&path)
        .ok()
        .and_then(|c| serde_json::from_str(&c).ok())
        .unwrap_or_default()
}

fn save_cache(cache: &UpdateCache) {
    let path = get_cache_path();
    if let Some(parent) = path.parent() {
        let _ = std::fs::create_dir_all(parent);
    }
    if let Ok(content) = serde_json::to_string_pretty(cache) {
        let _ = std::fs::write(&path, content);
    }
}

fn now_ts() -> u64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0)
}

fn cache_is_fresh(cache: &UpdateCache) -> bool {
    let age = now_ts().saturating_sub(cache.last_check_ts);
    age < CHECK_INTERVAL_SECS
}

/// Check for updates (called on startup).
///
/// Phase 1 (instant, no I/O): show notification from cache if newer version known.
/// Phase 2 (background): if cache is stale, fork a background process to refresh it.
pub fn check_for_update_if_needed() {
    let config = crate::config::load_config().unwrap_or_default();
    if !config.update.auto_check {
        return;
    }

    let cache = load_cache();

    // Phase 1: instant notification from cache (zero latency)
    if let Some(ref latest) = cache.latest_version {
        if is_newer(latest, CURRENT_VERSION) {
            eprintln!(
                "\n{} {} is available (current: {})",
                style("git-worktree-manager").bold(),
                style(format!("v{}", latest)).green(),
                style(format!("v{}", CURRENT_VERSION)).dim(),
            );
            eprintln!("Run '{}' to update.\n", style("gw upgrade").cyan().bold());
        }
    }

    // Phase 2: if cache is stale, refresh in background
    if !cache_is_fresh(&cache) {
        spawn_background_check();
    }
}

/// Spawn a background process to check for updates without blocking startup.
fn spawn_background_check() {
    let exe = match std::env::current_exe() {
        Ok(p) => p,
        Err(_) => return,
    };
    // Use a hidden subcommand to do the actual check
    let _ = Command::new(exe)
        .arg("_update-cache")
        .stdin(std::process::Stdio::null())
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .spawn();
}

/// Refresh the update cache (called by background process).
pub fn refresh_cache() {
    if let Some(latest) = fetch_latest_version() {
        let cache = UpdateCache {
            last_check_ts: now_ts(),
            latest_version: Some(latest),
            ..Default::default()
        };
        save_cache(&cache);
    } else {
        // Save timestamp even on failure to avoid hammering
        let cache = UpdateCache {
            last_check_ts: now_ts(),
            latest_version: load_cache().latest_version, // keep previous
            ..Default::default()
        };
        save_cache(&cache);
    }
}

/// Get a GitHub auth token if available.
/// Checks GITHUB_TOKEN env var first, then falls back to `gh auth token`.
fn gh_auth_token() -> Option<String> {
    // 1. Environment variable (fast, no subprocess)
    if let Ok(token) = std::env::var("GITHUB_TOKEN") {
        if !token.is_empty() {
            return Some(token);
        }
    }
    if let Ok(token) = std::env::var("GH_TOKEN") {
        if !token.is_empty() {
            return Some(token);
        }
    }

    // 2. gh CLI (only if binary exists)
    if which_exists("gh") {
        return Command::new("gh")
            .args(["auth", "token"])
            .stdin(std::process::Stdio::null())
            .stderr(std::process::Stdio::null())
            .output()
            .ok()
            .filter(|o| o.status.success())
            .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
            .filter(|t| !t.is_empty());
    }

    None
}

/// Check if a command exists in PATH without running it.
fn which_exists(cmd: &str) -> bool {
    std::env::var_os("PATH")
        .map(|paths| {
            std::env::split_paths(&paths).any(|dir| {
                let full = dir.join(cmd);
                full.is_file() || (cfg!(windows) && dir.join(format!("{}.exe", cmd)).is_file())
            })
        })
        .unwrap_or(false)
}

/// Fetch latest version string from GitHub Releases API.
/// Uses gh auth token if available to avoid unauthenticated rate limits (60/hr).
fn fetch_latest_version() -> Option<String> {
    if !which_exists("curl") {
        return None;
    }
    let url = format!(
        "https://api.github.com/repos/{}/{}/releases/latest",
        REPO_OWNER, REPO_NAME
    );

    let mut args = vec![
        "-s".to_string(),
        "--fail".to_string(),
        "--max-time".to_string(),
        "10".to_string(),
        "-H".to_string(),
        format!("User-Agent: gw/{}", CURRENT_VERSION),
        "-H".to_string(),
        "Accept: application/vnd.github+json".to_string(),
    ];

    if let Some(token) = gh_auth_token() {
        args.push("-H".to_string());
        args.push(format!("Authorization: Bearer {}", token));
    }

    args.push(url);

    let output = Command::new("curl").args(&args).output().ok()?;

    if !output.status.success() {
        return None;
    }

    let body = String::from_utf8_lossy(&output.stdout);
    let json: serde_json::Value = serde_json::from_str(&body).ok()?;
    let tag = json.get("tag_name")?.as_str()?;

    // Strip tag prefix: "v0.0.3" → "0.0.3"
    Some(tag.strip_prefix('v').unwrap_or(tag).to_string())
}

/// Compare version strings (simple semver).
fn is_newer(latest: &str, current: &str) -> bool {
    let parse = |s: &str| -> Vec<u32> { s.split('.').filter_map(|p| p.parse().ok()).collect() };
    let l = parse(latest);
    let c = parse(current);
    l > c
}

/// Detect if the binary was installed via Homebrew.
fn is_homebrew_install() -> bool {
    let exe = match std::env::current_exe() {
        Ok(p) => p,
        Err(_) => return false,
    };
    let real_path = match std::fs::canonicalize(&exe) {
        Ok(p) => p,
        Err(_) => exe,
    };
    let path_str = real_path.to_string_lossy();
    path_str.contains("/Cellar/") || path_str.contains("/homebrew/")
}

/// Determine the current platform target triple.
fn current_target() -> &'static str {
    #[cfg(all(target_arch = "x86_64", target_os = "macos"))]
    {
        "x86_64-apple-darwin"
    }
    #[cfg(all(target_arch = "aarch64", target_os = "macos"))]
    {
        "aarch64-apple-darwin"
    }
    #[cfg(all(target_arch = "x86_64", target_os = "windows"))]
    {
        "x86_64-pc-windows-msvc"
    }
    #[cfg(all(target_arch = "x86_64", target_os = "linux"))]
    {
        "x86_64-unknown-linux-musl"
    }
    #[cfg(all(target_arch = "aarch64", target_os = "linux"))]
    {
        "aarch64-unknown-linux-musl"
    }
}

/// Archive extension for the current platform.
fn archive_ext() -> &'static str {
    if cfg!(windows) {
        "zip"
    } else {
        "tar.gz"
    }
}

/// Download the release asset and extract the binary to a temp file.
/// Returns the path to the extracted binary.
fn download_and_extract(version: &str) -> Result<PathBuf, String> {
    if !which_exists("curl") {
        return Err("curl is required for gw upgrade but was not found in PATH".to_string());
    }
    let target = current_target();
    let asset_name = format!("gw-{}.{}", target, archive_ext());
    let url = format!(
        "https://github.com/{}/{}/releases/download/v{}/{}",
        REPO_OWNER, REPO_NAME, version, asset_name
    );

    // Download archive using curl (uses system TLS, works with MDM/proxy certs)
    let tmp_dir = tempfile::tempdir().map_err(|e| format!("Failed to create temp dir: {}", e))?;
    let archive_path = tmp_dir.path().join(&asset_name);

    let user_agent = format!("User-Agent: gw/{}", CURRENT_VERSION);
    let archive_path_str = archive_path.to_string_lossy().to_string();
    let token = gh_auth_token();
    let auth_header = token
        .as_ref()
        .map(|t| format!("Authorization: Bearer {}", t));

    let progress_flag = if std::io::stderr().is_terminal() {
        "--progress-bar"
    } else {
        "-sS" // silent + show errors (no noisy escape sequences in CI/pipes)
    };

    let mut args = vec![
        "-L",     // follow redirects (GitHub → CDN)
        "--fail", // exit non-zero on HTTP errors
        progress_flag,
        "--max-time",
        "300",
        "-H",
        &user_agent,
        "-o",
        &archive_path_str,
    ];

    if let Some(ref h) = auth_header {
        args.push("-H");
        args.push(h);
    }

    args.push(&url);

    let status = Command::new("curl")
        .args(&args)
        .stdin(std::process::Stdio::null())
        .status()
        .map_err(|e| format!("Failed to run curl: {}", e))?;

    if !status.success() {
        return Err(format!(
            "Download failed: curl exited with {} for {}",
            status
                .code()
                .map_or("signal".to_string(), |c| c.to_string()),
            asset_name
        ));
    }

    let downloaded = std::fs::read(&archive_path)
        .map_err(|e| format!("Failed to read downloaded archive: {}", e))?;
    let bin_name = if cfg!(windows) { "gw.exe" } else { "gw" };

    if cfg!(windows) {
        extract_zip(&downloaded, tmp_dir.path(), bin_name)?;
    } else {
        extract_tar_gz(&downloaded, tmp_dir.path(), bin_name)?;
    }

    let extracted_bin = tmp_dir.path().join(bin_name);
    if !extracted_bin.exists() {
        return Err(format!(
            "Binary '{}' not found in archive. Contents may have unexpected layout.",
            bin_name
        ));
    }

    // Move to a persistent temp file (tempdir would delete on drop)
    let persistent_path = std::env::temp_dir().join(format!("gw-update-{}", version));
    std::fs::copy(&extracted_bin, &persistent_path)
        .map_err(|e| format!("Failed to copy binary: {}", e))?;

    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let _ = std::fs::set_permissions(&persistent_path, std::fs::Permissions::from_mode(0o755));
    }

    Ok(persistent_path)
}

/// Extract a tar.gz archive and find the binary.
#[cfg(not(windows))]
fn extract_tar_gz(data: &[u8], dest: &std::path::Path, bin_name: &str) -> Result<(), String> {
    let gz = flate2::read::GzDecoder::new(data);
    let mut archive = tar::Archive::new(gz);

    for entry in archive.entries().map_err(|e| format!("tar error: {}", e))? {
        let mut entry = entry.map_err(|e| format!("tar entry error: {}", e))?;
        let path = entry
            .path()
            .map_err(|e| format!("tar path error: {}", e))?
            .into_owned();

        // Extract only the binary (may be at root or in a subdirectory)
        if path.file_name().and_then(|n| n.to_str()) == Some(bin_name) {
            let out_path = dest.join(bin_name);
            let mut out_file = std::fs::File::create(&out_path)
                .map_err(|e| format!("Failed to create file: {}", e))?;
            std::io::copy(&mut entry, &mut out_file)
                .map_err(|e| format!("Failed to write file: {}", e))?;
            return Ok(());
        }
    }
    Err(format!("'{}' not found in tar.gz archive", bin_name))
}

/// Extract a zip archive and find the binary.
#[cfg(windows)]
fn extract_zip(data: &[u8], dest: &std::path::Path, bin_name: &str) -> Result<(), String> {
    let cursor = std::io::Cursor::new(data);
    let mut archive = zip::ZipArchive::new(cursor).map_err(|e| format!("zip error: {}", e))?;

    for i in 0..archive.len() {
        let mut file = archive
            .by_index(i)
            .map_err(|e| format!("zip entry error: {}", e))?;
        let name = file.name().to_string();

        if name.ends_with(bin_name)
            || std::path::Path::new(&name)
                .file_name()
                .and_then(|n| n.to_str())
                == Some(bin_name)
        {
            let out_path = dest.join(bin_name);
            let mut out_file = std::fs::File::create(&out_path)
                .map_err(|e| format!("Failed to create file: {}", e))?;
            std::io::copy(&mut file, &mut out_file)
                .map_err(|e| format!("Failed to write file: {}", e))?;
            return Ok(());
        }
    }
    Err(format!("'{}' not found in zip archive", bin_name))
}

// Provide stub functions for platforms where the other archive format isn't used,
// so the code compiles on all targets (they're never called).
#[cfg(windows)]
fn extract_tar_gz(_data: &[u8], _dest: &std::path::Path, _bin_name: &str) -> Result<(), String> {
    Err("tar.gz extraction not supported on Windows".to_string())
}

#[cfg(not(windows))]
fn extract_zip(_data: &[u8], _dest: &std::path::Path, _bin_name: &str) -> Result<(), String> {
    Err("zip extraction not used on Unix".to_string())
}

/// Manual upgrade command — downloads and installs the latest version.
pub fn upgrade() {
    println!("git-worktree-manager v{}", CURRENT_VERSION);

    // Check for Homebrew installation
    if is_homebrew_install() {
        println!(
            "{}",
            style("Installed via Homebrew. Use brew to upgrade:").yellow()
        );
        println!("  brew upgrade git-worktree-manager");
        return;
    }

    let latest_version = match fetch_latest_version() {
        Some(v) => v,
        None => {
            let msg = if which_exists("curl") {
                "Could not check for updates. Check your internet connection."
            } else {
                "Could not check for updates. curl is required but was not found in PATH."
            };
            println!("{}", style(msg).red());
            return;
        }
    };

    // Update cache with fresh data
    let cache = UpdateCache {
        last_check_ts: now_ts(),
        latest_version: Some(latest_version.clone()),
        ..Default::default()
    };
    save_cache(&cache);

    if !is_newer(&latest_version, CURRENT_VERSION) {
        println!("{}", style("Already up to date.").green());
        return;
    }

    println!(
        "New version available: {}{}",
        style(format!("v{}", CURRENT_VERSION)).dim(),
        style(format!("v{}", latest_version)).green().bold()
    );

    // Non-interactive: just print the info
    if !std::io::stdin().is_terminal() {
        println!(
            "Download from: https://github.com/{}/{}/releases/latest",
            REPO_OWNER, REPO_NAME
        );
        return;
    }

    // Prompt user
    let confirm = dialoguer::Confirm::new()
        .with_prompt("Upgrade now?")
        .default(true)
        .interact()
        .unwrap_or(false);

    if !confirm {
        println!("Upgrade cancelled.");
        return;
    }

    println!("Downloading v{}...", latest_version);

    match download_and_extract(&latest_version) {
        Ok(new_binary) => {
            // Update companion (cw↔gw) BEFORE self_replace, using the downloaded
            // binary directly. This ensures both binaries get the new version even
            // when upgrading from old versions that had a broken companion update.
            update_companion_from(&new_binary);

            // Replace the running binary
            if let Err(e) = self_replace::self_replace(&new_binary) {
                println!(
                    "{}",
                    style(format!("Failed to replace binary: {}", e)).red()
                );
                println!(
                    "Download manually: https://github.com/{}/{}/releases/latest",
                    REPO_OWNER, REPO_NAME
                );
                let _ = std::fs::remove_file(&new_binary);
                return;
            }

            // Clean up temp file
            let _ = std::fs::remove_file(&new_binary);

            println!(
                "{}",
                style(format!("Upgraded to v{}!", latest_version))
                    .green()
                    .bold()
            );
        }
        Err(e) => {
            println!("{}", style(format!("Upgrade failed: {}", e)).red());
            println!(
                "Download manually: https://github.com/{}/{}/releases/latest",
                REPO_OWNER, REPO_NAME
            );
        }
    }
}

/// Update the companion binary (`cw` or `gw`) from the downloaded new binary.
///
/// Copies `new_binary` directly to the companion path, so both `gw` and `cw`
/// get the new version regardless of which one is currently running.
/// Called BEFORE `self_replace` to avoid depending on already-replaced binary state.
fn update_companion_from(new_binary: &std::path::Path) {
    let current_exe = match std::env::current_exe() {
        Ok(p) => p,
        Err(_) => return,
    };
    let bin_dir = match current_exe.parent() {
        Some(d) => d,
        None => return,
    };

    let bin_ext = if cfg!(windows) { ".exe" } else { "" };
    let exe_name = current_exe
        .file_stem()
        .and_then(|n| n.to_str())
        .unwrap_or("gw");

    // Determine companion: gw↔cw
    let companion_name = if exe_name == "cw" { "gw" } else { "cw" };
    let companion_path = bin_dir.join(format!("{}{}", companion_name, bin_ext));

    if companion_path.exists() {
        let _ = std::fs::copy(new_binary, &companion_path);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_is_newer() {
        assert!(is_newer("0.2.0", "0.1.0"));
        assert!(is_newer("1.0.0", "0.10.0"));
        assert!(!is_newer("0.1.0", "0.1.0"));
        assert!(!is_newer("0.1.0", "0.2.0"));
    }

    #[test]
    fn test_is_homebrew_install() {
        assert!(!is_homebrew_install());
    }

    #[test]
    fn test_cache_freshness() {
        let fresh = UpdateCache {
            last_check_ts: now_ts(),
            latest_version: Some("1.0.0".into()),
            ..Default::default()
        };
        assert!(cache_is_fresh(&fresh));

        let stale = UpdateCache {
            last_check_ts: now_ts() - CHECK_INTERVAL_SECS - 1,
            latest_version: Some("1.0.0".into()),
            ..Default::default()
        };
        assert!(!cache_is_fresh(&stale));

        let empty = UpdateCache::default();
        assert!(!cache_is_fresh(&empty));
    }

    #[test]
    fn test_current_target() {
        let target = current_target();
        assert!(!target.is_empty());
        // Should match one of our supported targets
        let valid = [
            "x86_64-apple-darwin",
            "aarch64-apple-darwin",
            "x86_64-pc-windows-msvc",
            "x86_64-unknown-linux-musl",
            "aarch64-unknown-linux-musl",
        ];
        assert!(valid.contains(&target));
    }

    #[test]
    fn test_archive_ext() {
        let ext = archive_ext();
        if cfg!(windows) {
            assert_eq!(ext, "zip");
        } else {
            assert_eq!(ext, "tar.gz");
        }
    }
}