Skip to content

Commit

Permalink
Added support for git worktrees
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Ozias committed Oct 8, 2018
1 parent b8d8f0d commit 86981ed
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ license = "MIT/Apache-2.0"
name = "vergen"
readme = "README.md"
repository = "https://github.com/rustyhorde/vergen"
version = "3.0.1"
version = "3.0.2"

[dependencies]
bitflags = "1"
Expand Down
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@
[![Crates.io](https://img.shields.io/crates/l/vergen.svg)](https://crates.io/crates/vergen)
[![Crates.io](https://img.shields.io/crates/d/vergen.svg)](https://crates.io/crates/vergen)

## Version 3.x.x
[![Build
Status](https://travis-ci.org/rustyhorde/vergen.svg?branch=master)](https://travis-ci.org/rustyhorde/vergen)
## Build Status
| | |
---------------|-----------------------------------------------------------------------------------|
| Travis | [![Build Status](https://travis-ci.org/rustyhorde/vergen.svg?branch=master)](https://travis-ci.org/rustyhorde/vergen)|
| GitLab CI/CD | [![pipeline status](https://gitlab.com/rustyhorde/vergen/badges/master/pipeline.svg)](https://gitlab.com/rustyhorde/vergen/commits/master)|
| Appveyor | [![Build status](https://ci.appveyor.com/api/projects/status/rcdjlx0sxvk3wnww/branch/master?svg=true)](https://ci.appveyor.com/project/CraZySacX/vergen/branch/master)|

## Code Coverage
[![codecov](https://codecov.io/gh/rustyhorde/vergen/branch/master/graph/badge.svg)](https://codecov.io/gh/rustyhorde/vergen)

## Version 3.x.x
Introduces `generate_cargo_keys()` and support for rebuild when `.git/HEAD` changes.
Internally converted to use `failure` so `Result` is no longer exported and changed to the Rust 2018
edition.
Expand All @@ -16,7 +23,7 @@ edition.
hits stable (12/06/18)**

## Version 2.1.x
[![Build Status](https://travis-ci.org/rustyhorde/vergen.svg?branch=v2.1.0)](https://travis-ci.org/rustyhorde/vergen)
[![Build Status](https://travis-ci.org/rustyhorde/vergen.svg?branch=v2.1.1)](https://travis-ci.org/rustyhorde/vergen)

Backport of the 3.x.x changes to work on stable until Rust 2018 hits stable.

Expand Down
54 changes: 40 additions & 14 deletions src/output/envvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,31 +44,57 @@ pub fn generate_cargo_keys(flags: ConstantsFlags) -> Fallible<()> {
let git_dir_or_file = PathBuf::from(".git");
let metadata = fs::metadata(&git_dir_or_file)?;

let mut f = if metadata.is_dir() {
if metadata.is_dir() {
// Echo the HEAD path
let git_head_path = git_dir_or_file.join("HEAD");
println!("cargo:rerun-if-changed={}", git_head_path.display());
File::open(&git_head_path)?

// Determine where HEAD points and echo that path also.
let mut f = File::open(&git_head_path)?;
let mut git_head_contents = String::new();
let _ = f.read_to_string(&mut git_head_contents)?;
let ref_vec: Vec<&str> = git_head_contents.split(": ").collect();

if ref_vec.len() == 2 {
let current_head_file = ref_vec[1];
let git_refs_path = PathBuf::from(".git").join(current_head_file);
println!("cargo:rerun-if-changed={}", git_refs_path.display());
} else {
return Err(failure::err_msg("Invalid HEAD file"));
}
} else if metadata.is_file() {
// We are in a worktree, so find out where the actual worktrees/<name>/HEAD file is.
let mut git_file = File::open(&git_dir_or_file)?;
let mut git_contents = String::new();
let _ = git_file.read_to_string(&mut git_contents)?;
let dir_vec: Vec<&str> = git_contents.split(": ").collect();
let git_head_path = PathBuf::from(dir_vec[1].trim()).join("HEAD");
let git_path = dir_vec[1].trim();

// Echo the HEAD psth
let git_head_path = PathBuf::from(git_path).join("HEAD");
println!("cargo:rerun-if-changed={}", git_head_path.display());
File::open(&git_head_path)?

// Find out what the full path to the .git dir is.
let mut actual_git_dir = PathBuf::from(git_path);
actual_git_dir.pop();
actual_git_dir.pop();

// Determine where HEAD points and echo that path also.
let mut f = File::open(&git_head_path)?;
let mut git_head_contents = String::new();
let _ = f.read_to_string(&mut git_head_contents)?;
let ref_vec: Vec<&str> = git_head_contents.split(": ").collect();

if ref_vec.len() == 2 {
let current_head_file = ref_vec[1];
let git_refs_path = actual_git_dir.join(current_head_file);
println!("cargo:rerun-if-changed={}", git_refs_path.display());
} else {
return Err(failure::err_msg("Invalid HEAD file"));
}
} else {
return Err(failure::err_msg("Invalid .git format"));
};

let mut git_head_contents = String::new();
let _ = f.read_to_string(&mut git_head_contents)?;
let ref_vec: Vec<&str> = git_head_contents.split(": ").collect();

if ref_vec.len() == 2 {
let current_head_file = ref_vec[1];
let git_refs_path = PathBuf::from(".git").join(current_head_file);
println!("cargo:rerun-if-changed={}", git_refs_path.display());
}

Ok(())
}

0 comments on commit 86981ed

Please sign in to comment.