Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list the commits of a patch file?

I have a patch file (which somebody made with git format-patch HEAD~3 HEAD --stdout > his_last_3_commits.patch following more or less that site)

I would like to know how to list the content (I mean here the title of the commits) which are in that his_last_3_commits.patch ?

like image 515
Anthony O. Avatar asked Oct 24 '25 04:10

Anthony O.


1 Answers

Given that the output of git format-patch is in mbox format, a mail client can do what you are looking for. Point it to the patch file and you'll have each commit as a message. From the command line, something like mail -H -f "$file" may do.

If you are on a git repo (even if you just go to an empty directory and run git init) you can "apply" the patchset excluding everything. You'd change nothing and as a side effect get the list of patches: git am --exclude='*' "$file".

If you want to run it everywhere, you'll have to extract the subject lines and unencode them, as you've found out. The "gitty" command would be git mailinfo, but it works on a message at a time, so not sure it's an advantage over other options like python or your php line. You can count and extract each message:

msg_count=$(grep -c '^From ' "$1")
for (( i=1 ; i<=msg_count ; i++ )); do
  awk "/^From /{msg+=1}msg==$i" "$1" | git mailinfo /dev/null /dev/null | sed -n 's/^Subject: //p'
done

Or, better yet, kudos to Kamil Maciorowski for his answer on how to split a file and run a command on each part, you can have something you can pipe the patchfile into and get the titles out of:

set -o pipefail
while ! sed -n '/^From /q1;p' | git mailinfo /dev/null /dev/null | sed -n 's/^Subject: //p'; do
  :
done
like image 75
Máximo Castañeda Avatar answered Oct 26 '25 17:10

Máximo Castañeda