-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgit-package
More file actions
executable file
·218 lines (189 loc) · 7.1 KB
/
Copy pathgit-package
File metadata and controls
executable file
·218 lines (189 loc) · 7.1 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/bin/bash
# Given any commit/tag/branch, build the corresponding moodle package.
# It differentiates, in order to decide the name of the packages, between:
# - Dated packages: If the commit does not match any tag.
# - name: moodle_branchname_date.zip/tgz
# - Tagged packages: If the commit matches any tag.
# - name: moodle_branchname_tag.zip/tgz
# (with branchname being the git name of the branch, date in YYYMMDD
# the commit date, and tag the name of the tag without the "v" prefix)
set -e # Exit on error
#set -x # Debug execution
# Calculate some variables
mydir="$(cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd)"
# Calculate own pid, for tmp storage
mypid="$$"
# git-package version
myversion="v0.9.2 by Eloy Lafuente (stronk7), license BSD New (3-Clause)"
# Init some variables
packagetype="unknown"
# Add the functions
. ${mydir}/git-package-funcs
### Main code
# Create defaults
# Format, defaults to zip
format="zip"
# Path, defaults to /tmp
path="/tmp"
# Prefix, defaults to ""
prefix=""
# Manifest, defaults to ""
manifest=""
# Branch, defaults to ""
branch=""
# Ignoretags, defaults to ""
# Process the args
myargs=$(getopt -n $(basename ${0}) -u -a -l "dir: format: manifest:: prefix: branch: ignoretags" -o "d:f:m::p:b:i" -- "$@")
if [ $? -ne 0 ]; then
git_package_show_usage && exit 1
fi
eval set -- "${myargs}"
while true; do
case "${1}" in
-d|--dir)
path=${2}
shift 2
;;
-f|--format)
format=${2}
shift 2
;;
-m|--manifest)
manifest="1"
if [[ -n "${2}" ]] && [[ "${2}" != "--" ]]; then
manifest=${2}
shift 1
fi
shift 1
;;
-p|--prefix)
prefix=${2}
shift 2
;;
-b|--branch)
branch=${2}
shift 2
;;
-i|--ignoretags)
ignoretags="1"
shift 1
;;
--)
shift
break
;;
*)
echo "$(basename ${0}): unknown option ${1}"
git_package_show_usage && exit 1
;;
esac
done
# Get the non-option parameters
eval set -- "$@"
# Commit, defaults to current HEAD
commit=${1:-$(git currentcommit)}
# Verify that format is valid
if [[ "${format}" != "zip" ]] && [[ "${format}" != "tar" ]] && [[ "${format}" != "tgz" ]]; then
echo "$(basename ${0}): incorrect format (${format})" && git_package_show_usage && exit 1
fi
# Verify the path exists, and is writable
path=$(readlink -f ${path})
if [[ ! -d "${path}" ]]; then
echo "$(basename ${0}): directory does not exist (${path})" && git_package_show_usage && exit 1
fi
if [[ ! -w "${path}" ]]; then
echo "$(basename ${0}): directory is not writeable (${path})" && git_package_show_usage && exit 1
exit 1
fi
# Clean prefix (trim spaces and slashes)
prefix=$(echo ${prefix} | sed 's/^[ /]*//g' | sed 's/[ /]*$//g')
# Get the short hash, to compare with git describe results
commit=$(git rev-parse --short ${commit})
# Look if the commit exists
describe=$(git describe ${commit})
# Verify the commit belongs to the the passed branch
if [[ -n ${branch} ]]; then
if [[ -z $(git branch --contains ${commit} | sed 's/[* ]* \(.*\)/\1/' | grep ${branch}) ]]; then
echo "$(basename ${0}): commit ${commit} does not belong to branch ${branch}" && git_package_show_usage && exit 1
fi
fi
# If the output of describe matches the commit or if we are ignoring tags, then
# it is a "date package", else a "tagged package"
if [[ ${describe} =~ .*-g${commit} ]] || [[ -n ${ignoretags} ]]; then
# Get the commit date
fullcommitdate=$(git show -s --format="%ci" ${commit})
commitdate=$(date --date="${fullcommitdate}" "+%Y%m%d")
# Let's calculate the dated package name
packagename=$(dated_package_name "${commit}" "${commitdate}" "${format}" "${prefix}" "${branch}")
packagetype="dated"
else
committag=${describe}
# we need the commit the tag is pointing to
commit=$(git rev-list ${committag} | head -n 1)
# Get the short hash
commit=$(git rev-parse --short ${commit})
# Get the commit date
fullcommitdate=$(git show -s --format="%ci" ${commit})
commitdate=$(date --date="${fullcommitdate}" "+%Y%m%d")
# Let's calculate the tagged package
packagename=$(tagged_package_name "${commit}" "${committag}" "${format}" "${prefix}" "${branch}")
packagetype="tagged"
fi
echo "Building the ${packagetype} package ${packagename} into ${path}"
tempdir=`mktemp -d`
echo " - Creating the temp dir ${tempdir}"
# Extract the tree index with prefix (if defined) to the temp directory
if [[ "${prefix}" == "" ]]; then
git archive ${commit} --format=tar | tar -x -C ${tempdir}
else
git archive ${commit} --format=tar --prefix=${prefix}/ | tar -x -C ${tempdir}
fi
# Apply all the prefix.includes
echo " - Applying ${prefix}.includes"
apply_prefix_includes "${commit}" "${tempdir}" "${prefix}"
# Apply all the prefix.excludes
echo " - Applying ${prefix}.excludes"
apply_prefix_excludes "${commit}" "${tempdir}" "${prefix}"
# Execute the prefix.executes
echo " - Executing ${prefix}.executes"
execute_prefix_executes "${commit}" "${tempdir}" "${packagetype}" "${format}" "${prefix}" "${branch}"
# Everything's ready, let's package
echo " - Creating the ${path}/${packagename} archive"
if [[ "${format}" == "zip" ]]; then
output=$(cd ${tempdir} && zip -r ${path}/${packagename}.tmp *)
elif [[ "${format}" == "tar" ]]; then
output=$(cd ${tempdir} && tar -cf ${path}/${packagename}.tmp *)
elif [[ "${format}" == "tgz" ]]; then
output=$(cd ${tempdir} && tar -czf ${path}/${packagename}.tmp *)
fi
# Create the final package with the last commit date
mv ${path}/${packagename}.tmp "${path}/${packagename}"
touch --date="${fullcommitdate}" "${path}/${packagename}"
# Generate the manifest file if requested
if [[ -n "${manifest}" ]]; then
# If no manifest name was specified, use packagename.manifest
if [[ "${manifest}" == "1" ]]; then
manifest="${path}/${packagename}.manifest"
else
manifest="${path}/${manifest}"
fi
echo " - Generating manifest ${manifest}"
# Get the branch associated with the package
branch=$(package_best_branch $commit)
echo "# manifest file for package ${packagename} (${commit})" > "${manifest}"
echo "# generated with git-package ${myversion} (https://github.com/stronk7/lovingit)" >> "${manifest}"
echo "file name: ${packagename}" >> "${manifest}"
echo "file date: $(date -r ${path}/${packagename} -R)" >> "${manifest}"
echo "package type: ${packagetype}" >> "${manifest}"
echo "package format: ${format}" >> "${manifest}"
echo "package prefix: ${prefix}" >> "${manifest}"
echo "git commit branch: ${branch}" >> "${manifest}"
echo "git commit hash: ${commit}" >> "${manifest}"
if [[ "${packagetype}" == "tagged" ]]; then
echo "git commit tag: ${committag}" >> "${manifest}"
fi
echo "git commit date: ${fullcommitdate}" >> "${manifest}"
echo "git commit desc: ${describe}" >> "${manifest}"
fi
echo " - Deleting the temp dir ${tempdir}"
rm -fr ${tempdir}