0

I have the following code where I want to push changes in my branch to gerrit using GitpYthon.

repo_path_new = repo_path+repo_name
repo_obj = Repo(repo_path_new)
os.chdir(repo_path_new)
repo_obj.git.add(A=True)
if commit_command_line(commit_message, repo_obj, repo_name):
    repo_obj.git.push("origin", "HEAD:refs/for/master")

It adds and commits the file(s), but when I want to push to refs/for/master I get an issue:

remote: Processing changes: refs: 1, done
remote: ERROR: [0f7c907] missing Change-Id in commit message footer
remote:
remote: Hint: To automatically insert Change-Id, install the hook:
remote:   gitdir=$(git rev-parse --git-dir); scp -p -P port 
username@url:hooks/commit-msg ${gitdir}/hooks/
remote: And then amend the commit:
remote:   git commit --amend
remote:
 To ssh://url:port/repo-name
 ! [remote rejected]   HEAD -> refs/for/master ([0f7c907] missing Change-Id in commit message footer)
error: failed to push some refs to 'ssh://url:port/repo-name''

Do note that I am standing in the cloned repo. Of course I can install the git hook change-id manually in git bash. But since this is supposed to be done automatically I want to know if there is a way to do that via gitpython.

user535081
  • 13
  • 7

1 Answers1

0

Did you manually clone the repository or it was done through gitpython? Note that you only need to install the commit-msg hook once (the first time you clone the repository), after that all commits will have the Change-Id automatically.

If you access Gerrit through SSH run:

$ gitdir=$(git rev-parse --git-dir); scp -p -P 29418 GERRIT-SERVER:hooks/commit-msg ${gitdir}/hooks

If you access Gerrit throught HTTPS run:

$ gitdir=$(git rev-parse --git-dir); curl --create-dirs -Lo ${gitdir}/hooks/commit-msg https://GERRIT-SERVER/tools/hooks/commit-msg

You can execute it manually after you clone the repository or you can add this to the gitpython.

  • I manually cloned the repository. When I install the way you said it works, but since this script will be run in the Go cd (just like Jenkins) there is no way to tell that the commit-msg hook exist. In my code I call there first option you gave me and it fails on gitdir (can't find it). – user535081 Jul 18 '17 at 08:20
  • Can you set go-cd to execute a bash script (with the commands to install the commit-msg hook) just after it clone the repository and before it execute the gitpython script? – Marcelo Ávila de Oliveira Jul 18 '17 at 23:17