Skip to content

Commit

Permalink
fix(git_status): Fix when the worktree != root dir (starship#2831)
Browse files Browse the repository at this point in the history
Have updated the git_status module to work if the worktree has been
changed to a different directory than the repository root directory.
  • Loading branch information
andytom authored Jul 5, 2021
1 parent 57a523e commit 9e2da88
Showing 1 changed file with 38 additions and 23 deletions.
61 changes: 38 additions & 23 deletions src/modules/git_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ use regex::Regex;
use super::{Context, Module, RootModuleConfig};

use crate::configs::git_status::GitStatusConfig;
use crate::context::Repo;
use crate::formatter::StringFormatter;
use crate::segment::Segment;
use std::path::Path;
use std::sync::Arc;

const ALL_STATUS_FORMAT: &str = "$conflicted$stashed$deleted$renamed$modified$staged$untracked";
Expand All @@ -27,8 +25,7 @@ const ALL_STATUS_FORMAT: &str = "$conflicted$stashed$deleted$renamed$modified$st
/// - `»` — A renamed file has been added to the staging area
/// - `✘` — A file's deletion has been added to the staging area
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let repo = context.get_repo().ok()?;
let info = Arc::new(GitStatusInfo::load(context, repo));
let info = Arc::new(GitStatusInfo::load(context));

let mut module = context.new_module("git_status");
let config: GitStatusConfig = GitStatusConfig::try_load(module.config);
Expand Down Expand Up @@ -110,16 +107,14 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {

struct GitStatusInfo<'a> {
context: &'a Context<'a>,
repo: &'a Repo,
repo_status: OnceCell<Option<RepoStatus>>,
stashed_count: OnceCell<Option<usize>>,
}

impl<'a> GitStatusInfo<'a> {
pub fn load(context: &'a Context, repo: &'a Repo) -> Self {
pub fn load(context: &'a Context) -> Self {
Self {
context,
repo,
repo_status: OnceCell::new(),
stashed_count: OnceCell::new(),
}
Expand All @@ -130,31 +125,25 @@ impl<'a> GitStatusInfo<'a> {
}

pub fn get_repo_status(&self) -> &Option<RepoStatus> {
self.repo_status.get_or_init(|| {
let repo_root = self.repo.root.as_ref()?;

match get_repo_status(self.context, repo_root) {
self.repo_status
.get_or_init(|| match get_repo_status(self.context) {
Some(repo_status) => Some(repo_status),
None => {
log::debug!("get_repo_status: git status execution failed");
None
}
}
})
})
}

pub fn get_stashed(&self) -> &Option<usize> {
self.stashed_count.get_or_init(|| {
let repo_root = self.repo.root.as_ref()?;

match get_stashed_count(self.context, repo_root) {
self.stashed_count
.get_or_init(|| match get_stashed_count(self.context) {
Some(stashed_count) => Some(stashed_count),
None => {
log::debug!("get_stashed_count: git stash execution failed");
None
}
}
})
})
}

pub fn get_conflicted(&self) -> Option<usize> {
Expand Down Expand Up @@ -183,15 +172,15 @@ impl<'a> GitStatusInfo<'a> {
}

/// Gets the number of files in various git states (staged, modified, deleted, etc...)
fn get_repo_status(context: &Context, repo_root: &Path) -> Option<RepoStatus> {
fn get_repo_status(context: &Context) -> Option<RepoStatus> {
log::debug!("New repo status created");

let mut repo_status = RepoStatus::default();
let status_output = context.exec_cmd(
"git",
&[
"-C",
&repo_root.to_string_lossy(),
&context.current_dir.to_string_lossy(),
"--no-optional-locks",
"status",
"--porcelain=2",
Expand All @@ -211,12 +200,12 @@ fn get_repo_status(context: &Context, repo_root: &Path) -> Option<RepoStatus> {
Some(repo_status)
}

fn get_stashed_count(context: &Context, repo_root: &Path) -> Option<usize> {
fn get_stashed_count(context: &Context) -> Option<usize> {
let stash_output = context.exec_cmd(
"git",
&[
"-C",
&repo_root.to_string_lossy(),
&context.current_dir.to_string_lossy(),
"--no-optional-locks",
"stash",
"list",
Expand Down Expand Up @@ -757,6 +746,32 @@ mod tests {
repo_dir.close()
}

#[test]
fn worktree_in_different_dir() -> io::Result<()> {
let worktree_dir = tempfile::tempdir()?;
let repo_dir = fixture_repo(FixtureProvider::Git)?;

Command::new("git")
.args(&[
"config",
"core.worktree",
&worktree_dir.path().to_string_lossy(),
])
.current_dir(repo_dir.path())
.output()?;

File::create(worktree_dir.path().join("test_file"))?.sync_all()?;

let actual = ModuleRenderer::new("git_status")
.path(&repo_dir.path())
.collect();
let expected = format_output("✘?");

assert_eq!(expected, actual);
worktree_dir.close()?;
repo_dir.close()
}

// Whenever a file is manually renamed, git itself ('git status') does not treat such file as renamed,
// but as untracked instead. The following test checks if manually deleted and manually renamed
// files are tracked by git_status module in the same way 'git status' does.
Expand Down

0 comments on commit 9e2da88

Please sign in to comment.