This is my first python-based script. If you can improve the script, go ahead, I'll be more than happy to merge your proposal.
You can check a working example here: HaKIMus/activity-collection
A Python script that logs your Git commit activity to a specified GitHub repository. This script is useful for tracking your coding activity across different repositories, including those hosted on platforms like Bitbucket or GitLab.
- Automatic Activity Logging: Triggers on every Git commit and logs activity to GitHub.
- Customizable Messages: Configure project names and messages based on repository patterns.
- Supports Multiple Repositories: Use wildcard patterns to apply configurations to multiple repositories.
- Environment Variable Support: Securely manage sensitive information using
.envfiles. - Local Configuration Overrides: Use local configuration files to override settings without affecting the main configuration.
- Global Git Hook: A global
post-commitGit hook triggers the script after every commit. - Repository Detection: The script detects the repository path and matches it against configured patterns.
- Configuration Matching: Based on the matched pattern, it sets the project name and message.
- Activity Logging: The script updates a file in your specified GitHub repository, creating a new commit that logs the activity.
- Python 3.6 or higher
- Git installed on your machine
- A GitHub account
Clone this repository to your local machine:
git clone https://github.com/yourusername/activity-logger.git
cd activity-loggerInstall the required Python packages:
pip install -r requirements.txtCreate a .env.local file in the script directory by copying the example:
cp .env.example .envGenerate a GitHub Personal Access Token
- Navigate to: GitHub Settings > Developer settings > Personal access tokens.
- Click on "Generate new token".
- Select scopes: Choose repo (or more limited scopes as needed).
- Generate and copy the token.
- Paste the token into the GITHUB_ACCESS_TOKEN field in your .env file.
Important: Do not share your personal access token or commit it to version control.
Create a repos_config.local.json file in the script directory to define patterns for your repositories:
cp repos_config.json repos_config.local.jsonGlobal Git Hook Set up a global Git post-commit hook to trigger the script after every commit.
Create a global hooks directory:
mkdir -p ~/.git-templates/hooksConfigure Git to use the global hooks directory:
git config --global core.hooksPath ~/.git-templates/hooksCreate the post-commit hook:
nano ~/.git-templates/hooks/post-commitAdd the following content:
#!/bin/bash
# Path to your activity logging script
SCRIPT_PATH="/path/to/activity.py"
# Get the absolute path of the repository
REPO_PATH=$(git rev-parse --show-toplevel)
# Execute the activity logging script
python3 "$SCRIPT_PATH" --repo-path "$REPO_PATH"
# Check if a local post-commit hook exists and is executable
LOCAL_HOOK="$REPO_PATH/.git/hooks/post-commit"
if [ -x "$LOCAL_HOOK" ]; then
"$LOCAL_HOOK"
fiReplace /path/to/activity.py with the actual path to your script.
Make the hook executable:
chmod +x ~/.git-templates/hooks/post-commitSkip Logging When Committing to GitHub Repositories You can add an option to skip logging activity when committing to a GitHub repository.
Set PUSH_ON_GITHUB in your .env file:
PUSH_ON_GITHUB=falseAfter completing the setup, the script will automatically log your activity to GitHub whenever you make a Git commit in repositories that match your configured patterns.
- Protect Your Access Token: Never share your GitHub personal access token or commit it to a repository.
- Use Environment Variables: Store sensitive information in .env files and exclude them from version control.
- Exclude Sensitive Files: Ensure .env, .env.local, and repos_config.local.json are listed in .gitignore.
This project is licensed under the MIT License.