diff options
Diffstat (limited to '')
| -rwxr-xr-x | git-base.sh | 54 | ||||
| -rwxr-xr-x | test-branches.sh | 39 | ||||
| -rwxr-xr-x | test-commits.sh | 118 | 
3 files changed, 211 insertions, 0 deletions
| diff --git a/git-base.sh b/git-base.sh index 39e7367..9ffda78 100755 --- a/git-base.sh +++ b/git-base.sh @@ -1,5 +1,6 @@  dot_git=""  cwd="" +remote=""  in_current_dir() {    local wd="$(pwd)" @@ -96,3 +97,56 @@ fetch() {    git fetch    debug_print $debug "Finished fetch"  } + +branch_name() { +  if is_repo; then +    local localBranch="$(git symbolic-ref --short HEAD)" +    echo $localBranch +  fi +} + +is_tracking_remote() { +  if [[ -n "$(remote_branch_name)" ]]; then +    return 0 +  else +    return 1 +  fi +} + +remote_branch_name() { +  if is_repo; then +    local remoteBranch="$(git for-each-ref --format='%(upstream:short)' | grep "$(branch_name)")" +    if [[ -n $remoteBranch ]]; then +      echo $remoteBranch +      return 0 +    else +      return 1 +    fi +  fi +} + +commits_behind_of_remote() { +  if is_tracking_remote; then +    set -- +    set -- $(git rev-list --left-right --count $(remote_branch_name)...HEAD) +    behind=$1 +    ahead=$2 +    set -- +    echo $behind +  else +    echo "0" +  fi +} + +commits_ahead_of_remote() { +  if is_tracking_remote; then +    set -- +    set -- $(git rev-list --left-right --count $(remote_branch_name)...HEAD) +    behind=$1 +    ahead=$2 +    set -- +    echo $ahead +  else +    echo "0" +  fi +} diff --git a/test-branches.sh b/test-branches.sh new file mode 100755 index 0000000..ddf442c --- /dev/null +++ b/test-branches.sh @@ -0,0 +1,39 @@ +scriptDir="$(cd "$(dirname "$0")"; pwd)" + +source "$scriptDir/git-base.sh" + +tmpfile="" + +cd_to_tmp() { +  tmpfile="/tmp/git-prompt-tests-$(time_now)" +  mkdir -p "$tmpfile" +  cd "$tmpfile" +} + +rm_tmp() { +  cd $scriptDir +  rm -r "$tmpfile" +} + +test_branch_name_in_repo() { +  cd_to_tmp +  git init --quiet +  git checkout -b foo --quiet +  assertEquals "foo" "$(branch_name)" + +  git checkout -b bar --quiet +  assertEquals "bar" "$(branch_name)" + +  git checkout -b baz --quiet +  assertEquals "baz" "$(branch_name)" + +  rm_tmp +} + +test_branch_name_not_in_repo() { +  cd_to_tmp +  assertEquals "" "$(branch_name)" +  rm_tmp +} + +. ./shunit/shunit2 diff --git a/test-commits.sh b/test-commits.sh new file mode 100755 index 0000000..003ebbf --- /dev/null +++ b/test-commits.sh @@ -0,0 +1,118 @@ +scriptDir="$(cd "$(dirname "$0")"; pwd)" + +source "$scriptDir/git-base.sh" + +tmpfile="" + +cd_to_tmp() { +  tmpfile="/tmp/git-prompt-tests-$(time_now)$1" +  mkdir -p "$tmpfile" +  cd "$tmpfile" +} + +rm_tmp() { +  cd $scriptDir +  rm -rf /tmp/git-prompt-tests* +} + +test_commits_with_no_commits() { +  cd_to_tmp +  git init --quiet + +  assertEquals "0" "$(commits_ahead_of_remote)" +  assertEquals "0" "$(commits_behind_of_remote)" + +  rm_tmp +} + +test_commits_behind_no_remote() { +  cd_to_tmp +  git init --quiet + +  echo "foo" > foo +  git add . +  git commit -m "test commit" --quiet +  assertEquals "0" "$(commits_behind_of_remote)" + +  rm_tmp +} + +test_commits_ahead_no_remote() { +  cd_to_tmp +  git init --quiet + +  echo "foo" > foo +  git add . +  git commit -m "test commit" --quiet +  assertEquals "0" "$(commits_ahead_of_remote)" + +  echo "bar" > bar +  git add . +  git commit -m "test commit" --quiet +  assertEquals "0" "$(commits_ahead_of_remote)" + +  rm_tmp +} + +test_commits_ahead_with_remote() { +  cd_to_tmp "remote" +  git init --quiet +  touch README +  git add . +  git commit -m "initial commit" --quiet +  remoteLocation="$(pwd)" + +  cd_to_tmp "new" +  git init --quiet +  git remote add origin $remoteLocation +  git fetch origin --quiet +  git checkout master --quiet +  repoLocation="$(pwd)" + +  cd "$remoteLocation" +  echo "foo" > foo +  git add . +  git commit -m "test commit" --quiet +  cd "$repoLocation" +  git fetch origin --quiet +  assertEquals "1" "$(commits_ahead_of_remote)" + +  cd "$remoteLocation" +  echo "bar" > bar +  git add . +  git commit -m "test commit" --quiet +  cd "$repoLocation" +  git fetch origin --quiet +  assertEquals "2" "$(commits_ahead_of_remote)" + +  rm_tmp +} + +test_commits_ahead_with_remote() { +  cd_to_tmp "remote" +  git init --quiet +  touch README +  git add . +  git commit -m "initial commit" --quiet +  remoteLocation="$(pwd)" + +  cd_to_tmp "new" +  git init --quiet +  git remote add origin $remoteLocation +  git fetch origin --quiet +  git checkout master --quiet + +  echo "foo" > foo +  git add . +  git commit -m "test commit" --quiet +  assertEquals "1" "$(commits_ahead_of_remote)" + +  echo "bar" > bar +  git add . +  git commit -m "test commit" --quiet +  assertEquals "2" "$(commits_ahead_of_remote)" + +  rm_tmp +} + +. ./shunit/shunit2 | 
