Skip to content

Feature: go jinja2 load latest git sha#1403

Open
macmoritz wants to merge 9 commits intokluctl:mainfrom
macmoritz:feat-go-jinja2-load-latest-git-sha
Open

Feature: go jinja2 load latest git sha#1403
macmoritz wants to merge 9 commits intokluctl:mainfrom
macmoritz:feat-go-jinja2-load-latest-git-sha

Conversation

@macmoritz
Copy link
Copy Markdown

@macmoritz macmoritz commented Dec 15, 2025

Description

As described in #1400 I want a way to add some kind of revision to rendered files.
I liked the idea of @benedikt-bartscher to use git as a source and went with the git revision sha.
In total this PR still needs some work, f.e. adding more tests and adding docs with examples. I am happy to work on these after my early questions are answered and you provided some early feedback.

Fixes #1400

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update
  • This change requires a new example

Questions from my side

  1. Is adding all these gitpython library files correct?
  2. Any idea how to get a git sha into the test? Currently there is only a simple test which checks the rendered string length
  3. Is the usage of the library gitpython okay? Any idea on how to retrieve the git revision are welcome.

Example

some_data: |-
         /* TODO: add latest file change, see https://github.com/kluctl/kluctl/issues/1400 */
         /* git sha: {{ load_latest_git_sha("some_file.yaml") }} */

renders into (following output was reduced to provide better readability):

Diff for object example
+-------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Path                                                  | Diff                                                                                                                                                                                                                                                                                             |
+-------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| spec.content                                          |                                                                                                                                                                                                                                                                        
|                                                       |        some_data: |-                                                                                                                                                                                                                                                                   |
|                                                       |          /* TODO: add latest file change, see https://github.com/kluctl/kluctl/issues/1400 */                                                                                                                                                                                                    |
|                                                       | +        /* git sha: a48c1e86993361b57b035add3af9ab96293ce26b */ 

@macmoritz macmoritz marked this pull request as ready for review December 21, 2025 22:02
@macmoritz
Copy link
Copy Markdown
Author

@codablock any news on this?

@codablock
Copy link
Copy Markdown
Collaborator

Hey @macmoritz, sorry for the very late response. First, thanks for the PR, this feature was already requested multiple times in the past.

Regarding your questions:

  1. How did you add the files? I assume via go generate ./...? If yes, then it was the correct way.
  2. Hmm, you won't be able to test this properly as a unit test, because the test-utils (from e2e) package is not available in ./lib. I would suggest to leave it with the length based test in ./lib/go-jinja2 and at the same time add a few tests in e2e that leverage the TestProject and TestGitServer stuff.
  3. GitPython is something that makes me a bit nervous to be honest. It requires Git to be installed and in PATH, which will be an issue on Windows. Did you look into https://github.com/jelmer/dulwich as an alternative? It's a pure-python Git implementation, maybe it's a better choice? An alternative would be to make the go <-> python communication in go-jinja2 bidirectional allowing the Python side to call Go functions, which would then allow us to use go-git...but this is probably a bit too much for this single feature.

@macmoritz
Copy link
Copy Markdown
Author

macmoritz commented Feb 10, 2026

No worries @codablock, you are welcome.

  1. Alright!
  2. e2esounds like the correct place for these. Thanks for your input, will look into it.
  3. I will look into Dulwich, but honestly the nicest solution would be to use go-git by calling go from python.
    What ist the estimated complexity for implementing this? Could you explain the steps? Feel free to link to or create a seperate issue for this.
    An option would be to implement this feature with an external python library and refactor it later by using go-git.

@macmoritz
Copy link
Copy Markdown
Author

How do you feel about adding the commit and author date too?
Dulwich provides these properties in the commit object and jinja2 can handle datetime formatting

@macmoritz
Copy link
Copy Markdown
Author

@codablock I've worked on all discussed topics (migrated to dulwich, added an e2e test, fixed unit test). Would appreciate a review from you.

@macmoritz
Copy link
Copy Markdown
Author

@codablock ping!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Function to get modified date of file

2 participants