Skip to content

Commit

Permalink
fix: issue where odd length prefixes did not work
Browse files Browse the repository at this point in the history
  • Loading branch information
acheronfail committed May 10, 2020
1 parent fae1f69 commit fae13e2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,22 @@ use spiral::Spiral;

// Validate prefix is hex or handle special "hook" value.
fn validate_prefix(prefix: &str) {
match hex::decode(&prefix) {
// In order to validate as hex the string must be of even length.
let normalised_prefix = if prefix.len() % 2 == 0 {
String::from(prefix)
} else {
format!("{}0", prefix)
};

match hex::decode(normalised_prefix) {
Ok(_) => {}
Err(_) => {
if prefix == "hook" {
git::create_post_commit_hook().expect("Failed to create git hook");
process::exit(0);
}

println!(
eprintln!(
"The prefix must only contain hex characters! Got: {}",
&prefix,
);
Expand Down
31 changes: 31 additions & 0 deletions tests_integration/test_prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,37 @@ Patching last commit to include new hash... Success!
assert_eq!(expected, stdout);
});

// Works with a prefix of odd length.
gashtest!(allows_prefixes_of_odd_length, |mut tcmd: TestCommand| {
let odd_prefix = "123";
let stdout = tcmd.args(&[odd_prefix]).stdout();

let expected = format!(
"\
Searching for hash with prefix {prefix}
Found hash {prefix}{hash}
Patching last commit to include new hash... Success!
",
prefix = odd_prefix,
hash = &git_last_hash(tcmd.dir())[odd_prefix.len()..]
);

assert_eq!(expected, stdout);
});

// Does not allow non-hex characters as prefix.
gashtest!(does_not_allow_non_hex_chars, |mut tcmd: TestCommand| {
let bad_prefix = "hello";
let stderr = tcmd.args(&[bad_prefix]).stderr();
let expected = format!(
"\
The prefix must only contain hex characters! Got: {}
",
bad_prefix
);
assert_eq!(expected, stderr);
});

// Does not patch the commit with --dry-run.
gashtest!(dry_run_long_prefix, |mut tcmd: TestCommand| {
let hash_before = git_last_hash(tcmd.dir());
Expand Down

0 comments on commit fae13e2

Please sign in to comment.