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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
use error::*;
use prepare::GitWorkArea;
use std::ffi::OsStr;
use std::fmt::{self, Display};
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CommitId(String);
impl CommitId {
pub fn new<I: ToString>(id: I) -> Self {
CommitId(id.to_string())
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl Display for CommitId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.as_str())
}
}
#[derive(Debug, Clone)]
pub struct GitContext {
gitdir: PathBuf,
config: Option<PathBuf>,
}
#[derive(Debug, PartialEq, Eq)]
pub struct Identity {
pub name: String,
pub email: String,
}
impl Identity {
pub fn new<N, E>(name: N, email: E) -> Self
where N: ToString,
E: ToString,
{
Identity {
name: name.to_string(),
email: email.to_string(),
}
}
}
impl Clone for Identity {
fn clone(&self) -> Self {
Self::new(&self.name, &self.email)
}
}
impl Display for Identity {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} <{}>", self.name, self.email)
}
}
#[derive(Debug)]
pub enum MergeStatus {
NoCommonHistory,
AlreadyMerged,
Mergeable(Vec<CommitId>),
}
impl Display for MergeStatus {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f,
"{}",
match *self {
MergeStatus::NoCommonHistory => "no common history",
MergeStatus::AlreadyMerged => "already merged",
MergeStatus::Mergeable(_) => "mergeable",
})
}
}
impl GitContext {
pub fn new<P>(gitdir: P) -> Self
where P: AsRef<Path>,
{
GitContext {
gitdir: gitdir.as_ref().to_path_buf(),
config: None,
}
}
pub fn new_with_config<P, C>(gitdir: P, config: C) -> Self
where P: AsRef<Path>,
C: AsRef<Path>,
{
GitContext {
gitdir: gitdir.as_ref().to_path_buf(),
config: Some(config.as_ref().to_path_buf()),
}
}
pub fn git(&self) -> Command {
let mut git = Command::new("git");
git.env("GIT_DIR", &self.gitdir);
self.config
.as_ref()
.map(|config| git.env("GIT_CONFIG", config));
git
}
pub fn fetch<R, I, N>(&self, remote: R, refnames: I) -> Result<()>
where R: AsRef<str>,
I: IntoIterator<Item = N>,
N: AsRef<OsStr>,
{
let fetch = self.git()
.arg("fetch")
.arg(remote.as_ref())
.args(&refnames.into_iter()
.collect::<Vec<_>>())
.output()
.chain_err(|| "failed to construct fetch command")?;
if !fetch.status.success() {
bail!(ErrorKind::Git(format!("fetch from {} failed: {}",
remote.as_ref(),
String::from_utf8_lossy(&fetch.stderr))));
}
Ok(())
}
pub fn fetch_into<R, N, T>(&self, remote: R, refname: N, target: T) -> Result<()>
where R: AsRef<str>,
N: AsRef<str>,
T: AsRef<str>,
{
self.fetch(remote,
&[&format!("{}:{}", refname.as_ref(), target.as_ref())])
}
pub fn force_fetch_into<R, N, T>(&self, remote: R, refname: N, target: T) -> Result<()>
where R: AsRef<str>,
N: AsRef<str>,
T: AsRef<str>,
{
self.fetch_into(remote.as_ref(),
format!("+{}", refname.as_ref()),
target.as_ref())
}
pub fn prepare(&self, rev: &CommitId) -> Result<GitWorkArea> {
GitWorkArea::new(self.clone(), rev)
}
pub fn reserve_ref<N>(&self, name: N, commit: &CommitId) -> Result<(String, usize)>
where N: AsRef<str>,
{
let ref_prefix = format!("refs/{}/heads", name.as_ref());
debug!(target: "git", "reserving ref under {}", ref_prefix);
loop {
let for_each_ref = self.git()
.arg("for-each-ref")
.arg("--format=%(refname)")
.arg("--")
.arg(&ref_prefix)
.output()
.chain_err(|| "failed to construct for-each-ref command")?;
if !for_each_ref.status.success() {
bail!(ErrorKind::Git(format!("listing all {} refs: {}",
ref_prefix,
String::from_utf8_lossy(&for_each_ref.stderr))));
}
let refs = String::from_utf8_lossy(&for_each_ref.stdout);
let nrefs = refs.lines().count();
let new_ref = format!("{}/{}", ref_prefix, nrefs);
debug!(target: "git", "trying to reserve ref {}", new_ref);
let lock_ref = self.git()
.arg("update-ref")
.arg(&new_ref)
.arg(commit.as_str())
.arg("0000000000000000000000000000000000000000")
.stdout(Stdio::null())
.output()
.chain_err(|| "failed to construct update-ref command")?;
if lock_ref.status.success() {
debug!(target: "git", "successfully reserved {}", new_ref);
return Ok((new_ref, nrefs));
}
let err = String::from_utf8_lossy(&lock_ref.stderr);
if err.contains("with nonexistent object") {
bail!(ErrorKind::InvalidRef("no such commit".to_string()));
} else if err.contains("not a valid SHA1") {
bail!(ErrorKind::InvalidRef("invalid SHA".to_string()));
}
}
}
pub fn reserve_refs<N>(&self, name: N, commit: &CommitId) -> Result<(String, String)>
where N: AsRef<str>,
{
let (new_ref, id) = self.reserve_ref(name.as_ref(), commit)?;
let new_base = format!("refs/{}/bases/{}", name.as_ref(), id);
debug!(target: "git", "successfully reserved {} and {}", new_ref, new_base);
Ok((new_ref, new_base))
}
pub fn mergeable(&self, base: &CommitId, topic: &CommitId) -> Result<MergeStatus> {
let merge_base = self.git()
.arg("merge-base")
.arg("--all")
.arg(base.as_str())
.arg(topic.as_str())
.output()
.chain_err(|| "failed to construct merge-base command")?;
if !merge_base.status.success() {
return Ok(MergeStatus::NoCommonHistory);
}
let bases = String::from_utf8_lossy(&merge_base.stdout);
let bases = bases.split_whitespace()
.map(CommitId::new)
.collect::<Vec<_>>();
Ok(if Some(topic) == bases.first() {
MergeStatus::AlreadyMerged
} else {
MergeStatus::Mergeable(bases)
})
}
pub fn gitdir(&self) -> &Path {
&self.gitdir
}
}