Exemple #1
0
	def set_parent_commit(self, commit, check=True):
		"""Set this instance to use the given commit whose tree is supposed to 
		contain the .gitmodules blob.
		
		:param commit: Commit'ish reference pointing at the root_tree
		:param check: if True, relatively expensive checks will be performed to verify
			validity of the submodule.
		:raise ValueError: if the commit's tree didn't contain the .gitmodules blob.
		:raise ValueError: if the parent commit didn't store this submodule under the
			current path
		:return: self"""
		pcommit = self.repo.commit(commit)
		pctree = pcommit.tree
		if self.k_modules_file not in pctree:
			raise ValueError("Tree of commit %s did not contain the %s file" % (commit, self.k_modules_file))
		# END handle exceptions
		
		prev_pc = self._parent_commit
		self._parent_commit = pcommit
		
		if check:
			parser = self._config_parser(self.repo, self._parent_commit, read_only=True)
			if not parser.has_section(sm_section(self.name)):
				self._parent_commit = prev_pc
				raise ValueError("Submodule at path %r did not exist in parent commit %s" % (self.path, commit)) 
			# END handle submodule did not exist
		# END handle checking mode
		
		# update our sha, it could have changed
		self.binsha = pctree[self.path].binsha
		
		self._clear_cache()
		
		return self
Exemple #2
0
    def remove(self,
               module=True,
               force=False,
               configuration=True,
               dry_run=False):
        """Remove this submodule from the repository. This will remove our entry
        from the .gitmodules file and the entry in the .git/config file.

        :param module: If True, the module we point to will be deleted
            as well. If the module is currently on a commit which is not part
            of any branch in the remote, if the currently checked out branch
            working tree, or untracked files,
            is ahead of its tracking branch,  if you have modifications in the
            In case the removal of the repository fails for these reasons, the
            submodule status will not have been altered.
            If this submodule has child-modules on its own, these will be deleted
            prior to touching the own module.
        :param force: Enforces the deletion of the module even though it contains
            modifications. This basically enforces a brute-force file system based
            deletion.
        :param configuration: if True, the submodule is deleted from the configuration,
            otherwise it isn't. Although this should be enabled most of the times,
            this flag enables you to safely delete the repository of your submodule.
        :param dry_run: if True, we will not actually do anything, but throw the errors
            we would usually throw
        :return: self
        :note: doesn't work in bare repositories
        :raise InvalidGitRepositoryError: thrown if the repository cannot be deleted
        :raise OSError: if directories or files could not be removed"""
        if not (module + configuration):
            raise ValueError(
                "Need to specify to delete at least the module, or the configuration"
            )
        # END handle params

        # DELETE MODULE REPOSITORY
        ##########################
        if module and self.module_exists():
            if force:
                # take the fast lane and just delete everything in our module path
                # TODO: If we run into permission problems, we have a highly inconsistent
                # state. Delete the .git folders last, start with the submodules first
                mp = self.abspath
                method = None
                if os.path.islink(mp):
                    method = os.remove
                elif os.path.isdir(mp):
                    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
                if not dry_run:
                    assert method
                    method(mp)
                #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)
                # END check for dirt

                # figure out whether we have new commits compared to the remotes
                # NOTE: If the user pulled all the time, the remote heads might
                # not have been updated, so commits coming from the remote look
                # as if they come from us. But we stay strictly read-only and
                # don't fetch beforhand.
                for remote in mod.remotes:
                    num_branches_with_new_commits = 0
                    rrefs = remote.refs
                    for rref in rrefs:
                        num_branches_with_new_commits = len(
                            mod.git.cherry(rref)) != 0
                    # 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)
                    # 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
                    del (rrefs)
                    del (remote)
                # END for each remote

                # gently remove all submodule repositories
                for sm in self.children():
                    sm.remove(module=True,
                              force=False,
                              configuration=False,
                              dry_run=dry_run)
                    del (sm)
                # END for each child-submodule

                # finally delete our own submodule
                if not dry_run:
                    wtd = mod.working_tree_dir
                    del (mod)  # release file-handles (windows)
                    rmtree(wtd)
                # END delete tree if possible
            # END handle force
        # END handle module deletion

        # DELETE CONFIGURATION
        ######################
        if configuration and not dry_run:
            # first the index-entry
            index = self.repo.index
            try:
                del (index.entries[index.entry_key(self.path, 0)])
            except KeyError:
                pass
            #END delete entry
            index.write()

            # now git config - need the config intact, otherwise we can't query
            # inforamtion anymore
            self.repo.config_writer().remove_section(sm_section(self.name))
            self.config_writer().remove_section()
        # END delete configuration

        # void our data not to delay invalid access
        self._clear_cache()

        return self
