Skip to content

Commit

Permalink
Add support for a help flag
Browse files Browse the repository at this point in the history
While lucky-commit has a pretty simple interface, because it operates
by side-effects it can be safer to check its behaviour.

Currently, to get the help it's necessary to be lucky enough to
mistakenly give it 2 args, thus failing to match any case. Otherwise,
it will panic if called from not-within-a-repository, and silently
rewrite the HEAD if invoked from one.
  • Loading branch information
masklinn authored and not-an-aardvark committed Nov 17, 2023
1 parent 0000000 commit 0000000
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,28 @@ use std::{
process::{self, Command, Stdio},
};

fn usage() -> ! {
eprintln!(
"\
Usage: lucky_commit [commit-hash-prefix]\n\
\n\
`commit-hash-prefix` must contain hex characters and underscores. An underscore indicates \
that the hash may have any value in the given position.
"
);
process::exit(1)
}
fn main() -> Result<(), ParseHashSpecErr> {
let args = env::args().collect::<Vec<String>>();
let maybe_prefix = match args.as_slice() {
[_, arg] if arg == "--benchmark" => {
benchmark::run_benchmark();
process::exit(0)
}
[_, arg] if arg == "-h" || arg == "--help" => usage(),
[_, prefix] => Some(prefix.as_str()),
[_] => None,
_ => {
eprintln!("\
Usage: lucky_commit [commit-hash-prefix]\n\
\n\
`commit-hash-prefix` must contain hex characters and underscores. An underscore indicates \
that the hash may have any value in the given position.
");
process::exit(1)
}
_ => usage(),
};

let existing_commit = spawn_git(&["cat-file", "commit", "HEAD"], None);
Expand Down

0 comments on commit 0000000

Please sign in to comment.