From cc405bc2ab872cb2b7690d5a9d8c5ff35691a062 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Tue, 21 May 2024 08:00:00 +0100 Subject: [PATCH] Initial commit --- .gitignore | 1 + git-commit-length-counter.sh | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 .gitignore create mode 100755 git-commit-length-counter.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c4a847d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/result diff --git a/git-commit-length-counter.sh b/git-commit-length-counter.sh new file mode 100755 index 0000000..721a127 --- /dev/null +++ b/git-commit-length-counter.sh @@ -0,0 +1,34 @@ +#!/usr/bin/env bash + +set -o errexit +set -o nounset +set -o pipefail + +# Default to running in the current directory, but allow for overriding the +# repo path using an environment variable. +# e.g. `REPO=../somewhere-else ./git-commit-length-counter` +REPO="${REPO:-$(pwd)}" + +# Return early if the repository directory doesn't exist. +[[ ! -d "${REPO}" ]] && exit 2 + +result_file="./result" + +for commit_id in $(git -C "${REPO}" rev-list --all --no-merges); do + echo "Processing commit ${commit_id}..." + # Calculate the legnth of the commit message. + commit_message=$(GIT_PAGER=cat git -C "${REPO}" show "${commit_id}" -s --format=%B) + commit_message_length=$(echo "${commit_message}" | wc -l) + + # Store the commit IDs and the message length. + echo "${commit_message_length} ${commit_id}" >> "${result_file}" +done + +# Sort the result file so all files are sorted by the length of the message. +sort "${result_file}" --reverse --numeric-sort --output "${result_file}" + +head "${result_file}" --lines 5 + +# Show the commit with the longest message. +commit_with_the_longest_message=$(head "${result_file}" --lines 1 | cut -d " " -f2) +git -C "${REPO}" show "${commit_with_the_longest_message}"