2016-09-29 381 views
2

我試圖使用gitpython模塊向上推新的git倉庫。下面是我做的步驟,並得到一個錯誤128gitpython返回'git push --porcelain origin'返回退出代碼128

# Initialize a local git repo 
init_repo = Repo.init(gitlocalrepodir+"%s" %(gitinitrepo)) 

# Add a file to this new local git repo 
init_repo.index.add([filename]) 

# Initial commit 
init_repo.index.commit('Initial Commit - %s' %(timestr)) 

# Create remote 
init_repo.create_remote('origin', giturl+gitinitrepo+'.git') 

# Push upstream (Origin) 
init_repo.remotes.origin.push() 

當執行推(),gitpython拋出一個異常:

'git push --porcelain origin' returned with exit code 128 

訪問github上是通過SSH。

你看到我在做什麼不對嗎?

回答

0

您需要捕獲git命令的輸出。

鑑於這種進步類:

class Progress(git.RemoteProgress): 
    def __init__(self, progress_call_back): 
     self.progress_call_back = progress_call_back 
     super().__init__() 

     self.__all_dropped_lines = [] 

    def update(self, op_code, cur_count, max_count=None, message=''): 
     pass 

    def line_dropped(self, line): 
     if line.startswith('POST git-upload-pack'): 
      return 

     self.__all_dropped_lines.append(line) 

    def allErrorLines(self): 
     return self.error_lines() + self.__all_dropped_lines 

    def allDroppedLines(self): 
     return self.__all_dropped_lines 

你可以這樣寫代碼:

progress = Progress() 

try: 
    for info in remote.push(progress=progress): 
     info_callback(info) 

    for line in progress.allDroppedLines(): 
     log.info(line) 

except GitCommandError: 
    for line in progress.allErrorLines(): 
     log.error(line) 

    raise 

當你與此運行,你仍然會得到128錯誤,但你也將有輸出og git來解釋問題。