1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use std::{env, fs, fs::File, io, io::Read, path::PathBuf};
pub fn rerun_if_git_head_changed() {
let mut manifest_dir = PathBuf::from(
env::var("CARGO_MANIFEST_DIR").expect("`CARGO_MANIFEST_DIR` is always set by cargo."),
);
let manifest_dir_copy = manifest_dir.clone();
while manifest_dir.parent().is_some() {
match get_git_paths(&manifest_dir) {
Err(err) => {
eprintln!("cargo:warning=Unable to read the Git repository: {}", err);
return
},
Ok(None) => {},
Ok(Some(paths)) => {
for p in paths {
println!("cargo:rerun-if-changed={}", p.display());
}
return
},
}
manifest_dir.pop();
}
println!(
"cargo:warning=Could not find `.git/HEAD` searching from `{}` upwards!",
manifest_dir_copy.display(),
);
}
fn get_git_paths(path: &PathBuf) -> Result<Option<Vec<PathBuf>>, io::Error> {
let git_dir_or_file = path.join(".git");
if let Ok(metadata) = fs::metadata(&git_dir_or_file) {
if metadata.is_dir() {
let git_head_path = git_dir_or_file.join("HEAD");
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 = git_dir_or_file.join(current_head_file);
Ok(Some(vec![git_head_path, git_refs_path]))
} else {
Err(io::Error::new(
io::ErrorKind::Other,
"You are most likely in a detached HEAD state",
))
}
} else if metadata.is_file() {
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_path = dir_vec[1].trim();
let git_head_path = PathBuf::from(git_path).join("HEAD");
let mut actual_git_dir = PathBuf::from(git_path);
actual_git_dir.pop();
actual_git_dir.pop();
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);
Ok(Some(vec![git_head_path, git_refs_path]))
} else {
Err(io::Error::new(
io::ErrorKind::Other,
"You are most likely in a detached HEAD state",
))
}
} else {
Err(io::Error::new(
io::ErrorKind::Other,
"Invalid .git format (Not a directory or a file)",
))
}
} else {
Ok(None)
}
}