twig-tmux 0.1.3

Tmux session manager with git worktree 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
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
use anyhow::{Context, Result};
use serde::Deserialize;
use std::fs;
use std::fs::File;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};

use crate::config::{GlobalConfig, Project};

/// Create a git worktree for a project
pub fn create_worktree(project: &Project, branch: &str) -> Result<PathBuf> {
    let config = GlobalConfig::load()?;
    let project_root = project.root_expanded();

    // Worktree path: {worktree_base}/{project}/{branch}
    let branch_safe = branch.replace('/', "-");
    let worktree_path = config
        .worktree_base_expanded()
        .join(&project.name)
        .join(&branch_safe);

    // Check if worktree already exists
    if worktree_path.exists() {
        anyhow::bail!("Worktree already exists at {:?}", worktree_path);
    }

    // Ensure parent directory exists
    if let Some(parent) = worktree_path.parent() {
        fs::create_dir_all(parent)
            .with_context(|| format!("Failed to create directory: {:?}", parent))?;
    }

    // Check if branch exists locally or remotely
    let branch_exists = check_branch_exists(&project_root, branch)?;

    // Create the worktree (suppress output to avoid breaking TUI)
    let mut cmd = Command::new("git");
    cmd.current_dir(&project_root);
    cmd.arg("worktree").arg("add");

    if branch_exists {
        // Checkout existing branch
        cmd.arg(&worktree_path).arg(branch);
    } else {
        // Create new branch from current HEAD
        cmd.arg("-b").arg(branch).arg(&worktree_path);
    }

    let output = cmd
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .output()
        .context("Failed to create git worktree")?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        anyhow::bail!("git worktree add failed: {}", stderr.trim());
    }

    // Copy files if configured
    if let Some(wt_config) = &project.worktree {
        for file in &wt_config.copy {
            let src = project_root.join(file);
            let dst = worktree_path.join(file);

            if src.exists() {
                // Create parent directories if needed
                if let Some(parent) = dst.parent() {
                    fs::create_dir_all(parent).ok();
                }

                copy_path_preserve_symlinks(&src, &dst)?;
            }
        }

        for file in &wt_config.symlink {
            let src = project_root.join(file);
            let dst = worktree_path.join(file);

            if src.exists() {
                if let Some(parent) = dst.parent() {
                    fs::create_dir_all(parent).ok();
                }

                create_symlink(&src, &dst)?;
            }
        }
    }

    Ok(worktree_path)
}

pub fn parse_pr_number(input: &str) -> Option<u64> {
    let trimmed = input.trim();
    let number = trimmed.strip_prefix('#')?;
    if number.is_empty() || !number.chars().all(|c| c.is_ascii_digit()) {
        return None;
    }
    number.parse().ok()
}

pub struct WorktreeFromPr {
    pub path: PathBuf,
    pub branch: String,
}

#[derive(Deserialize)]
struct GhPrInfo {
    #[serde(rename = "headRefName")]
    head_ref_name: String,
    #[serde(rename = "headRepository")]
    head_repository: GhRepository,
}

#[derive(Deserialize)]
struct GhRepository {
    #[serde(rename = "nameWithOwner")]
    name_with_owner: String,
}

#[derive(Deserialize)]
struct GhRepoView {
    #[serde(rename = "sshUrl")]
    ssh_url: Option<String>,
    url: Option<String>,
}

pub fn create_worktree_from_pr(project: &Project, pr_number: u64) -> Result<WorktreeFromPr> {
    let project_root = project.root_expanded();
    let pr_info = gh_pr_info(&project_root, pr_number)?;
    let repo_url = gh_repo_clone_url(&project_root, &pr_info.head_repository.name_with_owner)?;
    let branch_name = select_pr_branch_name(&project_root, pr_number, &pr_info.head_ref_name)?;

    fetch_pr_branch(&project_root, &repo_url, &pr_info.head_ref_name)?;
    create_local_branch_from_fetch(&project_root, &branch_name)?;

    let path = create_worktree(project, &branch_name)?;

    Ok(WorktreeFromPr {
        path,
        branch: branch_name,
    })
}

/// Delete a git worktree and its local branch
pub fn delete_worktree(project: &Project, branch: &str) -> Result<()> {
    let config = GlobalConfig::load()?;
    let project_root = project.root_expanded();

    let branch_safe = branch.replace('/', "-");
    let worktree_path = config
        .worktree_base_expanded()
        .join(&project.name)
        .join(&branch_safe);

    if !worktree_path.exists() {
        anyhow::bail!("Worktree does not exist at {:?}", worktree_path);
    }

    // Remove the worktree (suppress output to avoid breaking TUI)
    let output = Command::new("git")
        .current_dir(&project_root)
        .args(["worktree", "remove", "--force"])
        .arg(&worktree_path)
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .output()
        .context("Failed to remove git worktree")?;

    if !output.status.success() {
        // Try force removal of the directory
        fs::remove_dir_all(&worktree_path)
            .with_context(|| format!("Failed to remove worktree directory: {:?}", worktree_path))?;

        // Prune worktree references
        Command::new("git")
            .current_dir(&project_root)
            .args(["worktree", "prune"])
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status()
            .ok();
    }

    // Delete the local branch
    delete_local_branch(&project_root, branch)?;

    Ok(())
}

