-
Notifications
You must be signed in to change notification settings - Fork 0
/
track
executable file
·129 lines (107 loc) · 2.99 KB
/
track
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
#!/bin/sh
set -u
# to save
DEST_DIR=
# git repo
PROJ_DIR=
# The Nth commit
NTH_COMMIT=
OLD_WD=
usage() {
printf '%s: Get files from a specific commit ID.\n\n' "$0" >&2
printf 'Usage: %s [-h] [-g PROJ_DIR] [-n NTH_COMMIT] [-d DEST_DIR]\n' "$0" >&2
printf '\t-h: Display this help message and exit.\n' >&2
printf '\t-g: Specify the path to the directory of the cloned repo. Defaults to $PWD.\n' >&2
printf '\t-d: Specify the path to where to save the extracted files.\n' >&2
printf '\t-n: Specify the NTH_COMMIT commit from where files are extracted.\n' >&2
exit 1
}
check_dependencies() {
local CMD=
for CMD in git unzip ; do
command -v "$CMD" >/dev/null || {
printf "%s: ERROR: \`%s' not found\n" "$0" "$CMD"
exit 1
}
done
}
get_opts() {
while getopts :hd:g:n: OPTS ; do
case "$OPTS" in
h)
usage ;;
d)
DEST_DIR=$OPTARG ;;
g)
PROJ_DIR=$OPTARG ;;
n)
expr "$OPTARG" : '[0-9]\+$' >/dev/null || {
printf "%s: ERROR: \`-n' options takes a +ve integer as argument\n" "$0" >&2
usage
}
NTH_COMMIT=$OPTARG ;;
\?)
usage ;;
esac
done
}
check_global_vars() {
NTH_COMMIT=${NTH_COMMIT:-1}
PROJ_DIR=`readlink -f "${PROJ_DIR:-.}"`
if [ ! -d "$PROJ_DIR" ] ; then
printf "%s: ERROR: \`%s' isn't a directory\n" "$0" "$PROJ_DIR" >&2
exit 1
fi
if [ ! -x "$PROJ_DIR" ] ; then
printf "%s: ERROR: cannot enter \`%s'\n" "$0" "$PROJ_DIR" >&2
exit 1
fi
if [ -n "$DEST_DIR" ] ; then
DEST_DIR=`readlink -f "$DEST_DIR"`
if [ ! -w "$DEST_DIR" ] ; then
printf "%s: ERROR: cannot write in \`%s'\n" "$0" "$DEST_DIR" >&2
exit 1
fi
fi
}
get_files_from_commit_id() {
local COMMITS= N_COMMITS= COMMIT_ID= WD=
WD=`readlink -f "${PWD:-"$(pwd)"}"`
if [ "$WD" != "$PROJ_DIR" ] ; then
OLD_WD=$WD
cd "$PROJ_DIR"
fi
git status >/dev/null 2>&1
if test $? -ne 0 ; then
printf "%s: ERROR: \`%s' isn't a git repo.\n" "$0" "$PROJ_DIR" >&2
exit 1
fi
COMMITS=`git log | grep ^commit | awk '{print $2}'`
N_COMMITS=`printf '%s\n' "$COMMITS" | wc -l`
if test $N_COMMITS -lt $NTH_COMMIT ; then
printf '%s: ERROR: max number of commits: %d.\n' "$0" "$N_COMMITS" >&2
exit 1
fi
COMMIT_ID=`printf '%s\n' "$COMMITS" | tac | sed -n "${NTH_COMMIT}p"`
if [ -z "$DEST_DIR" ] ; then
if test -w . ; then
DEST_DIR="${WD%%/}/$COMMIT_ID-$NTH_COMMIT"
mkdir -p "$DEST_DIR"
else
printf "%s: ERROR: cannot write into \`%s'\n" "$0" "$WD" >&2
exit 1
fi
fi
ZIP_FILE=`mktemp -u /tmp/archive-XXXXXX`
git archive $COMMIT_ID --format=zip --output $ZIP_FILE
unzip -q -d "$DEST_DIR" $ZIP_FILE
rm -f $ZIP_FILE
[ "$OLD_WD" ] && cd "$OLD_WD"
}
track() {
check_dependencies
get_opts "$@"
check_global_vars
get_files_from_commit_id
}
track "$@"