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.
This pull request fixes #931 and fixes #442 by ensuring the independence of nearly all library functions from the current working directory (cwd).
Specifically, this pertains to functions that directly or indirectly use cwd, which can change anytime, either manually by the user or automatically through plugins. Cwd dependent functions make it difficult to reason about the state of Neogit and potentially lead to numerous bugs. Throughout the codebase, you can observe various attempts to circumvent this issue, often involving the tracking of the cwd via
state.cwd
.Notably, all functions in
neogit.lib.git.cli
are dependent on the cwd, withrequire("neogit.lib.git.cli").git_root()
being a noteworthy example.This pull request aims to eliminate all cwd dependencies by designating
require("neogit.lib.git.repository").git_root
as the source of truth for the git root that Neogit is targeting. Unlike the cwd, which can change at any point,repository.git_root
only updates throughrepository.refresh()
triggered by manual user intervention (e.g.,Ctrl+R
or:Neogit
) and should - hopefully - also invalidate all other state which depends on the git root.Key changes in this pull request include:
cli
functions are executed withcwd = repository.git_root
. This change is particularly impactful as it ensures that all cwd-relative file paths returned bygit
do not need to be converted to paths relative to the git root, ascwd == git_root
always holds.cli.git_root()
withrepository.git_root
in the codebase to maintain consistency.cli.git_root()
tocli.git_root_of_cwd()
to discourage its use and prevent undoing the modifications introduced by this pull request.diffview.root_prefix()
,repository.cwd
, and theuse_git_root
argument of hunk operations.Smaller changes include:
git_is_repository_sync
tois_inside_worktree
for clarity.neogit.get_repo()
due to its solitary use.cli.git_root_sync()
andcli.git_dir_path_sync()
.repository.git_path
a function of the module rather than part of the state, asgit_path
depends ongit_root
.Additionally, two tests that relied on Neogit being cwd dependent were adjusted to ensure that
status.reset()
is called after the cwd has been changed.