-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathnew.rs
More file actions
227 lines (212 loc) · 8.58 KB
/
Copy pathnew.rs
File metadata and controls
227 lines (212 loc) · 8.58 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
// SPDX-License-Identifier: GPL-2.0-only
//! `stg new` implementation.
use std::path::PathBuf;
use anyhow::{anyhow, Result};
use clap::{Arg, ArgGroup, ArgMatches};
use super::refresh;
use crate::{
color::get_color_stdout,
ext::{RepositoryExtended, SignatureExtended},
patch::{patchedit, PatchName},
stack::{InitializationPolicy, Stack, StackAccess, StackStateAccess},
stupid::Stupid,
};
pub(super) const STGIT_COMMAND: super::StGitCommand = super::StGitCommand {
name: "new",
category: super::CommandCategory::PatchManipulation,
make,
run,
};
fn make() -> clap::Command {
let app = clap::Command::new("new")
.about("Create a new patch at top of the stack")
.long_about(
"Create a new, empty patch on the current stack. The new patch is created \
on top of the currently applied patches, and is made the new top of the \
stack. Uncommitted changes in the work tree are not included in the patch \
-- that is handled by stg-refresh.\n\
\n\
The given patch name must be unique in the stack. If no name is given, \
one is generated from the first line of the patch's commit message.\n\
\n\
Patch names follow the rules for Git references with the additional \
constraint that patch names may not contain the '/' character. See \
git-check-ref-format(1) for details.\n\
\n\
Patch names may start with a leading '-'. When specifying such a patch \
name on the command line, the leading '-' may be escaped with a single \
backslash as in '\\-patch-name' to disambiguate the patch name from \
command line options.\n\
\n\
An editor will be launched to edit the commit message to be used for the \
patch, unless the '--message' flag already specified one. The \
'patchdescr.tmpl' template file (if available) is used to pre-fill the \
editor.",
)
.override_usage(super::make_usage(
"stg new",
&[
"[OPTIONS] [patchname] [-- <path>...]",
"[OPTIONS] [--name <patchname>] [-- <path>...]",
],
))
.arg(
Arg::new("patchname")
.help("Name for new patch")
.value_parser(clap::value_parser!(PatchName)),
)
.arg(
Arg::new("pathspecs")
.help("Refresh files matching path(s)")
.long_help(
"Refresh files matching path(s). \
Specifying paths implies '--refresh'. \
Using '--refresh' without any paths will target all modified files.",
)
.value_name("path")
.last(true)
.num_args(1..)
.value_parser(clap::value_parser!(PathBuf))
.conflicts_with("save-template"),
)
.arg(
Arg::new("name")
.long("name")
.short('n')
.help("Name for new patch")
.long_help(
"Alternative to the [patchname] argument for specifying the name \
of the new patch. This option allows the patch name to start with \
an unescaped leading '-'.",
)
.value_name("name")
.allow_hyphen_values(true)
.value_parser(clap::value_parser!(PatchName))
.conflicts_with("patchname"),
)
.next_help_heading("Refresh Options")
.arg(
Arg::new("refresh")
.long("refresh")
.short('r')
.help("Refresh new patch with changes from work tree or index")
.long_help(
"Refresh the new patch with changes from work tree. \
New patches are empty by default, but with this option \
the new patch will capture outstanding changes in the work \
tree as if 'stg refresh' was run. \
Use '--index' to refresh from the index instead of the work tree.",
)
.action(clap::ArgAction::SetTrue)
.conflicts_with("save-template"),
)
.arg(
Arg::new("index")
.long("index")
.short('i')
.help("Refresh from index instead of work tree")
.long_help(
"Instead of refreshing the patch with the current \
contents of the worktree, use the current contents \
of the index.",
)
.requires("refresh")
.action(clap::ArgAction::SetTrue)
.conflicts_with_all(["pathspecs", "submodules", "force"]),
)
.arg(
Arg::new("force")
.long("force")
.short('F')
.help("Force refresh with staged and unstaged changes")
.long_help(
"Force refresh with staged and unstaged changes.\n\
\n\
By default, if there are staged changes in the index along with \
unstaged changes in the work tree, the command will abort. This \
option forces the command to proceed using both the staged and \
unstaged changes.",
)
.requires("refresh")
.action(clap::ArgAction::SetTrue)
.conflicts_with("index"),
)
.arg(
Arg::new("submodules")
.long("submodules")
.help("Include submodules in patch content")
.action(clap::ArgAction::SetTrue)
.requires("refresh"),
)
.arg(
Arg::new("no-submodules")
.long("no-submodules")
.help("Exclude submodules in patch content")
.action(clap::ArgAction::SetTrue)
.requires("refresh"),
)
.group(ArgGroup::new("submodule-group").args(["submodules", "no-submodules"]));
patchedit::add_args(app, true, true)
}
fn run(matches: &ArgMatches) -> Result<()> {
let repo = gix::Repository::open()?;
let stack = Stack::current(&repo, InitializationPolicy::AutoInitialize)?;
let stupid = repo.stupid();
repo.check_repository_state()?;
let statuses = stupid.statuses(None)?;
statuses.check_conflicts()?;
stack.check_head_top_mismatch()?;
let patchname = if let Some(patchname) = matches
.get_one::<PatchName>("patchname")
.or_else(|| matches.get_one::<PatchName>("name"))
.cloned()
{
if let Some(colliding_patchname) = stack.collides(&patchname) {
Err(anyhow!("patch `{colliding_patchname}` already exists"))
} else {
Ok(Some(patchname))
}
} else {
Ok(None)
}?;
let is_refreshing = matches.get_flag("refresh") || matches.contains_id("pathspecs");
let tree_id = if is_refreshing {
refresh::assemble_refresh_tree(&stack, matches, None)?
} else {
stack.get_branch_head().tree_id()?.detach()
};
let parent_id = stack.get_branch_head().id;
let (patchname, commit_id) = match patchedit::EditBuilder::default()
.allow_autosign(true)
.allow_diff_edit(false)
.allow_implicit_edit(true)
.allow_template_save(!is_refreshing)
.original_patchname(patchname.as_ref())
.default_author(repo.get_author()?.override_author(matches)?)
.override_tree_id(tree_id)
.override_parent_id(parent_id)
.edit(&stack, &repo, matches)?
{
patchedit::EditOutcome::TemplateSaved(_) => return Ok(()),
patchedit::EditOutcome::Edited {
new_patchname,
new_commit_id,
} => (
new_patchname
.or(patchname)
.expect("either have original or new patchname"),
new_commit_id.expect("must have new commit id because no original patch commit"),
),
};
if let Some(template_path) = matches.get_one::<PathBuf>("save-template") {
let patch_commit = repo.find_commit(commit_id)?;
std::fs::write(template_path, patch_commit.message_raw()?)?;
return Ok(());
}
stack
.setup_transaction()
.with_output_stream(get_color_stdout(matches))
.transact(|trans| trans.new_applied(&patchname, commit_id))
.execute(&format!("new: {patchname}"))?;
Ok(())
}