From cd7559720a2d691aed1494aa7190b4eb09074901 Mon Sep 17 00:00:00 2001 From: William Hilton Date: Thu, 10 Dec 2015 19:09:03 -0500 Subject: [PATCH] Add 'branches'. Make 'rmbranch' robust. Prune remote branches on 'fetch'. --- bin/get | 50 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/bin/get b/bin/get index ba258bc..af01a58 100755 --- a/bin/get +++ b/bin/get @@ -13,6 +13,14 @@ git_root() { git rev-parse --show-toplevel } +confirm () { + read -r -n 1 -p "${1:-Are you sure? [y/N]} " response + case $response in + [yY]) true ;; + *) false ;; + esac +} + case "$1" in '?' | status) # TODO: Display help if not inside a repo @@ -156,19 +164,53 @@ rmbranch) echo 'Delete branch' if [ -z "$2" ]; then echo '! Specify branch name' -else - git branch -d "$2" + exit; fi +if ! git rev-parse --quiet --verify "$2" >/dev/null +then + echo "Branch not found: '$2'" + exit; +fi +# else +git branch -d "$2" +if [[ "$?" -ne 0 ]]; then + if confirm 'Are sure you want to delete this branch?'; then + echo '' + git branch -D "$2" + else + echo '' + fi +fi +;; + +branches) +# Make a temporary directory +TEMP=$(mktemp -d) +trap "rm -rf $TEMP" EXIT +# List all merged branches to >merged.branches +git branch --merged | sed 's/^..//' > "$TEMP/merged.branches" +# Pretty-print local branches +git for-each-ref --sort=-committerdate refs/heads \ +--format='%(HEAD) %(color:green)%(committerdate:relative)%(color:reset)|%(color:red)%(objectname:short)%(color:reset)|%(color:yellow)%(refname:short)%(color:reset) -> %(upstream:short)' > "$TEMP/local.branches" +sed -i 's/ -> $/ -> \?/g' "$TEMP/local.branches" +while read -r branch; do + sed -i "s~${branch}\b.*~\0|(merged)~" "$TEMP/local.branches" +done < "$TEMP/merged.branches" +# Pretty-print remote branches +git for-each-ref --sort=-committerdate refs/remotes \ +--format='%(HEAD) %(color:green)%(committerdate:relative)%(color:reset)|%(color:red)%(objectname:short)%(color:reset)|%(color:blue)%(refname:short)%(color:reset)' > "$TEMP/remote.branches" +# Print them in columns +cat "$TEMP/local.branches" "$TEMP/remote.branches" | column -ts'|' ;; fetch) echo Updating if [ -z "$2" ]; then - git fetch --all + git fetch --all --prune branches="$(list_branches)" else branches = "${@:2}" - git fetch "$branches" + git fetch --prune "$branches" fi # Fast-forward local branches. I owe a lot to http://stackoverflow.com/a/24451300/2168416 current_branch=$(git rev-parse --abbrev-ref HEAD)