在源的两个Git版本之间有效地比较生成的代码 [英] Efficiently comparing generated code between two Git revisions of the source

查看:84
本文介绍了在源的两个Git版本之间有效地比较生成的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设答案为我应该将生成的代码存储在源代码控制中为否,那么什么时候比较所生成的代码以进行更改(预期或意外)的最佳方法是什么?

Assuming the answer to should I store generated code in source control is "no", what is the best way to compare the generated code for changes (expected or unexpected) when the source code is changed?

在这种情况下,


  • 所有开发人员都有在本地执行代码生成的工具

  • 代码生成速度快

  • 生成的文件很大

推荐答案

下面是一个Bash脚本,可以比较先前版本和当前版本之间的单个生成文件:

Here is a Bash script that can compare a single generated file between the previous and current revisions:

#!/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 stash@{0}:$FILENAME -- $FILENAME
#git difftool -R stash@{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

这篇关于在源的两个Git版本之间有效地比较生成的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