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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use super::CrateTrait;
use crate::cmd::{Command, ProcessLinesActions};
use crate::prepare::PrepareError;
use crate::Workspace;
use failure::{Error, ResultExt};
use log::{info, warn};
use percent_encoding::{percent_encode, AsciiSet, CONTROLS};
use std::path::{Path, PathBuf};
const ENCODE_SET: AsciiSet = CONTROLS
.add(b'/')
.add(b'\\')
.add(b'<')
.add(b'>')
.add(b':')
.add(b'"')
.add(b'|')
.add(b'?')
.add(b'*')
.add(b' ');
pub(super) struct GitRepo {
url: String,
}
impl GitRepo {
pub(super) fn new(url: &str) -> Self {
Self { url: url.into() }
}
pub(super) fn git_commit(&self, workspace: &Workspace) -> Option<String> {
let res = Command::new(workspace, "git")
.args(&["rev-parse", "HEAD"])
.cd(&self.cached_path(workspace))
.run_capture();
match res {
Ok(out) => {
if let Some(shaline) = out.stdout_lines().get(0) {
if !shaline.is_empty() {
return Some(shaline.to_string());
}
}
warn!("bad output from `git rev-parse HEAD`");
}
Err(e) => {
warn!("unable to capture sha for {}: {}", self.url, e);
}
}
None
}
fn cached_path(&self, workspace: &Workspace) -> PathBuf {
workspace
.cache_dir()
.join("git-repos")
.join(percent_encode(self.url.as_bytes(), &ENCODE_SET).to_string())
}
fn suppress_password_prompt_args(&self, workspace: &Workspace) -> Vec<String> {
vec![
"-c".into(),
"credential.helper=".into(),
"-c".into(),
format!(
"credential.helper={}",
crate::tools::GIT_CREDENTIAL_NULL
.binary_path(workspace)
.to_str()
.unwrap()
.replace('\\', "/")
),
]
}
}
impl CrateTrait for GitRepo {
fn fetch(&self, workspace: &Workspace) -> Result<(), Error> {
let mut private_repository = false;
let mut detect_private_repositories = |line: &str, _actions: &mut ProcessLinesActions| {
if line.starts_with("fatal: credential helper") && line.ends_with("told us to quit") {
private_repository = true;
}
};
let path = self.cached_path(workspace);
let res = if path.join("HEAD").is_file() {
info!("updating cached repository {}", self.url);
Command::new(workspace, "git")
.args(&self.suppress_password_prompt_args(workspace))
.args(&["-c", "remote.origin.fetch=refs/heads/*:refs/heads/*"])
.args(&["fetch", "origin", "--force", "--prune"])
.cd(&path)
.process_lines(&mut detect_private_repositories)
.run()
.with_context(|_| format!("failed to update {}", self.url))
} else {
info!("cloning repository {}", self.url);
Command::new(workspace, "git")
.args(&self.suppress_password_prompt_args(workspace))
.args(&["clone", "--bare", &self.url])
.args(&[&path])
.process_lines(&mut detect_private_repositories)
.run()
.with_context(|_| format!("failed to clone {}", self.url))
};
if private_repository && res.is_err() {
Err(PrepareError::PrivateGitRepository.into())
} else {
Ok(res?)
}
}
fn purge_from_cache(&self, workspace: &Workspace) -> Result<(), Error> {
let path = self.cached_path(workspace);
if path.exists() {
remove_dir_all::remove_dir_all(&path)?;
}
Ok(())
}
fn copy_source_to(&self, workspace: &Workspace, dest: &Path) -> Result<(), Error> {
Command::new(workspace, "git")
.args(&["clone"])
.args(&[self.cached_path(workspace).as_path(), dest])
.run()
.with_context(|_| format!("failed to checkout {}", self.url))?;
Ok(())
}
}
impl std::fmt::Display for GitRepo {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "git repo {}", self.url)
}
}