Created
August 19, 2018 07:29
-
-
Save vanzaj/503c57c645f83df89f7cef5a29292c3c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
hash_string() { | |
if [[ -z "$1" ]]; then | |
echo "Usage: $0 input_string [hash_length]" | |
exit 0 | |
fi | |
echo "$1" | git hash-object --stdin | cut -c 1-"${2:-8}" | |
} | |
# testing | |
huston(){ | |
if [[ -z $any_problems ]]; then | |
echo "$(tput setaf 2)ALL PASS $(tput sgr0)" | |
else | |
echo "$(tput setaf 1)PROBLEMS $(tput sgr0)" | |
fi | |
} | |
assert_equals() { | |
local expected="$1" | |
local actual="$2" | |
if diff <(echo "$expected") <(echo "$actual"); then | |
echo "$(tput setaf 2)PASS $(tput sgr0)" | |
true | |
else | |
echo "$(tput setaf 1)FAIL $(tput sgr0)" | |
any_problems=true | |
false | |
fi | |
} | |
run_tests() { | |
assert_equals "Usage:" "$(hash_string | cut -d" " -f1)" | |
assert_equals "ce013625" "$(hash_string "hello")" | |
assert_equals "ce0136" "$(hash_string "hello" 6)" | |
assert_equals "cc628ccd" "$(hash_string "world")" | |
huston | |
} | |
# main | |
if [[ "$1" == "--test" ]] || [[ "$1" == "-t" ]]; then | |
run_tests | |
else | |
hash_string "$1" "$2" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment