Skip to content

Commit 523fb31

Browse files
committed
Implemented dry_run mode including tests for RootModule.update and Submodule
1 parent d810f27 commit 523fb31

File tree

3 files changed

+261
-170
lines changed

3 files changed

+261
-170
lines changed

‎objects/submodule/base.py

+76-51
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,15 @@ def update(self, recursive=False, init=True, to_latest_revision=False, progress=
329329
if progress is None:
330330
progress = UpdateProgress()
331331
#END handle progress
332+
prefix = ''
333+
if dry_run:
334+
prefix = "DRY-RUN: "
335+
#END handle prefix
336+
337+
# to keep things plausible in dry-run mode
338+
if dry_run:
339+
mrepo = None
340+
#END init mrepo
332341

333342
# ASSURE REPO IS PRESENT AND UPTODATE
334343
#####################################
@@ -342,14 +351,16 @@ def update(self, recursive=False, init=True, to_latest_revision=False, progress=
342351
op |= BEGIN
343352
#END handle start
344353

345-
progress.update(op, i, len_rmts, "Fetching remote %s" % remote)
354+
progress.update(op, i, len_rmts, prefix+"Fetching remote %s of submodule %r" % (remote, self.name))
346355
#===============================
347-
remote.fetch(progress=progress)
356+
if not dry_run:
357+
remote.fetch(progress=progress)
358+
#END handle dry-run
348359
#===============================
349360
if i == len_rmts-1:
350361
op |= END
351362
#END handle end
352-
progress.update(op, i, len_rmts, "Done fetching remote %s" % remote)
363+
progress.update(op, i, len_rmts, prefix+"Done fetching remote of submodule %r" % self.name)
353364
#END fetch new data
354365
except InvalidGitRepositoryError:
355366
if not init:
@@ -359,7 +370,7 @@ def update(self, recursive=False, init=True, to_latest_revision=False, progress=
359370

360371
# there is no git-repository yet - but delete empty paths
361372
module_path = join_path_native(self.repo.working_tree_dir, self.path)
362-
if os.path.isdir(module_path):
373+
if not dry_run and os.path.isdir(module_path):
363374
try:
364375
os.rmdir(module_path)
365376
except OSError:
@@ -369,42 +380,51 @@ def update(self, recursive=False, init=True, to_latest_revision=False, progress=
369380

370381
# don't check it out at first - nonetheless it will create a local
371382
# branch according to the remote-HEAD if possible
372-
progress.update(BEGIN|CLONE, 0, 1, "Cloning %s to %s" % (self.url, module_path))
373-
mrepo = git.Repo.clone_from(self.url, module_path, n=True)
374-
progress.update(END|CLONE, 0, 1, "Done cloning to %s" % module_path)
383+
progress.update(BEGIN|CLONE, 0, 1, prefix+"Cloning %s to %s in submodule %r" % (self.url, module_path, self.name))
384+
if not dry_run:
385+
mrepo = git.Repo.clone_from(self.url, module_path, n=True)
386+
#END handle dry-run
387+
progress.update(END|CLONE, 0, 1, prefix+"Done cloning to %s" % module_path)
375388

376-
# see whether we have a valid branch to checkout
377-
try:
378-
# find a remote which has our branch - we try to be flexible
379-
remote_branch = find_first_remote_branch(mrepo.remotes, self.branch_name)
380-
local_branch = mkhead(mrepo, self.branch_path)
381-
382-
# have a valid branch, but no checkout - make sure we can figure
383-
# that out by marking the commit with a null_sha
384-
local_branch.set_object(util.Object(mrepo, self.NULL_BIN_SHA))
385-
# END initial checkout + branch creation
386-
387-
# make sure HEAD is not detached
388-
mrepo.head.set_reference(local_branch, logmsg="submodule: attaching head to %s" % local_branch)
389-
mrepo.head.ref.set_tracking_branch(remote_branch)
390-
except IndexError:
391-
print >> sys.stderr, "Warning: Failed to checkout tracking branch %s" % self.branch_path
392-
#END handle tracking branch
393389

394-
# NOTE: Have to write the repo config file as well, otherwise
395-
# the default implementation will be offended and not update the repository
396-
# Maybe this is a good way to assure it doesn't get into our way, but
397-
# we want to stay backwards compatible too ... . Its so redundant !
398-
self.repo.config_writer().set_value(sm_section(self.name), 'url', self.url)
390+
if not dry_run:
391+
# see whether we have a valid branch to checkout
392+
try:
393+
# find a remote which has our branch - we try to be flexible
394+
remote_branch = find_first_remote_branch(mrepo.remotes, self.branch_name)
395+
local_branch = mkhead(mrepo, self.branch_path)
396+
397+
# have a valid branch, but no checkout - make sure we can figure
398+
# that out by marking the commit with a null_sha
399+
local_branch.set_object(util.Object(mrepo, self.NULL_BIN_SHA))
400+
# END initial checkout + branch creation
401+
402+
# make sure HEAD is not detached
403+
mrepo.head.set_reference(local_branch, logmsg="submodule: attaching head to %s" % local_branch)
404+
mrepo.head.ref.set_tracking_branch(remote_branch)
405+
except IndexError:
406+
print >> sys.stderr, "Warning: Failed to checkout tracking branch %s" % self.branch_path
407+
#END handle tracking branch
408+
409+
# NOTE: Have to write the repo config file as well, otherwise
410+
# the default implementation will be offended and not update the repository
411+
# Maybe this is a good way to assure it doesn't get into our way, but
412+
# we want to stay backwards compatible too ... . Its so redundant !
413+
self.repo.config_writer().set_value(sm_section(self.name), 'url', self.url)
414+
#END handle dry_run
399415
#END handle initalization
400416

401417

402418
# DETERMINE SHAS TO CHECKOUT
403419
############################
404420
binsha = self.binsha
405421
hexsha = self.hexsha
406-
is_detached = mrepo.head.is_detached
407-
if to_latest_revision:
422+
if mrepo is not None:
423+
# mrepo is only set if we are not in dry-run mode or if the module existed
424+
is_detached = mrepo.head.is_detached
425+
#END handle dry_run
426+
427+
if not dry_run and to_latest_revision:
408428
msg_base = "Cannot update to latest revision in repository at %r as " % mrepo.working_dir
409429
if not is_detached:
410430
rref = mrepo.head.ref.tracking_branch()
@@ -421,29 +441,35 @@ def update(self, recursive=False, init=True, to_latest_revision=False, progress=
421441
# END handle to_latest_revision option
422442

423443
# update the working tree
424-
if mrepo.head.commit.binsha != binsha:
425-
progress.update(BEGIN|UPDWKTREE, 0, 1, "Updating working tree at %s" % self.path)
426-
if is_detached:
427-
# NOTE: for now we force, the user is no supposed to change detached
428-
# submodules anyway. Maybe at some point this becomes an option, to
429-
# properly handle user modifications - see below for future options
430-
# regarding rebase and merge.
431-
mrepo.git.checkout(hexsha, force=True)
432-
else:
433-
# TODO: allow to specify a rebase, merge, or reset
434-
# TODO: Warn if the hexsha forces the tracking branch off the remote
435-
# branch - this should be prevented when setting the branch option
436-
mrepo.head.reset(hexsha, index=True, working_tree=True)
437-
# END handle checkout
438-
progress.update(END|UPDWKTREE, 0, 1, "Done updating working tree at %s" % self.path)
444+
# handles dry_run
445+
if mrepo is not None and mrepo.head.commit.binsha != binsha:
446+
progress.update(BEGIN|UPDWKTREE, 0, 1, prefix+"Updating working tree at %s for submodule %r" % (self.path, self.name))
447+
if not dry_run:
448+
if is_detached:
449+
# NOTE: for now we force, the user is no supposed to change detached
450+
# submodules anyway. Maybe at some point this becomes an option, to
451+
# properly handle user modifications - see below for future options
452+
# regarding rebase and merge.
453+
mrepo.git.checkout(hexsha, force=True)
454+
else:
455+
# TODO: allow to specify a rebase, merge, or reset
456+
# TODO: Warn if the hexsha forces the tracking branch off the remote
457+
# branch - this should be prevented when setting the branch option
458+
mrepo.head.reset(hexsha, index=True, working_tree=True)
459+
# END handle checkout
460+
#END handle dry_run
461+
progress.update(END|UPDWKTREE, 0, 1, prefix+"Done updating working tree for submodule %r" % self.name)
439462
# END update to new commit only if needed
440463

441464
# HANDLE RECURSION
442465
##################
443466
if recursive:
444-
for submodule in self.iter_items(self.module()):
445-
submodule.update(recursive, init, to_latest_revision, progress=progress)
446-
# END handle recursive update
467+
# in dry_run mode, the module might not exist
468+
if mrepo is not None:
469+
for submodule in self.iter_items(self.module()):
470+
submodule.update(recursive, init, to_latest_revision, progress=progress, dry_run=dry_run)
471+
# END handle recursive update
472+
#END handle dry run
447473
# END for each submodule
448474

449475
return self
@@ -843,8 +869,7 @@ def config_reader(self):
843869
def children(self):
844870
"""
845871
:return: IterableList(Submodule, ...) an iterable list of submodules instances
846-
which are children of this submodule
847-
:raise InvalidGitRepositoryError: if the submodule is not checked-out"""
872+
which are children of this submodule or 0 if the submodule is not checked out"""
848873
return self._get_intermediate_items(self)
849874

850875
#} END query interface

0 commit comments

Comments
 (0)