summaryrefslogtreecommitdiffstats
path: root/debian/_buildscripts/local/update_repositories.sh
blob: 5c0f5aa451010354e770c13eacb92c0a052b2894 (plain)
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
219
220
221
222
223
#!/bin/bash

# Check command line arguments and set options
# Run before loading configuration, to allow branch overriding
bool_INCREMENTAL="n"
bool_VERBOSE_LOG="n"
bool_SWITCH_ONLY="n"
OVERRIDE_DEFAULT_REPO_BRANCH=""
for ((idx=1; idx<=$#; idx++)); do
  arg="${!idx}"
	if [ "$arg" = "-i" ]; then 		    		# continue from last updated module (Incremental)
		bool_INCREMENTAL="y"
	elif [ "$arg" = "-v" ]; then 		    	# display and log git command output (Verbose)
		bool_VERBOSE_LOG="y"
	elif [ "$arg" = "-ub" ]; then 	   		# branches to update (Update Branches)
		((idx++))
		OVERRIDE_UPDATE_BRANCHES="${!idx}"
		[[ -z "$OVERRIDE_DEFAULT_REPO_BRANCH" ]] && OVERRIDE_DEFAULT_REPO_BRANCH="${!idx}"
	elif [ "$arg" = "-db" ]; then 	    	# default branch after update (Default Branch)
		((idx++))
		if [[ "$bool_SWITCH_ONLY" != 'y' ]]; then
		  # '-db' is only used if no '-so' argument is specified. If '-so <branch>'
		  # is given, '-db <branch> is ignored
			OVERRIDE_DEFAULT_REPO_BRANCH="${!idx}"
		fi
	elif [ "$arg" = "-so" ]; then 	   		# switch branch only (Switch Only)
		bool_SWITCH_ONLY="y" && ((idx++))
		OVERRIDE_DEFAULT_REPO_BRANCH="${!idx}"
	fi
done

# Load common code
. ./internals/_build_common.sh
init_common

UPDATE_LOCK_FILENAME="/var/lock/TDE_update_repo_lock"  	# Lock file for incremental update

#----------------------------
# Update a given module from the upstream repo
# Parameters:
#  $1 - module folder
#  $2 - operation type
#  $3 - branch to update
#  $4 - new branch flag (only for reporting)
function _do_update()
{
	local MOD_PATH=$1
	local OP_TYPE=$2
	local BRANCH=$3
	local NEW_BRANCH=$4
	local RESULT=""
	
	case "$OP_TYPE" in 
		"update")
			if [[ -z `grep "^$BRANCH - $MOD_PATH$" "$UPDATE_LOCK_FILENAME"` ]]; then
				cd "$MOD_PATH" &>/dev/null
				if [ $? -eq 0 ]; then
					# Clean up any possible uncommitted changes
					if [[ ! -z "`git status --porcelain $GIT_IGNORE_SUBMODULES`" ]]; then
						git reset --hard HEAD &>/dev/null
						git clean -dxff &>/dev/null
					fi
					# Make sure the local branch exists
					if [[ -z `git branch | grep -E "\b$BRANCH\b"` ]]; then
					  NEW_BRANCH="y"
						eval git checkout -b \"$BRANCH\" \"origin/$BRANCH\" $OPT_VERBOSE_LOG
					else	
						eval git checkout \"$BRANCH\" $OPT_VERBOSE_LOG
					fi
					# Make sure the local branch is a tracking branch
					if [[ -z `git config branch."$BRANCH".remote` ]]; then
						NEW_BRANCH="y"
						git branch -u "origin/$BRANCH" &>/dev/null #$
						git reset --hard "origin/$BRANCH" &>/dev/null
					fi
					# Update
					eval git reset --hard HEAD $OPT_VERBOSE_LOG
					eval git clean -dxff $OPT_VERBOSE_LOG
					eval git fetch $OPT_VERBOSE_LOG
					if [[ $(git rev-parse HEAD) != $(git rev-parse "origin/$BRANCH") ]]; then
						eval git pull --rebase $GIT_NO_RECURSE_SUBMODULES $OPT_VERBOSE_LOG
						if [[ `git rev-parse HEAD` == `git rev-parse "origin/$BRANCH"` ]]; then
							RESULT="${CLightGreen}[ UPDATE ]"
						else
							RESULT="${CLightRed}[  FAIL  ]"
						fi
					else
						if [[ "$NEW_BRANCH" = "y" ]]; then
							RESULT="${CLightGreen}[ UPDATE ]"
						else
							RESULT="[   OK   ]"
						fi
					fi
				else
					RESULT="${CLightRed}[  FAIL  ]"
				fi
				echo "$BRANCH - $MOD_PATH" >> "$UPDATE_LOCK_FILENAME"
			else
				RESULT="${CBrown}[  SKIP  ]"
			fi
			;;
		
		"switch-to")
			cd "$MOD_PATH" &>/dev/null
			eval git checkout \"$BRANCH\" $OPT_VERBOSE_LOG
			eval git reset --hard HEAD $OPT_VERBOSE_LOG
			eval git clean -dxff $OPT_VERBOSE_LOG
			if [[ ! -z `git branch -v | grep -E "^\*\s+$BRANCH"` ]]; then
				RESULT="[   OK   ]"
			else
				RESULT="${CLightRed}[  FAIL  ]"
			fi
			;;
	
		*)
			RESULT="[ INV-OP ]"
			;;
	esac
	
	echo_and_tee "$RESULT  $MOD_PATH${CNone}" "$LOG_UPDATE_REPO_FILENAME"
}