/// Delete a local git branch
fn delete_local_branch(repo_path: &Path, branch: &str) -> Result<()> {
    // Force delete the branch (-D) since the worktree is already removed
    let output = Command::new("git")
        .current_dir(repo_path)
        .args(["branch", "-D", branch])
        .output()
        .context("Failed to delete local branch")?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        // Ignore error if branch doesn't exist (may have been a remote-tracking branch)
        if !stderr.contains("not found") {
            eprintln!(
                "Warning: could not delete branch '{}': {}",
                branch,
                stderr.trim()
            );
        }
    }

    Ok(())
}

/// List worktrees for a project
pub fn list_worktrees(project: &Project) -> Result<Vec<WorktreeInfo>> {
    let config = GlobalConfig::load()?;
    let project_root = project.root_expanded();

    let output = Command::new("git")
        .current_dir(&project_root)
        .args(["worktree", "list", "--porcelain"])
        .output()
        .context("Failed to list git worktrees")?;

    if !output.status.success() {
        return Ok(vec![]);
    }

    let stdout = String::from_utf8(output.stdout)?;
    let mut worktrees = Vec::new();
    let mut current_path: Option<PathBuf> = None;
    let mut current_branch: Option<String> = None;

    let worktree_base = config.worktree_base_expanded().join(&project.name);

    for line in stdout.lines() {
        if line.starts_with("worktree ") {
            // Save previous worktree if any
            if let (Some(path), Some(branch)) = (current_path.take(), current_branch.take()) {
                // Only include worktrees under our worktree_base
                if path.starts_with(&worktree_base) {
                    worktrees.push(WorktreeInfo { path, branch });
                }
            }

            current_path = Some(PathBuf::from(line.strip_prefix("worktree ").unwrap()));
        } else if line.starts_with("branch ") {
            let branch = line
                .strip_prefix("branch refs/heads/")
                .unwrap_or(line.strip_prefix("branch ").unwrap_or(""));
            current_branch = Some(branch.to_string());
        }
    }

    // Don't forget the last one
    if let (Some(path), Some(branch)) = (current_path, current_branch) {
        if path.starts_with(&worktree_base) {
            worktrees.push(WorktreeInfo { path, branch });
        }
    }

    Ok(worktrees)
}

#[derive(Debug)]
pub struct WorktreeInfo {
    pub path: PathBuf,
    pub branch: String,
}

/// Check if a branch exists (locally or remotely)
fn check_branch_exists(repo_path: &Path, branch: &str) -> Result<bool> {
    // Check local branches
    let local = Command::new("git")
        .current_dir(repo_path)
        .args(["rev-parse", "--verify", branch])
        .output()?;

    if local.status.success() {
        return Ok(true);
    }

    // Check remote branches
    let remote = Command::new("git")
        .current_dir(repo_path)
        .args(["rev-parse", "--verify", &format!("origin/{}", branch)])
        .output()?;

    Ok(remote.status.success())
}

fn gh_pr_info(repo_path: &Path, pr_number: u64) -> Result<GhPrInfo> {
    let output = Command::new("gh")
        .current_dir(repo_path)
        .args([
            "pr",
            "view",
            &pr_number.to_string(),
            "--json",
            "headRefName,headRepository",
        ])
        .output()
        .context("Failed to run gh pr view")?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        anyhow::bail!("gh pr view failed: {}", stderr.trim());
    }

    let info: GhPrInfo =
        serde_json::from_slice(&output.stdout).context("Failed to parse gh pr view output")?;
    Ok(info)
}

fn gh_repo_clone_url(repo_path: &Path, name_with_owner: &str) -> Result<String> {
    let output = Command::new("gh")
        .current_dir(repo_path)
        .args(["repo", "view", name_with_owner, "--json", "sshUrl,url"])
        .output()
        .context("Failed to run gh repo view")?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        anyhow::bail!("gh repo view failed: {}", stderr.trim());
    }

    let info: GhRepoView =
        serde_json::from_slice(&output.stdout).context("Failed to parse gh repo view output")?;
    if let Some(url) = info.ssh_url.or(info.url) {
        if !url.is_empty() {
            return Ok(url);
        }
    }

    anyhow::bail!("gh repo view returned empty clone url")
}

fn select_pr_branch_name(repo_path: &Path, pr_number: u64, head_ref_name: &str) -> Result<String> {
    let primary = format!("pr-{}", pr_number);
    if !check_branch_exists(repo_path, &primary)? {
        return Ok(primary);
    }

    let base = format!("pr-{}-{}", pr_number, head_ref_name);
    if !check_branch_exists(repo_path, &base)? {
        return Ok(base);
    }

    for idx in 2..=50 {
        let candidate = format!("{}-{}", base, idx);
        if !check_branch_exists(repo_path, &candidate)? {
            return Ok(candidate);
        }
    }

    anyhow::bail!("Unable to find available branch name for PR #{}", pr_number)
}

