2013-05-25 2 views
1

Предполагая, что ответ на should I store generated code in source control «нет», что является лучшим способом для compare the generated code for changes (ожидаемый или неожиданный) при изменении исходного кода?Эффективное сравнение сгенерированного кода между двумя версиями Git исходного кода

В этом случае

  • Все разработчики имеют средства для выполнения генерации кода локально
  • Генерация кода быстро
  • Создаваемые файлы большого

ответ

1

Вот Bash скрипт, который может сравнивать один сгенерированный файл между предыдущими и текущими версиями:

#!/bin/bash -x 

# Compares a generated file between the current revision and an old revision. 
# 
# XXX We do not use the "-e" Bash option if our intent is merely to 
# inspect the changes in generated code, because the "diff" command 
# will return "1" if there are changes, terminating this script early. 
# 
# If, however, our intent is to use this script as a pre-commit hook 
# to ensure that the generated code does not change, we should 
# enable the "-e" flag and add the "--quiet" option to the diff command 
# (which implies the "--exit-code" option, according to the docs). 


# XXX Note: the leading "./" to indicate a current-directory-relative path 
# is important for the diff command below. 
FILENAME=./hellofile.txt 
OLD_REVISION=HEAD~ 

# TODO Replace this with any "build step", e.g. "make <target>" 
FILE_PRODUCTION_COMMAND='eval echo "hello $(git rev-parse --short HEAD)" > $FILENAME' 

# Stash the entire working tree, including build artifacts 
git stash --all 

# Produce the modern version of the derived content to be compared 
$FILE_PRODUCTION_COMMAND 

# Mark the file as something that should be saved in the stash 
# The --force command is for in case the file has been marked in the .gitignore file 
git add --force $FILENAME 

# Preserve the file for later comparison 
git stash 

# Move to the old version of the source code 
git checkout $OLD_REVISION 

# Produce the old version of the derived content to be compared 
$FILE_PRODUCTION_COMMAND 

# Launch a textual or graphical diff viewer to see the changes in the derived content 
# We use the "-R" option because we have stashed the newer content. 
git diff -R [email protected]{0}:$FILENAME -- $FILENAME 
#git difftool -R [email protected]{0}:$FILENAME -- $FILENAME 

# Discard the most recent stash 
git stash drop 

# Clean everything, including ignored files; leaves clone totally pristine 
git clean -fxd $(git rev-parse --show-toplevel) 

# Go back to modern version of the source code 
git checkout - 

# Restore our original working tree, including build artifacts 
git stash pop
Смежные вопросы