0

I'm using GitPython to initialize a new local repository, create the initial commit and push to a canonical repository. Unfortunately, the last step is failing and I'm having a lot of trouble understanding why. I'm sure I'm just using the GIT_SSH_COMMAND variable wrong, but I'm not sure how. There aren't many examples out there to go on.

I've read this SO question and dug into the relevant issue and commit, but I clearly haven't managed to put it together correctly.

"Proof" of Git v2.3+

$ git --version                                                                                                                          
git version 2.3.1

Script Snippet

# I've init'd the repo and any variables
# have been defined and initialized.
git_ssh_identity_file = os.path.expanduser('~/.ssh/id_rsa')
git_ssh_cmd = 'ssh -i %s' % git_ssh_identity_file
with git_project.git.custom_environment(GIT_SSH_COMMAND=git_ssh_cmd):
    git_project.remotes.origin.push(git_project.heads.master)

Resulting Error

Traceback (most recent call last):
  File "./ct-new-project.py", line 204, in <module>
    git_project.remotes.origin.push(git_project.heads.master)
  File "/Library/Python/2.7/site-packages/git/remote.py", line 667, in push
    return self._get_push_info(proc, progress or RemoteProgress())
  File "/Library/Python/2.7/site-packages/git/remote.py", line 588, in _get_push_info
    handle_process_output(proc, stdout_handler, progress_handler, finalize_process)
  File "/Library/Python/2.7/site-packages/git/cmd.py", line 202, in handle_process_output
    return finalizer(process)
  File "/Library/Python/2.7/site-packages/git/util.py", line 158, in finalize_process
    proc.wait()
  File "/Library/Python/2.7/site-packages/git/cmd.py", line 300, in wait
    raise GitCommandError(self.args, status, self.proc.stderr.read())
git.exc.GitCommandError: 'git push --porcelain origin master' returned with exit code 128
halfer
  • 19,824
  • 17
  • 99
  • 186
Rob Wilkerson
  • 40,476
  • 42
  • 137
  • 192
  • painfully obvious question, so I'm sure you've checked it ... but you never know. Does this work from the command line? i.e. `GIT_SSH_COMMAND='ssh -i ~/.ssh/id_rsa' git push --porcelain origin master`? Just in case there's something actually wrong with your repo. Or is `GIT_SSH_COMMAND` something specific to GitPython? – kdopen Mar 02 '15 at 16:35
  • I should have said 'some thing wrong with your repo status' (as in the push is failing for some reason unrelated to the SSH override). And yes, I've just checked and `GIT_SSH_COMMAND` is a `git` variable, not `GitPython` so you should be able to check from the command line. – kdopen Mar 02 '15 at 17:11
  • You know what? Yes and no. I tried, but it must've been in an earlier iteration where I had something else incorrect. In the meantime, my origin in `.git/config` got out of sync with the code I was trying to push and that was the actual problem at this point--I was trying to push to a repo that didn't actually exist. /punitive headslap – Rob Wilkerson Mar 02 '15 at 17:48
  • As "painfully obvious" as your question may've been, my brain cramp also makes it the answer, so you might as well add it as such and get some points. :-) Thanks for lending me that extra set of eyeballs I clearly needed. – Rob Wilkerson Mar 02 '15 at 17:49
  • Done :) At some time in the past, I did purgatory on a support desk. You have *no* idea how often the question "is it plugged in" generated a stunned silence from the caller .. followed by an expletive :) – kdopen Mar 02 '15 at 18:08

1 Answers1

2

When GitPython throws an error of this kind, it's always worth checking that the actual command it's trying to perform works from the command line. Something may have changed in your local clone that prevents the command completing successfully.

The equivalent of what you are trying to achieve with GitPython can be done with the following:

$ GIT_SSH_COMMAND='ssh -i ~/.ssh/id_rsa' git push --porcelain origin master

At least on Linux. On Windows you probably need to set the environment variable directly in a separate command.

When experimenting like this, I find it useful to have a "local" upstream I can push to somewhere on my hard drive so that I can throw it away and restart - or git push --force from a second (pristine) clone of the upstream .. because you just know you're going to mess it up at least once.

halfer
  • 19,824
  • 17
  • 99
  • 186
kdopen
  • 8,032
  • 7
  • 44
  • 52