Command executed via pexpect not taking effect

Viewed 61

I am trying to cherry-pick gerrit reviews using a python script. I already have the string required to cherry-pick a review. I have used pexpect to automate the password input step.

Here is an example. For each review I have to execute the following 2 commands:

  1. git fetch "ssh://myusername@my-gerrit.server.net:34343/project" refs/changes/45/255645/38
  2. git cherry-pick FETCH_HEAD

Only the first command asks for a password. Here is the code i have written:

child = pexpect.spawn('git fetch "ssh://myusername@my-gerrit.server.net:34343/project" refs/changes/45/255645/38')
child.logfile_read = sys.stdout
child.expect(r'Enter passphrase for key(.*?):', timeout=10)
child.sendline('mypwd')
child.expect([pexpect.TIMEOUT, 'host$', pexpect.EOF])

child.sendline('git cherry-pick FETCH_HEAD')
child.expect([pexpect.TIMEOUT, 'host$', pexpect.EOF])

When i run the script i dont see any error. However the second command (git cherry-pick FETCH_HEAD) doesnt seem to be executed. I say this because when i run these commands manually the second command throws a git merge conflict. When i run the script i dont see cherry-pick in progress (git status shows all clean). I tried some variants but cannot get it to work. Please help.

Thanks in advance

1 Answers

In order to add logs without modifying your script, use GIT_TRACE2 environment variables for tracing events.

export GIT_TRACE2=1
export GIT_TRACE2_EVENT=1

(or set, if you are on Windows)

Make also sure to launch your script with the same account you used to do manual testing.


If the second command does not seem to be executed, check if, as in this thread, the prompt is the issue:

It seems the problem was with the prompt I expected back.
If I change the expect to "child.expect('$ .*', timeout=15)", it works.

Related