diff options
author | Sebastian Thiel <byronimo@gmail.com> | 2014-11-17 10:14:43 +0100 |
---|---|---|
committer | Sebastian Thiel <byronimo@gmail.com> | 2014-11-17 10:14:43 +0100 |
commit | e4d8fb73daa82420bdc69c37f0d58f7cb4cd505a (patch) | |
tree | 38e1241fd6d756f783b6b56dc6628ac3ca41ed4f /git/index/base.py | |
parent | 7aba59a2609ec768d5d495dafd23a4bce8179741 (diff) | |
parent | c8e70749887370a99adeda972cc3503397b5f9a7 (diff) | |
download | gitpython-e4d8fb73daa82420bdc69c37f0d58f7cb4cd505a.tar.gz |
Merge pull request #204 from hashar/pep8-linting
Pep8 linting
Diffstat (limited to 'git/index/base.py')
-rw-r--r-- | git/index/base.py | 180 |
1 files changed, 86 insertions, 94 deletions
diff --git a/git/index/base.py b/git/index/base.py index 3bd8634c..f11f4492 100644 --- a/git/index/base.py +++ b/git/index/base.py @@ -13,13 +13,13 @@ from cStringIO import StringIO from stat import S_ISLNK from typ import ( - BaseIndexEntry, - IndexEntry, + BaseIndexEntry, + IndexEntry, ) from util import ( TemporaryFileSwap, - post_clear_cache, + post_clear_cache, default_index, git_working_dir ) @@ -35,18 +35,18 @@ from git.exc import ( from git.objects import ( Blob, Submodule, - Tree, - Object, + Tree, + Object, Commit, ) from git.objects.util import Serializable from git.util import ( - IndexFileSHA1Writer, - LazyMixin, - LockedFD, - join_path_native, + IndexFileSHA1Writer, + LazyMixin, + LockedFD, + join_path_native, file_contents_ro, to_native_path_linux, to_native_path @@ -58,7 +58,7 @@ from fun import ( read_cache, aggressive_tree_merge, write_tree_from_cache, - stat_mode_to_index_mode, + stat_mode_to_index_mode, S_IFGITLINK ) @@ -67,33 +67,34 @@ from gitdb.db import MemoryDB from gitdb.util import to_bin_sha from itertools import izip -__all__ = ( 'IndexFile', 'CheckoutError' ) +__all__ = ('IndexFile', 'CheckoutError') class IndexFile(LazyMixin, diff.Diffable, Serializable): + """ Implements an Index that can be manipulated using a native implementation in order to save git command function calls wherever possible. - + It provides custom merging facilities allowing to merge without actually changing your index or your working tree. This way you can perform own test-merges based on the index only without having to deal with the working copy. This is useful in case of partial working trees. ``Entries`` - + The index contains an entries dict whose keys are tuples of type IndexEntry to facilitate access. You may read the entries dict or manipulate it using IndexEntry instance, i.e.:: - + index.entries[index.entry_key(index_entry_instance)] = index_entry_instance - + Make sure you use index.write() once you are done manipulating the index directly before operating on it using the git command""" __slots__ = ("repo", "version", "entries", "_extension_data", "_file_path") _VERSION = 2 # latest version we support - S_IFGITLINK = S_IFGITLINK # a submodule + S_IFGITLINK = S_IFGITLINK # a submodule def __init__(self, repo, file_path=None): """Initialize this Index instance, optionally from the given ``file_path``. @@ -120,14 +121,14 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): return # END exception handling - # Here it comes: on windows in python 2.5, memory maps aren't closed properly - # Hence we are in trouble if we try to delete a file that is memory mapped, + # Here it comes: on windows in python 2.5, memory maps aren't closed properly + # Hence we are in trouble if we try to delete a file that is memory mapped, # which happens during read-tree. # In this case, we will just read the memory in directly. # Its insanely bad ... I am disappointed ! - allow_mmap = (os.name != 'nt' or sys.version_info[1] > 5) + allow_mmap = (os.name != 'nt' or sys.version_info[1] > 5) stream = file_contents_ro(fd, stream=True, allow_mmap=allow_mmap) - + try: self._deserialize(stream) finally: @@ -154,30 +155,29 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): pass # END exception handling - #{ Serializable Interface + #{ Serializable Interface def _deserialize(self, stream): """Initialize this instance with index values read from the given stream""" self.version, self.entries, self._extension_data, conten_sha = read_cache(stream) return self - + def _entries_sorted(self): """:return: list of entries, in a sorted fashion, first by path, then by stage""" entries_sorted = self.entries.values() entries_sorted.sort(key=lambda e: (e.path, e.stage)) # use path/stage as sort key return entries_sorted - + def _serialize(self, stream, ignore_tree_extension_data=False): entries = self._entries_sorted() write_cache(entries, stream, - (ignore_tree_extension_data and None) or self._extension_data) + (ignore_tree_extension_data and None) or self._extension_data) return self - - + #} END serializable interface - def write(self, file_path = None, ignore_tree_extension_data=False): + def write(self, file_path=None, ignore_tree_extension_data=False): """Write the current state to our file path or to the given one :param file_path: @@ -198,14 +198,14 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): :return: self""" # make sure we have our entries read before getting a write lock - # else it would be done when streaming. This can happen + # else it would be done when streaming. This can happen # if one doesn't change the index, but writes it right away self.entries lfd = LockedFD(file_path or self._file_path) stream = lfd.open(write=True, stream=True) - + self._serialize(stream, ignore_tree_extension_data) - + lfd.commit() # make sure we represent what we have written @@ -257,23 +257,22 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): :param repo: The repository treeish are located in. :param tree_sha: - 20 byte or 40 byte tree sha or tree objects + 20 byte or 40 byte tree sha or tree objects :return: - New IndexFile instance. Its path will be undefined. - If you intend to write such a merged Index, supply an alternate file_path + New IndexFile instance. Its path will be undefined. + If you intend to write such a merged Index, supply an alternate file_path to its 'write' method.""" base_entries = aggressive_tree_merge(repo.odb, [to_bin_sha(str(t)) for t in tree_sha]) - + inst = cls(repo) # convert to entries dict - entries = dict(izip(((e.path, e.stage) for e in base_entries), + entries = dict(izip(((e.path, e.stage) for e in base_entries), (IndexEntry.from_base(e) for e in base_entries))) - + inst.entries = entries return inst - @classmethod def from_tree(cls, repo, *treeish, **kwargs): """Merge the given treeish revisions into a new index which is returned. @@ -314,7 +313,7 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): arg_list = list() # ignore that working tree and index possibly are out of date - if len(treeish)>1: + if len(treeish) > 1: # drop unmerged entries when reading our index and merging arg_list.append("--reset") # handle non-trivial cases the way a real merge does @@ -323,7 +322,7 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): # tmp file created in git home directory to be sure renaming # works - /tmp/ dirs could be on another device - tmp_index = tempfile.mktemp('','',repo.git_dir) + tmp_index = tempfile.mktemp('', '', repo.git_dir) arg_list.append("--index-output=%s" % tmp_index) arg_list.extend(treeish) @@ -380,7 +379,7 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): # END path exception handling # END for each path - def _write_path_to_stdin(self, proc, filepath, item, fmakeexc, fprogress, + def _write_path_to_stdin(self, proc, filepath, item, fmakeexc, fprogress, read_from_stdout=True): """Write path to proc.stdin and make sure it processes the item, including progress. @@ -410,7 +409,7 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): fprogress(filepath, True, item) return rval - def iter_blobs(self, predicate = lambda t: True): + def iter_blobs(self, predicate=lambda t: True): """ :return: Iterator yielding tuples of Blob objects and stages, tuple(stage, Blob) @@ -419,7 +418,7 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): iterator. A default filter, the BlobFilter, allows you to yield blobs only if they match a given list of paths. """ for entry in self.entries.itervalues(): - # TODO: is it necessary to convert the mode ? We did that when adding + # TODO: is it necessary to convert the mode ? We did that when adding # it to the index, right ? mode = stat_mode_to_index_mode(entry.mode) blob = entry.to_blob(self.repo) @@ -472,13 +471,13 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): for blob in iter_blobs: stage_null_key = (blob.path, 0) if stage_null_key in self.entries: - raise ValueError( "Path %r already exists at stage 0" % blob.path ) + raise ValueError("Path %r already exists at stage 0" % blob.path) # END assert blob is not stage 0 already # delete all possible stages for stage in (1, 2, 3): try: - del( self.entries[(blob.path, stage)]) + del(self.entries[(blob.path, stage)]) except KeyError: pass # END ignore key errors @@ -503,9 +502,9 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): def write_tree(self): """Writes this index to a corresponding Tree object into the repository's object database and return it. - + :return: Tree object representing this index - :note: The tree will be written even if one or more objects the tree refers to + :note: The tree will be written even if one or more objects the tree refers to does not yet exist in the object database. This could happen if you added Entries to the index directly. :raise ValueError: if there are no entries in the cache @@ -515,17 +514,16 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): mdb = MemoryDB() entries = self._entries_sorted() binsha, tree_items = write_tree_from_cache(entries, mdb, slice(0, len(entries))) - + # copy changed trees only mdb.stream_copy(mdb.sha_iter(), self.repo.odb) - - + # note: additional deserialization could be saved if write_tree_from_cache # would return sorted tree entries root_tree = Tree(self.repo, binsha, path='') root_tree._cache = tree_items return root_tree - + def _process_diff_args(self, args): try: args.pop(args.index(self)) @@ -539,9 +537,9 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): if it is not within our git direcotory""" if not os.path.isabs(path): return path - relative_path = path.replace(self.repo.working_tree_dir+os.sep, "") + relative_path = path.replace(self.repo.working_tree_dir + os.sep, "") if relative_path == path: - raise ValueError("Absolute path %r is not in git repository at %r" % (path,self.repo.working_tree_dir)) + raise ValueError("Absolute path %r is not in git repository at %r" % (path, self.repo.working_tree_dir)) return relative_path def _preprocess_add_items(self, items): @@ -562,10 +560,10 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): return (paths, entries) @git_working_dir - def add(self, items, force=True, fprogress=lambda *args: None, path_rewriter=None, + def add(self, items, force=True, fprogress=lambda *args: None, path_rewriter=None, write=True): """Add files from the working tree, specific blobs or BaseIndexEntries - to the index. + to the index. :param items: Multiple types of items are supported, types can be mixed within one call. @@ -593,7 +591,7 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): must be a path relative to our repository. If their sha is null ( 40*0 ), their path must exist in the file system - relative to the git repository as an object will be created from + relative to the git repository as an object will be created from the data at the path. The handling now very much equals the way string paths are processed, except that the mode you have set will be kept. This allows you to create symlinks @@ -638,7 +636,7 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): :param write: If True, the index will be written once it was altered. Otherwise the changes only exist in memory and are not available to git commands. - + :return: List(BaseIndexEntries) representing the entries just actually added. @@ -655,16 +653,15 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): if paths and path_rewriter: for path in paths: abspath = os.path.abspath(path) - gitrelative_path = abspath[len(self.repo.working_tree_dir)+1:] - blob = Blob(self.repo, Blob.NULL_BIN_SHA, - stat_mode_to_index_mode(os.stat(abspath).st_mode), + gitrelative_path = abspath[len(self.repo.working_tree_dir) + 1:] + blob = Blob(self.repo, Blob.NULL_BIN_SHA, + stat_mode_to_index_mode(os.stat(abspath).st_mode), to_native_path_linux(gitrelative_path)) entries.append(BaseIndexEntry.from_blob(blob)) # END for each path del(paths[:]) # END rewrite paths - def store_path(filepath): """Store file at filepath in the database and return the base index entry""" st = os.lstat(filepath) # handles non-symlinks as well @@ -677,11 +674,10 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): fprogress(filepath, False, filepath) istream = self.repo.odb.store(IStream(Blob.type, st.st_size, stream)) fprogress(filepath, True, filepath) - return BaseIndexEntry((stat_mode_to_index_mode(st.st_mode), + return BaseIndexEntry((stat_mode_to_index_mode(st.st_mode), istream.binsha, 0, to_native_path_linux(filepath))) # END utility method - # HANDLE PATHS if paths: assert len(entries_added) == 0 @@ -691,22 +687,21 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): # END for each filepath # END path handling - # HANDLE ENTRIES if entries: - null_mode_entries = [ e for e in entries if e.mode == 0 ] + null_mode_entries = [e for e in entries if e.mode == 0] if null_mode_entries: raise ValueError("At least one Entry has a null-mode - please use index.remove to remove files for clarity") # END null mode should be remove # HANLDE ENTRY OBJECT CREATION # create objects if required, otherwise go with the existing shas - null_entries_indices = [ i for i,e in enumerate(entries) if e.binsha == Object.NULL_BIN_SHA ] + null_entries_indices = [i for i, e in enumerate(entries) if e.binsha == Object.NULL_BIN_SHA] if null_entries_indices: for ei in null_entries_indices: null_entry = entries[ei] new_entry = store_path(null_entry.path) - + # update null entry entries[ei] = BaseIndexEntry((null_entry.mode, new_entry.binsha, null_entry.stage, null_entry.path)) # END for each entry index @@ -716,7 +711,7 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): # If we have to rewrite the entries, do so now, after we have generated # all object sha's if path_rewriter: - for i,e in enumerate(entries): + for i, e in enumerate(entries): entries[i] = BaseIndexEntry((e.mode, e.binsha, e.stage, path_rewriter(e))) # END for each entry # END handle path rewriting @@ -736,11 +731,11 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): # add the new entries to this instance for entry in entries_added: self.entries[(entry.path, 0)] = IndexEntry.from_base(entry) - + if write: self.write() # END handle write - + return entries_added def _items_to_rela_paths(self, items): @@ -748,7 +743,7 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): may be absolute or relative paths, entries or blobs""" paths = list() for item in items: - if isinstance(item, (BaseIndexEntry,(Blob, Submodule))): + if isinstance(item, (BaseIndexEntry, (Blob, Submodule))): paths.append(self._to_relative_path(item.path)) elif isinstance(item, basestring): paths.append(self._to_relative_path(item)) @@ -806,7 +801,7 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): # process output to gain proper paths # rm 'path' - return [ p[4:-1] for p in removed_paths ] + return [p[4:-1] for p in removed_paths] @post_clear_cache @default_index @@ -852,7 +847,7 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): # parse result - first 0:n/2 lines are 'checking ', the remaining ones # are the 'renaming' ones which we parse - for ln in xrange(len(mvlines)/2, len(mvlines)): + for ln in xrange(len(mvlines) / 2, len(mvlines)): tokens = mvlines[ln].split(' to ') assert len(tokens) == 2, "Too many tokens in %s" % mvlines[ln] @@ -866,7 +861,6 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): return out # END handle dryrun - # now apply the actual operation kwargs.pop('dry_run') self.repo.git.mv(args, paths, **kwargs) @@ -887,7 +881,7 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): return Commit.create_from_tree(self.repo, tree, message, parent_commits, head) @classmethod - def _flush_stdin_and_wait(cls, proc, ignore_stdout = False): + def _flush_stdin_and_wait(cls, proc, ignore_stdout=False): proc.stdin.flush() proc.stdin.close() stdout = '' @@ -901,7 +895,7 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): def checkout(self, paths=None, force=False, fprogress=lambda *args: None, **kwargs): """Checkout the given paths or all files from the version known to the index into the working tree. - + :note: Be sure you have written pending changes using the ``write`` method in case you have altered the enties dictionary directly @@ -934,11 +928,11 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): ( as opposed to the original git command who ignores them ). Raise GitCommandError if error lines could not be parsed - this truly is an exceptional state - - .. note:: The checkout is limited to checking out the files in the - index. Files which are not in the index anymore and exist in + + .. note:: The checkout is limited to checking out the files in the + index. Files which are not in the index anymore and exist in the working tree will not be deleted. This behaviour is fundamentally - different to *head.checkout*, i.e. if you want git-checkout like behaviour, + different to *head.checkout*, i.e. if you want git-checkout like behaviour, use head.checkout instead of index.checkout. """ args = ["--index"] @@ -989,7 +983,6 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): raise CheckoutError("Some files could not be checked out from the index due to local modifications", failed_files, valid_files, failed_reasons) # END stderr handler - if paths is None: args.append("--all") kwargs['as_process'] = 1 @@ -997,7 +990,7 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): proc = self.repo.git.checkout_index(*args, **kwargs) proc.wait() fprogress(None, True, None) - rval_iter = ( e.path for e in self.entries.itervalues() ) + rval_iter = (e.path for e in self.entries.itervalues()) handle_stderr(proc, rval_iter) return rval_iter else: @@ -1005,15 +998,15 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): paths = [paths] # make sure we have our entries loaded before we start checkout_index - # which will hold a lock on it. We try to get the lock as well during + # which will hold a lock on it. We try to get the lock as well during # our entries initialization self.entries - + args.append("--stdin") kwargs['as_process'] = True kwargs['istream'] = subprocess.PIPE proc = self.repo.git.checkout_index(args, **kwargs) - make_exc = lambda : GitCommandError(("git-checkout-index",)+tuple(args), 128, proc.stderr.read()) + make_exc = lambda: GitCommandError(("git-checkout-index",) + tuple(args), 128, proc.stderr.read()) checked_out_files = list() for path in paths: @@ -1030,7 +1023,7 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): for entry in self.entries.itervalues(): if entry.path.startswith(dir): p = entry.path - self._write_path_to_stdin(proc, p, p, make_exc, + self._write_path_to_stdin(proc, p, p, make_exc, fprogress, read_from_stdout=False) checked_out_files.append(p) path_is_directory = True @@ -1039,7 +1032,7 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): # END path exception handlnig if not path_is_directory: - self._write_path_to_stdin(proc, co_path, path, make_exc, + self._write_path_to_stdin(proc, co_path, path, make_exc, fprogress, read_from_stdout=False) checked_out_files.append(co_path) # END path is a file @@ -1066,19 +1059,19 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): If False, the working tree will not be touched Please note that changes to the working copy will be discarded without warning ! - + :param head: If True, the head will be set to the given commit. This is False by default, but if True, this method behaves like HEAD.reset. - + :param paths: if given as an iterable of absolute or repository-relative paths, only these will be reset to their state at the given commit'ish. - The paths need to exist at the commit, otherwise an exception will be + The paths need to exist at the commit, otherwise an exception will be raised. :param kwargs: Additional keyword arguments passed to git-reset - + .. note:: IndexFile.reset, as opposed to HEAD.reset, will not delete anyfiles in order to maintain a consistent working tree. Instead, it will just checkout the files according to their state in the index. @@ -1108,11 +1101,11 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): # END for each path # END handle paths self.write() - + if working_tree: self.checkout(paths=paths, force=True) # END handle working tree - + if head: self.repo.head.set_commit(self.repo.commit(commit), logmsg="%s: Updating HEAD" % commit) # END handle head change @@ -1150,8 +1143,7 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): # if other is not None here, something is wrong if other is not None: - raise ValueError( "other must be None, Diffable.Index, a Tree or Commit, was %r" % other ) + raise ValueError("other must be None, Diffable.Index, a Tree or Commit, was %r" % other) # diff against working copy - can be handled by superclass natively return super(IndexFile, self).diff(other, paths, create_patch, **kwargs) - |