Showing posts with label git. Show all posts
Showing posts with label git. Show all posts

Tuesday, December 27, 2016

Adding Reviewed-by and Acked-by Tags with Git

This week's "Git Rocks!" moment came while I was investigating how I could automatically add Reviewed-by, Acked-by, Tested-by, etc. tags to a given commit message.

Git's interpret-trailers command is capable of testing for and manipulating arbitrary Key: Value tags in commit messages.

For example, appending Reviewed-by: MY NAME <my@email.com> to the top commit message is as simple as running:

> GIT_EDITOR='git interpret-trailers --trailer \
 "Reviewed-by: $(git config user.name) <$(git config user.email)>" \
 --in-place' git commit --amend 

Or with the help of a "git rb" alias, via:
> git config alias.rb "interpret-trailers --trailer \
 \"Reviewed-by: $(git config user.name) <$(git config user.email)>\" \
 --in-place"
> GIT_EDITOR="git rb" git commit --amend

The above examples work by replacing the normal git commit editor with a call to git interpret-trailers, which appends the desired tag to the commit message and then exits.

My specific use case is to add Reviewed-by: tags to specific commits during interactive rebase, e.g.:
> git rebase --interactive HEAD~3

This brings up an editor with a list of the top three commits in the current branch. Assuming the aforementioned rb alias has been configured, individual commits will be given a Reviewed-by tag when appended with the following line:

exec GIT_EDITOR="git rb" git commit --amend

As an example, the following will see three commits applied, with the commit message for two of them (d9e994e and 5f8c115) appended with my Reviewed-by tag.

pick d9e994e ctdb: Fix CID 1398179 Argument cannot be negative
exec GIT_EDITOR="git rb" git commit --amend
pick 0fb313c ctdb: Fix CID 1398178 Argument cannot be negative
#    ^^^^^^^ don't add a Reviewed-by tag for this one just yet 
pick 5f8c115 ctdb: Fix CID 1398175 Dereference after null check
exec GIT_EDITOR="git rb" git commit --amend

Bonus: By default, the vim editor includes git rebase --interactive syntax highlighting and key-bindings - if you press K while hovering over a commit hash (e.g. d9e994e from above), vim will call git show <commit-hash>, making reviewing and tagging even faster!



Note taking: Arbitrary notes can also be appended to commits using the same technique. E.g. From the git interactive rebase editor:

pick 12dd8972f6e fix build
x GIT_EDITOR='git interpret-trailers --trailer "TODO-ddiss: squash with prior" --in-place' git commit --amend

Thanks to:
  • Upstream Git developers, especially those who implemented the interpret-trailers functionality.
  • My employer, SUSE.

Update 20190123:
  • Add commit message note taking example

Monday, December 30, 2013

Git send-email PATCH version subject prefix

I use git send-email to submit patches to developer mailing lists. A reviewer may request a series of changes, in which case I find it easiest to make and test those changes locally, before sending a new round of patches to the mailing list with a new version number:

git send-email --subject-prefix="PATCH v4" --compose -14

Assigning a version number to each round of patches allows me to add a change log for the entire patch-set to the introductory mail, e.g.:
From: David Disseldorp 
Subject: [PATCH v4 00/14] add compression ioctl support

This patch series adds support for the FSCTL_GET_COMPRESSION and
FSCTL_SET_COMPRESSION ioctls, as well as the reporting of the current
compression state via the FILE_ATTRIBUTE_COMPRESSED flag.

Hooks are added to the Btrfs VFS module, which translates such requests
into corresponding FS_IOC_GETFLAGS and FS_IOC_SETFLAGS ioctls.

Changes since v3 (thanks for the feedback Jeremy):
- fixed and split copy-chunk dest unlock change into separate commit

Changes since v2:
- Check for valid fsp file descriptors
- Rebase atop filesystem specific selftest changes
- Change compression fsctl permission checks to match Windows behaviour
  + Add corresponding smbtorture test

Changes since v1:
- Only use smb_fname and fsp args with GET_COMPRESSION. The smb_fname
  argument is only needed for the dosmode() code-path, fsp is used
  everywhere else.
- Add an extra SetInfo(FILE_ATTRIBUTE_COMPRESSED) test.

GIT: [PATCH v4 01/14] selftest/s3: expose share with FS applicable config
...

Change logs can also be added to individual patches using the --annotate parameter:

git send-email --annotate --subject-prefix="PATCH v2" -1
 
 Subject: [PATCH v2] common: add CephFS support
...
---
Changes since version 1:
- Remove -ceph parameter for check and rely on $FSTYP instead
...
diff --git a/README.config-sections b/README.config-sections

Putting the change log between the first "---" and "diff --git a/..." lines ensures that it will be dropped when applying the patch via git-am.

[update 2016-11-03]
- Describe the --annotate parameter