本文整理汇总了Python中pygit2.Repository.lookup_branch方法的典型用法代码示例。如果您正苦于以下问题:Python Repository.lookup_branch方法的具体用法?Python Repository.lookup_branch怎么用?Python Repository.lookup_branch使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pygit2.Repository
的用法示例。
在下文中一共展示了Repository.lookup_branch方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Repo
# 需要导入模块: from pygit2 import Repository [as 别名]
# 或者: from pygit2.Repository import lookup_branch [as 别名]
class Repo(object):
def __init__(self, path="", branch="develop"):
self.path = path if path else os.getcwd()
# Retrieve the repo if its already defined
try:
self.repo = Repository(self.path)
# This is a little hacky, but we need a git command interface to do
# certain things
self.pygitrepo = git.Repo(self.repo.path)
self.git = self.pygitrepo.git
self.currentBranch = self.repo.lookup_branch(self.repo.head.shorthand)
self._user = self.repo.default_signature
except KeyError:
self.repo = None
self.currentBranch = None
self._user = None
# TODO Handle this another way
self.branch = branch
@property
def user(self):
if not self._user and self.repo:
self._user = self.repo.default_signature
return self._user
def clone(self, repourl):
self.repo = clone_repository(
repourl,
self.path,
checkout_branch=self.branch
)
def checkoutBranch(self, name):
# TODO check if a branch of this name exists
developBranch = self.repo.lookup_branch("develop")
self.repo.checkout(developBranch)
self.currentBranch = self.repo.create_branch(
name,
self.repo.head.get_object()
)
self.repo.checkout(self.currentBranch)
def merge(self, branch, delete=False, push=False):
pass
def release(self):
# Checkout the release branch
# Need some way to control versioning
# Internal versioning
pass
示例2: git_is_clean
# 需要导入模块: from pygit2 import Repository [as 别名]
# 或者: from pygit2.Repository import lookup_branch [as 别名]
def git_is_clean(srcdir, project):
repo = Repository(os.path.join(srcdir, project.workspace_path, ".git"))
for _, b in iteritems(repo.status()):
if b != GIT_STATUS_IGNORED and b != GIT_STATUS_CURRENT:
return False, "has uncommitted changes"
if repo.head_is_detached:
return False, "has detached HEAD"
origin = get_origin(repo, project)
if not origin:
return False, "has no upstream remote"
remote_refs = []
local_refs = {}
for refname in repo.listall_references():
if refname.startswith("refs/remotes/%s/" % origin.name):
ref = repo.lookup_reference(refname)
if ref.type == GIT_REF_OID:
remote_refs.append(ref.target)
elif not refname.startswith("refs/remotes/"):
ref = repo.lookup_reference(refname)
if ref.type == GIT_REF_OID:
local_refs[ref.peel().id] = refname
if not remote_refs:
return False, "has no upstream remote branches"
if not local_refs:
return False, "has no local branches"
if not repo.lookup_branch("%s/%s" % (origin.name, project.master_branch), GIT_BRANCH_REMOTE):
return False, "has no upstream master branch"
for remote_ref in remote_refs:
for commit in repo.walk(remote_ref):
if commit.id in local_refs:
del local_refs[commit.id]
if local_refs:
return False, "has local commits: %s" % ", ".join(["'%s'" % name for _, name in iteritems(local_refs)])
return True, ""
示例3: RepositoryInfo
# 需要导入模块: from pygit2 import Repository [as 别名]
# 或者: from pygit2.Repository import lookup_branch [as 别名]
class RepositoryInfo(object):
""" wraps an pygit2.Repository object
"""
def __init__(self, repo_path):
self._repo = Repository(repo_path)
self.count_unmodified = 0
self.count_wt_modified = 0
self.count_wt_new = 0
self.count_wt_deleted = 0
self.count_index_modified = 0
self.count_index_new = 0
self.count_index_deleted = 0
self._count()
@property
def path(self):
sep = '/'
splitted = self._repo.path.split(sep)[0:-2]
return sep.join(splitted)
@property
def has_workingtree_changes(self):
return self.count_wt_deleted > 0 or self.count_wt_modified > 0 or self.count_wt_new > 0
@property
def has_index_changes(self):
return self.count_index_deleted > 0 or self.count_index_modified > 0 or self.count_index_new > 0
def _count(self):
_status = self._repo.status()
for file_path, flags in _status.items():
if flags == GIT_STATUS_CURRENT:
self.count_unmodified += 1
elif flags == GIT_STATUS_WT_MODIFIED:
self.count_wt_modified += 1
elif flags == GIT_STATUS_WT_NEW:
self.count_wt_new += 1
elif flags == GIT_STATUS_INDEX_NEW:
self.count_index_new += 1
elif flags == GIT_STATUS_INDEX_MODIFIED:
self.count_index_modified += 1
elif flags == GIT_STATUS_INDEX_DELETED:
self.count_index_deleted += 1
elif flags == GIT_STATUS_WT_DELETED:
self.count_wt_deleted += 1
@property
def current_branch_name(self):
# ToDo: Why does self._repo.head.shorthand not work?
head = self._repo.head
head_name = head.name.split('/')[-1:]
return head_name[0]
@property
def is_head_upstream_branch(self):
""" determines if current head is the same commit as the remote commit
"""
if self._repo.head_is_detached:
return False
current_branch_name = self.current_branch_name
head = self._repo.head
remote_branch = self._repo.lookup_branch(current_branch_name).upstream
if remote_branch:
return remote_branch.target.hex == head.target.hex
return False
@property
def is_head_detached(self):
return self._repo.head_is_detached
示例4: GitStorage
# 需要导入模块: from pygit2 import Repository [as 别名]
# 或者: from pygit2.Repository import lookup_branch [as 别名]
#.........这里部分代码省略.........
return self._get_obj(path, Blob).data
def listdir(self, path):
if path:
tree = self._get_obj(path, Tree)
else:
if self._commit is None:
return []
tree = self._commit.tree
return [entry.name for entry in tree]
def format(self, **kw):
# XXX backwards compatibility??
return kw
def log(self, start, count, branch=None, shortlog=False):
"""
start and branch are literally the same thing.
"""
def _log(iterator):
for pos, commit in iterator:
if pos == count:
raise StopIteration
yield {
'author': commit.committer.name,
'email': self._commit.committer.email,
'date': self.strftime(committer_dt(commit.committer)),
'node': commit.hex,
'rev': commit.hex,
'desc': commit.message
}
if start is None:
# assumption.
start = 'HEAD'
try:
self.repo.revparse_single(start)
except KeyError:
return []
try:
rev = self.repo.revparse_single(start).hex
except KeyError:
raise RevisionNotFoundError('revision %s not found' % start)
iterator = enumerate(self.repo.walk(rev, GIT_SORT_TIME))
return list(_log(iterator))
def pathinfo(self, path):
obj = self._get_obj(path)
if isinstance(obj, Blob):
return self.format(**{
'type': 'file',
'basename': self.basename(path),
'size': obj.size,
'date': self.strftime(committer_dt(self._commit.committer)),
})
elif isinstance(obj, dict):
# special cases are represented as dict.
if obj[''] == '_subrepo':
return self.format(**{
'type': 'subrepo',
'date': '',
'size': 0,
'basename': self.basename(path),
# extra field.
'obj': obj,
})
elif obj[''] == '_empty_root':
return self.format(**{
'type': 'folder',
'date': '',
'size': 0,
'basename': self.basename(path),
})
# Assume this is a Tree.
return self.format(**{
'basename': self.basename(path),
'size': 0,
'type': 'folder',
'date': '',
})
def branches(self):
return tuple(
(b, self.repo.lookup_branch(b).target.hex)
for b in self.repo.listall_branches()
)
def tags(self):
return tuple(
(b[10:], self.repo.lookup_reference(b).target.hex)
for b in self.repo.listall_references()
if b.startswith('refs/tags')
)
示例5: show_status
# 需要导入模块: from pygit2 import Repository [as 别名]
# 或者: from pygit2.Repository import lookup_branch [as 别名]
def show_status(srcdir, packages, projects, other_git, ws_state, show_up_to_date=True, cache=None):
def create_upstream_status(repo, head_branch, master_branch, master_remote_branch, tracking_branch):
status = []
if not repo.head_is_detached and not has_pending_merge(repo):
if tracking_branch is not None:
if master_remote_branch is not None:
if tracking_branch.remote_name != master_remote_branch.remote_name:
status.append("@[email protected]{rf}remote '%s'" % tracking_branch.remote_name)
if need_push(repo, head_branch):
status.append("@[email protected]{yf}needs push")
elif need_pull(repo, head_branch):
status.append("@[email protected]{cf}needs pull")
elif not is_up_to_date(repo, head_branch):
status.append("@[email protected]{yf}needs pull -M")
else:
if head_branch:
status.append("@!on branch '%s'" % repo.head.shorthand)
else:
status.append("empty branch")
if master_remote_branch is None:
status.append("@[email protected]{rf}no remote")
elif master_branch is None:
status.append("@[email protected]{rf}untracked remote")
if is_up_to_date(repo, master_branch) or need_push(repo, master_branch):
if need_pull(repo, head_branch, master_branch):
status.append("@[email protected]{cf}needs pull -L")
else:
if not is_ancestor(repo, master_branch, head_branch):
status.append("@[email protected]{yf}needs merge --from-master")
if not is_up_to_date(repo, head_branch, master_branch):
status.append("@[email protected]{yf}needs merge --to-master")
if master_branch is not None and master_remote_branch is not None and (tracking_branch is None or tracking_branch.name != master_remote_branch.name):
if need_push(repo, master_branch):
status.append("@[email protected]{yf}%s needs push" % master_branch.shorthand)
elif need_pull(repo, master_branch):
status.append("@[email protected]{cf}%s needs pull" % master_branch.shorthand)
elif not is_up_to_date(repo, master_branch):
status.append("@[email protected]{yf}%s needs merge" % master_branch.shorthand)
return status
def create_local_status(repo, upstream_status, is_dirty):
status = []
if repo.head_is_detached:
status.append("@[email protected]{rf}detached HEAD")
return status
if has_pending_merge(repo):
if repo.index.conflicts:
status.append("@[email protected]{rf}merge conflicts")
else:
status.append("@[email protected]{yf}merged, needs commit")
return status
if is_dirty:
status.append("@[email protected]{yf}needs commit")
status += upstream_status
if not status:
if not show_up_to_date:
return None
status.append("@[email protected]{gf}up-to-date")
return status
table = TableView("Package", "Path", "Status")
found_packages = set()
for project in projects:
repo = Repository(os.path.join(srcdir, project.workspace_path, ".git"))
dirty_files = [a for a, b in iteritems(repo.status()) if b != GIT_STATUS_IGNORED and b != GIT_STATUS_CURRENT]
head_branch = get_head_branch(repo)
tracking_branch = head_branch.upstream if head_branch else None
master_remote = get_origin(repo, project)
if master_remote is not None:
master_remote_branch = repo.lookup_branch("%s/%s" % (master_remote.name, project.master_branch), GIT_BRANCH_REMOTE)
master_branch = None
if master_remote_branch is not None:
for name in repo.listall_branches(GIT_BRANCH_LOCAL):
b = repo.lookup_branch(name, GIT_BRANCH_LOCAL)
if b.upstream and b.upstream.branch_name == master_remote_branch.branch_name:
master_branch = b
break
else:
master_remote_branch = None
master_branch = None
ws_packages = find_catkin_packages(srcdir, project.workspace_path, cache=cache)
found_packages |= set(ws_packages.keys())
upstream_status = create_upstream_status(repo, head_branch, master_branch, master_remote_branch, tracking_branch)
for name, pkg_list in iteritems(ws_packages):
if name not in packages:
continue
for pkg in pkg_list:
is_dirty = False
local_path = os.path.relpath(pkg.workspace_path, project.workspace_path)
if dirty_files and local_path == ".":
is_dirty = True
else:
for fpath in dirty_files:
if path_has_prefix(fpath, local_path):
is_dirty = True
break
status = create_local_status(repo, upstream_status, is_dirty)
if status is not None:
head, tail = os.path.split(pkg.workspace_path)
#.........这里部分代码省略.........
示例6: GitRepo
# 需要导入模块: from pygit2 import Repository [as 别名]
# 或者: from pygit2.Repository import lookup_branch [as 别名]
class GitRepo(object):
''' git repo class '''
def __init__(self, path):
try:
self.__repo = Repository(path)
except Exception as e:
self.__repo = None
print(e)
def get_info(self):
if not self.__repo:
return None
signature = self.__repo.default_signature
result = {
'path': self.__repo.path,
'workdir': self.__repo.workdir,
'bare': self.__repo.is_bare,
'empty': self.__repo.is_empty,
'name': signature.name,
'email': signature.email,
'time': signature.time,
'offset': signature.offset,
}
return result
def get_all_references(self):
return self.__repo.listall_references()
def get_reference(self, name):
if not self.__repo:
return None
ref = None
try:
ref = self.__repo.lookup_reference(name)
except Exception as e:
print(e)
return ref
def get_all_branches(self, branch_type=None):
if not self.__repo:
return None
if branch_type:
return self.__repo.listall_branches(branch_type)
r = self.__repo.listall_branches(GIT_BRANCH_LOCAL | GIT_BRANCH_REMOTE)
return r
def get_branch(self, name, branch_type=GIT_BRANCH_LOCAL):
if not self.__repo:
return None
return self.__repo.lookup_branch(name, branch_type)
def check_branch(self, name, branch_type=None):
if not branch_type:
if '/' in name:
branch_type = GIT_BRANCH_REMOTE
else:
branch_type = GIT_BRANCH_LOCAL
try:
result = self.get_branch(name, branch_type)
return result
except Exception as e:
print(e)
return False
def get_current_commit(self):
if not self.__repo:
return None
commit = self.__repo.revparse_single('HEAD')
return self.get_commit(commit)
def get_commit_by_branch(self, branch):
if not self.__repo:
return None
query = 'refs/'
if hasattr(branch, 'remote_name'):
query += 'remotes/'
else:
query += 'heads/'
query += branch.branch_name
try:
ref = self.get_reference(query)
commit = ref.target
return self.get_commit(commit)
except Exception as e:
print(e)
return None
def get_commit_by_tag(self, tag):
if self.__repo is None:
return None
if tag:
commit = tag.get_object()
return self.get_commit(commit)
return None
def get_commit(self, oid_or_commit):
''' return a commit w/ json '''
if not self.__repo or not oid_or_commit:
#.........这里部分代码省略.........
示例7: GitMixin
# 需要导入模块: from pygit2 import Repository [as 别名]
# 或者: from pygit2.Repository import lookup_branch [as 别名]
class GitMixin(object):
tag_or_remote_regex = re.compile('^refs/(tags|remotes)/(.*)')
def __init__(self):
where = GitOperations.get_repository_location(self.user, self.name)
self.ondisk = Repository(where)
def refresh(self):
creds = GitOperations.get_credentials(self.git_user, self.user)
for remote in self.ondisk.remotes:
remote.credentials = creds
remote.fetch()
# update current reference
master_ref = self.ondisk.lookup_reference('refs/heads/master')
remote_ref = self.ondisk.lookup_reference('refs/remotes/origin/master')
master_ref.set_target(remote_ref.target)
def filter_references(self, regex):
return [ref for ref in self.ondisk.listall_references()
if regex.match(ref)]
def get_commit_time(self, name):
ref = self.ondisk.revparse_single(name)
if isinstance(ref, Tag):
return ref.get_object().commit_time
if isinstance(ref, Commit):
return ref.commit_time
raise GitException('invalid reference: commit time could not be found.') # pragma: no cover
def get_latest_refs(self, count=None):
info = self.filter_references(GitMixin.tag_or_remote_regex)
refs = list(zip(info, map(self.get_commit_time, info)))
refs.sort(key=itemgetter(1), reverse=True)
def ref_info(info):
(ref, commit_time) = info
what, name = GitMixin.tag_or_remote_regex.findall(ref)[0]
return (what, name, commit_time)
refs = map(ref_info, refs)
if not count:
return refs
return islice(refs, count)
def filter_commits(self, flags=0):
all_commits = self.ondisk.walk(self.ondisk.head.target, flags)
emails = [ue.email for ue in self.user.emails.all()]
return filter(lambda commit: commit.author.email in emails, all_commits)
def get_commits(self, count=None):
all_commits = self.filter_commits(GIT_SORT_TOPOLOGICAL)
if not count:
return all_commits
return islice(all_commits, count)
def get_commit_count(self):
return len(list(self.filter_commits()))
def get_shorthand_of_branch(self, branch):
commit = self.ondisk.lookup_branch(branch)
if commit:
return commit.shorthand
return '(none)'
def get_sha1_of_branch(self, branch):
commit = self.ondisk.lookup_branch(branch)
if commit:
return str(commit.get_object().id)[:6]
return '(none)'
def get_numstat(self, commit):
diff = None
try:
previous_commit = self.ondisk.revparse_single(str(commit.id) + '^')
diff = self.ondisk.diff(previous_commit, commit)
except KeyError:
# likely we hit the very first commit.
diff = commit.tree.diff_to_tree(swap=True)
additions, deletions = 0, 0
for patch in diff:
additions += patch.additions
deletions += patch.deletions
return (len(diff), additions, deletions)
def get_first_updated(self):
all_commits = self.ondisk.walk(self.ondisk.head.target,
GIT_SORT_TIME | GIT_SORT_REVERSE)
first_commit = next(all_commits)
return first_commit.commit_time
def get_last_updated(self):
all_commits = self.ondisk.walk(self.ondisk.head.target,
GIT_SORT_TIME)
last_commit = next(all_commits)
return last_commit.commit_time
def get_file_count(self):
diff = self.ondisk.head.get_object().tree.diff_to_tree()
return len([patch.old_file_path for patch in diff])
def get_line_count(self):
#.........这里部分代码省略.........