-
Notifications
You must be signed in to change notification settings - Fork 2
/
git-archive
executable file
·112 lines (100 loc) · 2.6 KB
/
git-archive
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
#!/bin/bash
# SPDX-FileCopyrightText: 2022 - 2024 sudorook <[email protected]>
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
set -eu
ROOT="$(dirname "${0}")"
source "${ROOT}"/globals
! check_command git && exit 3
print_usage() {
show_header "Usage: git-archive"
cat << EOF
-d|--dir directory of git repository to archive
-t|--tag tag/branch to archive (e.g. master, HEAD~5, etc.)
-z|--compress compression type (zip, gz, xz, or zst)
-h|--help show (this) help message
EOF
}
check_dir() {
if ! [[ -v DIR ]]; then
if git log > /dev/null 2>&1; then
DIR="$(git rev-parse --show-toplevel)"
else
show_error "No Git directory specified. Exiting."
exit 3
fi
elif [[ "${DIR}" = . ]]; then
if git log > /dev/null 2>&1; then
DIR="$(git rev-parse --show-toplevel)"
else
show_error "${PWD@Q} is not a Git directory. Exiting."
exit 3
fi
else
if ! git -C "${DIR}" log > /dev/null 2>&1; then
show_error "${DIR@Q} is not a Git directory. Exiting."
exit 3
fi
fi
}
TAG="HEAD"
Z="zip"
OPTIONS=d:t:z:h
LONGOPTIONS=dir:,tag:,compress:,help
PARSED=$(getopt -o "${OPTIONS}" --long "${LONGOPTIONS}" -n "${0}" -- "${@}")
eval set -- "${PARSED}"
while [ ${#} -ge 1 ]; do
case ${1} in
-d | --dir)
DIR="${2}"
shift 2
;;
-t | --tag)
TAG="${2}"
shift 2
;;
-z | --compress)
case "${2,,}" in
gz | xz | zst | zstd)
Z="tar.${2}"
;;
zip)
Z="zip"
;;
*)
show_warning "WARNING: ${2@Q} not understood. Defaulting to ${Z@Q}."
;;
esac
shift 2
;;
-h | --help)
print_usage
exit
;;
--)
shift
break
;;
*)
show_error "ERROR: invalid flag."
print_usage
exit 3
;;
esac
done
check_dir
PREFIX="$(basename "${DIR}")-${TAG}"
git -C "${DIR}" archive --prefix="${PREFIX}/" "${TAG}" -o "${PWD}/${PREFIX}.${Z}"