-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-makezip
executable file
·86 lines (69 loc) · 1.63 KB
/
git-makezip
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
#!/usr/bin/env bash
#
# Create a ZIP archive of the current repo's master branch.
#
# @author Morgan Estes <morgan.estes@gmail.com>
readonly E_DID_NOTHING=0
readonly E_SUCCESS=1
readonly E_FAILURE=2
main() {
if ! makezip; then
err "There was a problem creating ${zip_filename}."
exit "${E_DID_NOTHING}"
else
echo "Archive file ${zip_filename} created successfully."
exit "${E_SUCCESS}"
fi
}
#########################
# Creates a ZIP archive of the master branch at its current state.
#########################
makezip() {
local is_branch_clean
local current_branch
local has_stash=false
local success=false
is_branch_clean="$(git status --porcelain)"
current_branch="$(git rev-parse --abbrev-ref HEAD)"
filename_base="$(git describe master)"
zip_filename="${filename_base}.zip"
if [[ -n "${is_branch_clean}" ]]; then
has_stash=true
echo 'Stashing changes...'
git stash save -a -q
fi
# Switch to master, create the ZIP, then go back to where we were.
switch_branch 'master'
if create_archive; then
success=true
fi
switch_branch "${current_branch}"
if [ "${has_stash}" = true ]; then
git stash apply
fi
if [ "${success}" = true ]; then
return 0
else
return 1
fi
}
switch_branch() {
local branch="$1"
git checkout "${branch}" -q
}
create_archive() {
local success=false
git archive master --prefix="${filename_base}/" --format=zip > "${zip_filename}"
if [[ -e "${zip_filename}" ]]; then
success=true
fi
if [ "${success}" = true ]; then
return 0
else
return 1
fi
}
err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $@" >&2
}
main "$@"