1

I created the following pre-push script in my local repository .git/hooks/ to disallow pushing files with unresolved git conflicts to server:

#!/bin/bash
top_dir=$(git rev-parse --show-toplevel)
conflicts=$(grep -r -n -s --include=\*.{pm,pl,mc,mi,js,css} "<<<<<<< HEAD" "$top_dir")

if [ "$conflicts" = "" ]; then
    exit 0
else
    printf "Unresolved git conflict found, commit rejected.\n\n"
    echo "$conflicts"
    exit 1
fi

It works. But how can we reject all the commits with unresolved conflicts on server side pre-receive? Here I know top_dir - the dir, where to grep. But on server there are no saved files saved. I need to grep among the files that the client sends me.

How to do it?

user4035
  • 22,508
  • 11
  • 59
  • 94

1 Answers1

1

As torek documents in "pre-receive hook unable to read the committed file to push into remote master":

Pre-receive hooks are generally more difficult to write as you must handle many cases:

  • multiple commits
  • references that are not branches (tags)
  • objects that are not commits (annotated tags)
  • branch creations and deletions as well as updates

That kind of hook works well for commit messages.
That being said, you can see an example here, in the nominal case:

# <oldrev> <newrev> <refname>
while  read oldrev newrev ref ;
do
    list = $ ( git show --pretty = " format: " --name-only $ {newrev}  | grep -e ' .php ' -e ' .phtml ' )
    for  file  in  $ {list} ;  do
        git show $ {newrev} : $ {file}  >  $ TMP_FILE
        OUTPUT = $ ( $ PHPCS_BIN -s --standard = $ PHPCS_CODING_STANDARD  $ TMP_FILE )
        ...
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250