feat(git): honor GIT_DIR environment variable (#1348)

This commit is contained in:
Dario Vladović 2020-06-24 23:13:47 +02:00 committed by GitHub
parent dbe6bdf031
commit e034d51cb2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 1 deletions

View File

@ -136,7 +136,11 @@ impl<'a> Context<'a> {
pub fn get_repo(&self) -> Result<&Repo, std::io::Error> {
self.repo
.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
.as_ref()
.and_then(|repo| get_current_branch(repo));

View File

@ -1,6 +1,7 @@
use ansi_term::Color;
use remove_dir_all::remove_dir_all;
use std::io;
use std::path::Path;
use std::process::Command;
use crate::common::{self, TestCommand};
@ -122,6 +123,29 @@ fn test_works_with_unborn_master() -> io::Result<()> {
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(
branch_name: &str,
truncate_length: i64,