LinuxCommandLibrary

git-stamp

Update files with Git commit information

TLDR

Stamp the last commit message referencing it with the issue number from your bug tracker

$ git stamp [issue_number]
copy

Stamp the last commit message linking it to its review page
$ git stamp [Review https://example.org/path/to/review]
copy

Stamp the last commit message replacing previous issues with a new one
$ git stamp [[-r|--replace]] [issue_number]
copy

SYNOPSIS

git-stamp [options] [paths...]

PARAMETERS

-r, --recursive
    Recursively stamp all files in the current directory and its subdirectories.

-v, --verbose
    Enable verbose output, showing which files are being processed.

-h, --help
    Display a help message and exit.

--version
    Show the program's version number and exit.

DESCRIPTION

git-stamp is a utility designed to synchronize the modification and access times (mtime and atime) of files in a Git repository with the last commit timestamp recorded by Git. This tool is particularly useful in build environments where incremental builds rely heavily on file modification times to determine whether a file needs to be recompiled or rebuilt. By default, Git doesn't preserve file timestamps upon checkout, which can lead to unnecessary rebuilds or incorrect caching.

git-stamp addresses this by applying the commit timestamp of the last modification to the actual file's metadata. It can operate on specified files or recursively on all files in the current directory. This ensures that the filesystem's view of file modification times accurately reflects their last change within the Git history, thereby optimizing build processes and ensuring consistency across different development environments.

CAVEATS

Local Metadata Only: git-stamp modifies only the local file metadata (timestamps) and does not affect the Git repository's history or content.
Filesystem Compatibility: May encounter limitations or behave unexpectedly on certain network file systems or non-standard file systems that handle timestamps differently.
Untracked/Ignored Files: It only processes files tracked by Git. Untracked or ignored files will not be stamped.
Dependency on Git: The tool relies on the Git command-line utility being available and properly configured in the environment.

HOW IT WORKS

git-stamp fetches the Unix timestamp of a file's last modification from Git's history using commands like git log --pretty=format:%at -1 filename. It then applies this timestamp to the actual file's modification and access times using system calls, effectively 'touching' the file with the Git-derived timestamp. This process ensures the filesystem's metadata accurately reflects the file's last change within the Git repository.

TYPICAL USE CASES

Primarily utilized within build systems (e.g., Make, Ninja, Bazel) to ensure that only files truly modified according to Git history are reprocessed, optimizing build times. It also helps maintain consistent build outputs across various development environments and prevents unnecessary full rebuilds when checking out different Git revisions.

HISTORY

git-stamp emerged as a practical solution to a common challenge in development workflows: the disconnect between Git's internal timestamping and a filesystem's modification times. While not part of the official Git distribution, standalone implementations like 'srid/git-stamp' (first seen around 2012-2013) address the need for accurate file timestamps in build systems. Its development is driven by the demand for efficient incremental builds and consistent environments, where precise file modification times are critical.

SEE ALSO

git(1), touch(1), find(1)

Copied to clipboard