Browse code

Add git update tag support

Change-Id: I5ce1f05186d05b9cf0ccd74708af926ba054d2f0

Evgeniy Afonichev authored on 2012/07/10 20:02:43
Showing 1 changed files
... ...
@@ -222,6 +222,30 @@ GetOSVersion() {
222 222
     export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
223 223
 }
224 224
 
225
+# git update using reference as a branch.
226
+function git_update_branch() {
227
+
228
+    GIT_BRANCH=$1
229
+
230
+    git checkout -f origin/$GIT_BRANCH
231
+    # a local branch might not exist
232
+    git branch -D $GIT_BRANCH || true
233
+    git checkout -b $GIT_BRANCH
234
+}
235
+
236
+
237
+# git update using reference as a tag. Be careful editing source at that repo
238
+# as working copy will be in a detached mode
239
+function git_update_tag() {
240
+
241
+    GIT_TAG=$1
242
+
243
+    git tag -d $GIT_TAG
244
+    # fetching given tag only
245
+    git fetch origin tag $GIT_TAG
246
+    git checkout -f $GIT_TAG
247
+}
248
+
225 249
 
226 250
 # git clone only if directory doesn't exist already.  Since ``DEST`` might not
227 251
 # be owned by the installation user, we create the directory and change the
... ...
@@ -235,16 +259,16 @@ function git_clone {
235 235
 
236 236
     GIT_REMOTE=$1
237 237
     GIT_DEST=$2
238
-    GIT_BRANCH=$3
238
+    GIT_REF=$3
239 239
 
240
-    if echo $GIT_BRANCH | egrep -q "^refs"; then
240
+    if echo $GIT_REF | egrep -q "^refs"; then
241 241
         # If our branch name is a gerrit style refs/changes/...
242 242
         if [[ ! -d $GIT_DEST ]]; then
243 243
             [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1
244 244
             git clone $GIT_REMOTE $GIT_DEST
245 245
         fi
246 246
         cd $GIT_DEST
247
-        git fetch $GIT_REMOTE $GIT_BRANCH && git checkout FETCH_HEAD
247
+        git fetch $GIT_REMOTE $GIT_REF && git checkout FETCH_HEAD
248 248
     else
249 249
         # do a full clone only if the directory doesn't exist
250 250
         if [[ ! -d $GIT_DEST ]]; then
... ...
@@ -252,7 +276,7 @@ function git_clone {
252 252
             git clone $GIT_REMOTE $GIT_DEST
253 253
             cd $GIT_DEST
254 254
             # This checkout syntax works for both branches and tags
255
-            git checkout $GIT_BRANCH
255
+            git checkout $GIT_REF
256 256
         elif [[ "$RECLONE" == "yes" ]]; then
257 257
             # if it does exist then simulate what clone does if asked to RECLONE
258 258
             cd $GIT_DEST
... ...
@@ -263,10 +287,17 @@ function git_clone {
263 263
             # (due to the py files having older timestamps than our pyc, so python
264 264
             # thinks the pyc files are correct using them)
265 265
             find $GIT_DEST -name '*.pyc' -delete
266
-            git checkout -f origin/$GIT_BRANCH
267
-            # a local branch might not exist
268
-            git branch -D $GIT_BRANCH || true
269
-            git checkout -b $GIT_BRANCH
266
+
267
+            # handle GIT_REF accordingly to type (tag, branch)
268
+            if [[ -n "`git show-ref refs/tags/$GIT_REF`" ]]; then
269
+                git_update_tag $GIT_REF
270
+            elif [[ -n "`git show-ref refs/heads/$GIT_REF`" ]]; then
271
+                git_update_branch $GIT_REF
272
+            else
273
+                echo $GIT_REF is neither branch nor tag
274
+                exit 1
275
+            fi
276
+
270 277
         fi
271 278
     fi
272 279
 }