paritybit.ca

A Git Workflow With Claws Mail

Author: Jake Bauer | Published: 2021-10-26

When sending or receiving patches to or from git repositories, I prefer to use git send-email. I find this workflow more efficient and less distracting than jumping to a web interface to create a pull request. Plus, it’s not dependent on having an account wherever a repository is hosted.

I used to use aerc as my email client, but have since switched to Claws Mail as I find TUIs with multiple windows, tabs, and panes to be awkward to use. However, aerc came with some really nice features for software development which aren’t available in Claws Mail. Watch the video at the link for aerc above to see those features in action.

Sending emails with git send-email is easy and switching to Claws Mail required no changes to my workflow because it does not interact with an email client at all. On the other hand, patches sent to me do end up in my email client so I needed a way to quickly and easily apply the patches I receive. Luckily, it’s fairly easy to do this with Claws Mail since it is quite extensible.

First, I needed to write a small script to parse the email and extract the repository name from the subject line so the patch can be applied to the correct repository. Then, the script cds into the repository and uses git am to apply the patch. Here is the script:

#!/bin/sh

projectsDir="$HOME/Documents/projects"
patchFile="/tmp/patch"

IFS=''
while read -r line; do
    echo "$line" >> "$patchFile"
    if echo -n "$line" | grep -q "^Subject:"; then
        repo=$(echo "$line" | grep -o '\[PATCH .*\]' | cut -d' ' -f2 | tr -d ']')
    fi
done

echo "Applying patch to: $projectsDir/$repo"
cd "$projectsDir"/"$repo" && git am "$patchFile"
rm "$patchFile"

There are two variables of note: projectsDir and patchFile. patchFile is simply a temporary file where the script keeps the contents of the email. projectsDir is the directory in which the script will look to find your repositories. If you keep your repositories in a different location (e.g. $HOME/Projects) then you should edit this variable to point to that location.

Another important note is that the script looks for the repository using the text that follows the word “PATCH” in the subject. When sending emails with git send-email, you should specify the project name after the “PATCH” text so the person to which you are emailing the patch knows at a glance which repository your patch is for. If this is done correctly, a subject line will look something like:

Subject: [PATCH aerc] Fix whitespace in documentation

The script is licensed under the Unlicense, by the way.

After writing the script, I needed to add an Action in Claws Mail. This can be done through the top menu by navigating to “Configuration > Actions…” and creating an action to run a shell command. Prefixing the shell command with the pipe | symbol will tell Claws Mail to pipe the contents of the body of the email on which the action is invoked to the command. This is what my action looks like:

A screenshot of the Actions configuration menu with an action configured with the menu name 'Git Apply Patch' and the Command '|git-apply-patch'.

I also bound this action to a custom keyboard shortcut so I can press a key combination which will apply the patch. After running a shell command, Claws Mail helpfully opens up a window displaying the output of the command so I can see whether or not the command failed and what went wrong.

Here is an example of me applying a patch where you can first see a successfully applied patch, followed by what it looks like when the patch fails to apply:

So there you have it: a clean and easy way to apply git patches using Claws Mail. It’s even faster than using aerc too, since I only have to type a single keyboard shortcut instead of typing out whole commands.