Repository navigation
feat: show firmware version and git build in /logs and /api/version #363
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1172f0d
feat: show firmware version and git build in /logs and /api/version
sjordan0228 601d65f
test: cover firmware identity line boundaries and bake idempotence
sjordan0228 a52c0fe
fix: reject parent-checkout commits in bake hook and always prepend f…
sjordan0228 6af747d
fix: ignore inherited git directory overrides in the build-commit bak…
sjordan0228 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| """Bake the short git commit into src/gen/FirmwareBuildCommit.h. | ||
|
|
||
| Runs as a PlatformIO `pre:` hook. Only src/FirmwareBuildInfo.cpp includes the | ||
| header, and it is rewritten only when the commit changes, so a new commit | ||
| recompiles that one file. Without git (source archive, missing binary, timeout) | ||
| the commit is "unknown" and the build continues. | ||
| """ | ||
|
|
||
| import os | ||
| import re | ||
| import subprocess | ||
|
|
||
| UNKNOWN = "unknown" | ||
| COMMIT_RE = re.compile(r"^[0-9a-f]{4,40}$") | ||
| HEADER_REL = os.path.join("src", "gen", "FirmwareBuildCommit.h") | ||
| GIT_ENV_DROP = ("GIT_DIR", "GIT_WORK_TREE", "GIT_COMMON_DIR", "GIT_OBJECT_DIRECTORY") | ||
|
|
||
|
|
||
| def git_env(): | ||
| """The caller's environment without the overrides that redirect HEAD. | ||
|
|
||
| An exported GIT_DIR makes git read another checkout's HEAD while | ||
| --show-toplevel still reports project_dir, which would defeat the | ||
| nested-archive guard. | ||
| """ | ||
| return {k: v for k, v in os.environ.items() if k not in GIT_ENV_DROP} | ||
|
|
||
|
|
||
| def sanitize_commit(raw): | ||
| text = (raw or "").strip() | ||
| return text if COMMIT_RE.match(text) else UNKNOWN | ||
|
|
||
|
|
||
| def toplevel_matches(project_dir, git="git", timeout=5): | ||
| """True only when project_dir is itself the git work-tree root. | ||
|
|
||
| git searches parent directories, so an archive extracted inside another | ||
| checkout would otherwise bake that checkout's commit. | ||
| """ | ||
| try: | ||
| result = subprocess.run( | ||
| [git, "-C", project_dir, "rev-parse", "--show-toplevel"], | ||
| capture_output=True, text=True, timeout=timeout, check=False, | ||
| env=git_env()) | ||
| except (OSError, subprocess.SubprocessError): | ||
| return False | ||
| top = (result.stdout or "").strip() | ||
| if result.returncode != 0 or not top: | ||
| return False | ||
| return os.path.realpath(top) == os.path.realpath(project_dir) | ||
|
|
||
|
|
||
| def read_commit(project_dir, git="git", timeout=5): | ||
| try: | ||
| result = subprocess.run( | ||
| [git, "-C", project_dir, "rev-parse", "--short=7", "HEAD"], | ||
| capture_output=True, text=True, timeout=timeout, check=False, | ||
| env=git_env()) | ||
| except (OSError, subprocess.SubprocessError): | ||
| return UNKNOWN | ||
| if result.returncode != 0: | ||
| return UNKNOWN | ||
| commit = sanitize_commit(result.stdout) | ||
| if commit == UNKNOWN: | ||
| return UNKNOWN | ||
| if not toplevel_matches(project_dir, git, timeout): | ||
| return UNKNOWN | ||
| return commit | ||
|
|
||
|
|
||
| def render_header(commit): | ||
| return ("#pragma once\n" | ||
| "// Generated by scripts/bake_git_commit.py. Do not edit.\n" | ||
| '#define FW_GIT_COMMIT "%s"\n' % commit) | ||
|
|
||
|
|
||
| def write_if_changed(path, content): | ||
| try: | ||
| with open(path, "r", encoding="utf-8") as fh: | ||
| if fh.read() == content: | ||
| return False | ||
| except OSError: | ||
| pass | ||
| os.makedirs(os.path.dirname(path), exist_ok=True) | ||
| tmp = path + ".tmp" | ||
| with open(tmp, "w", encoding="utf-8") as fh: | ||
| fh.write(content) | ||
| os.replace(tmp, path) | ||
| return True | ||
|
|
||
|
|
||
| def bake(project_dir, git="git"): | ||
| commit = read_commit(project_dir, git) | ||
| changed = write_if_changed(os.path.join(project_dir, HEADER_REL), | ||
| render_header(commit)) | ||
| print("git-commit: %s (%s)" % (commit, "regenerated" if changed else "unchanged")) | ||
| return commit | ||
|
|
||
|
|
||
| try: | ||
| Import("env") # noqa: F821 (PlatformIO SConscript context) | ||
| except NameError: | ||
| env = None | ||
|
|
||
| if env is not None: | ||
| bake(env.subst("$PROJECT_DIR")) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| #include "FirmwareBuildInfo.h" | ||
|
|
||
| // Sole includer of the generated header, so a new commit rebuilds only this file. | ||
| #if __has_include("gen/FirmwareBuildCommit.h") | ||
| #include "gen/FirmwareBuildCommit.h" | ||
| #endif | ||
|
|
||
| #ifndef FW_GIT_COMMIT | ||
| #define FW_GIT_COMMIT "unknown" | ||
| #endif | ||
|
|
||
| const char* firmwareBuildCommit() { return FW_GIT_COMMIT; } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| #ifndef FIRMWARE_BUILD_INFO_H | ||
| #define FIRMWARE_BUILD_INFO_H | ||
|
|
||
| #include <cstddef> | ||
| #include <cstdio> | ||
| #include <cstring> | ||
|
|
||
| // Short git commit baked in at build time, or "unknown". | ||
| const char* firmwareBuildCommit(); | ||
|
|
||
| // PlatformIO env name; the OTA page downloads spoolsense_scanner_<board>.bin | ||
| // by this string. XIAO must precede BOARD_ESP32_C6: its env defines both. | ||
| inline const char* firmwareBoardName() { | ||
| #if defined(BOARD_SEEED_XIAO_ESP32_C6) | ||
| return "seeed_xiao_esp32c6"; | ||
| #elif defined(BOARD_ESP32_C6) | ||
| return "esp32c6"; | ||
| #elif defined(BOARD_ESP32_C5) | ||
| return "esp32c5"; | ||
| #elif defined(BOARD_ESP32_C3) | ||
| return "esp32c3"; | ||
| #elif defined(BOARD_S3_DEVKITC) | ||
| return "esp32s3devkitc"; | ||
| #elif defined(BOARD_ESP32_S3) | ||
| return "esp32s3zero"; | ||
| #else | ||
| return "esp32dev"; | ||
| #endif | ||
| } | ||
|
|
||
| // Writes "SpoolSense <version> (<commit>) <board>"; null or empty parts become | ||
| // "unknown". Returns snprintf's result, or 0 when out is null or size is 0. | ||
| inline int formatFirmwareLine(char* out, size_t size, const char* version, | ||
| const char* commit, const char* board) { | ||
| if (!out || size == 0) return 0; | ||
| auto part = [](const char* s) { return (s && *s) ? s : "unknown"; }; | ||
| return std::snprintf(out, size, "SpoolSense %s (%s) %s", | ||
| part(version), part(commit), part(board)); | ||
| } | ||
|
|
||
| // Writes "<identity line>\n<log>" into out (size bytes, NUL-terminated). | ||
| // Returns the number of characters written, or 0 when out is null or size is | ||
| // 0. The log is truncated to fit; log may overlap out. | ||
| inline int formatLogsWithIdentity(char* out, size_t size, const char* version, | ||
| const char* commit, const char* board, | ||
| const char* log) { | ||
| if (!out || size == 0) return 0; | ||
| int n = formatFirmwareLine(out, size, version, commit, board); | ||
| if (n < 0) return 0; | ||
| size_t pos = (static_cast<size_t>(n) < size) ? static_cast<size_t>(n) | ||
| : size - 1; | ||
| if (pos + 1 < size) { | ||
| out[pos] = '\n'; | ||
| pos++; | ||
| } | ||
| out[pos] = '\0'; | ||
| if (log) { | ||
| size_t room = size - pos - 1; | ||
| size_t len = std::strlen(log); | ||
| if (len > room) len = room; | ||
| std::memmove(out + pos, log, len); | ||
| pos += len; | ||
| out[pos] = '\0'; | ||
| } | ||
| return static_cast<int>(pos); | ||
| } | ||
|
|
||
| #endif // FIRMWARE_BUILD_INFO_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.