Exemple #3
0
    def update(self,
               recursive=False,
               init=True,
               to_latest_revision=False,
               progress=None,
               dry_run=False):
        """Update the repository of this submodule to point to the checkout
        we point at with the binsha of this instance.

        :param recursive: if True, we will operate recursively and update child-
            modules as well.
        :param init: if True, the module repository will be cloned into place if necessary
        :param to_latest_revision: if True, the submodule's sha will be ignored during checkout.
            Instead, the remote will be fetched, and the local tracking branch updated.
            This only works if we have a local tracking branch, which is the case
            if the remote repository had a master branch, or of the 'branch' option
            was specified for this submodule and the branch existed remotely
        :param progress: UpdateProgress instance or None of no progress should be shown
        :param dry_run: if True, the operation will only be simulated, but not performed.
            All performed operations are read-only
        :note: does nothing in bare repositories
        :note: method is definitely not atomic if recurisve is True
        :return: self"""
        if self.repo.bare:
            return self
        #END pass in bare mode

        if progress is None:
            progress = UpdateProgress()
        #END handle progress
        prefix = ''
        if dry_run:
            prefix = "DRY-RUN: "
        #END handle prefix

        # to keep things plausible in dry-run mode
        if dry_run:
            mrepo = None
        #END init mrepo

        # ASSURE REPO IS PRESENT AND UPTODATE
        #####################################
        try:
            mrepo = self.module()
            rmts = mrepo.remotes
            len_rmts = len(rmts)
            for i, remote in enumerate(rmts):
                op = FETCH
                if i == 0:
                    op |= BEGIN
                #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
                #===============================
                if i == len_rmts - 1:
                    op |= END
                #END handle end
                progress.update(
                    op, i, len_rmts, prefix +
                    "Done fetching remote of submodule %r" % self.name)
            #END fetch new data
        except InvalidGitRepositoryError:
            if not init:
                return self
            # END early abort if init is not allowed
            import git

            # there is no git-repository yet - but delete empty paths
            module_path = join_path_native(self.repo.working_tree_dir,
                                           self.path)
            if not dry_run and os.path.isdir(module_path):
                try:
                    os.rmdir(module_path)
                except OSError:
                    raise OSError(
                        "Module directory at %r does already exist and is non-empty"
                        % module_path)
                # END handle OSError
            # END handle directory removal

            # 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))
            if not dry_run:
                mrepo = git.Repo.clone_from(self.url, module_path, n=True)
            #END handle dry-run
            progress.update(END | CLONE, 0, 1,
                            prefix + "Done cloning to %s" % module_path)

            if not dry_run:
                # see whether we have a valid branch to checkout
                try:
                    # find  a remote which has our branch - we try to be flexible
                    remote_branch = find_first_remote_branch(
                        mrepo.remotes, self.branch_name)
                    local_branch = mkhead(mrepo, self.branch_path)

                    # have a valid branch, but no checkout - make sure we can figure
                    # that out by marking the commit with a null_sha
                    local_branch.set_object(
                        util.Object(mrepo, self.NULL_BIN_SHA))
                    # END initial checkout + branch creation

                    # make sure HEAD is not detached
                    mrepo.head.set_reference(
                        local_branch,
                        logmsg="submodule: attaching head to %s" %
                        local_branch)
                    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

                # 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

        # DETERMINE SHAS TO CHECKOUT
        ############################
        binsha = self.binsha
        hexsha = self.hexsha
        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

        if mrepo is not None and to_latest_revision:
            msg_base = "Cannot update to latest revision in repository at %r as " % mrepo.working_dir
            if not is_detached:
                rref = mrepo.head.ref.tracking_branch()
                if rref is not None:
                    rcommit = rref.commit
                    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)
                # END handle remote ref
            else:
                print >> sys.stderr, "%s there was no local tracking branch" % msg_base
            # END handle detached head
        # END handle to_latest_revision option

        # 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))
            if not dry_run:
                if is_detached:
                    # NOTE: for now we force, the user is no supposed to change detached
                    # submodules anyway. Maybe at some point this becomes an option, to
                    # properly handle user modifications - see below for future options
                    # regarding rebase and merge.
                    mrepo.git.checkout(hexsha, force=True)
                else:
                    # TODO: allow to specify a rebase, merge, or reset
                    # TODO: Warn if the hexsha forces the tracking branch off the remote
                    # 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
            progress.update(
                END | UPDWKTREE, 0, 1, prefix +
                "Done updating working tree for submodule %r" % self.name)
        # END update to new commit only if needed

        # HANDLE RECURSION
        ##################
        if recursive:
            # in dry_run mode, the module might not exist
            if mrepo is not None:
                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 for each submodule

        return self
