标签: pygit2


使用pygit2创建提交

我想在分支上进行提交(例如master).

我使用pygit2(pygit2.clone_repository)制作存储库克隆

然后我更改存储库中的现有文件.

然后我运行它来进行提交:

index = repository.index
index.add_all()
index.write()
author = pygit2.Signature(user_name, user_mail)
commiter = pygit2.Signature(user_name, user_mail)
tree = repository.TreeBuilder().write()
oid = repository.create_commit(reference, author, commiter, message,tree,[repository.head.get_object().hex])
Run Code Online (Sandbox Code Playgroud)

但是当我去存储库运行时git status:

On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file:   test.txt
Run Code Online (Sandbox Code Playgroud)

似乎添加了修改后的文件以进行提交,但提交未成功.使用返回的Oid,我可以在pygit2存储库中找到commit属性.

我错过了什么 ?

git libgit2 pygit2

9
推荐指数
1
解决办法
1946
查看次数

Git承诺父母的命令

提交的父母的顺序是否有一些约定?

因为提交的父项之一应该是当前正在合并的当前分支上的提交,其余的是其他合并分支的先前提交.

我想确定当前分支的先前提交,我正在使用pygit它返回一个提交的父母列表,直觉上我认为父母的顺序可能有意义,但我没有发现明确提到这一点.


我编写了这个实用程序函数,使用第一个父提交来遍历分支:

def walk_branch(pygit_repository, branch_oid):
    """
    Walk a single branch
    """
    from pygit2 import GIT_SORT_TOPOLOGICAL
    previous_first_parent_oid = None
    for commit in pygit_repository.walk(branch_oid, GIT_SORT_TOPOLOGICAL):
        if previous_first_parent_oid is None or commit.oid == previous_first_parent_oid:
            previous_first_parent_oid = commit.parents[0].oid if len(commit.parents) else None
            yield commit
Run Code Online (Sandbox Code Playgroud)

git libgit2 pygit2

7
推荐指数
2
解决办法
1853
查看次数

你如何用 pygit2 结帐分支?

我想用来pygit2结帐分支名称。

例如,如果我有两个分支:masterandnewHEADis at master,我希望能够做到:

import pygit2
repository = pygit2.Repository('.git')
repository.checkout('new')
Run Code Online (Sandbox Code Playgroud)

甚至

import pygit2
repository = pygit2.Repository('.git')
repository.lookup_branch('new').checkout()
Run Code Online (Sandbox Code Playgroud)

但两者都不起作用,pygit2 文档没有提到如何结帐分支。

python git pygit2

6
推荐指数
1
解决办法
2312
查看次数

如何使用 pygit2 变基?

我被困在尝试实现git rebasewith pygit2

假设这个回购的历史,如何变基topicmaster使用pygit2?(即,相当于git rebase master topic):

      A---B---C topic
     /
D---E---F---G master
Run Code Online (Sandbox Code Playgroud)

根据pygit2 文档merge_trees可以用于此目的。到目前为止,我得到了:

import pygit2 as git

repo = git.Repository(".")

master = repo.lookup_branch("master")
topic = repo.lookup_branch("topic")

base = repo.merge_base(master.target, topic.target)

master_tree = repo.get(master.target).tree
topic_tree = repo.get(topic.target).tree
base_tree = repo.get(base).tree

index = repo.merge_trees(base_tree,topic,master)
tree_id = index.write_tree(repo)

sig = git.Signature('FooBar', 'foo@bar.org')

# this is not going to work -> several commits need to be re-created
repo.create_commit(topic.name,sig,sig,"msg?",tree_id,[master.target]) …
Run Code Online (Sandbox Code Playgroud)

python git pygit2

5
推荐指数
0
解决办法
280
查看次数

未提交的dirs与pygit2提交

我正在使用pygit2处理非裸存储库

index = repo.index
index.read()

# write in test/test.txt

index.add('test/test.txt')
treeid = index.write_tree()

repo.create_commit(
    'HEAD',
    author, committer,
    'test commit',
    treeid,
    [repo.head.oid]
)
Run Code Online (Sandbox Code Playgroud)

