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 chang
If you need this for Jenkins.
prev_merge=$(git log |grep -C1 Merge | grep commit |head -n1 | awk {'print $2'})
commit=$(git log |head -n 1 | awk {'print $2'})
git diff --name-only $prev_merge $commit
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
Also, if you want to know the changes of previous commit only, the modified command is:
git whatchanged -n 1
Hope this help.
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 do git 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.
To break this problem down into parts:
$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.$LAST_HASH
and $CUR_HASH
- This is the aforementioned git diff --stat $LAST_HASH $CUR_HASH
command, which will print out something like the above.Get just the filenames from the output - doing this in bash, you could pipe the output of git diff
to grep '\|'
to get just the lines with filenames, and then pipe it to awk '{print $1}
to get just the filename, without the stats. So the command is now something like:
git diff --stat $LAST_HASH $CUR_HASH | grep '\|' | awk '{print $1}'
Create tarball from just the 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 $1}'`
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.
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:
git diff --name-only $GIT_PREVIOUS_COMMIT $GIT_COMMIT
This way you don't have to do some piping work afterward.
According to https://wiki.jenkins-ci.org/display/JENKINS/Git+Plugin#GitPlugin-Environmentvariables, Jenkins provides the environment variables:
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 $1}'`