mirror of
https://github.com/Llewellynvdm/starship.git
synced 2024-11-08 14:21:05 +00:00
feat(git): honor GIT_DIR
environment variable (#1348)
This commit is contained in:
parent
dbe6bdf031
commit
e034d51cb2
@ -136,7 +136,11 @@ impl<'a> Context<'a> {
|
|||||||
pub fn get_repo(&self) -> Result<&Repo, std::io::Error> {
|
pub fn get_repo(&self) -> Result<&Repo, std::io::Error> {
|
||||||
self.repo
|
self.repo
|
||||||
.get_or_try_init(|| -> Result<Repo, std::io::Error> {
|
.get_or_try_init(|| -> Result<Repo, std::io::Error> {
|
||||||
let repository = Repository::discover(&self.current_dir).ok();
|
let repository = if env::var("GIT_DIR").is_ok() {
|
||||||
|
Repository::open_from_env().ok()
|
||||||
|
} else {
|
||||||
|
Repository::discover(&self.current_dir).ok()
|
||||||
|
};
|
||||||
let branch = repository
|
let branch = repository
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.and_then(|repo| get_current_branch(repo));
|
.and_then(|repo| get_current_branch(repo));
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
use ansi_term::Color;
|
use ansi_term::Color;
|
||||||
use remove_dir_all::remove_dir_all;
|
use remove_dir_all::remove_dir_all;
|
||||||
use std::io;
|
use std::io;
|
||||||
|
use std::path::Path;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
|
||||||
use crate::common::{self, TestCommand};
|
use crate::common::{self, TestCommand};
|
||||||
@ -122,6 +123,29 @@ fn test_works_with_unborn_master() -> io::Result<()> {
|
|||||||
remove_dir_all(repo_dir)
|
remove_dir_all(repo_dir)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_git_dir_env_variable() -> io::Result<()> {
|
||||||
|
let repo_dir = tempfile::tempdir()?.into_path();
|
||||||
|
|
||||||
|
Command::new("git")
|
||||||
|
.args(&["init"])
|
||||||
|
.current_dir(&repo_dir)
|
||||||
|
.output()?;
|
||||||
|
|
||||||
|
let output = common::render_module("git_branch")
|
||||||
|
.env("GIT_DIR", Path::new(&repo_dir).join(".git"))
|
||||||
|
.output()
|
||||||
|
.unwrap();
|
||||||
|
let actual = String::from_utf8(output.stdout).unwrap();
|
||||||
|
|
||||||
|
let expected = format!(
|
||||||
|
"on {} ",
|
||||||
|
Color::Purple.bold().paint(format!("\u{e0a0} {}", "master")),
|
||||||
|
);
|
||||||
|
assert_eq!(expected, actual);
|
||||||
|
remove_dir_all(repo_dir)
|
||||||
|
}
|
||||||
|
|
||||||
fn test_truncate_length(
|
fn test_truncate_length(
|
||||||
branch_name: &str,
|
branch_name: &str,
|
||||||
truncate_length: i64,
|
truncate_length: i64,
|
||||||
|
Loading…
Reference in New Issue
Block a user