Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

A bash script for fetching all branches and tags of a git project as snapshots into separate folders

View gist:7562257
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
#!/bin/bash
# Created by Håvard Fossli <hfossli@gmail.com> 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 git@github.com:hfossli/drmobile-integration.git -o ~/Desktop/drmobile-integration/branches/{{name}}/ -t -b
Flags:
-g Git repo
E.g. git@github.com: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

mktemp will not work on some distros:

mktemp [OPTION]... [TEMPLATE]

Create a temporary file or directory, safely, and print its name.
TEMPLATE must contain at least 3 consecutive `X's in last component.
If TEMPLATE is not specified, use tmp.XXXXXXXXXX, and --tmpdir is implied.

Fixed that, and it worked! Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.