-
-
Notifications
You must be signed in to change notification settings - Fork 608
/
Copy pathmod.rs
393 lines (323 loc) · 10.1 KB
/
mod.rs
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
//! branch functions
pub mod merge;
pub mod rename;
use super::{
remotes::get_default_remote_in_repo, utils::bytes2string,
};
use crate::{
error::{Error, Result},
sync::{utils, CommitId},
};
use git2::{BranchType, Repository};
use scopetime::scope_time;
use utils::get_head_repo;
/// returns the branch-name head is currently pointing to
/// this might be expensive, see `cached::BranchName`
pub(crate) fn get_branch_name(repo_path: &str) -> Result<String> {
scope_time!("get_branch_name");
let repo = utils::repo(repo_path)?;
let iter = repo.branches(None)?;
for b in iter {
let b = b?;
if b.0.is_head() {
let name = b.0.name()?.unwrap_or("");
return Ok(name.into());
}
}
Err(Error::NoHead)
}
///
pub struct BranchInfo {
///
pub name: String,
///
pub reference: String,
///
pub top_commit_message: String,
///
pub top_commit: CommitId,
///
pub is_head: bool,
///
pub has_upstream: bool,
}
/// returns a list of `BranchInfo` with a simple summary of info about a single branch
pub fn get_branches_info(repo_path: &str) -> Result<Vec<BranchInfo>> {
scope_time!("get_branches_info");
let cur_repo = utils::repo(repo_path)?;
let branches_for_display = cur_repo
.branches(Some(BranchType::Local))?
.map(|b| {
let branch = b?.0;
let top_commit = branch.get().peel_to_commit()?;
Ok(BranchInfo {
name: bytes2string(branch.name_bytes()?)?,
reference: bytes2string(branch.get().name_bytes())?,
top_commit_message: bytes2string(
top_commit.summary_bytes().unwrap_or_default(),
)?,
top_commit: top_commit.id().into(),
is_head: branch.is_head(),
has_upstream: branch.upstream().is_ok(),
})
})
.filter_map(Result::ok)
.collect();
Ok(branches_for_display)
}
///
#[derive(Debug, Default)]
pub struct BranchCompare {
///
pub ahead: usize,
///
pub behind: usize,
}
///
pub(crate) fn branch_set_upstream(
repo: &Repository,
branch_name: &str,
) -> Result<()> {
scope_time!("branch_set_upstream");
let mut branch =
repo.find_branch(branch_name, BranchType::Local)?;
if branch.upstream().is_err() {
let remote = get_default_remote_in_repo(repo)?;
let upstream_name = format!("{}/{}", remote, branch_name);
branch.set_upstream(Some(upstream_name.as_str()))?;
}
Ok(())
}
///
pub fn branch_compare_upstream(
repo_path: &str,
branch: &str,
) -> Result<BranchCompare> {
scope_time!("branch_compare_upstream");
let repo = utils::repo(repo_path)?;
let branch = repo.find_branch(branch, BranchType::Local)?;
let upstream = branch.upstream()?;
let branch_commit =
branch.into_reference().peel_to_commit()?.id();
let upstream_commit =
upstream.into_reference().peel_to_commit()?.id();
let (ahead, behind) =
repo.graph_ahead_behind(branch_commit, upstream_commit)?;
Ok(BranchCompare { ahead, behind })
}
/// Modify HEAD to point to a branch then checkout head, does not work if there are uncommitted changes
pub fn checkout_branch(
repo_path: &str,
branch_ref: &str,
) -> Result<()> {
scope_time!("checkout_branch");
// This defaults to a safe checkout, so don't delete anything that
// hasn't been committed or stashed, in this case it will Err
let repo = utils::repo(repo_path)?;
let cur_ref = repo.head()?;
let statuses = repo.statuses(Some(
git2::StatusOptions::new().include_ignored(false),
))?;
if statuses.is_empty() {
repo.set_head(branch_ref)?;
if let Err(e) = repo.checkout_head(Some(
git2::build::CheckoutBuilder::new().force(),
)) {
// This is safe beacuse cur_ref was just found
repo.set_head(
bytes2string(cur_ref.name_bytes())?.as_str(),
)?;
return Err(Error::Git(e));
}
Ok(())
} else {
Err(Error::Generic(
format!("Cannot change branch. There are unstaged/staged changes which have not been committed/stashed. There is {:?} changes preventing checking out a different branch.", statuses.len()),
))
}
}
/// The user must not be on the branch for the branch to be deleted
pub fn delete_branch(
repo_path: &str,
branch_ref: &str,
) -> Result<()> {
scope_time!("delete_branch");
let repo = utils::repo(repo_path)?;
let branch_as_ref = repo.find_reference(branch_ref)?;
let mut branch = git2::Branch::wrap(branch_as_ref);
if !branch.is_head() {
branch.delete()?;
} else {
return Err(Error::Generic("You cannot be on the branch you want to delete, switch branch, then delete this branch".to_string()));
}
Ok(())
}
/// creates a new branch pointing to current HEAD commit and updating HEAD to new branch
pub fn create_branch(repo_path: &str, name: &str) -> Result<()> {
scope_time!("create_branch");
let repo = utils::repo(repo_path)?;
let head_id = get_head_repo(&repo)?;
let head_commit = repo.find_commit(head_id.into())?;
let branch = repo.branch(name, &head_commit, false)?;
let branch_ref = branch.into_reference();
let branch_ref_name = bytes2string(branch_ref.name_bytes())?;
repo.set_head(branch_ref_name.as_str())?;
Ok(())
}
#[cfg(test)]
mod tests_branch_name {
use super::*;
use crate::sync::tests::{repo_init, repo_init_empty};
#[test]
fn test_smoke() {
let (_td, repo) = repo_init().unwrap();
let root = repo.path().parent().unwrap();
let repo_path = root.as_os_str().to_str().unwrap();
assert_eq!(
get_branch_name(repo_path).unwrap().as_str(),
"master"
);
}
#[test]
fn test_empty_repo() {
let (_td, repo) = repo_init_empty().unwrap();
let root = repo.path().parent().unwrap();
let repo_path = root.as_os_str().to_str().unwrap();
assert!(matches!(
get_branch_name(repo_path),
Err(Error::NoHead)
));
}
}
#[cfg(test)]
mod tests_create_branch {
use super::*;
use crate::sync::tests::repo_init;
#[test]
fn test_smoke() {
let (_td, repo) = repo_init().unwrap();
let root = repo.path().parent().unwrap();
let repo_path = root.as_os_str().to_str().unwrap();
create_branch(repo_path, "branch1").unwrap();
assert_eq!(
get_branch_name(repo_path).unwrap().as_str(),
"branch1"
);
}
}
#[cfg(test)]
mod tests_branch_compare {
use super::*;
use crate::sync::tests::repo_init;
#[test]
fn test_smoke() {
let (_td, repo) = repo_init().unwrap();
let root = repo.path().parent().unwrap();
let repo_path = root.as_os_str().to_str().unwrap();
create_branch(repo_path, "test").unwrap();
let res = branch_compare_upstream(repo_path, "test");
assert_eq!(res.is_err(), true);
}
}
#[cfg(test)]
mod tests_branches {
use super::*;
use crate::sync::tests::repo_init;
#[test]
fn test_smoke() {
let (_td, repo) = repo_init().unwrap();
let root = repo.path().parent().unwrap();
let repo_path = root.as_os_str().to_str().unwrap();
assert_eq!(
get_branches_info(repo_path)
.unwrap()
.iter()
.map(|b| b.name.clone())
.collect::<Vec<_>>(),
vec!["master"]
);
}
#[test]
fn test_multiple() {
let (_td, repo) = repo_init().unwrap();
let root = repo.path().parent().unwrap();
let repo_path = root.as_os_str().to_str().unwrap();
create_branch(repo_path, "test").unwrap();
assert_eq!(
get_branches_info(repo_path)
.unwrap()
.iter()
.map(|b| b.name.clone())
.collect::<Vec<_>>(),
vec!["master", "test"]
);
}
}
#[cfg(test)]
mod tests_checkout {
use super::*;
use crate::sync::tests::repo_init;
#[test]
fn test_smoke() {
let (_td, repo) = repo_init().unwrap();
let root = repo.path().parent().unwrap();
let repo_path = root.as_os_str().to_str().unwrap();
assert!(
checkout_branch(repo_path, "refs/heads/master").is_ok()
);
assert!(
checkout_branch(repo_path, "refs/heads/foobar").is_err()
);
}
#[test]
fn test_multiple() {
let (_td, repo) = repo_init().unwrap();
let root = repo.path().parent().unwrap();
let repo_path = root.as_os_str().to_str().unwrap();
create_branch(repo_path, "test").unwrap();
assert!(checkout_branch(repo_path, "refs/heads/test").is_ok());
assert!(
checkout_branch(repo_path, "refs/heads/master").is_ok()
);
assert!(checkout_branch(repo_path, "refs/heads/test").is_ok());
}
}
#[cfg(test)]
mod test_delete_branch {
use super::*;
use crate::sync::tests::repo_init;
#[test]
fn test_delete_branch() {
let (_td, repo) = repo_init().unwrap();
let root = repo.path().parent().unwrap();
let repo_path = root.as_os_str().to_str().unwrap();
create_branch(repo_path, "branch1").unwrap();
create_branch(repo_path, "branch2").unwrap();
checkout_branch(repo_path, "refs/heads/branch1").unwrap();
assert_eq!(
repo.branches(None)
.unwrap()
.nth(1)
.unwrap()
.unwrap()
.0
.name()
.unwrap()
.unwrap(),
"branch2"
);
delete_branch(repo_path, "refs/heads/branch2").unwrap();
assert_eq!(
repo.branches(None)
.unwrap()
.nth(1)
.unwrap()
.unwrap()
.0
.name()
.unwrap()
.unwrap(),
"master"
);
}
}