Skip to content

Instantly share code, notes, and snippets.

@scriptogre
Last active August 17, 2023 10:38
Show Gist options
  • Select an option

  • Save scriptogre/1d97f599da72efa5e39731eaf5e4da96 to your computer and use it in GitHub Desktop.

Select an option

Save scriptogre/1d97f599da72efa5e39731eaf5e4da96 to your computer and use it in GitHub Desktop.
A script tailored for Linux developers working in PyCharm. Its purpose is to encourage and aid developers, especially those accustomed to chaotically writing code, to adopt the atomic commit approach. The script continuously monitors the number of uncommitted changes in a Git repository. Once changes surpass a defined threshold, a notification p…
#!/bin/bash
# ---------------------------------------------------------------------------
# Commit Reminder Script for Linux
# Author: Christian Tanul
# GitHub Gist: https://gist.github.com/scriptogre/1d97f599da72efa5e39731eaf5e4da96/
#
# Description:
# This script aids developers in transitioning to the atomic commit approach,
# especially those who tend to write code chaotically. It's designed to be used
# in conjunction with PyCharm. When executed, the script continuously checks if
# the number of uncommitted changes in the current Git repository exceeds a
# specified threshold. If so, it sends a notification prompting the developer
# to consider committing.
#
# Adopting the atomic commit idea means making smaller, more focused commits
# that address a single task, fix, or feature. This script serves as a helpful
# reminder to commit frequently, keeping changes organized and manageable.
#
# Usage in PyCharm:
# 1. Save this script to a known location.
# 2. Open PyCharm's terminal or set up a Run Configuration.
# 3. If using a Run Configuration, ensure the "Working directory" is set to
# the project's root (which contains the .git folder).
# 4. Run the script.
#
# Note: This script is intended for Linux environments and may need adjustments for other OS.
# ---------------------------------------------------------------------------
# Configurable variables
REPO_PATH=$(pwd) # Current working directory. Adjust if needed.
THRESHOLD=100 # Number of line changes threshold. Adjust as desired.
SLEEP_INTERVAL=30 # Time (in seconds) between checks. Adjust as desired.
# Ensure git command is available
if ! command -v git &> /dev/null; then
echo "Error: git command not found. Please ensure Git is installed."
exit 1
fi
# Ensure the directory is a git repository
if [ ! -d "${REPO_PATH}/.git" ]; then
echo "Error: The current directory is not a Git repository. Exiting."
exit 1
fi
# Continuous loop to check for changes
while true; do
# Get the number of lines changed since the last commit
LINES_CHANGED=$(git -C "$REPO_PATH" diff HEAD --shortstat | awk '{print $4}')
# If git diff does not yield a result, LINES_CHANGED will be empty.
# Set it to zero in such cases.
LINES_CHANGED=${LINES_CHANGED:-0}
# Check if the number of changed lines exceeds the threshold
if [ "$LINES_CHANGED" -gt "$THRESHOLD" ]; then
notify-send "Commit Reminder" "You have more than $THRESHOLD lines changed in $REPO_PATH. Consider committing!"
fi
# Sleep for the specified interval before checking again.
sleep "$SLEEP_INTERVAL"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment