def stage(self, fs_paths): """Stage a set of paths. :param fs_paths: List of paths, relative to the repository path """ root_path_bytes = self.path.encode(sys.getfilesystemencoding()) if not isinstance(fs_paths, list): fs_paths = [fs_paths] from dulwich.index import ( blob_from_path_and_stat, index_entry_from_stat, _fs_to_tree_path, ) index = self.open_index() for fs_path in fs_paths: if not isinstance(fs_path, bytes): fs_path = fs_path.encode(sys.getfilesystemencoding()) tree_path = _fs_to_tree_path(fs_path) full_path = os.path.join(root_path_bytes, fs_path) try: st = os.lstat(full_path) except OSError: # File no longer exists try: del index[tree_path] except KeyError: pass # already removed else: blob = blob_from_path_and_stat(full_path, st) self.object_store.add_object(blob) index[tree_path] = index_entry_from_stat(st, blob.id, 0) index.write()
def stage(self, paths, fsencoding=sys.getfilesystemencoding()): """Stage a set of paths. :param paths: List of paths, relative to the repository path """ if not isinstance(paths, list): paths = [paths] from dulwich.index import ( blob_from_path_and_stat, index_entry_from_stat, ) index = self.open_index() for path in paths: full_path = os.path.join(self.path, path) try: st = os.lstat(full_path) except OSError: # File no longer exists try: del index[path.encode(fsencoding)] except KeyError: pass # already removed else: blob = blob_from_path_and_stat(full_path, st) self.object_store.add_object(blob) index[path.encode(fsencoding)] = index_entry_from_stat(st, blob.id, 0) index.write()
def stage(self, paths, fsencoding=sys.getfilesystemencoding()): """Stage a set of paths. :param paths: List of paths, relative to the repository path """ if not isinstance(paths, list): paths = [paths] from dulwich.index import ( blob_from_path_and_stat, index_entry_from_stat, ) index = self.open_index() for path in paths: if not isinstance(path, bytes): disk_path_bytes = path.encode(sys.getfilesystemencoding()) repo_path_bytes = path.encode(fsencoding) else: disk_path_bytes, repo_path_bytes = path, path full_path = os.path.join(self._path_bytes, disk_path_bytes) try: st = os.lstat(full_path) except OSError: # File no longer exists try: del index[repo_path_bytes] except KeyError: pass # already removed else: blob = blob_from_path_and_stat(full_path, st) self.object_store.add_object(blob) index[repo_path_bytes] = index_entry_from_stat(st, blob.id, 0) index.write()
def stage(self, paths): """Stage a set of paths. :param paths: List of paths, relative to the repository path """ if isinstance(paths, basestring): paths = [paths] from dulwich.index import ( blob_from_path_and_stat, index_entry_from_stat, ) index = self.open_index() for path in paths: full_path = os.path.join(self.path, path) try: st = os.lstat(full_path) except OSError: # File no longer exists try: del index[path] except KeyError: pass # already removed else: blob = blob_from_path_and_stat(full_path, st) self.object_store.add_object(blob) index[path] = index_entry_from_stat(st, blob.id, 0) index.write()
def commit(repo: Repo, msg: str) -> str: """Commit everything.""" for tree_path, entry in repo.open_index().items(): full_path = os.path.join(repo.path.encode(), tree_path) blob = blob_from_path_and_stat(full_path, os.lstat(full_path)) if blob.id != entry.sha: repo.stage(tree_path) return repo.do_commit(msg.encode(), b"Source{d} ML Team <*****@*****.**>")
def write_index_workingdir_diff(f, store, index, names, filter_callback=None, diff_binary=False): """Write diff of index against current working dir Args: f: File-like object to write to. index: Index object for base of comparison names: list of working directory relative file paths (bytes) diff_binary: Whether to diff files even if they are considered binary files by is_binary(). """ entry_info = {} def lookup_entry(name): if name in entry_info: blob, fmode = entry_info[name] return (blob.id, fmode) return (None, None) # convert tree_paths that represent files in working dir # to an equivalent temp blob mode and store it # This should properly handle checkin normalization # which is required to make diffs work properly for name in names: filepath = name if os_sep_bytes != b'/': filepath = name.replace(b'/', os_sep_bytes) stat = os.stat(filepath) fmode = stat.st_mode blob = blob_from_path_and_stat(filepath, stat) if filter_callback: blob = filter_callback(blob, name) entry_info[name] = (blob, fmode) for change_entry in changes_from_workingdir(names, lookup_entry, store, index): (name1, name2), (mode1, mode2), (sha1, sha2) = change_entry content1 = b'' content2 = b'' if name2: if name2 in entry_info: blob, fmode = entry_info[name2] content2 = blob.as_raw_string() if name1: content1 = store[sha1].as_raw_string() _write_diff(f, (name1, mode1, sha1, content1), (name2, mode2, sha2, content2))
def stage(self, fs_paths): """Stage a set of paths. Args: fs_paths: List of paths, relative to the repository path """ root_path_bytes = os.fsencode(self.path) if not isinstance(fs_paths, list): fs_paths = [fs_paths] from dulwich.index import ( blob_from_path_and_stat, index_entry_from_stat, _fs_to_tree_path, ) index = self.open_index() blob_normalizer = self.get_blob_normalizer() for fs_path in fs_paths: if not isinstance(fs_path, bytes): fs_path = os.fsencode(fs_path) if os.path.isabs(fs_path): raise ValueError( "path %r should be relative to " "repository root, not absolute" % fs_path) tree_path = _fs_to_tree_path(fs_path) full_path = os.path.join(root_path_bytes, fs_path) try: st = os.lstat(full_path) except OSError: # File no longer exists try: del index[tree_path] except KeyError: pass # already removed else: if (not stat.S_ISREG(st.st_mode) and not stat.S_ISLNK(st.st_mode)): try: del index[tree_path] except KeyError: pass else: blob = blob_from_path_and_stat(full_path, st) blob = blob_normalizer.checkin_normalize(blob, fs_path) self.object_store.add_object(blob) index[tree_path] = index_entry_from_stat(st, blob.id, 0) index.write()
def stage(self, fs_paths): """Stage a set of paths. :param fs_paths: List of paths, relative to the repository path """ root_path_bytes = self.path.encode(sys.getfilesystemencoding()) if not isinstance(fs_paths, list): fs_paths = [fs_paths] from dulwich.index import ( blob_from_path_and_stat, index_entry_from_stat, _fs_to_tree_path, ) index = self.open_index() blob_normalizer = self.get_blob_normalizer() for fs_path in fs_paths: if not isinstance(fs_path, bytes): fs_path = fs_path.encode(sys.getfilesystemencoding()) if os.path.isabs(fs_path): raise ValueError( "path %r should be relative to " "repository root, not absolute" % fs_path) tree_path = _fs_to_tree_path(fs_path) full_path = os.path.join(root_path_bytes, fs_path) try: st = os.lstat(full_path) except OSError: # File no longer exists try: del index[tree_path] except KeyError: pass # already removed else: if not stat.S_ISDIR(st.st_mode): blob = blob_from_path_and_stat(full_path, st) blob = blob_normalizer.checkin_normalize(blob, fs_path) self.object_store.add_object(blob) index[tree_path] = index_entry_from_stat(st, blob.id, 0) else: try: del index[tree_path] except KeyError: pass index.write()
def remove(repo=".", paths=None, cached=False): """Remove files from the staging area. Args: repo: Repository for the files paths: Paths to remove """ with open_repo_closing(repo) as r: index = r.open_index() for p in paths: full_path = os.path.abspath(p).encode(sys.getfilesystemencoding()) tree_path = path_to_tree_path(r.path, p) try: index_sha = index[tree_path].sha except KeyError: raise Exception('%s did not match any files' % p) if not cached: try: st = os.lstat(full_path) except OSError: pass else: try: blob = blob_from_path_and_stat(full_path, st) except IOError: pass else: try: committed_sha = tree_lookup_path( r.__getitem__, r[r.head()].tree, tree_path)[1] except KeyError: committed_sha = None if blob.id != index_sha and index_sha != committed_sha: raise Exception( 'file has staged content differing ' 'from both the file and head: %s' % p) if index_sha != committed_sha: raise Exception( 'file has staged changes: %s' % p) os.remove(full_path) del index[tree_path] index.write()
def remove(repo=".", paths=None, cached=False): """Remove files from the staging area. :param repo: Repository for the files :param paths: Paths to remove """ with open_repo_closing(repo) as r: index = r.open_index() for p in paths: full_path = os.path.abspath(p).encode(sys.getfilesystemencoding()) tree_path = path_to_tree_path(r.path, p) try: index_sha = index[tree_path].sha except KeyError: raise Exception('%s did not match any files' % p) if not cached: try: st = os.lstat(full_path) except OSError: pass else: try: blob = blob_from_path_and_stat(full_path, st) except IOError: pass else: try: committed_sha = tree_lookup_path( r.__getitem__, r[r.head()].tree, tree_path)[1] except KeyError: committed_sha = None if blob.id != index_sha and index_sha != committed_sha: raise Exception( 'file has staged content differing ' 'from both the file and head: %s' % p) if index_sha != committed_sha: raise Exception( 'file has staged changes: %s' % p) os.remove(full_path) del index[tree_path] index.write()