Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 25 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,32 +115,40 @@ pub fn run(config: Arc<Config>, args: &cli::Args, term: &mut Term) -> Res<()> {

fn open_repo(dir: &Path) -> Res<Repository> {
log::debug!("Opening repo");
let repo = open_repo_from_env()?;
let mut repo = Repository::open(dir).map_err(Error::OpenRepo)?;
Copy link
Copy Markdown
Contributor Author

@kamilwaz kamilwaz Apr 13, 2026

Choose a reason for hiding this comment

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

I replaced open_from_env, as GIT_DIR should already be handled by git rev-parse --show-toplevel.


if repo.is_worktree() {
repo = Repository::open(repo.commondir()).map_err(Error::OpenRepo)?;
}

repo.set_workdir(dir, false).map_err(Error::OpenRepo)?;
Ok(repo)
}

fn find_git_dir() -> Res<PathBuf> {
log::debug!("Finding git dir");

let output = Command::new("git")
.args(["rev-parse", "--show-toplevel"])
.output()
.map_err(Error::FindGitDir)?;

let status = output.status;

if !status.success() {
return Err(Error::CmdBadExit(
String::from_utf8(output.stderr).unwrap(),
status.code(),
));
};

let dir = PathBuf::from(
String::from_utf8(
Command::new("git")
.args(["rev-parse", "--show-toplevel"])
.output()
.map_err(Error::FindGitDir)?
.stdout,
)
.map_err(Error::GitDirUtf8)?
.trim_end(),
String::from_utf8(output.stdout)
.map_err(Error::GitDirUtf8)?
.trim_end(),
);
Ok(dir)
}

fn open_repo_from_env() -> Res<Repository> {
match Repository::open_from_env() {
Ok(repo) => Ok(repo),
Err(err) => Err(Error::OpenRepo(err)),
}
Ok(dir)
}

fn keys_to_events(keys: &[(KeyModifiers, KeyCode)]) -> Vec<Event> {
Expand Down
7 changes: 4 additions & 3 deletions src/tests/helpers/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ use crate::{
config::{self, Config},
error::Error,
key_parser::parse_test_keys,
open_repo,
term::{Term, TermBackend},
tests::helpers::RepoTestContext,
};
use crossterm::event::{Event, KeyEvent, KeyModifiers, MouseButton, MouseEventKind};
use git2::Repository;
use ratatui::{Terminal, backend::TestBackend, layout::Size};
use regex::Regex;
use std::{path::PathBuf, rc::Rc, sync::Arc, time::Duration};
use std::path::PathBuf;
use std::{rc::Rc, sync::Arc, time::Duration};

use self::buffer::TestBuffer;

Expand Down Expand Up @@ -69,7 +70,7 @@ impl TestContext {

pub fn init_app_at_path(&mut self, path: PathBuf) -> App {
let mut app = App::create(
Rc::new(Repository::open(path).unwrap()),
Rc::new(open_repo(&path).unwrap()),
self.size,
&Args::default(),
Arc::clone(&self.config),
Expand Down
36 changes: 36 additions & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,42 @@ fn inside_submodule() {
insta::assert_snapshot!(ctx.redact_buffer());
}

#[test]
fn inside_shallow() {
let mut ctx = setup_clone!();
let url = Url::from_file_path(&ctx.remote_dir).unwrap().to_string();
run(&ctx.dir, &["git", "clone", "--depth=1", &url, "shallow"]);

let _app = ctx.init_app_at_path(ctx.dir.join("shallow"));
insta::assert_snapshot!(ctx.redact_buffer());
}

#[test]
fn inside_worktree() {
let mut ctx = setup_clone!();
run(&ctx.dir, &["git", "checkout", "-b", "new-branch"]);
run(&ctx.dir, &["git", "worktree", "add", "main"]);

let _app = ctx.init_app_at_path(ctx.dir.join("main"));
insta::assert_snapshot!(ctx.redact_buffer());
}

#[test]
fn inside_shallow_worktree() {
let mut ctx = setup_clone!();
clone_and_commit(&ctx.remote_dir, "new-file", "hello");

let url = Url::from_file_path(&ctx.remote_dir).unwrap().to_string();
run(&ctx.dir, &["git", "clone", "--depth=1", &url, "shallow"]);

let shallow_dir = ctx.dir.join("shallow");
run(&shallow_dir, &["git", "checkout", "-b", "new-branch"]);
run(&shallow_dir, &["git", "worktree", "add", "main"]);

let _app = ctx.init_app_at_path(shallow_dir.join("main"));
insta::assert_snapshot!(ctx.redact_buffer());
}

#[test]
fn syntax_highlighted() {
let ctx = setup_clone!();
Expand Down
25 changes: 25 additions & 0 deletions src/tests/snapshots/gitu__tests__inside_shallow.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
source: src/tests/mod.rs
expression: ctx.redact_buffer()
---
▌On branch main |
▌Your branch is up to date with 'origin/main'. |
|
Recent commits |
b66a0bf main origin/main add initial-file |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
styles_hash: f47a6512af0aca26
25 changes: 25 additions & 0 deletions src/tests/snapshots/gitu__tests__inside_shallow_worktree.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
source: src/tests/mod.rs
expression: ctx.redact_buffer()
---
▌On branch main |
▌Your branch is up to date with 'origin/main'. |
|
Recent commits |
46c81ca main new-branch origin/main add new-file |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
styles_hash: 4d5a9a1a08dc1d60
25 changes: 25 additions & 0 deletions src/tests/snapshots/gitu__tests__inside_worktree.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
source: src/tests/mod.rs
expression: ctx.redact_buffer()
---
▌On branch main |
▌Your branch is up to date with 'origin/main'. |
|
Recent commits |
b66a0bf main new-branch origin/main add initial-file |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
styles_hash: 4d5a9a1a08dc1d60