简体   繁体   中英

GIT - Get commit messages and diff from last pull

I have a project that contains GIT submodules. I'd like to gather the changes in that submodule into a text file and send that text file to a Jenkins email template.

Right now, I have this command, which gets me that changed files in that submodule:

git diff origin/master --stat >> fullLog.txt

And the generated file looks like this

 test.cs             | 8 +++++++-
 test1.cs            | 3 ++-
 2 files changed, 9 insertions(+), 2 deletions(-)

Ideally I would like the commit messages that we're just pulled but looking at the git-log documentation I would have to know which commit to limit from. Right now when I do git log -p I get every message since the start of the project.

My desired end result would look something like this:

Commit a2a4cd6a72892d7a6f0f6e2097d8bf13826a56c3 by jsmith
test-111: clears cache

Commit c58a86c8ebc7153d65c0bfdf0d8e5f92be571b9f by psmith
test-112: Changed setting

 test.cs             | 8 +++++++-
 test1.cs            | 3 ++-
 2 files changed, 9 insertions(+), 2 deletions(-)

If you know you just pulled AND the pull actually had new commits, then you can make use of the reflog to find out what the state of your HEAD was right before the pull, and use that as an argument to git log. The state of HEAD before the last update to HEAD is HEAD@{1} , so this might work:

git log HEAD@{1}.. --stat

You can run git reflog to see the reflog itself. Note that the reflog is not updated if a git pull did not pull in any new commits for the checked-out branch.

I assume you're running this in a script, so a solution that would work regardless of whether there were changes pulled or not is to save the previous state of origin/master before pulling.

prev_master=`git rev-parse origin/master`
git pull
new_master=`git rev-parse origin/master`
if [[ $prev_master == $new_master ]]; then
   #nothing pulled
else
   git log $prev_master..$new_master --stat
fi

And if you want the file stats globally but the log commit by commit, you could use these two commands instead of the single git log line:

git log $prev_master..$new_master
git diff --stat $prev_master..$new_master

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM