diff options
Diffstat (limited to '')
-rwxr-xr-x | git-base.sh | 67 | ||||
-rwxr-xr-x | test.sh | 49 |
2 files changed, 100 insertions, 16 deletions
diff --git a/git-base.sh b/git-base.sh index d0f349d..5ed680d 100755 --- a/git-base.sh +++ b/git-base.sh @@ -1,18 +1,45 @@ set -e +dot_git="" +cwd="" + +in_current_dir() { + local wd="$(pwd)" + if [[ "$wd" == $cwd ]]; then + cwd="$wd" + return 0 + else + cwd="$wd" + return 1 + fi +} + debug_print() { - debug=$1 - message=$2 + local debug=$1 + local message=$2 if [[ $debug == "debug" ]]; then echo $message fi } dot_git() { - if [ -d .git ]; then - echo ".git" + if [[ in_current_dir && -n "$dot_git" ]]; then + # cache dot_git to save calls to rev-parse + echo $dot_git + elif [ -d .git ]; then + dot_git=".git" + echo $dot_git else - echo "$(git rev-parse --git-dir)" + dot_git="$(git rev-parse --git-dir 2>/dev/null)" + echo $dot_git + fi +} + +is_repo() { + if [[ -n "$(dot_git)" ]]; then + return 0 + else + return 1 fi } @@ -20,16 +47,20 @@ git_root() { if [ -d .git ]; then echo "." else - echo "$(git rev-parse --show-toplevel)" + echo "$(git rev-parse --show-toplevel 2>/dev/null)" fi } record_timestamp() { - touch "$(dot_git)/lastupdatetime" + if is_repo; then + touch "$(dot_git)/lastupdatetime" + fi } timestamp() { - echo "$(stat -f%m "$(dot_git)/lastupdatetime")" + if is_repo; then + echo "$(stat -f%m "$(dot_git)/lastupdatetime")" + fi } time_now() { @@ -37,19 +68,23 @@ time_now() { } time_to_update() { - timesincelastupdate="$(($(time_now) - $(timestamp)))" - fiveminutes="$((5 * 60))" - if (( "$timesincelastupdate" > "$5minutes" )); then - # time to update return 0 (which is false) - return 0 + if is_repo; then + local timesincelastupdate="$(($(time_now) - $(timestamp)))" + local fiveminutes="$((5 * 60))" + if (( "$timesincelastupdate" > "$5minutes" )); then + # time to update return 0 (which is false) + return 0 + else + # not time to update return 1 (which is true) + return 1 + fi else - # not time to update return 1 (which is true) return 1 fi } fetch_async() { - debug="$1" + local debug="$1" if time_to_update; then debug_print $debug "Starting fetch" fetch $debug & @@ -59,7 +94,7 @@ fetch_async() { } fetch() { - debug="$1" + local debug="$1" git fetch debug_print $debug "Finished fetch" } @@ -4,12 +4,23 @@ scriptDir="$( dirname "$0" )" source "$scriptDir/git-base.sh" +echo "\n---------------------------" +echo "\n In a git repo" +echo "\n---------------------------" + echo "\nTest: Root of this git repo" echo "$(git_root)" echo "\nTest: Location of .git" echo "$(dot_git)" +echo "\nTest: is_repo should be false" +if is_repo; then + echo "is repo" +else + echo "not repo" +fi + echo "\nTest: Record the timestamp" record_timestamp echo "Timestamp = $(timestamp)" @@ -36,3 +47,41 @@ fi echo "\nTest: Do a non-blocking git fetch" fetch_async "debug" echo "Did I block?" + + +echo "\n---------------------------" +echo "\n Not in a git repo" +echo "\n---------------------------" + +mkdir -p /tmp/git-base-tests +cd /tmp/git-base-tests + +echo "\nTest: Root of this git repo" +echo "git_root is:$(git_root) (empty means no root)" + +echo "\nTest: Location of .git" +echo "dot_git is:$(dot_git) (empty means no root)" + +echo "\nTest: is_repo should be false" +if is_repo; then + echo "is repo" +else + echo "not repo" +fi + +echo "\nTest: Record the timestamp" +record_timestamp +echo "no output should be seen" + +echo "\nTest: Check the timestamp" +echo "timestamp is:$(timestamp) (empty means not in dir)" + +echo "\nTest: Is it time to update?" +if time_to_update; then + echo "time to update" +else + echo "not time yet" +fi + +echo "\nTest: Try to fetch" +fetch_async "debug" |