#----------------------------
# Update a given module and all submodules from the upstream repo
# Parameters:
#  $1 - module folder
#  $2 - operation type 
#  $3 - branch to update
#  $4 - new branch flag (only for reporting)
function _update_module()
{
	local MOD_PATH=$1
	local OP_TYPE=$2
	local BRANCH=$3
	
	# Current module
	_do_update "$@"
  # Submodules
	local NEW_BRANCH="n"
  local SUBMOD_LIST="$MOD_PATH/.gitmodules"
	if [[ -e "$SUBMOD_LIST" ]]; then
		sed -n "s|^\[submodule \"\([^\"]*\)\"\]$|\1|p" <$SUBMOD_LIST |\
		while read -r SUBMOD_PATH; do
			NEW_BRANCH="n"
			cd "$MOD_PATH" &>/dev/null
			if [[ -z "`git config --get submodule.$SUBMOD_PATH.url`" ]]; then
			  NEW_BRANCH="y"  # if a submodule is missing, need to report "update" status
				eval git submodule init -- \"$SUBMOD_PATH\" $OPT_VERBOSE_LOG
			fi
			if [[ ! -e "$MOD_PATH/$SUBMOD_PATH/.git" ]]; then
			  NEW_BRANCH="y"  # if a submodule is incomplete, need to report "update" status
				eval git submodule update -- \"$SUBMOD_PATH\" $OPT_VERBOSE_LOG
			fi
			_update_module "$MOD_PATH/$SUBMOD_PATH" "$OP_TYPE" "$BRANCH" "$NEW_BRANCH"
		done
	fi
}

#----------------------------
if [ "$bool_INCREMENTAL" = "y" ]; then
  [ ! -f "$UPDATE_LOCK_FILENAME" ] && bool_INCREMENTAL="n"
else
	[ -f "$UPDATE_LOCK_FILENAME" ] && rm "$UPDATE_LOCK_FILENAME"
fi

OPT_VERBOSE_LOG="&>/dev/null"
if [[ "$bool_VERBOSE_LOG" = "y" ]]; then
	OPT_VERBOSE_LOG=" |& tee -a \"$LOG_UPDATE_REPO_FILENAME\""
fi

#----------------------------
# Check git abilities
GIT_IGNORE_SUBMODULES=""
if [[ -n "`git status --help 2>/dev/null|grep -- '--ignore-submodules'`" ]]; then
  GIT_IGNORE_SUBMODULES="--ignore-submodules"
fi
GIT_NO_RECURSE_SUBMODULES=""
if [[ -n "`git pull --help |grep -- '--\[no-\]recurse-submodules'`" ]]; then
  GIT_NO_RECURSE_SUBMODULES="--no-recurse-submodules"
fi

# Start update
if [ "$bool_INCREMENTAL" != "y" ]; then
	echo "TDE repositories update started" > "$UPDATE_LOCK_FILENAME"
fi

_LAST_BRANCH=""
if [[ "$bool_SWITCH_ONLY" != 'y' ]]; then
	# Branch update
	for branch in "${BRANCHES[@]}"; do
		_LAST_BRANCH="$branch"
		echo_and_tee "${CLightCyan}---------------------------------------${CNone}" "$LOG_UPDATE_REPO_FILENAME" "y"
		echo_and_tee "${CLightCyan}        Updating branch ${CYellow}$branch        ${CNone}" "$LOG_UPDATE_REPO_FILENAME"
		echo_and_tee "${CLightCyan}---------------------------------------${CNone}" "$LOG_UPDATE_REPO_FILENAME"
			
		# Update TDE main repository
		_update_module "$REPO_TDE" "update" "$branch" "n"
		
		# Update TDE packaging repository
		_update_module "$TDE_DIR/$CFG_GIT_DIR/tde-packaging" "update" "$branch" "n"

		echo_and_tee "" "$LOG_UPDATE_REPO_FILENAME"
	done
fi

# Switch to specified branch if necessary
if [[ "$DEFAULT_REPO_BRANCH" != "$_LAST_BRANCH" ]]; then
	echo_and_tee "${CLightCyan}---------------------------------------${CNone}" "$LOG_UPDATE_REPO_FILENAME" "y"
	echo_and_tee "${CLightCyan}      Switching to branch ${CYellow}$DEFAULT_REPO_BRANCH      ${CNone}" "$LOG_UPDATE_REPO_FILENAME"
	echo_and_tee "${CLightCyan}---------------------------------------${CNone}" "$LOG_UPDATE_REPO_FILENAME"
		
	# Switch TDE main repository
	_update_module "$REPO_TDE" "switch-to" "$DEFAULT_REPO_BRANCH" "n"
	
	# Switch TDE packaging repository
	_update_module "$TDE_DIR/$CFG_GIT_DIR/tde-packaging" "switch-to" "$DEFAULT_REPO_BRANCH" "n"

	echo_and_tee "" "$LOG_UPDATE_REPO_FILENAME"
fi

# Update completed
[ -f "$UPDATE_LOCK_FILENAME" ] && rm "$UPDATE_LOCK_FILENAME"
cd $SCRIPT_DIR