-
Notifications
You must be signed in to change notification settings - Fork 2
/
git-archive.sh
executable file
·105 lines (86 loc) · 3.36 KB
/
git-archive.sh
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
#!/bin/bash
################################################################################
# This Bash script is used to zip a specified commit in current repository. #
# The commit is specified by input parameter $1. Use HEAD as the commit if #
# no input parameter $1 is specified. #
# #
# This script will be called by alias "git sar", see conf.git-config. #
################################################################################
#-------------------------------------------------------------------------------
# 1) Get the commit-id and output-dir
#-------------------------------------------------------------------------------
# Set default commit-id and output-dir
commitId="HEAD"
outputDir="."
path=`pwd`
withSubmodule="No"
usage="git-archive.sh { [-c commit-id] [-p path ] [-o output-dir] [-m] | [-h] }"
# Get commit-id and output-dir from input parameters
while getopts "c:p:o:mh" arg
do
case $arg in
c) # -c <commit-id>
commitId=$OPTARG
;;
p) # -p <path>
path=$OPTARG
;;
o) # -o <output-dir>
outputDir=$OPTARG
;;
m) # -m: archive submodules at the same time
withSubmodule="Yes"
;;
h) # help
echo ${usage}
exit 1
;;
?) # unkonw argument
echo "ERROR: Unknown argument"
exit 1
;;
esac
done
#-------------------------------------------------------------------------------
# 2) Archive the specified commit ID 'shortCommitId' to 'fileName'
#-------------------------------------------------------------------------------
# Get short commit ID corresponding to commitId
shortCommitId=`git rev-parse --short ${commitId}`
# Get the top directory of the repo
repoRootDir=`git rev-parse --show-toplevel`
# Get repo name
repoName=`basename ${repoRootDir}`
# Get commit describe corresponding to commitId
commitDesc=`git describe --always ${commitId}`
# Assemble file name
fileName=${repoName}-${commitDesc}
# Create temperay directory to collect all output
temperayDir=${outputDir}/${fileName}
echo Create temperay directory ${temperayDir}
mkdir -p ${temperayDir}
# Archive the main repo
echo Archive commit ${shortCommitId} of repo ${repoName} to ${temperayDir}
cd ${repoRootDir}
git archive --format=tar ${shortCommitId} ${path} | (cd ${temperayDir} && tar xf -)
# Update submodules corresponding to commit ID 'shortCommitId' and check submodules status
if [ ${withSubmodule} == "Yes" ]; then
echo Update submodules and current submodules status:
git submodule update --init --recursive
git submodule
# Archive each submodule
repoSubmodules=`git submodule | awk '{print $2}'`
for submodule in ${repoSubmodules}; do
submodulePath=${repoRootDir}/${submodule}
echo Archive submodule ${submodule} in directory ${submodulePath}
# echo cd ${submodulePath}
cd ${submodulePath}
git archive --format=tar --prefix=${submodule}/ HEAD | (cd ${temperayDir} && tar xf -)
done
fi
# Compress all modules to .tar.gz
echo Comparess all output to ${fileName}.tar.gz
cd ${outputDir}
tar cf ${fileName}.tar.gz ${fileName}
echo Remove temperay directory ${temperayDir}
rm -rf ${fileName}
echo Done