-
Notifications
You must be signed in to change notification settings - Fork 0
feat: make source optional with --source/-s and --target/-t flags #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5650a90
feat: add git module with main worktree auto-detection
km-tr 8b7be9b
feat: replace positional args with --source/-s and --target/-t flags
km-tr adf1a39
test: add porcelain parser verification test
km-tr 615b140
docs: update usage examples for flag-based CLI
km-tr 4536b2b
test: improve parse_main_worktree test to directly exercise the function
km-tr c3e1b20
fix: use default_value for target and include stderr in git error
km-tr c2e97fd
fix: detect main worktree from target directory instead of CWD
km-tr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| use anyhow::{bail, Context, Result}; | ||
| use std::fs; | ||
| use std::path::{Path, PathBuf}; | ||
| use std::process::Command; | ||
|
|
||
| /// Detect the main worktree from a specific directory by running `git worktree list --porcelain`. | ||
| /// | ||
| /// The first entry in porcelain output is always the main worktree. | ||
| /// Returns the canonicalized path of the main worktree. | ||
| pub(crate) fn detect_main_worktree_in(dir: &Path) -> Result<PathBuf> { | ||
| let output = Command::new("git") | ||
| .args(["worktree", "list", "--porcelain"]) | ||
| .current_dir(dir) | ||
| .output() | ||
| .context("Failed to run git. Use --source to specify the main worktree path.")?; | ||
|
|
||
| if !output.status.success() { | ||
| let stderr = String::from_utf8_lossy(&output.stderr); | ||
| bail!( | ||
| "Failed to detect main worktree: `git worktree list --porcelain` exited with {}.\nstderr:\n{}\nUse --source to specify the main worktree path.", | ||
| output.status, | ||
| stderr.trim_end(), | ||
| ); | ||
| } | ||
|
|
||
| let stdout = String::from_utf8_lossy(&output.stdout); | ||
| parse_main_worktree(&stdout) | ||
| } | ||
|
|
||
| /// Parse the first worktree path from `git worktree list --porcelain` output. | ||
| fn parse_main_worktree(porcelain_output: &str) -> Result<PathBuf> { | ||
| for line in porcelain_output.lines() { | ||
| if let Some(path_str) = line.strip_prefix("worktree ") { | ||
| let path = PathBuf::from(path_str); | ||
| return fs::canonicalize(&path).with_context(|| { | ||
| format!( | ||
| "Main worktree not found at: {path_str}. Use --source to specify it manually." | ||
| ) | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| bail!("Failed to detect main worktree from git output. Use --source to specify it manually.") | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use std::fs; | ||
|
|
||
| #[test] | ||
| fn detect_main_worktree_returns_main_path() { | ||
| let main_dir = git_tempdir("detect_main"); | ||
|
|
||
| // Create an initial commit so `git worktree add` works | ||
| fs::write(main_dir.join("README.md"), "# test").unwrap(); | ||
| let status = Command::new("git") | ||
| .args(["add", "."]) | ||
| .current_dir(&main_dir) | ||
| .status() | ||
| .unwrap(); | ||
| assert!(status.success()); | ||
| let status = Command::new("git") | ||
| .args(["commit", "-m", "init", "--quiet"]) | ||
| .current_dir(&main_dir) | ||
| .status() | ||
| .unwrap(); | ||
| assert!(status.success()); | ||
|
|
||
| // Add a linked worktree | ||
| let wt_dir = std::env::temp_dir().join("worktree-link-test-detect_main_wt"); | ||
| let _ = fs::remove_dir_all(&wt_dir); | ||
| let status = Command::new("git") | ||
| .args(["worktree", "add", wt_dir.to_str().unwrap(), "-b", "test-wt"]) | ||
| .current_dir(&main_dir) | ||
| .status() | ||
| .unwrap(); | ||
| assert!(status.success()); | ||
|
|
||
| // Detect from the linked worktree should return the main worktree path | ||
| let detected = detect_main_worktree_in(&wt_dir).unwrap(); | ||
| assert_eq!(detected, main_dir); | ||
|
|
||
| // Cleanup | ||
| let _ = Command::new("git") | ||
| .args(["worktree", "remove", "--force", wt_dir.to_str().unwrap()]) | ||
| .current_dir(&main_dir) | ||
| .status(); | ||
| let _ = fs::remove_dir_all(&wt_dir); | ||
| } | ||
|
|
||
| #[test] | ||
| fn detect_main_worktree_from_main_returns_self() { | ||
| let main_dir = git_tempdir("detect_self"); | ||
|
|
||
| let detected = detect_main_worktree_in(&main_dir).unwrap(); | ||
| assert_eq!(detected, main_dir); | ||
| } | ||
|
|
||
| #[test] | ||
| fn detect_main_worktree_outside_git_repo_fails() { | ||
| let dir = tempdir("detect_nogit"); | ||
|
|
||
| let result = detect_main_worktree_in(&dir); | ||
| assert!(result.is_err()); | ||
| let err_msg = result.unwrap_err().to_string(); | ||
| assert!( | ||
| err_msg.contains("--source"), | ||
| "Error message should mention --source, got: {err_msg}" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn parse_main_worktree_extracts_first_entry() { | ||
| let dir = git_tempdir("parse_first"); | ||
| let commit = Command::new("git") | ||
| .args(["commit", "--allow-empty", "-m", "init"]) | ||
| .current_dir(&dir) | ||
| .output() | ||
| .unwrap(); | ||
| assert!(commit.status.success(), "git commit failed"); | ||
|
|
||
| // Get raw porcelain output | ||
| let output = Command::new("git") | ||
| .args(["worktree", "list", "--porcelain"]) | ||
| .current_dir(&dir) | ||
| .output() | ||
| .unwrap(); | ||
| let stdout = String::from_utf8_lossy(&output.stdout); | ||
|
|
||
| // Delegate to the function under test | ||
| let parsed = parse_main_worktree(&stdout).unwrap(); | ||
| assert_eq!(parsed, dir); | ||
|
|
||
| let _ = fs::remove_dir_all(&dir); | ||
| } | ||
|
|
||
| fn git_tempdir(name: &str) -> PathBuf { | ||
| let dir = std::env::temp_dir().join(format!("worktree-link-test-{name}")); | ||
| let _ = fs::remove_dir_all(&dir); | ||
| fs::create_dir_all(&dir).unwrap(); | ||
| let status = Command::new("git") | ||
| .args(["init", "--quiet"]) | ||
| .current_dir(&dir) | ||
| .status() | ||
| .expect("git init failed"); | ||
| assert!(status.success(), "git init exited with {status}"); | ||
| // Set user config for commits | ||
| let status = Command::new("git") | ||
| .args(["config", "user.email", "test@test.com"]) | ||
| .current_dir(&dir) | ||
| .status() | ||
| .unwrap(); | ||
| assert!(status.success()); | ||
| let status = Command::new("git") | ||
| .args(["config", "user.name", "Test"]) | ||
| .current_dir(&dir) | ||
| .status() | ||
| .unwrap(); | ||
| assert!(status.success()); | ||
| // Return canonical path so comparisons work | ||
| fs::canonicalize(&dir).unwrap() | ||
| } | ||
|
|
||
| fn tempdir(name: &str) -> PathBuf { | ||
| let dir = std::env::temp_dir().join(format!("worktree-link-test-{name}")); | ||
| let _ = fs::remove_dir_all(&dir); | ||
| fs::create_dir_all(&dir).unwrap(); | ||
| fs::canonicalize(&dir).unwrap() | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.