2016-09-29 78 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来解释问题。