Exemple #4
0
 def _config_parser_constrained(self, read_only):
     """:return: Config Parser constrained to our submodule in read or write mode"""
     parser = self._config_parser(self.repo, self._parent_commit, read_only)
     parser.set_submodule(self)
     return SectionConstraint(parser, sm_section(self.name))
Exemple #5
0
	def remove(self, module=True, force=False, configuration=True, dry_run=False):
		"""Remove this submodule from the repository. This will remove our entry
		from the .gitmodules file and the entry in the .git/config file.
		
		:param module: If True, the module we point to will be deleted 
			as well. If the module is currently on a commit which is not part 
			of any branch in the remote, if the currently checked out branch 
			working tree, or untracked files,
			is ahead of its tracking branch,  if you have modifications in the
			In case the removal of the repository fails for these reasons, the 
			submodule status will not have been altered.
			If this submodule has child-modules on its own, these will be deleted
			prior to touching the own module.
		:param force: Enforces the deletion of the module even though it contains 
			modifications. This basically enforces a brute-force file system based
			deletion.
		:param configuration: if True, the submodule is deleted from the configuration, 
			otherwise it isn't. Although this should be enabled most of the times, 
			this flag enables you to safely delete the repository of your submodule.
		:param dry_run: if True, we will not actually do anything, but throw the errors
			we would usually throw
		:return: self
		:note: doesn't work in bare repositories
		:raise InvalidGitRepositoryError: thrown if the repository cannot be deleted
		:raise OSError: if directories or files could not be removed"""
		if not (module + configuration):
			raise ValueError("Need to specify to delete at least the module, or the configuration")
		# END handle params
		
		# DELETE MODULE REPOSITORY
		##########################
		if module and self.module_exists():
			if force:
				# take the fast lane and just delete everything in our module path
				# TODO: If we run into permission problems, we have a highly inconsistent
				# state. Delete the .git folders last, start with the submodules first
				mp = self.abspath
				method = None
				if os.path.islink(mp):
					method = os.remove
				elif os.path.isdir(mp):
					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
				if not dry_run:
					assert method
					method(mp)
				#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)
				# END check for dirt
				
				# figure out whether we have new commits compared to the remotes
				# NOTE: If the user pulled all the time, the remote heads might 
				# not have been updated, so commits coming from the remote look
				# as if they come from us. But we stay strictly read-only and
				# don't fetch beforhand.
				for remote in mod.remotes:
					num_branches_with_new_commits = 0
					rrefs = remote.refs
					for rref in rrefs:
						num_branches_with_new_commits = len(mod.git.cherry(rref)) != 0
					# 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)
					# 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
					del(rrefs)
					del(remote)
				# END for each remote
				
				# gently remove all submodule repositories
				for sm in self.children():
					sm.remove(module=True, force=False, configuration=False, dry_run=dry_run)
					del(sm)
				# END for each child-submodule
				
				# finally delete our own submodule
				if not dry_run:
					wtd = mod.working_tree_dir
					del(mod)		# release file-handles (windows)
					rmtree(wtd)
				# END delete tree if possible
			# END handle force
		# END handle module deletion
			
		# DELETE CONFIGURATION
		######################
		if configuration and not dry_run:
			# first the index-entry
			index = self.repo.index
			try:
				del(index.entries[index.entry_key(self.path, 0)])
			except KeyError:
				pass
			#END delete entry
			index.write()
			
			# now git config - need the config intact, otherwise we can't query 
			# inforamtion anymore
			self.repo.config_writer().remove_section(sm_section(self.name))
			self.config_writer().remove_section()
		# END delete configuration

		# void our data not to delay invalid access
		self._clear_cache()
		
		return self
