-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathsink.rs
More file actions
207 lines (188 loc) · 7.48 KB
/
sink.rs
File metadata and controls
207 lines (188 loc) · 7.48 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
// SPDX-License-Identifier: GPL-2.0-only
//! `stg sink` implementation.
use anyhow::{anyhow, Result};
use clap::{Arg, ArgMatches};
use crate::{
argset,
color::get_color_stdout,
ext::RepositoryExtended,
patch::{patchrange, LocationConstraint, PatchLocator, PatchName, PatchRange, RangeConstraint},
stack::{InitializationPolicy, Stack, StackStateAccess},
stupid::Stupid,
};
pub(super) const STGIT_COMMAND: super::StGitCommand = super::StGitCommand {
name: "sink",
category: super::CommandCategory::StackManipulation,
make,
run,
};
fn make() -> clap::Command {
clap::Command::new(STGIT_COMMAND.name)
.about("Reorder patches by moving them toward the bottom of the stack")
.long_about(
"Reorder patches by moving them toward the bottom of the stack.\n\
\n\
If no patch is specified on the command line, the current (topmost) patch \
is moved. By default, patches are moved to the bottom of the stack, but the \
'--above' or '--below' (alias '--to') options may be used to place them \
above or below any applied patch.\n\
\n\
Internally, this operation involves popping all patches to the bottom (or to \
the target patch if '--above' or '--below' is used), then pushing the patches \
being moved, and then, unless '--nopush' is specified, pushing back any other \
formerly applied patches.\n\
\n\
This may be useful, for example, to group stable patches at the bottom \
of the stack where they are less likely to be impacted by the push of another \
patch, and from where they can be more easily committed or pushed to \
another repository.\n\
",
)
.arg(
Arg::new("patchranges")
.help("Patches to sink")
.value_name("patch")
.num_args(1..)
.allow_hyphen_values(true)
.value_parser(clap::value_parser!(PatchRange)),
)
.arg(
Arg::new("nopush")
.long("nopush")
.short('n')
.help("Do not push any patches back after sinking")
.long_help(
"Do not push any formerly applied patches after sinking. \
Only the patches to sink are pushed.",
)
.action(clap::ArgAction::SetTrue),
)
.arg(
Arg::new("target-below")
.long("below")
.visible_alias("to")
.short('t')
.help("Sink patches below <target> patch")
.long_help(
"Sink patches below <target> patch.\n\
\n\
Specified patches are placed below <target> instead of at the \
bottom of the stack.",
)
.value_name("target")
.allow_hyphen_values(true)
.value_parser(clap::value_parser!(PatchLocator)),
)
.arg(
Arg::new("target-above")
.long("above")
.short('T')
.help("Sink patches above <target> patch")
.long_help(
"Sink patches above <target> patch.\n\
\n\
Specified patches are placed above <target> instead of at the \
bottom of the stack.",
)
.value_name("target")
.allow_hyphen_values(true)
.value_parser(clap::value_parser!(PatchLocator))
.conflicts_with("target-below"),
)
.arg(argset::keep_arg())
.arg(argset::committer_date_is_author_date_arg())
}
fn run(matches: &ArgMatches) -> Result<()> {
let repo = gix::Repository::open()?;
let stack = Stack::current(&repo, InitializationPolicy::AllowUninitialized)?;
let stupid = repo.stupid();
let nopush_flag = matches.get_flag("nopush");
let keep_flag = matches.contains_id("keep");
repo.check_repository_state()?;
let statuses = stupid.statuses(None)?;
statuses.check_conflicts()?;
stack.check_head_top_mismatch()?;
if !keep_flag {
statuses.check_index_and_worktree_clean()?;
}
let is_above = matches.contains_id("target-above");
let opt_target: Option<PatchName> = matches
.get_one::<PatchLocator>("target-above")
.or_else(|| matches.get_one::<PatchLocator>("target-below"))
.map(|loc| loc.resolve_name(&stack))
.transpose()
.map_err(|e| anyhow!("target: {e}"))?
.map(|name| name.constrain(&stack, LocationConstraint::Applied))
.transpose()
.map_err(|e| match e {
crate::patch::name::Error::PatchNotAllowed { patchname, .. } => {
anyhow!(
"cannot sink {} `{patchname}` since it is not applied",
if is_above { "above" } else { "below" }
)
}
_ => e.into(),
})?;
let patches: Vec<PatchName> =
if let Some(range_specs) = matches.get_many::<PatchRange>("patchranges") {
patchrange::resolve_names(&stack, range_specs, RangeConstraint::All)?
} else if let Some(patchname) = stack.applied().last() {
vec![patchname.clone()]
} else {
return Err(super::Error::NoAppliedPatches.into());
};
if let Some(target_patch) = &opt_target {
if patches.contains(target_patch) {
return Err(anyhow!(
"target patch `{target_patch}` may not also be a patch to sink",
));
}
}
let mut remaining_unapplied: Vec<PatchName> = stack
.unapplied()
.iter()
.filter(|pn| !patches.contains(pn))
.cloned()
.collect();
let mut remaining_applied: Vec<PatchName> = stack
.applied()
.iter()
.filter(|pn| !patches.contains(pn))
.cloned()
.collect();
let target_pos = if let Some(target_patch) = &opt_target {
remaining_applied
.iter()
.position(|pn| pn == target_patch)
.expect("already validated that target is applied")
+ if is_above { 1 } else { 0 }
} else {
0
};
let mut patches = patches;
let (applied, unapplied) = if nopush_flag {
let mut applied: Vec<PatchName> = Vec::with_capacity(target_pos + patches.len());
applied.extend(remaining_applied.drain(0..target_pos));
applied.append(&mut patches);
let mut unapplied: Vec<PatchName> =
Vec::with_capacity(remaining_applied.len() + remaining_unapplied.len());
unapplied.append(&mut remaining_applied);
unapplied.append(&mut remaining_unapplied);
(applied, unapplied)
} else {
let mut applied: Vec<PatchName> =
Vec::with_capacity(remaining_applied.len() + patches.len());
applied.extend(remaining_applied.drain(0..target_pos));
applied.append(&mut patches);
applied.append(&mut remaining_applied);
(applied, remaining_unapplied)
};
stack
.setup_transaction()
.use_index_and_worktree(true)
.committer_date_is_author_date(matches.get_flag("committer-date-is-author-date"))
.with_output_stream(get_color_stdout(matches))
.transact(|trans| trans.reorder_patches(Some(&applied), Some(&unapplied), None))
.execute("sink")?;
Ok(())
}