• Intro Headers Instructions Macros Commands
Fork me on GitHub
    • Getting Started
      • Introduction
      • How to install bnd
      • Guided Tour Workspace & Projects
      • bnd / bndlib Features and JAR Wrapping Techniques
      • FAQ - Frequently Asked Questions
    • Concepts and Practices
      • Concepts
      • Background
      • Best practices
    • Build and Development
      • Build
      • Generating JARs
      • Versioning
      • Baselining
      • Service Components
      • Metatype
      • Contracts
      • Bundle Annotations
      • Accessor Properties
      • SPI Annotations
    • Dependency and Launching
      • Resolving Dependencies
      • Launching
      • Startlevels
    • Testing
      • Testing
      • Testing with Launchpad
    • Packaging and Distribution
      • Packaging Applications
      • JPMS Libraries
      • Wrapping Libraries to OSGi Bundles
    • Documentation and Tools
      • Generating Documentation
      • bnd CLI Commands
      • For Developers
      • Templates for Workspaces
      • Tips for Windows users
      • Tools bound to bnd
    • Reference Material
      • Reference
      • Headers
      • Instruction Reference
      • Instruction Index
      • Macro Reference
      • Macro Index
      • Plugins
      • External Plugins
    • Configuration and Troubleshooting
      • Settings
      • Errors
      • Warnings
  • githead

    Prev Next
    Builder
    Get the Git commit SHA of the current HEAD

    Summary

    The githead macro returns the SHA-1 hash of the current Git HEAD commit. It searches for a .git directory starting from the project base directory and walking up the file hierarchy, then resolves symbolic references to find the actual commit hash.

    Syntax

    ${githead}
    

    Parameters

    None - this macro takes no parameters.

    Behavior

    • Searches for a .git directory starting from the project base, going up the directory tree
    • Handles both regular Git repositories and Git worktrees
    • Reads the .git/HEAD file to find the current commit
    • Resolves symbolic references (e.g., ref: refs/heads/main) to actual commit SHAs
    • Handles packed references (when Git has optimized/compressed references)
    • Returns the commit SHA in uppercase hexadecimal format
    • Returns empty string if no Git repository is found

    Examples

    Include Git commit in manifest:

    Bundle-SCM-Revision: ${githead}
    # Result: Bundle-SCM-Revision: 4A3B2C1D0E9F8A7B6C5D4E3F2A1B0C9D8E7F6A5B
    

    Create version with Git hash:

    Implementation-Version: ${version}-${githead}
    

    Document build source:

    Build-From: commit ${githead}
    

    Conditional on Git availability:

    git.revision=${if;${githead};${githead};not-in-git-repo}
    

    Use Cases

    • Tracking exact source code version in built artifacts
    • Creating reproducible builds with commit information
    • Debugging - knowing exactly which commit produced an artifact
    • Audit trails for compliance and security
    • Continuous integration - linking builds to source commits
    • Version identification beyond semantic versioning

    Notes

    • Returns empty string if not in a Git repository
    • Works with Git worktrees (separate working directories)
    • Handles both direct SHA references and symbolic references
    • Handles Git’s packed-refs optimization
    • The returned SHA is in uppercase
    • The search walks up the directory tree to find the Git repository
    • Useful for embedding in bundle manifests for traceability

    See test cases in MacroTestsForDocsExamples.java

Prev Next
Search
    • Home