Last active
November 2, 2017 14:22
-
-
Save triangletodd/924bb29a9bdb7214a6947a791f731474 to your computer and use it in GitHub Desktop.
Backup your private Github organization.
This file contains hidden or 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
| #!/usr/bin/env bash | |
| set -e | |
| trap cleanup EXIT | |
| cleanup() { | |
| cd "$(dirs -l -0)" && dirs -c | |
| } | |
| setup() { | |
| pushd "${0%/*}" > /dev/null | |
| export GH_AUTH_TOKEN | |
| export GH_ORG | |
| chk_disk_for_conf | |
| sanity_check | |
| chk_org_folder | |
| } | |
| chk_org_folder() { | |
| if [[ ! -d "$GH_ORG" ]]; then | |
| mkdir -p "$GH_ORG" | |
| fi | |
| } | |
| chk_disk_for_conf() { | |
| if [[ -f .github_auth ]]; then | |
| GH_AUTH_TOKEN="$(cat .github_auth)" | |
| fi | |
| if [[ -f .github_org ]]; then | |
| GH_ORG="$(cat .github_org)" | |
| fi | |
| } | |
| mirror() { | |
| pushd "$GH_ORG" > /dev/null | |
| declare -i page=1 | |
| while true; do | |
| repos=$(get_repos $page) | |
| if [ -z "$repos" ]; then | |
| break | |
| fi | |
| for repo in $repos; do | |
| if [[ -d "${repo}.git" ]]; then | |
| pushd "${repo}.git" > /dev/null | |
| echo "## Fetch: ${GH_ORG}/${repo}.git" | |
| git fetch --all | |
| popd > /dev/null | |
| else | |
| echo "## Clone: ${GH_ORG}/${repo}.git" | |
| git clone --mirror [email protected]:${GH_ORG}/${repo}.git | |
| fi | |
| done | |
| ((page++)) | |
| done | |
| popd > /dev/null | |
| } | |
| isnt_null() { | |
| if [ -z "${!1}" ]; then | |
| >&2 echo "Please make sure $1 is populated"; exit 1 | |
| fi | |
| } | |
| is_installed() { | |
| if ! type $1 >/dev/null 2>&1; then | |
| >&2 echo "Please install $1"; exit 1 | |
| fi | |
| } | |
| sanity_check() { | |
| for var in GH_AUTH_TOKEN GH_ORG; do isnt_null $var; done | |
| for tool in jq curl; do is_installed $tool; done | |
| } | |
| get_repos() { | |
| curl -su ${GH_AUTH_TOKEN}:x-oauth-basic https://api.github.com/orgs/${GH_ORG}/repos\?page=$1 | jq -r .[].name | |
| } | |
| main() { | |
| setup | |
| mirror | |
| } | |
| main "$@" | |
This file contains hidden or 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
| #!/usr/bin/env bash | |
| set -e | |
| grep_all() { | |
| git branch -a | clean_text | xargs_git_grep $* | |
| } | |
| clean_text() { | |
| perl -pe 's/(\*|\s+)\s+?//' | sed '/->/d' | |
| } | |
| xargs_git_grep() { | |
| xargs git -c color.status=always -c color.ui=always --no-pager \ | |
| grep --threads 8 -I --full-name $* | |
| } | |
| gh_org() { | |
| cat .github_org | |
| } | |
| banner() { | |
| echo; echo | |
| echo "########################################################################################################################" | |
| echo "### ${1}" | |
| echo "########################################################################################################################" | |
| echo | |
| } | |
| main() { | |
| for codebase in $(gh_org)/*; do | |
| pushd $codebase > /dev/null | |
| results="$(grep_all $* || true)" | |
| if [[ -n "$results" ]]; then | |
| banner "${codebase}" | |
| printf "$results" | |
| fi | |
| popd > /dev/null | |
| done | |
| } | |
| main "$@" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment