This repository has been archived by the owner on Jan 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
update_submodule
executable file
·197 lines (158 loc) · 5 KB
/
update_submodule
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
#!/bin/bash
APPS_DIR=~/APPS
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# A POSIX variable
OPTIND=1 # Reset in case getopts has been used previously in the shell.
# Initialize our own variables:
SUBMOD=""
TEST=0
function usage(){
cat <<EOF
update_submodule -s submodule_name [-r repo_name] [-a apps_directory][-n] [-h]
EOF
}
function show_help(){
usage
cat <<EOF
-s submodule_name : Supply the name of the module to be updated ( eg apps/dashboard )
-a apps_dir : the directory in which the real non-submodule versions of the submodule app is (defaults to ~/APPS)
-r repo_name : By default assumes that repo has been cloned in $APPS_DIR with the submodule_name, however if
this is not the case the repo_name can be suppoied (eg TWI-dashboard)
-n : not-really, just show me what will be copied to $APPS_DIR and what the git diff is
-h : help, show this message and exit
EOF
}
while getopts "h?ns:r:a:" opt; do
case "$opt" in
h)
show_help
exit 0
;;
\?)
usage
exit 0
;;
n) TEST=1
;;
s)
if [[ $OPTARG = -* ]]; then
((OPTIND--))
continue
fi
SUBMOD=$OPTARG
;;
a)
if [[ $OPTARG = -* ]]; then
((OPTIND--))
continue
fi
APPS_DIR=$OPTARG
;;
r)
if [[ $OPTARG = -* ]]; then
((OPTIND--))
continue
fi
REPONAME=$OPTARG
;;
esac
done
[[ $SUBMOD == "" ]] && (usage; exit 0)
shift $((OPTIND-1))
[ "$1" = "--" ] && shift
[ -z "$REPONAME" ] && REPONAME=${SUBMOD##*/}
SUB_DIR=$SUBMOD
#In the APP_DIR we We assume that the repository will be installed to just the app name so apps/dashboard becomes dashboard
#In case a git clone was done and the default repo name was (eg TWI-dashboard) we provide the -r option
APP_DIR=$APPS_DIR/$REPONAME
if [ $TEST == 0 ]
then
cat <<EOF
############################################ TWI SUBMODULE UPDATER ################################################
#
# Before we get going you must understand the following:
# - This script will copy changes from the submodule directory $SUBMOD to the app direcotry $APP_DIR
# - This script will then revert the changes in $SUBMOD
# - commit and push the changes in $APP_DIR
# - It will update the submodule and push that change to the project repository
# - All pushes and pulls will be to/from origin master
#
####################################################################################################################
This process is for updating submodule repositories only. It is probablly best to commit (or revert) any changes to the project files before you run this just in case.
Is this OK?[Y/N]
EOF
read response
[ $response != 'Y' ] && exit
fi
#collect files that have changed #TODO and new files added
cd $SUBMOD
##This is easier!!!
echo "######## MODIFIED FILES ##########"
CHANGED_FILES=$(git ls-files --modified --exclude-standard)
#CHANGED_FILES=$(git status | grep '\s\+modified:' | cut -f 2- -d :)
for MOD_PATH in $CHANGED_FILES
do
:
cd $DIR/$SUBMOD
MOD_PATH="$(echo -e "${MOD_PATH}" | sed -e 's/^[[:space:]]*//')"
echo "$SUBMOD/$MOD_PATH ==> $APP_DIR/$MOD_PATH"
#Copy them from the project submodule to the app clone
if [ $TEST == 0 ]
then
#echo "cp $MOD_PATH $APPS_DIR/$REPONAME/$MOD_PATH"
echo "Copying $SUBMOD/$MOD_PATH"
cp $MOD_PATH $APP_DIR/$MOD_PATH || { echo "Copy failed" ; exit 1; }
#revert the copied file in the sumodule
DIFF=$(diff $MOD_PATH $APP_DIR/$MOD_PATH)
if [ "$DIFF" != "" ]
then
echo "Stopping as the file $SUBMOD/$MOD_PATH wasn't copied to $APP_DIR/$MOD_PATH properly" && exit
fi
# Go to the APP clone and commit the change
cd $APP_DIR
echo "commiting to real app repository" && git commit $MOD_PATH
else
git diff
fi
done
#echo "reverting submodule" && git checkout -- $MOD_PATH
echo "######## ADDED FILES ##########"
ADDED_FILES=$(git ls-files --others --exclude-standard)
for ADD_PATH in $ADDED_FILES
do
:
cd $DIR/$SUBMOD
echo "$SUBMOD/$ADD_PATH ==> $APP_DIR/$ADD_PATH"
ADD_DIR=$(dirname "${ADD_PATH}")
echo "ADD_DIR: "$ADD_DIR
echo "$SUBMOD/$ADD_PATH ==> $APP_DIR/$ADD_PATH"
if [ $TEST == 0 ]
then
[ ! -d $APP_DIR/$ADD_DIR ] && mkdir -p $APP_DIR/$ADD_DIR
cp $ADD_PATH $APP_DIR/$ADD_PATH || { echo "Copy failed" ; exit 1; }
#revert the copied file in the sumodule
DIFF=$(diff $ADD_PATH $APP_DIR/$ADD_PATH)
if [ "$DIFF" != "" ]
then
echo "Stopping as the file $SUBMOD/$ADD_PATH wasn't copied to $APP_DIR/$ADD_PATH properly" && exit
fi
# Go to the APP clone and commit the change
cd $APP_DIR
echo "Adding $ADD_PATH" && git add $ADD_PATH
echo "Commiting to real app repository" && git commit $ADD_PATH
fi
done
#echo "removing file from submodule" && rm $ADD_PATH
if [ $TEST == 1 ]
then
exit
fi
#push the changed app
(cd $APP_DIR && git push origin master)
#go to submodule and pull
cd $DIR/$SUBMOD
git pull origin master
#go to project and commit and push
cd $DIR
git commit $SUBMOD
git push origin master