如何PyGithub
问题描述:
合并分支到主例如,我有一个文件t.json,内容是:如何PyGithub
{
"a": "abcdefg"
}
和文件t.json被推到掌握分支。然后我一些内容到文件,并检出添加到一个新的分支,因此文件看起来像现在这样:
{
"a": "abcdefg",
"b": "mkjuujj"
}
现在我可以用PyGithub比较两次提交。代码是这样的:
WORKING_BRANCH = "my_new_branch"
new_branch_ref_str = "refs/heads/%s" % WORKING_BRANCH
branch_ref = None
all_ref = repo.get_git_refs()
for ref in all_ref:
if ref.ref == new_branch_ref_str:
branch_ref = ref
break
if not branch_ref:
# create branch from this commit
b = repo.get_branch("master")
branch_ref = repo.create_git_ref(ref=new_branch_ref_str,
sha=b.commit.sha)
last_head = repo.get_branch(WORKING_BRANCH)
fc = repo.get_file_contents("/t.json", ref=WORKING_BRANCH)
file = 't.json'
commit_message = "create a new branch with changes"
input_file = open(file, 'rb')
data = input_file.read()
result = repo.update_file("/t.json",
commit_message,
data,
fc.sha, branch=WORKING_BRANCH)
diff_url = repo.compare(last_head.commit.sha,
result['commit'].sha)
print diff_url.diff_url
这是我得到:
diff --git a/t.json b/t.json
index ef03bf5..b775e51 100644
--- a/t.json
+++ b/t.json
@@ -1,3 +1,4 @@
{
- "a": "abcdefg"
+ "a": "abcdefg",
+ "b": "mkjuujj"
}
我应该怎么做,通过使用PyGithub合并my_new_branch到主分支。非常感谢你。真的很感激它。
答
try:
base = repo.get_branch("master")
head = repo.get_branch(WORKING_BRANCH)
merge_to_master = repo.merge("master",
head.commit.sha, "merge to master")
except Exception as ex:
print ex