-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathfloat.rs
More file actions
181 lines (163 loc) · 5.68 KB
/
float.rs
File metadata and controls
181 lines (163 loc) · 5.68 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
// SPDX-License-Identifier: GPL-2.0-only
//! `stg float` implementation.
use std::{
path::{Path, PathBuf},
str::FromStr,
};
use anyhow::{anyhow, Context, Result};
use clap::{Arg, ArgMatches};
use crate::{
argset,
color::get_color_stdout,
ext::RepositoryExtended,
patch::{patchrange, PatchName, PatchRange, RangeConstraint},
stack::{InitializationPolicy, Stack, StackStateAccess},
stupid::Stupid,
};
pub(super) const STGIT_COMMAND: super::StGitCommand = super::StGitCommand {
name: "float",
category: super::CommandCategory::StackManipulation,
make,
run,
};
fn make() -> clap::Command {
clap::Command::new(STGIT_COMMAND.name)
.about("Reorder patches by moving them to the top of the stack")
.long_about(
"Reorder patches by moving them to the top of the stack.\n\
\n\
Move one or more patches to become the topmost applied patches. The patches \
may currently be either applied or unapplied. The necessary pop and push \
operations will be performed to reorder the named patches. Patches not \
specified will remain applied or unapplied as they were prior to the \
operation.",
)
.override_usage(super::make_usage(
"stg float",
&["[OPTIONS] <patch>...", "[OPTIONS] <-S|--series> <file>"],
))
.arg(
Arg::new("patchranges")
.help("Patches to float")
.value_name("patch")
.num_args(1..)
.allow_hyphen_values(true)
.value_parser(clap::value_parser!(PatchRange))
.conflicts_with("series")
.required_unless_present("series"),
)
.arg(
Arg::new("noapply")
.long("noapply")
.help("Reorder patches without reapplying any patches")
.action(clap::ArgAction::SetTrue),
)
.arg(
Arg::new("series")
.long("series")
.short('S')
.help("Rearrange according to a series <file>")
.value_name("file")
.value_hint(clap::ValueHint::FilePath)
.value_parser(clap::value_parser!(PathBuf)),
)
.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 noapply_flag = matches.get_flag("noapply");
let keep_flag = matches.get_flag("keep");
let opt_series = matches.get_one::<PathBuf>("series").map(PathBuf::as_path);
repo.check_repository_state()?;
let statuses = stupid.statuses(None)?;
statuses.check_conflicts()?;
stack.check_head_top_mismatch()?;
let patches: Vec<PatchName> = if let Some(series_path) = opt_series {
parse_series(series_path, &stack)?
} else {
let range_specs = matches
.get_many::<PatchRange>("patchranges")
.expect("clap ensures either patches or series");
patchrange::resolve_names(&stack, range_specs, RangeConstraint::Visible)?
};
if patches.is_empty() {
return Err(anyhow!("no patches to float"));
}
if !keep_flag && (!noapply_flag || patches.iter().any(|pn| stack.is_applied(pn))) {
statuses.check_index_and_worktree_clean()?;
}
let (applied, unapplied) = if noapply_flag {
let applied: Vec<PatchName> = stack
.applied()
.iter()
.filter(|pn| !patches.contains(pn))
.cloned()
.collect();
let unapplied: Vec<PatchName> = patches
.iter()
.chain(stack.unapplied().iter().filter(|pn| !patches.contains(pn)))
.cloned()
.collect();
(applied, unapplied)
} else {
let applied: Vec<PatchName> = stack
.applied()
.iter()
.filter(|pn| !patches.contains(pn))
.chain(patches.iter())
.cloned()
.collect();
let unapplied: Vec<PatchName> = stack
.unapplied()
.iter()
.filter(|pn| !patches.contains(pn))
.cloned()
.collect();
(applied, 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("float")?;
Ok(())
}
fn parse_series(path: &Path, stack: &Stack) -> Result<Vec<PatchName>> {
let use_stdin = path == Path::new("-");
let contents: String = if use_stdin {
use std::io::Read;
let mut stdin = std::io::stdin();
let mut contents = String::new();
stdin.read_to_string(&mut contents)?;
contents
} else {
std::fs::read_to_string(path)?
};
let mut series: Vec<PatchRange> = Vec::new();
for s in contents
.lines()
.map(|line| {
if let Some((content, _comment)) = line.split_once('#') {
content
} else {
line
}
.trim()
})
.filter(|s| !s.is_empty())
{
series.push(PatchRange::from_str(s)?);
}
patchrange::resolve_names(stack, series.iter(), RangeConstraint::Visible).with_context(|| {
if use_stdin {
"<stdin>".to_string()
} else {
path.to_string_lossy().to_string()
}
})
}