Skip to content

Commit ef05c84

Browse files
author
Alessandro Bertulli
committed
Add API documentation
1 parent 6c0cb87 commit ef05c84

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ target_link_libraries(your_target
2424
cmake_git_version_tracking
2525
)
2626
```
27-
Then [`#include git.h`](./git.h) and use the provided functions to retrieve git metadata.
27+
Then [`#include "git.h"`](./git.h) and use the provided functions to retrieve git metadata.
2828

2929
## Intended use case
3030
You're continuously shipping prebuilt binaries for an
@@ -40,6 +40,27 @@ Dirty: false (there were no uncommitted changes at time of build)
4040
This allows you to investigate the _precise_ version of the
4141
application that the bug was reported in.
4242

43+
## API reference
44+
This CMake module exports both a C and a C++ API. Both are accessed by including ["git.h"](./git.h). Here is the list of functions you can
45+
call after including the module.
46+
47+
| Description | C API | C++ API[^1] | Output | Git command |
48+
| ----------- | ----- | ---------- | ------ | ----------- |
49+
| Check if repo is correctly configured | `bool git_IsPopulated()` | `bool git::IsPopulated()` | `true`/`false` | (Set to `0` if git commands signal an error) |
50+
| Check if the worktree is dirty (there are uncommited changes)[^2] | `bool git_AnyUncommittedChanges()` | `bool git::AnyUncommittedChanges()` | `true`/`false` | `git status --porcelain -unormal` |
51+
| Get the author's name | <pre>const char* git_AuthorName()</pre> | `const StringOrView& git::AuthorName()` | `"Andrew Hardin"` | `git show -s "--format=%an"` |
52+
| Get the author's email | <code>const char* git_AuthorEmail()</code> | `const StringOrView& git::AuthorEmail()` | `"andrew.hardin@github.com"` | `git show -s "--format=%ae"` |
53+
| Get the commit full SHA1 ID | <pre><code>const char* git_CommitSHA1()</code></pre> | `const StringOrView& git::CommitSHA1()` | `"815da8c5a326f10931c0ce5944c62da11bbe784e"` | `git show -s "--format=%H"` |
54+
| Get the commit date | <code><pre>const char* git_CommitDate()</pre></code> | `const StringOrView& git::CommitDate()` | `"2025-11-05 17:01:07 +0100"` | `git show -s "--format=%ci"` |
55+
| Get the commit subject | `const char* git_CommitSubject()` | `const StringOrView& git::CommitSubject()` | `"An example commit subject"` | `git show -s "--format=%s"` |
56+
| Get the commit full body | `const char* git_CommitBody()` | `const StringOrView& git::CommitBody()` | `"An example commit body\nthat can span multiple lines"`[^3] | `git show -s "--format=%b"` |
57+
| Get the commit describe string | `const char* git_Describe()` | `const StringOrView& git::Describe()` | `"v1.0.2-5g815da8c"` | `git describe --always` |
58+
| Get the current branch | `const char* git_Branch()` | `const StringOrView& git::Branch()` | `"main"` | `git symbolic-ref --short -q` |
59+
60+
[^1]: The `StringOrView` type is an alias to `std::string`. By setting the configuration variable `GIT_VERSION_USE_STRING_VIEW` to true, it becomes an alias to `std::string_view`.
61+
[^2]: By default, it considers untracked files as a dirty worktree. By setting the configuration variable `GIT_IGNORE_UNTRACKED` to true, it ignores them and only considers tracked modified files.
62+
[^3]: The returned string has whitespace automatically escaped (it will contain printable sequences `"\n"` and `"\r"`)
63+
4364
## Q: What if I want to track `$special_git_field`?
4465
Fork the project and modify [git_watcher.cmake](git_watcher.cmake)
4566
to track new additional fields (e.g. kernel version or build hostname).

0 commit comments

Comments
 (0)