Registry / devops / githead

githead

JSON →
library1.2.2pypypi✓ verified 86d ago

Githead is a lightweight Python utility library designed to retrieve the current Git commit hash (HEAD) of a repository. It provides a straightforward function to get this information, making it useful for embedding version data in applications or scripts. The library is currently at version 1.2.2 and is actively maintained, with updates released as needed for its focused functionality. It requires Python 3.9 or higher.

pip install githead
INSTALL
IMPORT
SIG · GITHEAD
G
githead
devopspythonv1.2.2
Install
1.6s avg
Import
21ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.2.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.023s · 17.8MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 1.6s · import 0.020s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

githead
from githead import githead
import githead
The primary function `githead` is imported directly from the package, not the package itself.

This quickstart demonstrates how to import and use the `githead` function to retrieve the current Git commit hash. The example includes robust error handling for environments that may not be within an initialized Git repository, creating a temporary one for demonstration purposes.

import os from githead import githead def get_current_commit_hash(): try: # Ensure we are in a directory that is a git repository # For demonstration, we'll try to create a dummy git repo if not found # In a real application, you'd assume a git repo or handle the error. # Check if .git directory exists in current or parent directory current_dir = os.getcwd() is_git_repo = False for _ in range(5): # Check up to 5 parent directories if os.path.exists(os.path.join(current_dir, '.git')): is_git_repo = True break parent_dir = os.path.dirname(current_dir) if parent_dir == current_dir: # Reached root break current_dir = parent_dir if not is_git_repo: # This block is for demonstration; normally, you'd expect a repo. # For a quickstart to run, we mock a git repo for a moment. print("Not in a Git repository. Initializing a temporary one for demonstration...") os.makedirs("temp_git_repo", exist_ok=True) original_cwd = os.getcwd() os.chdir("temp_git_repo") os.system("git init > /dev/null 2>&1") with open("test_file.txt", "w") as f: f.write("Hello Githead!") os.system("git add . > /dev/null 2>&1") os.system("git commit -m \"Initial commit\" > /dev/null 2>&1") commit_hash = githead() os.chdir(original_cwd) import shutil shutil.rmtree("temp_git_repo") return commit_hash else: return githead() except Exception as e: return f"Error getting commit hash: {e}" print(f"Current Git HEAD commit hash: {get_current_commit_hash()}")
Debug
Known issues
gotchaThe `githead` library requires an initialized Git repository in the current working directory or a parent directory to function correctly. If run outside a Git repository, it will raise an error (e.g., 'fatal: not a git repository').
fix
Ensure your Python script is executed within a directory that is part of a Git repository. You can initialize a new one with `git init` or navigate to an existing repository.
affects: All versions
gotchaThe name 'githead' can be ambiguous, as it refers to the general Git concept of the HEAD pointer, as well as other unrelated tools like 'GitAhead' (a graphical Git client) or a Node.js CLI helper also named 'githead'. Ensure you are using the Python `githead` library (pypi.org/project/githead).
fix
Always import explicitly with `from githead import githead` and refer to the PyPI page or GitHub repository (github.com/Zaczero/githead) for the correct Python library.
affects: All versions
Errors
Common errors & fixes
fatal: not a git repository (or any of the parent directories): .git
The `githead` Python library was called in a directory that is not an initialized Git repository.
fix
Run your script from within a Git repository. If you want to create a new one, use `git init` in your desired directory, then `git add .` and `git commit -m "Initial commit"` before using `githead`.
ModuleNotFoundError: No module named 'githead'
The `githead` Python library has not been installed in the active Python environment.
fix
Install the library using pip: `pip install githead`.
Error: Command '['git', 'rev-parse', 'HEAD']' returned non-zero exit status 128.
This error typically indicates that the `git` command-line tool is not installed or not accessible in the system's PATH, or the repository is corrupted/in an uncommitted state (e.g., just `git init` without any commits).
fix
Verify that Git is installed and configured correctly on your system (`git --version`). Ensure the repository has at least one commit (`git commit -m "Initial commit"`).
Upgrade
Version history
1.2.2latest on PyPI · released Feb 4, 2025
Audit
Dependencies
gitrequiredThe library internally relies on the 'git' command-line tool being installed and accessible in the system's PATH to function correctly.
Agent activity
10 hits · last 30 days
node
10
Resources
githead — pip install githead · libregistry