2016-07-26 131 views
11

我創建了github.com新的存儲庫,然後將其與如何將新文件推送到GitHub?

git clone https://github.com/usrname/mathematics.git 

克隆到我的本地機器我增加3個新的文件的文件夾下,現在mathematics

$ tree 
. 
├── LICENSE 
├── numerical_analysis 
│   └── regression_analysis 
│    ├── simple_regression_analysis.md 
│    ├── simple_regression_analysis.png 
│    └── simple_regression_analysis.py 

,我想上傳3個新文件到我的GitHub使用Python,更具體地說,PyGithub。這是我曾嘗試:

#!/usr/bin/env python 
# *-* coding: utf-8 *-* 
from github import Github 

def main(): 
    # Step 1: Create a Github instance: 
    g = Github("usrname", "passwd") 
    repo = g.get_user().get_repo('mathematics') 

    # Step 2: Prepare files to upload to GitHub 
    files = ['mathematics/numerical_analysis/regression_analysis/simple_regression_analysis.py', 'mathematics/numerical_analysis/regression_analysis/simple_regression_analysis.png'] 

    # Step 3: Make a commit and push 
    commit_message = 'Add simple regression analysis' 

    tree = repo.get_git_tree(sha) 
    repo.create_git_commit(commit_message, tree, []) 
    repo.push() 

if __name__ == '__main__': 
    main() 

我不知道

  • 如何得到字符串sharepo.get_git_tree
  • 我怎麼做的步驟2和3之間,即連接推具體文件

個人而言,PyGithub documentation是不可讀的。尋找很長時間後,我無法找到正確的api。

+1

爲了得到'沙'你需要使用'hashlib' –

+5

@WayneWerner這絕對*不*他應該做什麼。 'sha'是由'git'計算的,如果你自己計算一下,你肯定會錯誤的。 –

+0

@BrianMalehorn我有一個bash腳本,可以通過CURL和github API上傳我的git提交,IIRC - 它不是*那*不好。 –

回答

7

我試圖用GitHub API提交多個文件。 Git Data API的這個頁面說它應該「非常簡單」。調查結果見this answer

我建議使用類似GitPython

from git import Repo 

repo_dir = 'mathematics' 
repo = Repo(repo_dir) 
file_list = [ 
    'numerical_analysis/regression_analysis/simple_regression_analysis.py', 
    'numerical_analysis/regression_analysis/simple_regression_analysis.png' 
] 
commit_message = 'Add simple regression analysis' 
repo.index.add(file_list) 
repo.index.commit(commit_message) 
origin = repo.remote('origin') 
origin.push() 

注:這個腳本的版本在倉庫中的父目錄中運行。

0
import subprocess 
p = subprocess.Popen("git rev-parse HEAD".split(), stdout=subprocess.PIPE) 
out, err = p.communicate() 
sha = out.strip() 

可能有一種方法可以用PyGithub做到這一點,但是這應該適用於快速入侵。

1

我可以給你一些信息支持,也是一個具體的解決方案。

Here你可以找到添加新文件到你的倉庫的例子,here是一個視頻教程。

但你也可以在IPython的命令,把你的文件,如果你需要:

In [1]: import subprocess 
In [2]: print subprocess.check_output('git init', shell=True) 
Initialized empty Git repository in /home/code/.git/ 
In [3]: print subprocess.check_output('git add .', shell=True) 
In [4]: print subprocess.check_output('git commit -m "a commit"', shell=True) 
1

如果PyGithub的文檔不可用(而且看起來並不如此),並且您只是想推送一個提交(對問題和回購配置等沒有做任何事情),那麼您直接可能會更好與git連接,要麼調用git可執行文件,要麼使用包裝庫,如GitPython

使用git直接與subprocess.Popen等東西,你提到可能會更容易的傾斜曲線,但也更難以處理錯誤等長期來看,因爲你真的沒有很好的抽象來傳遞,並且必須自己進行解析。

擺脫PyGithub也使您免於被綁定到GitHub及其API,使您可以推送到任何回購站,甚至是計算機上的其他文件夾。

1

注意:此版本的腳本是從GIT存儲庫中調用的,因爲我從文件路徑中刪除了存儲庫名稱。

我終於想通了如何使用PyGithub提交多個文件:

import base64 
from github import Github 
from github import InputGitTreeElement 

token = '5bf1fd927dfb8679496a2e6cf00cbe50c1c87145' 
g = Github(token) 
repo = g.get_user().get_repo('mathematics') 
file_list = [ 
    'numerical_analysis/regression_analysis/simple_regression_analysis.png', 
    'numerical_analysis/regression_analysis/simple_regression_analysis.py' 
] 
commit_message = 'Add simple regression analysis' 
master_ref = repo.get_git_ref('heads/master') 
master_sha = master_ref.object.sha 
base_tree = repo.get_git_tree(master_sha) 
element_list = list() 
for entry in file_list: 
    with open(entry, 'rb') as input_file: 
     data = input_file.read() 
    if entry.endswith('.png'): 
     data = base64.b64encode(data) 
    element = InputGitTreeElement(entry, '100644', 'blob', data) 
    element_list.append(element) 
tree = repo.create_git_tree(element_list, base_tree) 
parent = repo.get_git_commit(master_sha) 
commit = repo.create_git_commit(commit_message, tree, [parent]) 
master_ref.edit(commit.sha) 
""" An egregious hack to change the PNG contents after the commit """ 
for entry in file_list: 
    with open(entry, 'rb') as input_file: 
     data = input_file.read() 
    if entry.endswith('.png'): 
     old_file = repo.get_contents(entry) 
     commit = repo.update_file('/' + entry, 'Update PNG content', data, old_file.sha) 

如果我嘗試從一個PNG文件添加的原始數據,調用create_git_tree最終調用Requester.pyjson.dumps,這將導致要提出以下異常:

UnicodeDecodeError: 'utf8' codec can't decode byte 0x89 in position 0: invalid start byte

我通過對base64編碼PNG數據並提交它來解決此問題。之後,我使用update_file方法更改PNG數據。這會導致對存儲庫的兩次單獨提交,這可能不是您想要的。