-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathmod.rs
More file actions
218 lines (201 loc) · 7.36 KB
/
mod.rs
File metadata and controls
218 lines (201 loc) · 7.36 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
// SPDX-License-Identifier: GPL-2.0-only
//! `stg branch` implementation.
mod cleanup;
mod clone;
mod create;
mod delete;
mod describe;
mod list;
mod protect;
mod rename;
mod reset;
mod unprotect;
use anyhow::Result;
use bstr::ByteSlice;
use crate::{
branchloc::BranchLocator, ext::RepositoryExtended, stupid::Stupid, wrap::PartialRefName,
};
pub(super) const STGIT_COMMAND: super::StGitCommand = super::StGitCommand {
name: "branch",
category: super::CommandCategory::StackManipulation,
make,
run,
};
fn make() -> clap::Command {
clap::Command::new(STGIT_COMMAND.name)
.about("Branch operations: switch, list, create, rename, delete, ...")
.long_about(
"Create, clone, switch, rename, or delete StGit-enabled branches.\n\
\n\
With no arguments, the current branch is printed to stdout.\n\
\n\
With a single argument, switch to the named branch.\n\
\n\
StGit supports specifying a branch using the `@{-<n>}` syntax supported \
by git, including `-` as a synonym for `@{-1}`. Thus `stg branch -` may \
be used to switch to the last checked-out HEAD. Note that `@{-<n>}` \
refers to the <n>th last HEAD, which is not necessarily a local branch. \
Using an `@{-<n>}` value that refers to anything but a local branch will \
result in an error.",
)
.disable_help_subcommand(true)
.args_conflicts_with_subcommands(true)
.override_usage(super::make_usage(
"stg branch",
&[
"",
"[--merge] <branch>",
"{--list,-l}",
"{--create,-c} <new-branch> [committish]",
"{--clone,-C} [new-branch]",
"{--rename,-r} [old-name] <new-name>",
"{--protect,-p} [branch]",
"{--unprotect,-u} [branch]",
"{--delete,-D} [--force] [branch]",
"--cleanup [--force] [branch]",
"{--describe,-d} <description> [branch]",
"--reset [branch]",
],
))
.subcommand(self::list::command())
.subcommand(self::create::command())
.subcommand(self::clone::command())
.subcommand(self::rename::command())
.subcommand(self::protect::command())
.subcommand(self::unprotect::command())
.subcommand(self::delete::command())
.subcommand(self::cleanup::command())
.subcommand(self::describe::command())
.subcommand(self::reset::command())
.arg(
clap::Arg::new("merge")
.long("merge")
.help("Merge work tree changes into the other branch")
.action(clap::ArgAction::SetTrue)
.requires("branch-any"),
)
.arg(
clap::Arg::new("branch-any")
.help("Branch to switch to")
.value_name("branch")
.value_parser(clap::value_parser!(BranchLocator)),
)
}
fn run(matches: &clap::ArgMatches) -> Result<()> {
let repo = gix::Repository::open()?;
if let Some((subname, submatches)) = matches.subcommand() {
match subname {
"--list" => self::list::dispatch(&repo, submatches),
"--create" => self::create::dispatch(&repo, submatches),
"--clone" => self::clone::dispatch(&repo, submatches),
"--rename" => self::rename::dispatch(&repo, submatches),
"--protect" => self::protect::dispatch(&repo, submatches),
"--unprotect" => self::unprotect::dispatch(&repo, submatches),
"--delete" => self::delete::dispatch(&repo, submatches),
"--cleanup" => self::cleanup::dispatch(&repo, submatches),
"--describe" => self::describe::dispatch(&repo, submatches),
"--reset" => self::reset::dispatch(&repo, submatches),
s => panic!("unhandled branch subcommand {s}"),
}
} else if let Some(target_branch_loc) = matches.get_one::<BranchLocator>("branch-any") {
switch(&repo, matches, target_branch_loc)
} else if let Ok(branch) = repo.get_current_branch() {
println!("{}", branch.get_branch_name()?);
Ok(())
} else {
// Print nothing if HEAD is detached; same as `git branch --show-current`.
Ok(())
}
}
fn switch(
repo: &gix::Repository,
matches: &clap::ArgMatches,
target_branch_loc: &BranchLocator,
) -> Result<()> {
let current_branch = repo.get_current_branch().ok();
let current_branchname = current_branch
.as_ref()
.and_then(|branch| branch.get_branch_partial_name().ok());
let target_branch = target_branch_loc.resolve(repo)?;
let target_branchname = target_branch.get_branch_partial_name()?;
if Some(&target_branchname) == current_branchname.as_ref() {
return Err(anyhow::anyhow!(
"{target_branchname} is already the current branch"
));
}
let stupid = repo.stupid();
let statuses = stupid.statuses(None)?;
if !matches.get_flag("merge") {
statuses.check_worktree_clean()?;
}
statuses.check_conflicts()?;
stupid.checkout(target_branchname.as_ref())
}
fn set_description(
repo: &gix::Repository,
branchname: &PartialRefName,
description: &str,
) -> Result<()> {
let mut local_config_file = repo.local_config_file()?;
if description.is_empty() {
if let Ok(mut value) =
local_config_file.raw_value_mut_by("branch", Some(branchname.into()), "description")
{
value.delete();
}
if let Ok(section) = local_config_file.section("branch", Some(branchname.into())) {
if section.num_values() == 0 {
local_config_file.remove_section_by_id(section.id());
}
}
} else {
local_config_file.set_raw_value_by(
"branch",
Some(branchname.into()),
"description",
description,
)?;
}
repo.write_local_config(local_config_file)?;
Ok(())
}
fn get_stgit_parent(config: &gix::config::Snapshot, branchname: &PartialRefName) -> Option<String> {
config
.string_by(
"branch",
Some(format!("{branchname}.stgit").as_str().into()),
"parentbranch",
)
.and_then(|bs| bs.to_str().ok().map(str::to_string))
}
fn set_stgit_parent(
repo: &gix::Repository,
branchname: &PartialRefName,
parent_branchname: Option<&PartialRefName>,
) -> Result<()> {
let subsection = format!("{branchname}.stgit");
let mut local_config_file = repo.local_config_file()?;
if let Some(parent_branchname) = parent_branchname {
local_config_file.set_raw_value_by(
"branch",
Some(subsection.as_str().into()),
"parentbranch",
parent_branchname.as_ref(),
)?;
} else {
if let Ok(mut value) = local_config_file.raw_value_mut_by(
"branch",
Some(subsection.as_str().into()),
"parentbranch",
) {
value.delete();
}
if let Ok(section) = local_config_file.section("branch", Some(subsection.as_str().into())) {
if section.num_values() == 0 {
local_config_file.remove_section_by_id(section.id());
}
}
}
repo.write_local_config(local_config_file)?;
Ok(())
}