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
use std::convert::TryInto;
use std::ffi::OsString;
use std::path::PathBuf;
use branchless::commands::wrap;
use branchless::core::formatting::Glyphs;
use branchless::git::{GitRunInfo, NonZeroOid};
use branchless::tui::Effects;
use structopt::StructOpt;
#[derive(StructOpt)]
enum WrappedCommand {
#[structopt(external_subcommand)]
WrappedCommand(Vec<String>),
}
/// Branchless workflow for Git.
///
/// See the documentation at https://github.com/arxanas/git-branchless/wiki.
#[derive(StructOpt)]
#[structopt(version = env!("CARGO_PKG_VERSION"), author = "Waleed Khan <me@waleedkhan.name>")]
enum Opts {
/// Initialize the branchless workflow for this repository.
Init {
/// Uninstall the branchless workflow instead of initializing it.
#[structopt(long = "--uninstall")]
uninstall: bool,
},
/// Display a nice graph of the commits you've recently worked on.
Smartlog,
/// Hide the provided commits from the smartlog.
Hide {
/// Zero or more commits to hide.
///
/// Can either be hashes, like `abc123`, or ref-specs, like `HEAD^`.
commits: Vec<String>,
/// Also recursively hide all children commits of the provided commits.
#[structopt(short = "-r", long = "--recursive")]
recursive: bool,
},
/// Unhide previously-hidden commits from the smartlog.
Unhide {
/// Zero or more commits to unhide.
///
/// Can either be hashes, like `abc123`, or ref-specs, like `HEAD^`.
commits: Vec<String>,
/// Also recursively unhide all children commits of the provided commits.
#[structopt(short = "-r", long = "--recursive")]
recursive: bool,
},
/// Move to an earlier commit in the current stack.
Prev {
/// The number of commits backward to go.
num_commits: Option<isize>,
},
/// Move to a later commit in the current stack.
Next {
/// The number of commits forward to go.
///
/// If not provided, defaults to 1.
num_commits: Option<isize>,
/// When encountering multiple next commits, choose the oldest.
#[structopt(short = "-o", long = "--oldest")]
oldest: bool,
/// When encountering multiple next commits, choose the newest.
#[structopt(short = "-n", long = "--newest", conflicts_with("oldest"))]
newest: bool,
},
/// Move a subtree of commits from one location to another.
///
/// By default, `git move` tries to move the entire current stack if you
/// don't pass a `--source` or `--base` option (equivalent to writing
/// `--base HEAD`).
///
/// By default, `git move` attempts to rebase all commits in-memory. If you
/// want to force an on-disk rebase, pass the `--on-disk` flag. Note that
/// `post-commit` hooks are not called during in-memory rebases.
Move {
/// The source commit to move. This commit, and all of its descendants,
/// will be moved.
#[structopt(short = "-s", long = "--source")]
source: Option<String>,
/// A commit inside a subtree to move. The entire subtree, starting from
/// the main branch, will be moved, not just the commits descending from
/// this commit.
#[structopt(short = "-b", long = "--base", conflicts_with = "source")]
base: Option<String>,
/// The destination commit to move all source commits onto. If not
/// provided, defaults to the current commit.
#[structopt(short = "-d", long = "--dest")]
dest: Option<String>,
/// Only attempt to perform an in-memory rebase. If it fails, do not
/// attempt an on-disk rebase.
#[structopt(long = "--in-memory", conflicts_with = "force_on_disk")]
force_in_memory: bool,
/// Skip attempting to use an in-memory rebase, and try an
/// on-disk rebase directly.
#[structopt(long = "--on-disk")]
force_on_disk: bool,
/// Debugging option. Print the constraints used to create the rebase
/// plan before executing it.
#[structopt(long = "--debug-dump-rebase-constraints")]
dump_rebase_constraints: bool,
/// Debugging option. Print the rebase plan that will be executed before
/// executing it.
#[structopt(long = "--debug-dump-rebase-plan")]
dump_rebase_plan: bool,
},
/// Fix up commits abandoned by a previous rewrite operation.
Restack {
/// The IDs of the abandoned commits whose descendants should be
/// restacked. If not provided, all abandoned commits are restacked.
commits: Vec<String>,
/// Debugging option. Print the constraints used to create the rebase
/// plan before executing it.
#[structopt(long = "--debug-dump-rebase-constraints")]
dump_rebase_constraints: bool,
/// Debugging option. Print the rebase plan that will be executed before
/// executing it.
#[structopt(long = "--debug-dump-rebase-plan")]
dump_rebase_plan: bool,
},
/// Browse or return to a previous state of the repository.
Undo,
/// Run internal garbage collection.
Gc,
/// Wrap a Git command inside a branchless transaction.
Wrap {
#[structopt(long = "--git-executable")]
git_executable: Option<PathBuf>,
#[structopt(subcommand)]
command: WrappedCommand,
},
/// Internal use.
HookPreAutoGc,
/// Internal use.
HookPostRewrite { rewrite_type: String },
/// Internal use.
HookRegisterExtraPostRewriteHook,
/// Internal use.
HookDetectEmptyCommit { old_commit_oid: NonZeroOid },
/// Internal use.
HookSkipUpstreamAppliedCommit { commit_oid: NonZeroOid },
/// Internal use.
HookPostCheckout {
previous_commit: String,
current_commit: String,
is_branch_checkout: isize,
},
/// Internal use.
HookPostCommit,
/// Internal use.
HookReferenceTransaction { transaction_state: String },
}
fn main() -> eyre::Result<()> {
color_eyre::install()?;
install_tracing();
let opts = Opts::from_args();
let path_to_git = std::env::var_os("PATH_TO_GIT").unwrap_or_else(|| OsString::from("git"));
let path_to_git = PathBuf::from(&path_to_git);
let git_run_info = GitRunInfo {
path_to_git,
working_directory: std::env::current_dir()?,
env: std::env::vars_os().collect(),
};
let effects = Effects::new(Glyphs::detect());
let exit_code = match opts {
Opts::Init { uninstall: false } => {
branchless::commands::init::init(&effects, &git_run_info)?;
0
}
Opts::Init { uninstall: true } => {
branchless::commands::init::uninstall(&effects)?;
0
}
Opts::Smartlog => {
branchless::commands::smartlog::smartlog(&effects)?;
0
}
Opts::Hide { commits, recursive } => {
branchless::commands::hide::hide(&effects, commits, recursive)?
}
Opts::Unhide { commits, recursive } => {
branchless::commands::hide::unhide(&effects, commits, recursive)?
}
Opts::Prev { num_commits } => {
branchless::commands::navigation::prev(&effects, &git_run_info, num_commits)?
}
Opts::Next {
num_commits,
oldest,
newest,
} => {
let towards = match (oldest, newest) {
(false, false) => None,
(true, false) => Some(branchless::commands::navigation::Towards::Oldest),
(false, true) => Some(branchless::commands::navigation::Towards::Newest),
(true, true) => eyre::bail!("Both --oldest and --newest were set"),
};
branchless::commands::navigation::next(&effects, &git_run_info, num_commits, towards)?
}
Opts::Move {
source,
dest,
base,
force_in_memory,
force_on_disk,
dump_rebase_constraints,
dump_rebase_plan,
} => branchless::commands::r#move::r#move(
&effects,
&git_run_info,
source,
dest,
base,
force_in_memory,
force_on_disk,
dump_rebase_constraints,
dump_rebase_plan,
)?,
Opts::Restack {
commits,
dump_rebase_constraints,
dump_rebase_plan,
} => branchless::commands::restack::restack(
&effects,
&git_run_info,
commits,
dump_rebase_constraints,
dump_rebase_plan,
)?,
Opts::Undo => branchless::commands::undo::undo(&effects, &git_run_info)?,
Opts::Gc | Opts::HookPreAutoGc => {
branchless::commands::gc::gc(&effects)?;
0
}
Opts::Wrap {
git_executable: explicit_git_executable,
command: WrappedCommand::WrappedCommand(args),
} => {
let git_run_info = match explicit_git_executable {
Some(path_to_git) => GitRunInfo {
path_to_git,
..git_run_info
},
None => git_run_info,
};
let exit_code = wrap::wrap(&git_run_info, args.as_slice())?;
exit_code
}
Opts::HookPostRewrite { rewrite_type } => {
branchless::commands::hooks::hook_post_rewrite(&effects, &git_run_info, &rewrite_type)?;
0
}
Opts::HookRegisterExtraPostRewriteHook => {
branchless::commands::hooks::hook_register_extra_post_rewrite_hook()?;
0
}
Opts::HookDetectEmptyCommit { old_commit_oid } => {
branchless::commands::hooks::hook_drop_commit_if_empty(&effects, old_commit_oid)?;
0
}
Opts::HookSkipUpstreamAppliedCommit { commit_oid } => {
branchless::commands::hooks::hook_skip_upstream_applied_commit(&effects, commit_oid)?;
0
}
Opts::HookPostCheckout {
previous_commit,
current_commit,
is_branch_checkout,
} => {
branchless::commands::hooks::hook_post_checkout(
&effects,
&previous_commit,
¤t_commit,
is_branch_checkout,
)?;
0
}
Opts::HookPostCommit => {
branchless::commands::hooks::hook_post_commit(&effects)?;
0
}
Opts::HookReferenceTransaction { transaction_state } => {
branchless::commands::hooks::hook_reference_transaction(&effects, &transaction_state)?;
0
}
};
let exit_code: i32 = exit_code.try_into()?;
std::process::exit(exit_code)
}
fn install_tracing() {
// From https://github.com/yaahc/color-eyre/blob/07b9f0351544e2b07fcd173dc1fc602a7fc8bb6b/examples/usage.rs
// Licensed under MIT.
use tracing_error::ErrorLayer;
use tracing_subscriber::prelude::*;
use tracing_subscriber::{fmt, EnvFilter};
let filter_layer = EnvFilter::try_from_default_env()
.or_else(|_| EnvFilter::try_new("warn"))
.unwrap();
let fmt_layer = fmt::layer()
.with_span_events(fmt::format::FmtSpan::CLOSE)
.with_target(false);
tracing_subscriber::registry()
.with(filter_layer)
.with(fmt_layer)
.with(ErrorLayer::default())
.init();
}