Skip to content

Instantly share code, notes, and snippets.

@adamzaninovich
Last active July 28, 2021 19:57
Show Gist options
  • Select an option

  • Save adamzaninovich/1f16ff481ac1285c9efb4181adea75b2 to your computer and use it in GitHub Desktop.

Select an option

Save adamzaninovich/1f16ff481ac1285c9efb4181adea75b2 to your computer and use it in GitHub Desktop.

Revisions

  1. adamzaninovich revised this gist Jul 28, 2021. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions gg_cleanup notes.md
    Original file line number Diff line number Diff line change
    @@ -12,6 +12,10 @@ You can remove all the other functions if you want to just replace `ohai` and `e
    - outputs number of cleaned branches
    - squeaky clean ✨

    ## Screenshot

    ![Screenshot](https://i.imgur.com/OKkbE15.png)

    ## Todo

    - even if another main branch is specified, do not delete `master` or `main`
  2. adamzaninovich renamed this gist Jul 28, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. adamzaninovich revised this gist Jul 28, 2021. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    # Notes

    This is not a script. You will want to just add these functions to your bash config (or make it into a script, do what you want, I'm not your mom)

    You can remove all the other functions if you want to just replace `ohai` and `error` with `echo` for simplicity (I just have those formatting functions at the top of my alias/function file in my bash config and use them in a lot of my functions. They are originally from the homebrew sourcecode)

    ## New Features
  4. adamzaninovich revised this gist Jul 28, 2021. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion gg_cleanup.sh
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,6 @@ shell_join() {
    done
    }

    # helpers
    ohai() {
    printf "${tty_blue}==>${tty_bold} %s${tty_reset}\n" "$(shell_join "$@")"
    }
  5. adamzaninovich revised this gist Jul 28, 2021. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    # Notes

    You can remove all the other functions if you want to just replace `ohai` and `error` with `echo` for simplicity (I just have those formatting functions at the top of my alias/function file in my bash config and use them in a lot of my functions. They are originally from the homebrew sourcecode)

    ## New Features

    - exit if not in git repo
    - support a main branch other than `master` (useful if you are using `main` or if you use something like gitflow and want to compare to `develop`, `staging`, etc
    - exit if master/main branch doesn’t exist
    - outputs number of cleaned branches
    - squeaky clean ✨

    ## Todo

    - even if another main branch is specified, do not delete `master` or `main`
  6. adamzaninovich created this gist Jul 28, 2021.
    74 changes: 74 additions & 0 deletions gg_cleanup.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,74 @@
    # string formatters
    if [[ -t 1 ]]; then
    tty_escape() { printf "\033[%sm" "$1"; }
    else
    tty_escape() { :; }
    fi
    tty_mkbold() { tty_escape "1;$1"; }
    tty_underline="$(tty_escape "4;39")"
    tty_blue="$(tty_mkbold 34)"
    tty_red="$(tty_mkbold 31)"
    tty_bold="$(tty_mkbold 39)"
    tty_reset="$(tty_escape 0)"

    shell_join() {
    local arg
    printf "%s" "$1"
    shift
    for arg in "$@"; do
    printf " "
    printf "%s" "${arg// /\ }"
    done
    }

    # helpers
    ohai() {
    printf "${tty_blue}==>${tty_bold} %s${tty_reset}\n" "$(shell_join "$@")"
    }

    error() {
    printf "${tty_red}ERROR:${tty_bold} %s${tty_reset}\n" "$(shell_join "$@")"
    }

    # gg_cleanup:
    # Accepts main comparison branch as the first arg
    # Defaults to `master`
    # Usage:
    # gg_cleanup main
    gg_cleanup () {
    local main_branch before after cleaned_branches branches_left

    if [[ "$(git status 2>&1)" =~ "not a git repository" ]]; then
    error "Not in a git repository"
    return 1
    fi

    if [[ -n "${1-}" ]]; then
    main_branch="$1"
    else
    main_branch="master"
    fi

    branch=$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')
    if [[ $branch != "$main_branch" ]]; then
    if [[ "$(git checkout "$main_branch" 2>&1)" =~ "did not match" ]]; then
    error "$main_branch does not exist. Please specify a main branch"
    return 1
    fi
    echo ""
    fi
    before=$(git branch | wc -l)
    git branch --merged | grep -v "\*" | xargs -n 1 git branch -d
    after=$(git branch | wc -l)
    cleaned_branches=$(( before - after ))
    if [[ $cleaned_branches == 0 ]]; then
    ohai "Nothing to clean."
    else
    echo ""
    ohai "Cleaned up $cleaned_branches $(plural "branch" $cleaned_branches "es")."
    fi
    branches_left=$(( after - 1 ))
    ohai "You have $branches_left $(plural "branch" $branches_left "es") left (other than $main_branch)."
    [[ $branch != "$main_branch" ]] && [[ -n $(git branch | grep "$branch") ]] && echo "" && git checkout "$branch"
    ohai ""
    }