Skip to content

fix: resolve --file paths from subdirectories#2910

Open
lawrence3699 wants to merge 1 commit intogitui-org:masterfrom
lawrence3699:fix/file-flag-subdir
Open

fix: resolve --file paths from subdirectories#2910
lawrence3699 wants to merge 1 commit intogitui-org:masterfrom
lawrence3699:fix/file-flag-subdir

Conversation

@lawrence3699
Copy link
Copy Markdown

Fixes #2871

Before this change, --file only resolved reliably when GitUI was started from the repository root. From a subdirectory, a cwd-relative path like src/index.html normalized to ./src/index.html instead of the repo-relative ./frontend/src/index.html, and a repo-root-relative path like frontend/src/index.html failed 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 --file work 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

Copilot AI review requested due to automatic review settings April 19, 2026 02:51
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 --file canonicalization/strip-prefix logic with a dedicated resolve_initial_select_file* helper.
  • Resolve relative --file inputs against both cwd and 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 thread src/app.rs
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");
Copy link

Copilot AI Apr 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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");

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flag --file not working on sub directories

2 participants