Categories
Development

A Command Line Reminder for Better Commit Messages

The quality of my commit messages has been inconsistent for too long now. I can write good commit messages, but I still fail to do so about thirty percent of the time.

Bad commit messages are short and non-descriptive. They’re quick to fire off in the moment, but when reviewing later, they’re essentially useless for knowing what was committed. You’ve got to open the diff and read the commit’s code directly, which is the tedious step a good commit message is meant to save us from in the first place.

A Quick and Dirty Solution

So the goal is to improve the quality (and consistency of quality) of my commit messages. The first idea that came to mind was to use pre-commit git hooks: I could write a script that would ask me if I used a good commit message. If I answered “yes”, the commit would go through; if not, the commit would not go through, and I’d be prompted to commit again with a better message.

I started tinkering with this on my own, but it turns out Victoria Drake already made a great pre-commit hook that does just what I wanted.

I tweaked her original bash script a bit to make a simpler prompt. I eventually arrived at this:

Installation is easy:

  • In any git repo you’re working in, simply head to the git hooks directory: cd .git/hooks.
  • Then create the pre-commit hook file: touch pre-commit.
  • Add the bash script’s contents to that file.
  • Finally, make the script executable: chmod +x .git/hooks/pre-commit.

The Prompt in Action

Here’s a short video showing the prompt:

Categories
Development

Updating WordPress Post Modified Dates

The standard way of updating post values in WordPress is to use wp_update_post(). For example:

wp_update_post( [
    'ID'         => 30,
    'post_title' => 'New Title',
] );

But you can’t update a post’s post_modified / post_modified_gmt values in this way; wp_update_post() calls wp_insert_post(), which sets these values in a way that can’t be overridden .

The effect is a catch-22: You can modify the post_modified values with that function, but that function then modifies those values again to record the fact that you modified those values.

The workaround is to update the values via the database directly. Here’s a handy snippet I put together for doing that:

/**
 * Sets the post_modified and post_modified_gmt values for the specified post.
 *
 * @author George Gecewicz <[email protected]>
 * @since 2019-09-10
 *
 * @param int    $post_id ID of the post whose values will be updated.
 * @param string $time_to_set The value of post_modified to set.
 * @param string $time_to_set_gmt Optional value of post_modified_gmt to set; if not set, $time_to_set will be used.
 * @return int|bool Number of rows affected. Boolean false on error.
 */
function set_modified_time_for_post( int $post_id, string $time_to_set, string $time_to_set_gmt = '' ) {
    global $wpdb;

    return $wpdb->query(
        $wpdb->prepare(
            "UPDATE $wpdb->posts
            SET post_modified = %s, post_modified_gmt = %s
            WHERE ID = %d",
            $time_to_set,
            $time_to_set_gmt ?: $time_to_set,
            $post_id
        )
    );
}

So to set the post_modified time of a post with the ID of 30, you’d simply call that function like this:

$a_day_ago = date( 'Y-m-d H:i:s', time() - DAY_IN_SECONDS );

set_modified_time_for_post( 30, $a_day_ago );