diff options
Diffstat (limited to 'git/objects/submodule')
-rw-r--r-- | git/objects/submodule/base.py | 118 | ||||
-rw-r--r-- | git/objects/submodule/root.py | 97 | ||||
-rw-r--r-- | git/objects/submodule/util.py | 6 |
3 files changed, 119 insertions, 102 deletions
diff --git a/git/objects/submodule/base.py b/git/objects/submodule/base.py index 14e1c930..e3d58077 100644 --- a/git/objects/submodule/base.py +++ b/git/objects/submodule/base.py @@ -1,27 +1,27 @@ import util from util import ( - mkhead, - sm_name, - sm_section, - unbare_repo, - SubmoduleConfigParser, - find_first_remote_branch - ) + mkhead, + sm_name, + sm_section, + unbare_repo, + SubmoduleConfigParser, + find_first_remote_branch +) from git.objects.util import Traversable from StringIO import StringIO # need a dict to set bloody .name field from git.util import ( - Iterable, - join_path_native, - to_native_path_linux, - RemoteProgress, - rmtree - ) + Iterable, + join_path_native, + to_native_path_linux, + RemoteProgress, + rmtree +) from git.config import SectionConstraint from git.exc import ( - InvalidGitRepositoryError, - NoSuchPathError - ) + InvalidGitRepositoryError, + NoSuchPathError +) import stat import git @@ -160,7 +160,8 @@ class Submodule(util.IndexObject, Iterable, Traversable): try: fp_module = cls._sio_modules(parent_commit) except KeyError: - raise IOError("Could not find %s file in the tree of parent commit %s" % (cls.k_modules_file, parent_commit)) + raise IOError("Could not find %s file in the tree of parent commit %s" % + (cls.k_modules_file, parent_commit)) # END handle exceptions # END handle non-bare working tree @@ -237,7 +238,7 @@ class Submodule(util.IndexObject, Iterable, Traversable): # like it ... if url != None: url = to_native_path_linux(url) - #END assure url correctness + # END assure url correctness # INSTANTIATE INTERMEDIATE SM sm = cls(repo, cls.NULL_BIN_SHA, cls.k_default_mode, path, name) @@ -260,7 +261,8 @@ class Submodule(util.IndexObject, Iterable, Traversable): branch_is_default = branch is None if has_module and url is not None: if url not in [r.url for r in sm.module().remotes]: - raise ValueError("Specified URL '%s' does not match any remote url of the repository at '%s'" % (url, sm.abspath)) + raise ValueError( + "Specified URL '%s' does not match any remote url of the repository at '%s'" % (url, sm.abspath)) # END check url # END verify urls match @@ -307,7 +309,7 @@ class Submodule(util.IndexObject, Iterable, Traversable): return sm def update(self, recursive=False, init=True, to_latest_revision=False, progress=None, - dry_run=False): + dry_run=False): """Update the repository of this submodule to point to the checkout we point at with the binsha of this instance. @@ -327,20 +329,20 @@ class Submodule(util.IndexObject, Iterable, Traversable): :return: self""" if self.repo.bare: return self - #END pass in bare mode + # END pass in bare mode if progress is None: progress = UpdateProgress() - #END handle progress + # END handle progress prefix = '' if dry_run: prefix = "DRY-RUN: " - #END handle prefix + # END handle prefix # to keep things plausible in dry-run mode if dry_run: mrepo = None - #END init mrepo + # END init mrepo # ASSURE REPO IS PRESENT AND UPTODATE ##################################### @@ -352,19 +354,19 @@ class Submodule(util.IndexObject, Iterable, Traversable): op = FETCH if i == 0: op |= BEGIN - #END handle start + # END handle start progress.update(op, i, len_rmts, prefix + "Fetching remote %s of submodule %r" % (remote, self.name)) #=============================== if not dry_run: remote.fetch(progress=progress) - #END handle dry-run + # END handle dry-run #=============================== if i == len_rmts - 1: op |= END - #END handle end + # END handle end progress.update(op, i, len_rmts, prefix + "Done fetching remote of submodule %r" % self.name) - #END fetch new data + # END fetch new data except InvalidGitRepositoryError: if not init: return self @@ -383,10 +385,11 @@ class Submodule(util.IndexObject, Iterable, Traversable): # don't check it out at first - nonetheless it will create a local # branch according to the remote-HEAD if possible - progress.update(BEGIN | CLONE, 0, 1, prefix + "Cloning %s to %s in submodule %r" % (self.url, module_path, self.name)) + progress.update(BEGIN | CLONE, 0, 1, prefix + "Cloning %s to %s in submodule %r" % + (self.url, module_path, self.name)) if not dry_run: mrepo = git.Repo.clone_from(self.url, module_path, n=True) - #END handle dry-run + # END handle dry-run progress.update(END | CLONE, 0, 1, prefix + "Done cloning to %s" % module_path) if not dry_run: @@ -406,15 +409,15 @@ class Submodule(util.IndexObject, Iterable, Traversable): mrepo.head.ref.set_tracking_branch(remote_branch) except IndexError: print >> sys.stderr, "Warning: Failed to checkout tracking branch %s" % self.branch_path - #END handle tracking branch + # END handle tracking branch # NOTE: Have to write the repo config file as well, otherwise # the default implementation will be offended and not update the repository # Maybe this is a good way to assure it doesn't get into our way, but # we want to stay backwards compatible too ... . Its so redundant ! self.repo.config_writer().set_value(sm_section(self.name), 'url', self.url) - #END handle dry_run - #END handle initalization + # END handle dry_run + # END handle initalization # DETERMINE SHAS TO CHECKOUT ############################ @@ -423,7 +426,7 @@ class Submodule(util.IndexObject, Iterable, Traversable): if mrepo is not None: # mrepo is only set if we are not in dry-run mode or if the module existed is_detached = mrepo.head.is_detached - #END handle dry_run + # END handle dry_run if mrepo is not None and to_latest_revision: msg_base = "Cannot update to latest revision in repository at %r as " % mrepo.working_dir @@ -434,7 +437,8 @@ class Submodule(util.IndexObject, Iterable, Traversable): binsha = rcommit.binsha hexsha = rcommit.hexsha else: - print >> sys.stderr, "%s a tracking branch was not set for local branch '%s'" % (msg_base, mrepo.head.ref) + print >> sys.stderr, "%s a tracking branch was not set for local branch '%s'" % ( + msg_base, mrepo.head.ref) # END handle remote ref else: print >> sys.stderr, "%s there was no local tracking branch" % msg_base @@ -444,7 +448,8 @@ class Submodule(util.IndexObject, Iterable, Traversable): # update the working tree # handles dry_run if mrepo is not None and mrepo.head.commit.binsha != binsha: - progress.update(BEGIN | UPDWKTREE, 0, 1, prefix + "Updating working tree at %s for submodule %r to revision %s" % (self.path, self.name, hexsha)) + progress.update(BEGIN | UPDWKTREE, 0, 1, prefix + + "Updating working tree at %s for submodule %r to revision %s" % (self.path, self.name, hexsha)) if not dry_run: if is_detached: # NOTE: for now we force, the user is no supposed to change detached @@ -458,7 +463,7 @@ class Submodule(util.IndexObject, Iterable, Traversable): # branch - this should be prevented when setting the branch option mrepo.head.reset(hexsha, index=True, working_tree=True) # END handle checkout - #END handle dry_run + # END handle dry_run progress.update(END | UPDWKTREE, 0, 1, prefix + "Done updating working tree for submodule %r" % self.name) # END update to new commit only if needed @@ -470,7 +475,7 @@ class Submodule(util.IndexObject, Iterable, Traversable): for submodule in self.iter_items(self.module()): submodule.update(recursive, init, to_latest_revision, progress=progress, dry_run=dry_run) # END handle recursive update - #END handle dry run + # END handle dry run # END for each submodule return self @@ -498,7 +503,7 @@ class Submodule(util.IndexObject, Iterable, Traversable): """ if module + configuration < 1: raise ValueError("You must specify to move at least the module or the configuration of the submodule") - #END handle input + # END handle input module_path = to_native_path_linux(module_path) if module_path.endswith('/'): @@ -508,7 +513,7 @@ class Submodule(util.IndexObject, Iterable, Traversable): # VERIFY DESTINATION if module_path == self.path: return self - #END handle no change + # END handle no change dest_path = join_path_native(self.repo.working_tree_dir, module_path) if os.path.isfile(dest_path): @@ -520,25 +525,25 @@ class Submodule(util.IndexObject, Iterable, Traversable): # if the target item already exists, fail if configuration and tekey in index.entries: raise ValueError("Index entry for target path did alredy exist") - #END handle index key already there + # END handle index key already there # remove existing destination if module: if os.path.exists(dest_path): if len(os.listdir(dest_path)): raise ValueError("Destination module directory was not empty") - #END handle non-emptyness + # END handle non-emptyness if os.path.islink(dest_path): os.remove(dest_path) else: os.rmdir(dest_path) - #END handle link + # END handle link else: # recreate parent directories # NOTE: renames() does that now pass - #END handle existance + # END handle existance # END handle module # move the module into place if possible @@ -547,7 +552,7 @@ class Submodule(util.IndexObject, Iterable, Traversable): if module and os.path.exists(cur_path): os.renames(cur_path, dest_path) renamed_module = True - #END move physical module + # END move physical module # rename the index entry - have to manipulate the index directly as # git-mv cannot be used on submodules ... yeah @@ -561,7 +566,7 @@ class Submodule(util.IndexObject, Iterable, Traversable): index.entries[tekey] = nentry except KeyError: raise InvalidGitRepositoryError("Submodule's entry at %r did not exist" % (self.path)) - #END handle submodule doesn't exist + # END handle submodule doesn't exist # update configuration writer = self.config_writer(index=index) # auto-write @@ -574,7 +579,7 @@ class Submodule(util.IndexObject, Iterable, Traversable): os.renames(dest_path, cur_path) # END undo module renaming raise - #END handle undo rename + # END handle undo rename return self @@ -623,16 +628,17 @@ class Submodule(util.IndexObject, Iterable, Traversable): method = rmtree elif os.path.exists(mp): raise AssertionError("Cannot forcibly delete repository as it was neither a link, nor a directory") - #END handle brutal deletion + # END handle brutal deletion if not dry_run: assert method method(mp) - #END apply deletion method + # END apply deletion method else: # verify we may delete our module mod = self.module() if mod.is_dirty(untracked_files=True): - raise InvalidGitRepositoryError("Cannot delete module at %s with any modifications, unless force is specified" % mod.working_tree_dir) + raise InvalidGitRepositoryError( + "Cannot delete module at %s with any modifications, unless force is specified" % mod.working_tree_dir) # END check for dirt # figure out whether we have new commits compared to the remotes @@ -648,13 +654,14 @@ class Submodule(util.IndexObject, Iterable, Traversable): # END for each remote ref # not a single remote branch contained all our commits if num_branches_with_new_commits == len(rrefs): - raise InvalidGitRepositoryError("Cannot delete module at %s as there are new commits" % mod.working_tree_dir) + raise InvalidGitRepositoryError( + "Cannot delete module at %s as there are new commits" % mod.working_tree_dir) # END handle new commits # have to manually delete references as python's scoping is # not existing, they could keep handles open ( on windows this is a problem ) if len(rrefs): del(rref) - #END handle remotes + # END handle remotes del(rrefs) del(remote) # END for each remote @@ -683,7 +690,7 @@ class Submodule(util.IndexObject, Iterable, Traversable): del(index.entries[index.entry_key(self.path, 0)]) except KeyError: pass - #END delete entry + # END delete entry index.write() # now git config - need the config intact, otherwise we can't query @@ -796,7 +803,7 @@ class Submodule(util.IndexObject, Iterable, Traversable): if hasattr(self, attr): loc[attr] = getattr(self, attr) # END if we have the attribute cache - #END for each attr + # END for each attr self._clear_cache() try: @@ -907,7 +914,8 @@ class Submodule(util.IndexObject, Iterable, Traversable): entry = index.entries[index.entry_key(p, 0)] sm = Submodule(repo, entry.binsha, entry.mode, entry.path) except KeyError: - raise InvalidGitRepositoryError("Gitmodule path %r did not exist in revision of parent commit %s" % (p, parent_commit)) + raise InvalidGitRepositoryError( + "Gitmodule path %r did not exist in revision of parent commit %s" % (p, parent_commit)) # END handle keyerror # END handle critical error diff --git a/git/objects/submodule/root.py b/git/objects/submodule/root.py index 581c5a7c..f68f7567 100644 --- a/git/objects/submodule/root.py +++ b/git/objects/submodule/root.py @@ -1,7 +1,7 @@ from base import Submodule, UpdateProgress from util import ( - find_first_remote_branch - ) + find_first_remote_branch +) from git.exc import InvalidGitRepositoryError import git @@ -13,7 +13,8 @@ __all__ = ["RootModule", "RootUpdateProgress"] class RootUpdateProgress(UpdateProgress): """Utility class which adds more opcodes to the UpdateProgress""" - REMOVE, PATHCHANGE, BRANCHCHANGE, URLCHANGE = [1 << x for x in range(UpdateProgress._num_op_codes, UpdateProgress._num_op_codes + 4)] + REMOVE, PATHCHANGE, BRANCHCHANGE, URLCHANGE = [ + 1 << x for x in range(UpdateProgress._num_op_codes, UpdateProgress._num_op_codes + 4)] _num_op_codes = UpdateProgress._num_op_codes + 4 __slots__ = tuple() @@ -38,15 +39,15 @@ class RootModule(Submodule): def __init__(self, repo): # repo, binsha, mode=None, path=None, name = None, parent_commit=None, url=None, ref=None) super(RootModule, self).__init__( - repo, - binsha=self.NULL_BIN_SHA, - mode=self.k_default_mode, - path='', - name=self.k_root_name, - parent_commit=repo.head.commit, - url='', - branch_path=git.Head.to_full_path(self.k_head_default) - ) + repo, + binsha=self.NULL_BIN_SHA, + mode=self.k_default_mode, + path='', + name=self.k_root_name, + parent_commit=repo.head.commit, + url='', + branch_path=git.Head.to_full_path(self.k_head_default) + ) def _clear_cache(self): """May not do anything""" @@ -55,7 +56,7 @@ class RootModule(Submodule): #{ Interface def update(self, previous_commit=None, recursive=True, force_remove=False, init=True, - to_latest_revision=False, progress=None, dry_run=False): + to_latest_revision=False, progress=None, dry_run=False): """Update the submodules of this repository to the current HEAD commit. This method behaves smartly by determining changes of the path of a submodules repository, next to changes to the to-be-checked-out commit or the branch to be @@ -84,7 +85,7 @@ class RootModule(Submodule): if progress is None: progress = RootUpdateProgress() - #END assure progress is set + # END assure progress is set prefix = '' if dry_run: @@ -100,11 +101,11 @@ class RootModule(Submodule): previous_commit = repo.commit(repo.head.log_entry(-1).oldhexsha) if previous_commit.binsha == previous_commit.NULL_BIN_SHA: raise IndexError - #END handle initial commit + # END handle initial commit except IndexError: # in new repositories, there is no previous commit previous_commit = cur_commit - #END exception handling + # END exception handling else: previous_commit = repo.commit(previous_commit) # obtain commit object # END handle previous commit @@ -122,7 +123,7 @@ class RootModule(Submodule): op = REMOVE if i == 0: op |= BEGIN - #END handle begin + # END handle begin # fake it into thinking its at the current commit to allow deletion # of previous module. Trigger the cache to be updated before that @@ -130,11 +131,11 @@ class RootModule(Submodule): rsm._parent_commit = repo.head.commit if not dry_run: rsm.remove(configuration=False, module=True, force=force_remove) - #END handle dry-run + # END handle dry-run if i == len_rrsm - 1: op |= END - #END handle end + # END handle end progress.update(op, i, len_rrsm, prefix + "Done removing submodule %r" % rsm.name) # END for each removed submodule @@ -147,15 +148,17 @@ class RootModule(Submodule): psm = psms[csm.name] sm = sms[csm.name] - #PATH CHANGES + # PATH CHANGES ############## if sm.path != psm.path and psm.module_exists(): - progress.update(BEGIN | PATHCHANGE, i, len_csms, prefix + "Moving repository of submodule %r from %s to %s" % (sm.name, psm.abspath, sm.abspath)) + progress.update(BEGIN | PATHCHANGE, i, len_csms, prefix + + "Moving repository of submodule %r from %s to %s" % (sm.name, psm.abspath, sm.abspath)) # move the module to the new path if not dry_run: psm.move(sm.path, module=True, configuration=False) - #END handle dry_run - progress.update(END | PATHCHANGE, i, len_csms, prefix + "Done moving repository of submodule %r" % sm.name) + # END handle dry_run + progress.update( + END | PATHCHANGE, i, len_csms, prefix + "Done moving repository of submodule %r" % sm.name) # END handle path changes if sm.module_exists(): @@ -171,7 +174,8 @@ class RootModule(Submodule): # don't do anything if we already have the url we search in place if len([r for r in rmts if r.url == sm.url]) == 0: - progress.update(BEGIN | URLCHANGE, i, len_csms, prefix + "Changing url of submodule %r from %s to %s" % (sm.name, psm.url, sm.url)) + progress.update(BEGIN | URLCHANGE, i, len_csms, prefix + + "Changing url of submodule %r from %s to %s" % (sm.name, psm.url, sm.url)) if not dry_run: assert nn not in [r.name for r in rmts] @@ -181,7 +185,8 @@ class RootModule(Submodule): # If we have a tracking branch, it should be available # in the new remote as well. if len([r for r in smr.refs if r.remote_head == sm.branch_name]) == 0: - raise ValueError("Submodule branch named %r was not available in new submodule remote at %r" % (sm.branch_name, sm.url)) + raise ValueError( + "Submodule branch named %r was not available in new submodule remote at %r" % (sm.branch_name, sm.url)) # END head is not detached # now delete the changed one @@ -204,8 +209,9 @@ class RootModule(Submodule): # and its okay to fail here # Alternatively we could just generate a unique name and leave all # existing ones in place - raise InvalidGitRepositoryError("Couldn't find original remote-repo at url %r" % psm.url) - #END handle one single remote + raise InvalidGitRepositoryError( + "Couldn't find original remote-repo at url %r" % psm.url) + # END handle one single remote # END handle check we found a remote orig_name = rmt_for_deletion.name @@ -241,11 +247,12 @@ class RootModule(Submodule): # the user will be able to commit the change easily print >> sys.stderr, "WARNING: Current sha %s was not contained in the tracking branch at the new remote, setting it the the remote's tracking branch" % sm.hexsha sm.binsha = rref.commit.binsha - #END reset binsha + # END reset binsha - #NOTE: All checkout is performed by the base implementation of update - #END handle dry_run - progress.update(END | URLCHANGE, i, len_csms, prefix + "Done adjusting url of submodule %r" % (sm.name)) + # NOTE: All checkout is performed by the base implementation of update + # END handle dry_run + progress.update( + END | URLCHANGE, i, len_csms, prefix + "Done adjusting url of submodule %r" % (sm.name)) # END skip remote handling if new url already exists in module # END handle url @@ -254,7 +261,8 @@ class RootModule(Submodule): if sm.branch_path != psm.branch_path: # finally, create a new tracking branch which tracks the # new remote branch - progress.update(BEGIN | BRANCHCHANGE, i, len_csms, prefix + "Changing branch of submodule %r from %s to %s" % (sm.name, psm.branch_path, sm.branch_path)) + progress.update(BEGIN | BRANCHCHANGE, i, len_csms, prefix + + "Changing branch of submodule %r from %s to %s" % (sm.name, psm.branch_path, sm.branch_path)) if not dry_run: smm = sm.module() smmr = smm.remotes @@ -263,7 +271,7 @@ class RootModule(Submodule): except OSError: # ... or reuse the existing one tbr = git.Head(smm, sm.branch_path) - #END assure tracking branch exists + # END assure tracking branch exists tbr.set_tracking_branch(find_first_remote_branch(smmr, sm.branch_name)) # figure out whether the previous tracking branch contains @@ -273,19 +281,20 @@ class RootModule(Submodule): tbr = find_first_remote_branch(smmr, psm.branch_name) if len(smm.git.cherry(tbr, psm.branch)) == 0: psm.branch.delete(smm, psm.branch) - #END delete original tracking branch if there are no changes + # END delete original tracking branch if there are no changes except InvalidGitRepositoryError: # ignore it if the previous branch couldn't be found in the # current remotes, this just means we can't handle it pass # END exception handling - #NOTE: All checkout is done in the base implementation of update - #END handle dry_run + # NOTE: All checkout is done in the base implementation of update + # END handle dry_run - progress.update(END | BRANCHCHANGE, i, len_csms, prefix + "Done changing branch of submodule %r" % sm.name) - #END handle branch - #END handle + progress.update( + END | BRANCHCHANGE, i, len_csms, prefix + "Done changing branch of submodule %r" % sm.name) + # END handle branch + # END handle # END for each common submodule # FINALLY UPDATE ALL ACTUAL SUBMODULES @@ -293,7 +302,7 @@ class RootModule(Submodule): for sm in sms: # update the submodule using the default method sm.update(recursive=False, init=init, to_latest_revision=to_latest_revision, - progress=progress, dry_run=dry_run) + progress=progress, dry_run=dry_run) # update recursively depth first - question is which inconsitent # state will be better in case it fails somewhere. Defective branch @@ -303,10 +312,10 @@ class RootModule(Submodule): # the module would exist by now if we are not in dry_run mode if sm.module_exists(): type(self)(sm.module()).update(recursive=True, force_remove=force_remove, - init=init, to_latest_revision=to_latest_revision, - progress=progress, dry_run=dry_run) - #END handle dry_run - #END handle recursive + init=init, to_latest_revision=to_latest_revision, + progress=progress, dry_run=dry_run) + # END handle dry_run + # END handle recursive # END for each submodule to update def module(self): diff --git a/git/objects/submodule/util.py b/git/objects/submodule/util.py index bbdf5e1e..01bd03b3 100644 --- a/git/objects/submodule/util.py +++ b/git/objects/submodule/util.py @@ -5,7 +5,7 @@ from StringIO import StringIO import weakref __all__ = ('sm_section', 'sm_name', 'mkhead', 'unbare_repo', 'find_first_remote_branch', - 'SubmoduleConfigParser') + 'SubmoduleConfigParser') #{ Utilities @@ -33,7 +33,7 @@ def unbare_repo(func): def wrapper(self, *args, **kwargs): if self.repo.bare: raise InvalidGitRepositoryError("Method '%s' cannot operate on bare repositories" % func.__name__) - #END bare method + # END bare method return func(self, *args, **kwargs) # END wrapper wrapper.__name__ = func.__name__ @@ -48,7 +48,7 @@ def find_first_remote_branch(remotes, branch_name): except IndexError: continue # END exception handling - #END for remote + # END for remote raise InvalidGitRepositoryError("Didn't find remote branch %r in any of the given remotes", branch_name) #} END utilities |