It might be that I'm misunderstanding how to solve this problem and I just found gitpython so the opposite of expert right now.
I'm trying to commit/push a repo manually using the git client on linux. But I want a pre-commit hook to edit a file on every git commit
and add that file back the the same commit before I push anything.
This is how far I got:
sha_last_run = get_previous() # calling a CD pipeline api
repo = git.Repo(search_parent_directories=True)
new_sha_to_run = repo.head.object.hexsha[:7]
with fileinput.FileInput("file.yml", inplace=True, backup=".bak") as f:
for line in f:
print(line.replace(sha_last_run, new_sha_to_run), end="")
repo.git.add(update=True)
I'm replacing the 'version' or 'last_sha_run' in file.yml
with the sha I'm about to push. Basically it replaces the old sha with the new sha and then stages the file but I don't want that because I end up in a loop of always having that file staged and needed to be recommited.
Is there a simpler way to do this where I can add a modified file to the same sha and then commit that sha using hooks?
---- extra info ----
I want to make clear what I have so far:
If my pipeline ran on the commit/merge to master abc123. Then the version in my file.yml -- app:version is app:acb123.
The pre-commit hook is finding both the abc123 (from the pipeline api last_run_sha) and the local commit that could potentially be pushed to branch.
What I want the pre-commit hook to do is automate updating this version abc123 in file.yml for me, which involved editing a tracked from the hook and having it be apart of the git commit
that triggered the hook.
So the workflow would be like this:
- Not have to worry about updating the app:version because git will do it for me
touch some_file && git add -u
git commit -m "I just added some file"
- hook kicks in and gets last run sha abc123 without issue and the new sha from the command I just run 123abc
- it then replaces the line in file.yml with the new "version" that will be pushed and trigger the pipeline
This is part of a much more complicated body of work and would be nice to have (code for essential at this point).
The above code in the scenario I've laid out is working... ish. The only problem is that it's replacing "last_sha_run" in file.yml with the second to last commit not the current one that I could push.
So if my file.yml app:version is app:abc123
and I've done
- commit1 def123
- commit2 cba123
- commit3 123123
I could push 123123 but my file.yml file is reading app:cba123
I'm one behind, maybe this is an error on my part. I'm just trying to capture the current commit sha.
Q
I wonder if my above solution is working but the new commit sha to push is being generated after my hook has run, getting essentially the current sha, and putting it in place? But I'm still one behind...
A
This is exactly whats happening. Commenting out the part in the hook that edits the file, and printing repo.commit() sees the current commit not the one being created. I don't know if this has been generated yet, or if there's a look ahead I could do with head.object.
But apart from a post-commit script ammend I'm not sure how else to make this method work. However I feel like I'd have the same issue.