Open all files from the last commit in your editor
My team is currently using Gerrit for code review. Usually when a review is done, I'd need to pull down the change and edit a couple of the files in the commit based on the review that was given.
Here's my current workflow,
# download the change $ git review -d <id> # find and edit files in the last commit $ atom # commit updates # --no-edit means don't edit the commit message # though sometimes I may need to $ git commit -a --amend --no-edit # push new changes for further review $ git review -f dev
Usually it's a hassle to find all the files that may need editing from the last commit. But, I've been able to concoct a really nice solution.
According to this Stack Overflow answer, you can list all the files that were part of a given commit using
git diff-tree --no-commit-id --name-only -r <commit-hash>
And I recently learned a way to show the abbreviated commit hash of the most recent commit using
git log --pretty="%h" -n1
Combining the two I can now easily list the files that were in the last commit using
git diff-tree --no-commit-id --name-only -r `git log --pretty="%h" -n1`
Which would give the following output for example,
The only remaining problem is how to get my editor to open up the list of files as given by the previous output. Turns out xargs is pretty neat and can handle precisely this problem (and a lot more). See here for examples of xargs in action.
Essentially, piping the output of the previous command through xargs would give
And instructing xargs to execute the atom -n command on the input would yield the result we want.
git diff-tree --no-commit-id --name-only -r `git log --pretty="%h" -n1` | xargs atom -n
will open all the files from the last commit in Atom.
Written by Dwayne Crooks.