fn fetch_pr_branch(repo_path: &Path, repo_url: &str, head_ref_name: &str) -> Result<()> {
    let output = Command::new("git")
        .current_dir(repo_path)
        .args(["fetch", repo_url, head_ref_name])
        .output()
        .context("Failed to fetch PR branch")?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        anyhow::bail!("git fetch failed: {}", stderr.trim());
    }

    Ok(())
}

fn create_local_branch_from_fetch(repo_path: &Path, branch_name: &str) -> Result<()> {
    let output = Command::new("git")
        .current_dir(repo_path)
        .args(["branch", branch_name, "FETCH_HEAD"])
        .output()
        .context("Failed to create local branch from fetched PR")?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        anyhow::bail!("git branch failed: {}", stderr.trim());
    }

    Ok(())
}

/// Get the default branch (main or master) for a repository
pub fn get_default_branch(repo_path: &Path) -> Result<String> {
    // Try to get from remote HEAD
    let output = Command::new("git")
        .current_dir(repo_path)
        .args(["symbolic-ref", "refs/remotes/origin/HEAD", "--short"])
        .output()
        .context("Failed to get default branch")?;

    if output.status.success() {
        let branch = String::from_utf8_lossy(&output.stdout)
            .trim()
            .strip_prefix("origin/")
            .unwrap_or("main")
            .to_string();
        return Ok(branch);
    }

    // Fallback: check if main or master exists
    for branch in ["main", "master"] {
        let status = Command::new("git")
            .current_dir(repo_path)
            .args(["rev-parse", "--verify", branch])
            .output()?;

        if status.status.success() {
            return Ok(branch.to_string());
        }
    }

    Ok("main".to_string())
}

/// Merge a branch into the default branch (main/master)
pub fn merge_branch_to_default(repo_path: &Path, branch: &str) -> Result<()> {
    let default_branch = get_default_branch(repo_path)?;

    // Checkout default branch (suppress output to avoid breaking TUI)
    let output = Command::new("git")
        .current_dir(repo_path)
        .args(["checkout", &default_branch])
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .output()
        .context("Failed to checkout default branch")?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        anyhow::bail!("Failed to checkout '{}': {}", default_branch, stderr.trim());
    }

    // Merge the branch (suppress output to avoid breaking TUI)
    let output = Command::new("git")
        .current_dir(repo_path)
        .args(["merge", branch])
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .output()
        .context("Failed to merge branch")?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        anyhow::bail!(
            "Merge failed: {}. Please resolve conflicts manually in the main repository.",
            stderr.trim()
        );
    }

    Ok(())
}

/// Copy a file or directory, preserving symlinks
fn copy_path_preserve_symlinks(src: &Path, dst: &Path) -> Result<()> {
    let metadata = fs::symlink_metadata(src)
        .with_context(|| format!("Failed to read metadata for {:?}", src))?;

    if metadata.file_type().is_symlink() {
        let target = fs::read_link(src)
            .with_context(|| format!("Failed to read symlink target for {:?}", src))?;
        create_symlink(&target, dst)?;
        return Ok(());
    }

    if metadata.is_dir() {
        copy_dir_recursive(src, dst)?;
    } else {
        fs::copy(src, dst).with_context(|| format!("Failed to copy {:?} to {:?}", src, dst))?;

        // Ensure the file is fully synced to disk before returning
        // This prevents race conditions where the file appears corrupted
        // to processes that read it immediately after copying
        let file = File::open(dst)
            .with_context(|| format!("Failed to open copied file for sync: {:?}", dst))?;
        file.sync_all()
            .with_context(|| format!("Failed to sync copied file to disk: {:?}", dst))?;
    }

    Ok(())
}

/// Recursively copy a directory, preserving symlinks
fn copy_dir_recursive(src: &Path, dst: &Path) -> Result<()> {
    fs::create_dir_all(dst)?;

    for entry in fs::read_dir(src)? {
        let entry = entry?;
        let src_path = entry.path();
        let dst_path = dst.join(entry.file_name());

        copy_path_preserve_symlinks(&src_path, &dst_path)?;
    }

    Ok(())
}

#[cfg(unix)]
fn create_symlink(target: &Path, link: &Path) -> Result<()> {
    use std::os::unix::fs::symlink;

    symlink(target, link)
        .with_context(|| format!("Failed to create symlink {:?} -> {:?}", link, target))
}

#[cfg(not(unix))]
fn create_symlink(_target: &Path, _link: &Path) -> Result<()> {
    anyhow::bail!("Symlink copying is only supported on Unix systems")
}

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

    #[test]
    fn test_parse_pr_number() {
        assert_eq!(parse_pr_number("#123"), Some(123));
        assert_eq!(parse_pr_number(" #42 "), Some(42));
        assert_eq!(parse_pr_number("#"), None);
        assert_eq!(parse_pr_number("#abc"), None);
        assert_eq!(parse_pr_number("123"), None);
    }
}