如何在 git 中找出完整的 jenkins 构建而不是特定提交的所有更改文件的列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17979573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to find out list of all changed files in git for full jenkins build and not for a particular commit?
提问by user2571169
How to find all changed files since last commited build in GIT ? I want to build not only the changed files at head revision but also, all the changed files which got changed before that as well since last successful build.
如何查找自上次在 GIT 中提交构建以来所有更改的文件?我不仅要构建在头部修订时更改的文件,还要构建自上次成功构建以来在此之前更改的所有更改文件。
git show --pretty="format:" --name-only will do the build only for top commit changed files. Like this, i need to build for all changed files in different commits since last successful build.
git show --pretty="format:" --name-only 将仅对顶部提交更改的文件进行构建。像这样,我需要为自上次成功构建以来不同提交中的所有更改文件进行构建。
Like , last build done in Jenkins at X SHA-1 id, and after that , there are 3 more commits on top of it. So, my aim is to checkout whole repository codebase till head and then find out list of all files which got changed after X SHA-1 id , that are 3 commits on top of last commit at X SHA-1 id ?
比如,最后一次构建是在 Jenkins 中以 X SHA-1 id 完成的,在那之后,还有 3 次提交。所以,我的目标是检查整个存储库代码库直到 head ,然后找出在 X SHA-1 id 之后更改的所有文件的列表,即在 X SHA-1 id 的最后一次提交之上的 3 次提交?
Thanks
谢谢
回答by ford prefect
Little late to the party but a slightly better way is to use the --name-only
functionality on git diff. What I used is as follows:
聚会有点晚,但更好的方法是使用--name-only
git diff 上的功能。我使用的是以下内容:
git diff --name-only $GIT_PREVIOUS_COMMIT $GIT_COMMIT
This way you don't have to do some piping work afterward.
这样你以后就不必做一些管道工作了。
回答by Ian
According to https://wiki.jenkins-ci.org/display/JENKINS/Git+Plugin#GitPlugin-Environmentvariables, Jenkins provides the environment variables:
根据https://wiki.jenkins-ci.org/display/JENKINS/Git+Plugin#GitPlugin-Environmentvariables,Jenkins 提供了环境变量:
- GIT_COMMIT - SHA of the current
- GIT_PREVIOUS_COMMIT - SHA of the previous built commit from the same branch (the current SHA on first build in branch)
- GIT_COMMIT - 当前的 SHA
- GIT_PREVIOUS_COMMIT - 来自同一分支的先前构建提交的 SHA(第一个构建分支的当前 SHA)
I plan to leverage those to accomplish something similar.
我计划利用这些来完成类似的事情。
So what you'll want is something like:
所以你想要的是:
tar cvzf /tmp/build.tar.gz `git diff --stat $GIT_PREVIOUS_COMMIT $GIT_COMMIT | grep '\|' | awk '{print }'`
回答by Xiian
I'm not entirely sure I follow your question, but two things to consider:
我不完全确定我是否遵循您的问题,但需要考虑两件事:
git diff --stat {X SHA-1 ID}
should work for what you're looking for. Or, you could be more explicit and dogit diff --stat {X SHA-1 ID} {X SHA-1 ID + 3 commits}
which will give you the files changes between the two commits given. This will output something like:Library/ENV/4.3/cc | 3 ++- Library/Formula/cmu-sphinxbase.rb | 10 +++++++--- Library/Formula/fb-client.rb | 11 +++++++++++
I leave it as an exercise for the reader to parse that output into just a list of files.
You should probably be doing a full, clean build in Jenkins, and not only building certain files. You may end up getting caught by weird incompatibilities between built files. But that's beyond the scope of this question.
git diff --stat {X SHA-1 ID}
应该为你正在寻找的东西工作。或者,您可以更明确地执行git diff --stat {X SHA-1 ID} {X SHA-1 ID + 3 commits}
这会给您提供两次提交之间的文件更改。这将输出如下内容:Library/ENV/4.3/cc | 3 ++- Library/Formula/cmu-sphinxbase.rb | 10 +++++++--- Library/Formula/fb-client.rb | 11 +++++++++++
我把它作为练习让读者把输出解析成一个文件列表。
您可能应该在 Jenkins 中进行完整、干净的构建,而不仅仅是构建某些文件。您最终可能会被构建文件之间奇怪的不兼容性所困扰。但这超出了这个问题的范围。
---Edit with more info---
---编辑更多信息---
To break this problem down into parts:
把这个问题分解成几个部分:
- Get SHA1 hashes to work with- You need the current HEAD of repo (Checking out Revision, which we will call
$CUR_HASH
) and commit when Jenkins last built (Last Build Revision, which we will call$LAST_HASH
). It sounds like you already have these, and if not, that's sort of beyond the scope of the question. - Get list of files changed between
$LAST_HASH
and$CUR_HASH
- This is the aforementionedgit diff --stat $LAST_HASH $CUR_HASH
command, which will print out something like the above. Get justthe filenames from the output- doing this in bash, you could pipe the output of
git diff
togrep '\|'
to get justthe lines with filenames, and then pipe it toawk '{print $1}
to get justthe filename, without the stats. So the command is now something like:git diff --stat $LAST_HASH $CUR_HASH | grep '\|' | awk '{print }'
Create tarball from justthe files changed- you can send the output of the above to tar like so:
tar cvzf /tmp/build.tar.gz `git diff --stat $LAST_HASH $CUR_HASH | grep '\|' | awk '{print }'`
- 获取要使用的 SHA1 哈希- 您需要 repo 的当前 HEAD(检查修订,我们将称之为
$CUR_HASH
)并在 Jenkins 上次构建时提交(上次构建修订,我们将称之为$LAST_HASH
)。听起来你已经有了这些,如果没有,那有点超出了问题的范围。 - 获取在
$LAST_HASH
和之间更改的文件列表$CUR_HASH
- 这是前面提到的git diff --stat $LAST_HASH $CUR_HASH
命令,它将打印出类似上面的内容。 仅从输出中获取文件名- 在 bash 中执行此操作,您可以通过管道
git diff
传输grep '\|'
to的输出以仅获取带有文件名的行,然后通过管道将其传输awk '{print $1}
到仅获取文件名,而无需统计信息。所以命令现在是这样的:git diff --stat $LAST_HASH $CUR_HASH | grep '\|' | awk '{print }'
仅从更改的文件创建 tarball- 您可以将上述输出发送到 tar,如下所示:
tar cvzf /tmp/build.tar.gz `git diff --stat $LAST_HASH $CUR_HASH | grep '\|' | awk '{print }'`
I think that covers everything that you're trying to do. Obviously, these commands can't just be copy/pasted since I don't know everything about your environment, but it's a good start.
我认为这涵盖了您正在尝试做的所有事情。显然,这些命令不能只是复制/粘贴,因为我对您的环境一无所知,但这是一个好的开始。
回答by Yatharth7
Maybe this is what you are looking for:
也许这就是你要找的:
git whatchanged origin/master -n 1
This will tell you all changed files since last commited build.
这将告诉您自上次提交构建以来所有更改的文件。
To know more about git-whatchanged command: https://git-scm.com/docs/git-whatchanged/1.8.3
要了解有关 git-whatchanged 命令的更多信息:https://git-scm.com/docs/git-whatchanged/1.8.3
Also, if you want to know the changes of previous commit only, the modified command is:
另外,如果只想知道上次提交的变化,修改后的命令是:
git whatchanged -n 1
Hope this help.
希望这有帮助。
回答by Ahmed
If you need this for Jenkins.
如果你需要这个给詹金斯。
prev_merge=$(git log |grep -C1 Merge | grep commit |head -n1 | awk {'print '})
commit=$(git log |head -n 1 | awk {'print '})
git diff --name-only $prev_merge $commit