这很成功,但是当我执行时git status,我得到了这个:

# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   deleted:    test/test.txt
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   test/
Run Code Online (Sandbox Code Playgroud)

git reset --hard一切之后,一切都得到了解决.

有没有办法用pygit正确更新索引?

python git libgit2 pygit2

4
推荐指数
1
解决办法
607
查看次数

使用 Pygit2 实现拉取

我正在尝试使用 pygit2 实现一些“瓷器”命令。似乎我在实施拉动时遇到了一些障碍。特别是最简单的拉动情况,快进。

设置:

我有两个 git 仓库。一个“远程”和一个“本地”。我在远程仓库上进行一次提交,然后使用 pygit2 的clone_repository(). 我在遥控器上进行了后续提交,然后尝试运行pull()下面概述的功能。

我的实现:

def pull(repo, remote_name='origin'):
    for remote in repo.remotes:
        if remote.name == remote_name:
            remote.fetch()
            remote_master_id = repo.lookup_reference('refs/remotes/origin/master').target
            merge_result, _ = repo.merge_analysis(remote_master_id)
            # Up to date, do nothing
            if merge_result & pygit2.GIT_MERGE_ANALYSIS_UP_TO_DATE:
                return
            # We can just fastforward
            elif merge_result & pygit2.GIT_MERGE_ANALYSIS_FASTFORWARD:
                print repo.head.target
                print repo.status()
                master_ref = repo.lookup_reference('refs/heads/master')
                master_ref.set_target(remote_master_id)
                repo.head.set_target(master_ref)
                repo.checkout_head()
                print repo.status()
            elif merge_result & pygit2.GIT_MERGE_ANALYSIS_NORMAL:
                repo.merge(remote_master_id)
                print repo.index.conflicts

                assert repo.index.conflicts is None, 'Conflicts, ahhhh!'
                user …
Run Code Online (Sandbox Code Playgroud)

python libgit2 pygit2

4
推荐指数
1
解决办法
2739
查看次数

"(无分支)"与"(abc1234分离)"之间的区别

通常当你在git仓库中运行这样的东西时:

git checkout abc1234
Run Code Online (Sandbox Code Playgroud)

你最终处于一个独立的HEAD状态.如果你运行git branch,输出将如下所示:

* (detached from abc1234)
  master
Run Code Online (Sandbox Code Playgroud)

这是很好的预期行为.

我最近一直在玩pygit2,并且遇到过我以前没见过的东西.假设我做了以下事情:

repo = pygit2.discover_repository("/path/to/repo")
repo.head = "abc1234"
Run Code Online (Sandbox Code Playgroud)

我希望存储库处于独立的HEAD状态.出于所有意图和目的,我相信它是在这样做之后.但是,输出git branch看起来有点不同:

* (no branch)
  master
Run Code Online (Sandbox Code Playgroud)

有谁知道有什么区别,为什么会有差异,这意味着什么?

编辑:

下面是使用pygit2克隆存储库后的reflog,为repo.head分配提交SHA1哈希,然后运行git checkout master,然后运行git checkout myhash:

69df316 HEAD@{0}: checkout: moving from master to 69df3161f315e9b13ba4bd811635c11f67616598
d6ece61 HEAD@{1}: checkout: moving from 69df3161f315e9b13ba4bd811635c11f67616598 to master
69df316 HEAD@{2}:
d6ece61 HEAD@{3}: clone: from file:///path/to/repo
Run Code Online (Sandbox Code Playgroud)

git libgit2 git-branch pygit2

3
推荐指数
1
解决办法
724
查看次数

使用PyGitHub和PyGit2创建,克隆和推送到GitHub存储库

如何创建一个新的GitHub存储库,对其进行克隆,更改文件,然后使用python和pyGitHub和pyGit2库将其推回github?

这两个库的文档都很稀疏,几乎没有示例。

python pygit2 pygithub

3
推荐指数
1
解决办法
1824
查看次数

标签 统计

pygit2 ×9

git ×7

python ×6

libgit2 ×5

git-branch ×1

pygithub ×1