def ls_remotes(self, callbacks=None): """Return a list of dicts that maps to `git_remote_head` from a `ls_remotes` call.""" self.connect(callbacks=callbacks) refs = ffi.new('git_remote_head ***') refs_len = ffi.new('size_t *') err = C.git_remote_ls(refs, refs_len, self._remote) check_error(err) results = [] for i in range(int(refs_len[0])): local = bool(refs[0][i].local) if local: loid = Oid(raw=bytes(ffi.buffer(refs[0][i].loid.id)[:])) else: loid = None remote = { "local": local, "loid": loid, "name": maybe_string(refs[0][i].name), "symref_target": maybe_string(refs[0][i].symref_target), "oid": Oid(raw=bytes(ffi.buffer(refs[0][i].oid.id)[:])), } results.append(remote) return results
def blame(self, path, flags=None, min_match_characters=None, newest_commit=None, oldest_commit=None, min_line=None, max_line=None): """blame(path, [flags, min_match_characters, newest_commit, oldest_commit,\n" min_line, max_line]) -> Blame Get the blame for a single file. Arguments: path Path to the file to blame. flags A GIT_BLAME_* constant. min_match_characters The number of alphanum chars that must be detected as moving/copying within a file for it to associate those lines with the parent commit. newest_commit The id of the newest commit to consider. oldest_commit The id of the oldest commit to consider. min_line The first line in the file to blame. max_line The last line in the file to blame. Examples:: repo.blame('foo.c', flags=GIT_BLAME_TRACK_COPIES_SAME_FILE)"); """ options = ffi.new('git_blame_options *') C.git_blame_init_options(options, C.GIT_BLAME_OPTIONS_VERSION) if min_match_characters: options.min_match_characters = min_match_characters if newest_commit: if not isinstance(newest_commit, Oid): newest_commit = Oid(hex=newest_commit) ffi.buffer(ffi.addressof(options, 'newest_commit'))[:] = newest_commit.raw if oldest_commit: if not isinstance(oldest_commit, Oid): oldest_commit = Oid(hex=oldest_commit) ffi.buffer(ffi.addressof(options, 'oldest_commit'))[:] = oldest_commit.raw if min_line: options.min_line = min_line if max_line: options.max_line = max_line cblame = ffi.new('git_blame **') err = C.git_blame_file(cblame, self._repo, to_bytes(path), options) check_error(err) return Blame._from_c(self, cblame[0])
def _update_tips_cb(refname, a, b, data): self = ffi.from_handle(data) if not hasattr(self, 'update_tips') or not self.update_tips: return 0 try: s = maybe_string(refname) a = Oid(raw=bytes(ffi.buffer(a)[:])) b = Oid(raw=bytes(ffi.buffer(b)[:])) self.update_tips(s, a, b) except Exception as e: self._stored_exception = e return C.GIT_EUSER return 0
def _from_c(cls, centry): if centry == ffi.NULL: return None entry = cls.__new__(cls) entry.path = to_str(ffi.string(centry.path)) entry.mode = centry.mode entry.id = Oid(raw=bytes(ffi.buffer(ffi.addressof(centry, 'id'))[:])) return entry
def stash(self, stasher, message=None, keep_index=False, include_untracked=False, include_ignored=False): """ Save changes to the working directory to the stash. Returns: The Oid of the stash merge commit (Oid). Parameters: stasher : Signature The identity of the person doing the stashing. message : str An optional description of stashed state. keep_index : bool Leave changes already added to the index in the working directory. include_untracked : bool Also stash untracked files. include_ignored : bool Also stash ignored files. Example:: >>> repo = pygit2.Repository('.') >>> repo.stash(repo.default_signature(), 'WIP: stashing') """ if message: stash_msg = ffi.new('char[]', to_bytes(message)) else: stash_msg = ffi.NULL flags = 0 flags |= keep_index * C.GIT_STASH_KEEP_INDEX flags |= include_untracked * C.GIT_STASH_INCLUDE_UNTRACKED flags |= include_ignored * C.GIT_STASH_INCLUDE_IGNORED stasher_cptr = ffi.new('git_signature **') ffi.buffer(stasher_cptr)[:] = stasher._pointer[:] coid = ffi.new('git_oid *') err = C.git_stash_save(coid, self._repo, stasher_cptr[0], stash_msg, flags) check_error(err) return Oid(raw=bytes(ffi.buffer(coid)[:]))
def write_tree(self, repo=None): """Create a tree out of the Index. Return the <Oid> object of the written tree. The contents of the index will be written out to the object database. If there is no associated repository, 'repo' must be passed. If there is an associated repository and 'repo' is passed, then that repository will be used instead. It returns the id of the resulting tree. """ coid = ffi.new('git_oid *') if repo: err = C.git_index_write_tree_to(coid, self._index, repo._repo) else: err = C.git_index_write_tree(coid, self._index) check_error(err) return Oid(raw=bytes(ffi.buffer(coid)[:]))
def orig_commit_id(self): return Oid(raw=bytes( ffi.buffer(ffi.addressof(self._hunk, 'orig_commit_id'))[:]))
def head_id(self): """Head of the submodule.""" head = C.git_submodule_head_id(self._subm) return Oid(raw=bytes(ffi.buffer(head)[:]))