fix: resolve --file paths from subdirectories#2910
Open
lawrence3699 wants to merge 1 commit intogitui-org:masterfrom
Open
fix: resolve --file paths from subdirectories#2910lawrence3699 wants to merge 1 commit intogitui-org:masterfrom
lawrence3699 wants to merge 1 commit intogitui-org:masterfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes --file path resolution when GitUI is launched from a subdirectory by resolving relative paths against both the current working directory and the repository root, then converting the result back into a repo-relative path for initial selection.
Changes:
- Replace inline
--filecanonicalization/strip-prefix logic with a dedicatedresolve_initial_select_file*helper. - Resolve relative
--fileinputs against bothcwdand repo root before selecting the file. - Add unit tests covering repo-root invocation and subdirectory invocation with both cwd-relative and repo-relative inputs.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+1273
to
+1284
| use std::{fs, path::Path, process::Command}; | ||
| use tempfile::TempDir; | ||
|
|
||
| fn make_repo_with_nested_file() -> (TempDir, RepoPath) { | ||
| let temp_dir = TempDir::new().expect("create temp repo"); | ||
| let status = Command::new("git") | ||
| .arg("init") | ||
| .arg("-q") | ||
| .arg(temp_dir.path()) | ||
| .status() | ||
| .expect("run git init"); | ||
| assert!(status.success(), "git init should succeed"); |
There was a problem hiding this comment.
The new unit tests spawn the git binary (Command::new("git")) to initialize a repo. If the project tries to keep unit tests runnable without requiring an external git executable on PATH, consider using the existing git2-testing helper (used in src/gitui.rs tests) or git2::Repository::init instead.
Suggested change
| use std::{fs, path::Path, process::Command}; | |
| use tempfile::TempDir; | |
| fn make_repo_with_nested_file() -> (TempDir, RepoPath) { | |
| let temp_dir = TempDir::new().expect("create temp repo"); | |
| let status = Command::new("git") | |
| .arg("init") | |
| .arg("-q") | |
| .arg(temp_dir.path()) | |
| .status() | |
| .expect("run git init"); | |
| assert!(status.success(), "git init should succeed"); | |
| use std::{fs, path::Path}; | |
| use tempfile::TempDir; | |
| fn make_repo_with_nested_file() -> (TempDir, RepoPath) { | |
| let temp_dir = TempDir::new().expect("create temp repo"); | |
| git2::Repository::init(temp_dir.path()) | |
| .expect("initialize git repository"); |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #2871
Before this change,
--fileonly resolved reliably when GitUI was started from the repository root. From a subdirectory, a cwd-relative path likesrc/index.htmlnormalized to./src/index.htmlinstead of the repo-relative./frontend/src/index.html, and a repo-root-relative path likefrontend/src/index.htmlfailed to resolve at all.This keeps the existing repo-relative selection flow, but resolves relative paths against both the current directory and the repository root before converting them back to a repo-relative path. That makes
--filework from subdirectories for both path styles without changing the rest of the Files tab behavior.Validation:
cargo test 'app::tests::resolve_initial_select_file_from_repo_root' -- --exact\n-cargo test 'app::tests::resolve_initial_select_file_from_subdir_cwd_relative_path' -- --exact\n-cargo test 'app::tests::resolve_initial_select_file_from_subdir_repo_relative_path' -- --exact\n-cargo test 'gitui::tests::gitui_starts' -- --exact\n-cargo fmt --check\n-cargo clippy --bin gitui -- -D warnings