Exemple #6
0
	def update(self, recursive=False, init=True, to_latest_revision=False, progress=None, 
				dry_run=False, ):
		"""Update the repository of this submodule to point to the checkout
		we point at with the binsha of this instance.
		
		:param recursive: if True, we will operate recursively and update child-
			modules as well.
		:param init: if True, the module repository will be cloned into place if necessary
		:param to_latest_revision: if True, the submodule's sha will be ignored during checkout.
			Instead, the remote will be fetched, and the local tracking branch updated.
			This only works if we have a local tracking branch, which is the case
			if the remote repository had a master branch, or of the 'branch' option 
			was specified for this submodule and the branch existed remotely
		:param progress: UpdateProgress instance or None of no progress should be shown
		:param dry_run: if True, the operation will only be simulated, but not performed.
			All performed operations are read-only
		:note: does nothing in bare repositories
		:note: method is definitely not atomic if recurisve is True
		:return: self"""
		if self.repo.bare:
			return self
		#END pass in bare mode
		
		if progress is None:
			progress = UpdateProgress()
		#END handle progress
		prefix = ''
		if dry_run:
			prefix = "DRY-RUN: "
		#END handle prefix
		
		# to keep things plausible in dry-run mode
		if dry_run:
			mrepo = None
		#END init mrepo
		
		# ASSURE REPO IS PRESENT AND UPTODATE
		#####################################
		try:
			mrepo = self.module()
			rmts = mrepo.remotes
			len_rmts = len(rmts)
			for i, remote in enumerate(rmts):
				op = FETCH
				if i == 0:
					op |= BEGIN
				#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
				#===============================
				if i == len_rmts-1:
					op |= END
				#END handle end
				progress.update(op, i, len_rmts, prefix+"Done fetching remote of submodule %r" % self.name)
			#END fetch new data
		except InvalidGitRepositoryError:
			if not init:
				return self
			# END early abort if init is not allowed
			
			# there is no git-repository yet - but delete empty paths
			module_path = join_path_native(self.repo.working_tree_dir, self.path)
			if not dry_run and os.path.isdir(module_path):
				try:
					os.rmdir(module_path)
				except OSError:
					raise OSError("Module directory at %r does already exist and is non-empty" % module_path)
				# END handle OSError
			# END handle directory removal
			
			# 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))
			if not dry_run:
				mrepo = type(self.repo).clone_from(self.url, module_path, n=True)
			#END handle dry-run
			progress.update(END|CLONE, 0, 1, prefix+"Done cloning to %s" % module_path)
			
			
			if not dry_run:
				# see whether we have a valid branch to checkout
				try:
					# find  a remote which has our branch - we try to be flexible
					remote_branch = find_first_remote_branch(mrepo.remotes, self.branch_name)
					local_branch = mkhead(mrepo, self.branch_path)
					
					# have a valid branch, but no checkout - make sure we can figure
					# that out by marking the commit with a null_sha
					local_branch.set_object(util.Object(mrepo, self.NULL_BIN_SHA))
					# END initial checkout + branch creation
					
					# make sure HEAD is not detached
					mrepo.head.set_reference(local_branch, logmsg="submodule: attaching head to %s" % local_branch)
					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
				
				# 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
		
		
		# DETERMINE SHAS TO CHECKOUT
		############################
		binsha = self.binsha
		hexsha = self.hexsha
		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
		
		if mrepo is not None and to_latest_revision:
			msg_base = "Cannot update to latest revision in repository at %r as " % mrepo.working_dir
			if not is_detached:
				rref = mrepo.head.ref.tracking_branch()
				if rref is not None:
					rcommit = rref.commit
					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) 
				# END handle remote ref
			else:
				print >> sys.stderr, "%s there was no local tracking branch" % msg_base
			# END handle detached head
		# END handle to_latest_revision option
		
		# 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))
			if not dry_run:
				if is_detached:
					# NOTE: for now we force, the user is no supposed to change detached
					# submodules anyway. Maybe at some point this becomes an option, to 
					# properly handle user modifications - see below for future options
					# regarding rebase and merge.
					mrepo.git.checkout(hexsha, force=True)
				else:
					# TODO: allow to specify a rebase, merge, or reset
					# TODO: Warn if the hexsha forces the tracking branch off the remote
					# 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
			progress.update(END|UPDWKTREE, 0, 1, prefix+"Done updating working tree for submodule %r" % self.name)
		# END update to new commit only if needed
		
		# HANDLE RECURSION
		##################
		if recursive:
			# in dry_run mode, the module might not exist
			if mrepo is not None:
				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 for each submodule
			
		return self
Exemple #7
0
	def _config_parser_constrained(self, read_only):
		""":return: Config Parser constrained to our submodule in read or write mode"""
		parser = self._config_parser(self.repo, self._parent_commit, read_only)
		parser.set_submodule(self)
		return SectionConstraint(parser, sm_section(self.name))