2016-06-15 13 views
1

レポ(GitPython) - どのように新しいローカルブランチを作成し、いくつかのファイルを追加し、GitPythonを使ってリモートにプッシュできますか?GitPythonを使用して新しいブランチをチェックアウトし、リモートにプッシュ

レポ例:

def publish_changes_to_git(commit_msg): 
    curr_time = time.time() 
    ts = datetime.datetime.fromtimestamp(curr_time).strftime('%Y-%m-%d-%H-%M-%S') 
    branch_name = "auto-commit-{ts}".format(ts=ts) 
    subprocess.check_output(["git", "checkout", "-b", branch_name]) 
    subprocess.check_output(["git", "add", SOME_PATH]) 
    subprocess.check_output(
     ["git", "commit", "-m", "auto-git-commit: {msg}".format(msg=commit_msg)]) 

誰かが他のgit-pythonのライブラリを持っている場合、私は聞いて幸せになるでしょう:今私はちょうどサブプロセスを使用しているため

from git import * 

curr_dir = os.path.dirname(os.path.realpath(__file__)) 
repo = Repo(curr_dir) 

答えて

0

gitypythonは、gitへの低レベルアクセスとレベルアクセスの両方を提供するようですが、gitコマンドを呼び出すことができます。http://gitpython.readthedocs.io/en/stable/reference.html#module-git.cmdを参照してください。

代わりに、http://www.pygit2.org/の代わりに、より高いレベルのアクセスのためにhttp://www.pygit2.org/を使用することを検討してください。いくつかの例がここにありますhttp://www.pygit2.org/recipes.html

+0

ありがとうが、gitモジュールを使って正確にコマンドはサブプロセスと全く同じです。病気は深くpygit2に行きますが、モジュールは彼が私に必要なものを欠いているようです(またはそれらを達成するのが非常に難しい) – etlsh

0

私は、リモートブランチのリモートブランチにtxtを作成し、コミットしてからコミットし、リモートにプッシュするようなことをしました。ここに私のコードです

import git 
import datetime 
import os 
from time import * 
from os import path 
from git import Repo 

def commit_files(): 
    if repo != None: 
     new_branch = 'your_new_branch' 
     current = repo.create_head(new_branch) 
     current.checkout() 
     master = self.repo.heads.master 
     repo.git.pull('origin', master) 
     #creating file 
     dtime = strftime('%d-%m-%Y %H:%M:%S', localtime()) 
     with open(self.local_repo_path + path.sep + 'lastCommit' + '.txt', 'w') as f: 
      f.write(str(dtime)) 
     if not path.exists(self.local_repo_path): 
      os.makedirs(self.local_repo_path) 
     print('file created---------------------') 

     if repo.index.diff(None) or repo.untracked_files: 

      repo.git.add(A=True) 
      repo.git.commit(m='msg') 
      repo.git.push('--set-upstream', 'origin', current) 
      print('git push') 
     else: 
      print('no changes') 
関連する問題