forked from gitext-rs/git-stack
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathops.rs
More file actions
483 lines (439 loc) · 14.9 KB
/
Copy pathops.rs
File metadata and controls
483 lines (439 loc) · 14.9 KB
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
use bstr::ByteSlice;
use eyre::WrapErr;
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct AnnotatedOid {
pub id: git2::Oid,
pub branch: Option<git_stack::git::Branch>,
}
impl AnnotatedOid {
pub fn new(id: git2::Oid) -> Self {
Self { id, branch: None }
}
pub fn with_branch(branch: git_stack::git::Branch) -> Self {
Self {
id: branch.id,
branch: Some(branch),
}
}
pub fn update(&mut self, repo: &dyn git_stack::git::Repo) -> eyre::Result<()> {
let branch = self.branch.as_ref().and_then(|branch| {
if let Some(remote) = &branch.remote {
repo.find_remote_branch(remote, &branch.name)
} else {
repo.find_local_branch(&branch.name)
}
});
if let Some(branch) = branch {
*self = Self::with_branch(branch);
}
Ok(())
}
}
impl std::fmt::Display for AnnotatedOid {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if let Some(branch) = &self.branch {
branch.fmt(f)
} else {
self.id.fmt(f)
}
}
}
pub fn resolve_explicit_base(
repo: &git_stack::git::GitRepo,
base: &str,
) -> eyre::Result<AnnotatedOid> {
let (obj, r) = repo.raw().revparse_ext(base)?;
if let Some(r) = r {
if r.is_tag() {
return Ok(AnnotatedOid::new(obj.id()));
}
let branch = if r.is_remote() {
let shorthand = r
.shorthand()
.ok_or_else(|| eyre::eyre!("Expected branch, got `{}`", base))?;
let (remote, name) = shorthand
.split_once('/')
.expect("removes should always have at least one `/`");
repo.find_remote_branch(remote, name)
.ok_or_else(|| eyre::eyre!("Could not find branch {:?}", r.shorthand()))
} else {
let shorthand = r
.shorthand()
.ok_or_else(|| eyre::eyre!("Expected branch, got `{}`", base))?;
if shorthand == "HEAD" {
return Ok(AnnotatedOid::new(obj.id()));
}
repo.find_local_branch(shorthand)
.ok_or_else(|| eyre::eyre!("Could not find branch {:?}", shorthand))
}?;
Ok(AnnotatedOid::with_branch(branch))
} else {
Ok(AnnotatedOid::new(obj.id()))
}
}
pub fn resolve_implicit_base(
repo: &dyn git_stack::git::Repo,
head_oid: git2::Oid,
branches: &git_stack::graph::BranchSet,
auto_base_commit_count: Option<usize>,
) -> AnnotatedOid {
match git_stack::graph::find_protected_base(repo, branches, head_oid) {
Some(branch) => {
let merge_base_id = repo
.merge_base(branch.id(), head_oid)
.expect("to be a base, there must be a merge base");
if let Some(max_commit_count) = auto_base_commit_count {
let ahead_count = repo
.commit_count(merge_base_id, head_oid)
.expect("merge_base should ensure a count exists ");
let behind_count = repo
.commit_count(merge_base_id, branch.id())
.expect("merge_base should ensure a count exists ");
if max_commit_count <= ahead_count + behind_count {
let assumed_base_oid =
git_stack::graph::infer_base(repo, head_oid).unwrap_or(head_oid);
log::warn!(
"`{}` is {} ahead and {} behind `{}`, using `{}` as `--base` instead",
branches
.get(head_oid)
.map(|b| b[0].name())
.or_else(|| {
repo.find_commit(head_oid)?
.summary
.to_str()
.ok()
.map(ToOwned::to_owned)
})
.unwrap_or_else(|| "target".to_owned()),
ahead_count,
behind_count,
branch.display_name(),
assumed_base_oid
);
return AnnotatedOid::new(assumed_base_oid);
}
}
log::debug!(
"Chose branch `{}` as the base for `{}`",
branch.display_name(),
branches
.get(head_oid)
.map(|b| b[0].name())
.or_else(|| {
repo.find_commit(head_oid)?
.summary
.to_str()
.ok()
.map(ToOwned::to_owned)
})
.unwrap_or_else(|| "target".to_owned())
);
AnnotatedOid::with_branch(branch.git().to_owned())
}
None => {
let assumed_base_oid = git_stack::graph::infer_base(repo, head_oid).unwrap_or(head_oid);
log::warn!(
"Could not find protected branch for {}, assuming {}",
head_oid,
assumed_base_oid
);
AnnotatedOid::new(assumed_base_oid)
}
}
}
pub fn resolve_base_from_onto(repo: &git_stack::git::GitRepo, onto: &AnnotatedOid) -> AnnotatedOid {
// HACK: Assuming the local branch is the current base for all the commits
onto.branch
.as_ref()
.filter(|b| b.remote.is_some())
.and_then(|b| repo.find_local_branch(&b.name))
.map(AnnotatedOid::with_branch)
.unwrap_or_else(|| onto.clone())
}
pub fn git_prune_development(
repo: &mut git_stack::git::GitRepo,
branches: &[&str],
dry_run: bool,
) -> eyre::Result<()> {
if branches.is_empty() {
return Ok(());
}
let remote = repo.push_remote();
let output = std::process::Command::new("git")
.arg("ls-remote")
.arg("--heads")
.arg(remote)
.args(branches)
.stdout(std::process::Stdio::piped())
.spawn()
.wrap_err("Could not run `git fetch`")?
.wait_with_output()?;
if !output.status.success() {
eyre::bail!("Could not run `git fetch`");
}
let stdout = String::from_utf8(output.stdout).wrap_err("Could not run `git fetch`")?;
#[allow(clippy::needless_collect)]
let remote_branches: Vec<_> = stdout
.lines()
.filter_map(|l| l.split_once('\t').map(|s| s.1))
.filter_map(|l| l.strip_prefix("refs/heads/"))
.collect();
if !branches.is_empty() {
log::trace!("Local branches:\n {}", branches.join("\n "));
}
if !remote_branches.is_empty() {
log::trace!("Remote branches:\n {}", remote_branches.join("\n "));
}
for branch in branches {
if !remote_branches.contains(branch) {
let remote_branch = format!("{remote}/{branch}");
log::info!("Pruning {}", remote_branch);
if !dry_run {
let mut branch = repo
.raw()
.find_branch(&remote_branch, git2::BranchType::Remote)?;
branch.delete()?;
}
}
}
Ok(())
}
pub fn git_fetch_upstream(remote: &str, branch_name: &str) -> eyre::Result<()> {
log::debug!("git fetch {} {}", remote, branch_name);
// A little uncertain about some of the weirder authentication needs, just deferring to `git`
// instead of using `libgit2`
let status = std::process::Command::new("git")
.arg("fetch")
.arg(remote)
.arg(branch_name)
.status()
.wrap_err("Could not run `git fetch`")?;
if !status.success() {
eyre::bail!("`git fetch {} {}` failed", remote, branch_name,);
}
Ok(())
}
/// Switch to the best-guess branch
///
/// # Panic
///
/// Panics if `current_id` is not present
pub fn switch(
repo: &mut git_stack::git::GitRepo,
branches: &git_stack::graph::BranchSet,
current_id: git2::Oid,
stderr_palette: Palette,
dry_run: bool,
) -> Result<(), git2::Error> {
use std::io::Write;
let current_commit = repo
.find_commit(current_id)
.expect("children/head are always present");
if let Some(current) = branches.get(current_id) {
let mut current = current.to_owned();
current.sort_by_key(|b| b.kind());
let current_branch = current.first().expect("always at least one");
let _ = writeln!(
anstream::stderr(),
"{} to {}: {}",
stderr_palette.good("Switching"),
stderr_palette.highlight(current_branch.display_name()),
stderr_palette.hint(¤t_commit.summary)
);
if !dry_run {
repo.switch_branch(
current_branch
.local_name()
.expect("only local branches present"),
)?;
}
} else {
let abbrev_id = repo
.raw()
.find_object(current_id, None)
.unwrap_or_else(|e| panic!("Unexpected git2 error: {e}"))
.short_id()
.unwrap_or_else(|e| panic!("Unexpected git2 error: {e}"));
let _ = writeln!(
anstream::stderr(),
"{} to {}: {}",
stderr_palette.good("Switching"),
stderr_palette.highlight(abbrev_id.as_str().unwrap()),
stderr_palette.hint(¤t_commit.summary)
);
if !dry_run {
repo.switch_commit(current_id)?;
}
}
Ok(())
}
pub fn render_id(
repo: &git_stack::git::GitRepo,
branches: &git_stack::graph::BranchSet,
id: git2::Oid,
) -> String {
if let Some(current) = branches.get(id) {
let mut current = current.to_owned();
current.sort_by_key(|b| b.kind());
let current_branch = current.first().expect("always at least one");
let name = current_branch.display_name().to_string();
name
} else {
repo.raw()
.find_object(id, None)
.unwrap_or_else(|e| panic!("Unexpected git2 error: {e}"))
.short_id()
.unwrap_or_else(|e| panic!("Unexpected git2 error: {e}"))
.as_str()
.unwrap()
.to_owned()
}
}
pub fn edit_commit(
git_path: &std::path::Path,
editor: &str,
initial: &str,
) -> eyre::Result<Option<String>> {
let edit_path = git_path.join("COMMIT_EDITMSG");
std::fs::write(&edit_path, initial)?;
let start = std::fs::metadata(&edit_path)?.modified()?;
let mut args = shlex::Shlex::new(editor);
let cmd = args.next().unwrap_or_else(|| "vi".to_owned());
let status = std::process::Command::new(cmd)
.args(args)
.arg(&edit_path)
.spawn()?
.wait()?;
if !status.success() {
eyre::bail!(
"failed to edit `{}` with `{}`: code {}",
edit_path.display(),
editor,
status
.code()
.map(|c| c.to_string())
.unwrap_or_else(|| "interrupted".to_owned())
);
}
let end = std::fs::metadata(&edit_path)?.modified()?;
if start == end {
return Ok(None);
}
let edited = std::fs::read_to_string(&edit_path)?;
if edited == initial {
return Ok(None);
}
let sanitized = sanitize_message(&edited);
if sanitized.is_empty() {
eyre::bail!("Aborting commit due to empty commit message.")
}
Ok(Some(sanitized))
}
pub(crate) fn sanitize_message(message: &str) -> String {
let mut lines = LinesWithTerminator::new(message).collect::<Vec<_>>();
lines.retain(|l| !l.starts_with('#'));
while !lines.is_empty() {
if lines.first().unwrap().trim().is_empty() {
lines.remove(0);
} else {
break;
}
}
while !lines.is_empty() {
if lines.last().unwrap().trim().is_empty() {
lines.pop();
} else {
break;
}
}
let message = lines.join("");
message.trim_end().to_owned()
}
#[derive(Clone, Debug)]
pub(crate) struct LinesWithTerminator<'a> {
data: &'a str,
}
impl<'a> LinesWithTerminator<'a> {
pub(crate) fn new(data: &'a str) -> LinesWithTerminator<'a> {
LinesWithTerminator { data }
}
}
impl<'a> Iterator for LinesWithTerminator<'a> {
type Item = &'a str;
#[inline]
fn next(&mut self) -> Option<&'a str> {
match self.data.find('\n') {
None if self.data.is_empty() => None,
None => {
let line = self.data;
self.data = "";
Some(line)
}
Some(end) => {
let line = &self.data[..end + 1];
self.data = &self.data[end + 1..];
Some(line)
}
}
}
}
#[derive(Copy, Clone, Debug, Default)]
#[non_exhaustive]
pub struct Palette {
pub error: anstyle::Style,
pub warn: anstyle::Style,
pub info: anstyle::Style,
pub good: anstyle::Style,
pub highlight: anstyle::Style,
pub hint: anstyle::Style,
}
impl Palette {
pub fn colored() -> Self {
Self {
error: anstyle::AnsiColor::Red.on_default() | anstyle::Effects::BOLD,
warn: anstyle::AnsiColor::Yellow.on_default() | anstyle::Effects::BOLD,
info: anstyle::AnsiColor::Blue.on_default() | anstyle::Effects::BOLD,
good: anstyle::AnsiColor::Cyan.on_default() | anstyle::Effects::BOLD,
highlight: anstyle::AnsiColor::Green.on_default() | anstyle::Effects::BOLD,
hint: anstyle::Effects::DIMMED.into(),
}
}
pub(crate) fn error<D: std::fmt::Display>(self, display: D) -> Styled<D> {
Styled::new(display, self.error)
}
pub(crate) fn warn<D: std::fmt::Display>(self, display: D) -> Styled<D> {
Styled::new(display, self.warn)
}
pub(crate) fn info<D: std::fmt::Display>(self, display: D) -> Styled<D> {
Styled::new(display, self.info)
}
pub(crate) fn good<D: std::fmt::Display>(self, display: D) -> Styled<D> {
Styled::new(display, self.good)
}
pub(crate) fn highlight<D: std::fmt::Display>(self, display: D) -> Styled<D> {
Styled::new(display, self.highlight)
}
pub(crate) fn hint<D: std::fmt::Display>(self, display: D) -> Styled<D> {
Styled::new(display, self.hint)
}
}
#[derive(Debug)]
pub(crate) struct Styled<D> {
display: D,
style: anstyle::Style,
}
impl<D: std::fmt::Display> Styled<D> {
pub(crate) fn new(display: D, style: anstyle::Style) -> Self {
Self { display, style }
}
}
impl<D: std::fmt::Display> std::fmt::Display for Styled<D> {
#[inline]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.style.render())?;
self.display.fmt(f)?;
write!(f, "{}", self.style.render_reset())?;
Ok(())
}
}
pub const STASH_STACK_NAME: &str = "git-stack";