-
-
Save tzarskyz/8088562 to your computer and use it in GitHub Desktop.
This file contains 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
#!/bin/bash | |
# Created by Håvard Fossli <[email protected]> in 2013 | |
# This is free and unencumbered software released into the public domain. | |
# For more information, please refer to <http://unlicense.org/> | |
# | |
# Description | |
# A bash script for fetching all branches and tags of a git project as snapshots into separate folders | |
# | |
# Keywords | |
# Terminal, bash, unix, mac, shell, script, git, folders, directories, branch, branches, tag, tags, snapshot | |
# | |
function usage { | |
echo "For help and detailed guide type:" | |
echo "\$ $0 -h" | |
} | |
# Allow to be terminated with ctrl + c | |
trap "exit" INT | |
# Set variables and default values | |
OUT_FORMAT='' | |
REPO='' | |
VERBOSE=false | |
PLAY_SOUND=false | |
DRY_RUN=false | |
GET_TAGS=false | |
GET_BRANCHES=false | |
HELP=false | |
# Grab input arguments | |
while getopts “g:o:vptbhd” OPTION | |
do | |
case $OPTION in | |
g) REPO=$(echo "$OPTARG" | sed 's/ /\\ /g' ) ;; | |
o) OUT_FORMAT=$(echo "$OPTARG" | sed 's/ /\\ /g' ) ;; | |
v) VERBOSE=true ;; | |
p) PLAY_SOUND=true ;; | |
t) GET_TAGS=$(echo "$OPTARG" | sed 's/ /\\ /g' ) ;; | |
b) GET_BRANCHES=$(echo "$OPTARG" | sed 's/ /\\ /g' ) ;; | |
h) HELP=true ;; | |
d) DRY_RUN=true ;; | |
?) usage | |
exit 1 | |
;; | |
esac | |
done | |
function detailed_guide { | |
echo " | |
Example gets all branches and tags: | |
\$ $0 -g [email protected]:hfossli/drmobile-integration.git -o ~/Desktop/drmobile-integration/branches/{{name}}/ -t -b | |
Flags: | |
-g Git repo | |
E.g. [email protected]:hfossli/drmobile-integration.git | |
-t Tags | |
-b Branches | |
-o Path to folder with format (string) | |
Possible variables: | |
- {{name}} (name of tag or branch) | |
- {{type}} ('tag' | 'branch') | |
E.g. ./path/{{name}}/ will produce ./path/develop/ | |
-h Takes no arguments | |
Help - detailed guide | |
-d Takes no arguments | |
Dry-run (no actual writing) | |
" | |
} | |
if $HELP ; then | |
detailed_guide | |
exit 1 | |
fi | |
if [[ -z $REPO ]] ; then | |
echo "Invalid git repo" | |
usage | |
exit 1 | |
fi | |
if !$GET_TAGS && !$GET_BRANCHES ; then | |
echo "Specify wether you want branches or tags or both" | |
usage | |
exit 1 | |
fi | |
TMP_DIR=$(mktemp -dt "git_branches_as_folders") | |
if $VERBOSE ; then | |
echo "Cloning repo $REPO to $TMP_DIR" | |
fi | |
git clone ${REPO} ${TMP_DIR} | |
if $VERBOSE ; then | |
echo "Looping over all branches:" | |
fi | |
local_git="git --git-dir=${TMP_DIR}/.git --work-tree=${TMP_DIR}/" | |
for branch in `$local_git branch -a | grep remotes | grep -v HEAD | grep -v master `; do | |
branch_name=`echo $branch | sed -e "s/remotes\/origin\///g"` | |
folder=`echo $OUT_FORMAT | sed -e "s/{{name}}/$branch_name/g" | sed -e "s/{{type}}/branch/g" ` | |
if $VERBOSE ; then | |
echo " Checking out branch $branch as $branch_name" | |
fi | |
$local_git checkout -b $branch $branch_name | |
if $VERBOSE ; then | |
echo " Creating directory: '$folder'"; | |
fi | |
mkdir -p $folder | |
if $VERBOSE ; then | |
echo " Copying contents of folder $TMP_DIR to $folder"; | |
fi | |
cp -R $TMP_DIR/* $folder | |
done | |
for tag in `$local_git tag -l`; do | |
tag_name=`echo $tag | sed -e "s/remotes\/origin\///g"` | |
folder=`echo $OUT_FORMAT | sed -e "s/{{name}}/$tag_name/g" | sed -e "s/{{type}}/tag/g" ` | |
if $VERBOSE ; then | |
echo " Checking out tag $tag as $tag_name" | |
fi | |
$local_git checkout -b $tag $tag_name | |
if $VERBOSE ; then | |
echo " Creating directory: '$folder'"; | |
fi | |
mkdir -p $folder | |
if $VERBOSE ; then | |
echo " Copying contents of folder $TMP_DIR to $folder"; | |
fi | |
cp -R $TMP_DIR/* $folder | |
done | |
if $VERBOSE ; then | |
echo "Deleting temp folder"; | |
fi | |
rm -rf $TMP_DIR | |
if $PLAY_SOUND ; then | |
# & means async | |
say "Done! ." & | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment