Skip to content

show current commit in the navbar#16

Merged
landryb merged 3 commits intomasterfrom
feat/show-commit-in-navbar
Oct 22, 2025
Merged

show current commit in the navbar#16
landryb merged 3 commits intomasterfrom
feat/show-commit-in-navbar

Conversation

@landryb
Copy link
Member

@landryb landryb commented Jul 17, 2025

relies on git rev-parse HEAD and thus the presence of the git command, and a repository..

requested by @fphg

@jeanmi151 how would that work for docker stuff ?

@jeanmi151
Copy link
Collaborator

we don't have the whole git in the docker image
but we could do it with an env var that is automatically filled by the CI inside the image docker (https://github.com/georchestra/gaia/blob/master/.github/workflows/ci.yml)
I could do it if you want
(sorry for the answer delay)

@jeanmi151
Copy link
Collaborator

Actually never mind the previous comment
we have the .git in the docker image as we make a copy of all the file https://github.com/georchestra/gaia/blob/master/docker/Dockerfile_flask#L28 inside /geordash
but we don't have "git" command installed (I see it used here https://github.com/georchestra/gaia/pull/16/files#diff-d630b8c2d8ce43e76a308cc11505c380dd7c70c0e06f75c3e402d19aee371a5aR54) we can add it in here https://github.com/georchestra/gaia/blob/master/docker/Dockerfile_flask#L16

@jeanmi151
Copy link
Collaborator

it is also possible to get the commit hash head just by reading the file .git/FETCH_HEAD
it will avoid installing git

@landryb
Copy link
Member Author

landryb commented Oct 21, 2025

if that works for you then that would be the simplest, yeah.

anyway after having read https://dev.to/hasan_ashab/from-12gb-to-54mb-my-docker-image-went-on-a-diet-apj i'll always have concerns about the way images are built/huge... but that's not my problem :)

relying on popen and spawning a git subprocess feels ugly, at least that's only done once at georchestraconfig object initialization.. but i wonder if adding a dep on gitpython to get the commit from native python code would be a better way to do it.

@landryb
Copy link
Member Author

landryb commented Oct 21, 2025

it is also possible to get the commit hash head just by reading the file .git/FETCH_HEAD

feels like poking in the git internals, that would be potentially fragile, no ?

@jeanmi151
Copy link
Collaborator

it is also possible to get the commit hash head just by reading the file .git/FETCH_HEAD

feels like poking in the git internals, that would be potentially fragile, no ?

I guess just reading this file is okay, it is what probably does the rev-parse command

@landryb
Copy link
Member Author

landryb commented Oct 21, 2025

soo... awk 'NR==1 {print $1}' .git/FETCH_HEAD ? is awk present in docker images ?

@landryb
Copy link
Member Author

landryb commented Oct 21, 2025

that's not a good idea, because FETCH_HEAD contains the sha of the fetched remotes, not necessarely the sha of the current worktree.. after a git fetch they're not in sync:

[21/10 12:36] landry@build.fluela:/data/src/georchestra/cadastrapp $awk 'NR==1 {print $1}' .git/FETCH_HEAD 
c32eb59bb0b238615efed68ff8925c9e2c6ebe9a
[21/10 12:36] landry@build.fluela:/data/src/georchestra/cadastrapp $git rev-parse HEAD
943f8e96c1fab5f8d5976ad59782d1e53faa3d1f

the next option is to read .git/HEAD which leads you to read the sha from the corresponding branch, but at that point it really feels hackish:

[21/10 12:39] landry@build.fluela:/data/src/georchestra/cadastrapp $cat .git/HEAD 
ref: refs/heads/master
[21/10 12:39] landry@build.fluela:/data/src/georchestra/cadastrapp $cat .git/refs/heads/master 
943f8e96c1fab5f8d5976ad59782d1e53faa3d1f

@jeanmi151
Copy link
Collaborator

jeanmi151 commented Oct 21, 2025

soo... awk 'NR==1 {print $1}' .git/FETCH_HEAD ? is awk present in docker images ?

yes (mawk binded as awk)

gunicorn@b2d44e5165c3:/geordash$ awk
Usage: mawk [Options] [Program] [file ...]

Program:
    The -f option value is the name of a file containing program text.
    If no -f option is given, a "--" ends option processing; the following
    parameters are the program text.

Options:
    -f program-file  Program  text is read from file instead of from the
                     command-line.  Multiple -f options are accepted.
    -F value         sets the field separator, FS, to value.
    -v var=value     assigns value to program variable var.
    --               unambiguous end of options.

    Implementation-specific options are prefixed with "-W".  They can be
    abbreviated:

    -W version       show version information and exit.
    -W dump          show assembler-like listing of program and exit.
    -W help          show this message and exit.
    -W interactive   set unbuffered output, line-buffered input.
    -W exec file     use file as program as well as last option.
    -W random=number set initial random seed.
    -W sprintf=number adjust size of sprintf buffer.
    -W posix_space   do not consider "\n" a space.
    -W usage         show this message and exit.

but i would keep the logic in python than depend on cmd installed on the system
eg how we could do it in python

import os
prefix= os.getcwd() + "/.git/"
file = open(prefix+"HEAD","r")
filetogoread = file.read()
file.close()
commit_value_file = open(prefix + filetogoread.split(" ")[1].strip(), "r")
commit_value = commit_value.read().strip()
commit_value_file.close()

@landryb
Copy link
Member Author

landryb commented Oct 21, 2025

yeah that looks .. simpler than popen(). i'll try that :)

relies on 'git rev-parse HEAD' and thus the presence of the git
command, and a repository..
this way no need for popen nor git binary

show 'version inconnue' if we failed finding the ref
@landryb landryb force-pushed the feat/show-commit-in-navbar branch from 56bf1bc to c1cd3c3 Compare October 22, 2025 12:58
@landryb
Copy link
Member Author

landryb commented Oct 22, 2025

@jeanmi151 if you can retry this branch, i think i've handled most cases (sometimes .git/HEAD contains a commit sha when you've checked out a given tag)

@jeanmi151
Copy link
Collaborator

It seems working in my local docker context
image

@landryb landryb merged commit c70ae82 into master Oct 22, 2025
@landryb
Copy link
Member Author

landryb commented Oct 22, 2025

perfect ;)

@jeanmi151 jeanmi151 deleted the feat/show-commit-in-navbar branch October 22, 2025 14:01
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.

2 participants