0

Could you please show me how to implement git hook?

Before committing, the hook should run a python script. Something like this:

cd c:\my_framework & run_tests.py --project Proxy-Tests\Aeries \
   --client Aeries --suite <Commit_file_Name> --dryrun

If the dry run fails then commit should be stopped.

Joel Peltonen
  • 13,025
  • 6
  • 64
  • 100
Lakshmi narayana
  • 184
  • 1
  • 3
  • 10

2 Answers2

1

You need to tell us in what way the dry run will fail. Will there be an output .txt with errors? Will there be an error displayed on terminal?

In any case you must name the pre-commit script as pre-commit and save it in .git/hooks/ directory.

Since your dry run script seems to be in a different path than the pre-commit script, here's an example that finds and runs your script.

I assume from the backslash in your path that you are on a windows machine and I also assume that your dry-run script is contained in the same project where you have git installed and in a folder called tools (of course you can change this to your actual folder).

#!/bin/sh

#Path of your python script
FILE_PATH=tools/run_tests.py/

#Get relative path of the root directory of the project
rdir=`git rev-parse --git-dir`
rel_path="$(dirname "$rdir")"

#Cd to that path and run the file. 
cd $rel_path/$FILE_PATH
echo "Running dryrun script..."
python run_tests.py

#From that point on you need to handle the dry run error/s.
#For demonstrating purproses I'll asume that an output.txt file that holds
#the result is produced.

#Extract the result from the output file
final_res="tac output | grep -m 1 . | grep 'error'"

echo -e "--------Dry run result---------\n"${final_res}

#If a warning and/or error exists abort the commit
eval "$final_res" |  while read -r line; do
if [ $line != "0" ]; then
  echo -e "Dry run failed.\nAborting commit..."
  exit 1
fi
done

Now every time you fire git commit -m the pre-commit script will run the dry run file and abort the commit if any errors have occured, keeping your files in the stagin area.

kingJulian
  • 5,601
  • 5
  • 17
  • 30
  • Thanks kingJulian . So much grateful to the reply. On to your questions- Yes, outpul xml is created which will be having - status like 1 critical test, 0 passed, 1 failed. Or console log will be having "[ ERROR ]" in either case commit should fail – Lakshmi narayana Dec 21 '17 at 10:26
  • Hi kingJulian , could you please get me the logic for multiple file commits ? – Lakshmi narayana Dec 21 '17 at 10:33
  • You should then parse the xml file and search for failed. If failed is preceded by any number other than 0 then the commit must be aborted. When you want to have multiple files commited, you should add them in staging area by `git add file1, file2, etc` and then perfrom a single commit with `git commit -m "". Then the pre-commit hook will normally run. – kingJulian Dec 21 '17 at 10:48
  • Hi KingJulian, if I have file1,file2 and file3 to be committed then above logic should be called for thrice ? ie., for each file level. Where I'm bothering is - above mentioned logic , how should it be run for each file ? – Lakshmi narayana Dec 21 '17 at 13:07
  • 1
    The above script executes the dry run script you mentioned and then searches for any errors in the output file. It doesn't have any logic as to **how** your dry run script must be ran (i.e. per file or for all files). It is up to you to define how you want the dry run script to be ran by modifying accordingly the `python run_tests.py` line. – kingJulian Dec 21 '17 at 13:56
0

I have implemented this in my hook. Here is the code snippet.

#!/bin/sh
#Path of your python script

RUN_TESTS="run_tests.py"
FRAMEWORK_DIR="/my-framework/"
CUR_DIR=`echo ${PWD##*/}`

`$`#Get full path of the root directory of the project under RUN_TESTS_PY_FILE

rDIR=`git rev-parse --git-dir --show-toplevel | head -2 | tail -1`
OneStepBack=/../
CD_FRAMEWORK_DIR="$rDIR$OneStepBack$FRAMEWORK_DIR"

#Find list of modified files - to be committed
LIST_OF_FILES=`git status --porcelain | awk -F" " '{print $2}' | grep ".txt" `

for FILE in $LIST_OF_FILES; do
    cd $CD_FRAMEWORK_DIR
        python $RUN_TESTS  --dryrun   --project $CUR_DIR/$FILE
    OUT=$?

    if [ $OUT -eq 0 ];then
        continue
    else
        return 1
    fi
done
Joel Peltonen
  • 13,025
  • 6
  • 64
  • 100
Lakshmi narayana
  • 184
  • 1
  • 3
  • 10