how to find out list of all changed files in git for full jenkins build and not for a particular commit?

前端 未结 5 1649
梦毁少年i
梦毁少年i 2020-12-05 19:57

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

相关标签:
5条回答
  • 2020-12-05 20:13

    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
    
    0 讨论(0)
  • 2020-12-05 20:15

    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.

    0 讨论(0)
  • 2020-12-05 20:20

    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.

    ---Edit with more info---

    To break this problem down into parts:

    1. 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.
    2. Get list of files changed between $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.
    3. 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}'
      
    4. 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.

    0 讨论(0)
  • 2020-12-05 20:36

    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.

    0 讨论(0)
  • 2020-12-05 20:37

    According to https://wiki.jenkins-ci.org/display/JENKINS/Git+Plugin#GitPlugin-Environmentvariables, Jenkins provides the environment variables:

    • 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)

    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}'`
    
    0 讨论(0)
提交回复
热议问题