@@ -211,18 +211,26 @@ def add(cls, repo, path, url, skip_init=False):
211
211
:param skip_init: if True, the new repository will not be cloned to its location.
212
212
:return: The newly created submodule instance"""
213
213
214
- def update (self , recursive = False , init = True ):
214
+ def update (self , recursive = False , init = True , to_latest_revision = False ):
215
215
"""Update the repository of this submodule to point to the checkout
216
216
we point at with the binsha of this instance.
217
217
:param recursive: if True, we will operate recursively and update child-
218
218
modules as well.
219
219
:param init: if True, the module repository will be cloned into place if necessary
220
+ :param to_latest_revision: if True, the submodule's sha will be ignored during checkout.
221
+ Instead, the remote will be fetched, and the local tracking branch updated.
222
+ This only works if we have a local tracking branch, which is the case
223
+ if the remote repository had a master branch, or of the 'branch' option
224
+ was specified for this submodule and the branch existed remotely
220
225
:note: does nothing in bare repositories
221
226
:return: self"""
222
227
if self .repo .bare :
223
228
return self
224
229
#END pass in bare mode
225
230
231
+
232
+ # ASSURE REPO IS PRESENT AND UPTODATE
233
+ #####################################
226
234
try :
227
235
mrepo = self .module ()
228
236
for remote in mrepo .remotes :
@@ -277,22 +285,45 @@ def update(self, recursive=False, init=True):
277
285
#END handle tracking branch
278
286
#END handle initalization
279
287
288
+
289
+ # DETERMINE SHAS TO CHECKOUT
290
+ ############################
291
+ binsha = self .binsha
292
+ hexsha = self .hexsha
293
+ is_detached = mrepo .head .is_detached
294
+ if to_latest_revision :
295
+ msg_base = "Cannot update to latest revision in repository at %r as " % mrepo .working_dir
296
+ if not is_detached :
297
+ rref = mrepo .head .ref .tracking_branch ()
298
+ if rref is not None :
299
+ rcommit = rref .commit
300
+ binsha = rcommit .binsha
301
+ hexsha = rcommit .hexsha
302
+ else :
303
+ print >> sys .stderr , "%s a tracking branch was not set for local branch '%s'" % (msg_base , mrepo .head .ref )
304
+ # END handle remote ref
305
+ else :
306
+ print >> sys .stderr , "%s there was no local tracking branch" % msg_base
307
+ # END handle detached head
308
+ # END handle to_latest_revision option
309
+
280
310
# update the working tree
281
- if mrepo .head .commit .binsha != self . binsha :
282
- if mrepo . head . is_detached :
283
- mrepo .git .checkout (self . hexsha )
311
+ if mrepo .head .commit .binsha != binsha :
312
+ if is_detached :
313
+ mrepo .git .checkout (hexsha )
284
314
else :
285
315
# TODO: allow to specify a rebase, merge, or reset
286
316
# TODO: Warn if the hexsha forces the tracking branch off the remote
287
317
# branch - this should be prevented when setting the branch option
288
- mrepo .head .reset (self . hexsha , index = True , working_tree = True )
318
+ mrepo .head .reset (hexsha , index = True , working_tree = True )
289
319
# END handle checkout
290
320
# END update to new commit only if needed
291
321
292
322
# HANDLE RECURSION
323
+ ##################
293
324
if recursive :
294
325
for submodule in self .iter_items (self .module ()):
295
- submodule .update (recursive , init )
326
+ submodule .update (recursive , init , to_latest_revision )
296
327
# END handle recursive update
297
328
# END for each submodule
298
329
0 commit comments