Dependence on git hash and status

962 views
Skip to first unread message

Onath Claridge

unread,
May 26, 2015, 6:33:08 PM5/26/15
to bazel-...@googlegroups.com
In order to synchronize build information across different processes, we have a generated C file that contains a small amount of git metadata -- the git hash, and an indication of whether the status is clean. Is there a way to make this work? My first thought is to write a genrule that will always rebuild the generated file when it's needed, but I don't see a way to do that.

Austin Schuh

unread,
May 26, 2015, 6:42:46 PM5/26/15
to Onath Claridge, bazel-...@googlegroups.com
The following should give you a pretty good starting point.  Feedback is welcome though if you find issues with it :)

# Generate a cc file with the git revision in a string.
genrule(
  name = 'git_version',
  srcs = glob(['.git/HEAD', '.git/refs/**']),
  outs = [ 
    'git_rev_autogen.cc',
  ],  
  cmd = 'echo "char git_rev_autogen[] = \\"$$(git rev-parse HEAD)\\";"' +
        ' > $(location git_rev_autogen.cc)',
)

cc_library(
  name = 'git_rev_autogen',
  srcs = [ 
    ':git_version',
  ],  
)

Austin

On Tue, May 26, 2015 at 3:33 PM, Onath Claridge <clar...@google.com> wrote:
In order to synchronize build information across different processes, we have a generated C file that contains a small amount of git metadata -- the git hash, and an indication of whether the status is clean. Is there a way to make this work? My first thought is to write a genrule that will always rebuild the generated file when it's needed, but I don't see a way to do that.

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/ee60d275-8433-455c-a469-07799c4b1eaf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jonathan Nieder

unread,
May 26, 2015, 6:45:14 PM5/26/15
to Austin Schuh, Onath Claridge, bazel-...@googlegroups.com
You'll also want .git/packed-refs in srcs.

Austin Schuh

unread,
May 26, 2015, 6:56:35 PM5/26/15
to Jonathan Nieder, Onath Claridge, bazel-...@googlegroups.com
Thanks!  Greatly appreciated.

Onath Claridge

unread,
May 26, 2015, 7:00:51 PM5/26/15
to bazel-...@googlegroups.com, clar...@google.com, austin...@gmail.com
Two problems so far... First, a genrule executes in a Bazel-owned environment that isn't a Git repo. It's possible to work around this by writing a script to generate the file and tracing symlinks back to the script's actual location. But then there's the second problem -- the output of the glob() is empty. glob only matches files within the current package, and .git doesn't lie inside of any package.

Is there a file in .git that indicates whether there are any uncommitted modifications? It seems like git wouldn't necessarily know that if you've made changes without running any git commands.

Austin Schuh

unread,
May 26, 2015, 7:03:02 PM5/26/15
to Onath Claridge, bazel-...@googlegroups.com
I placed that in a BUILD file in the root of the repo, so the .git folder is then in the root package.

Onath Claridge

unread,
May 26, 2015, 7:24:23 PM5/26/15
to bazel-...@googlegroups.com, clar...@google.com
Interesting, my Bazel doesn't seem to pay attention to a BUILD file in the root directory. Maybe my version is too old. It does look like I can point to .git with a symlink, though, and glob will recognize the files!

This gets me rebuilds when I check in a change, but this approach doesn't seem to catch uncommitted changes. I'm not too familiar with the contents of .git, but for the reason mentioned before, I'm suspicious whether .git can know that at all.

I'm also curious whether Bazel makes this complicated for a good reason. :)

Lukács T. Berki

unread,
May 27, 2015, 6:48:55 AM5/27/15
to Onath Claridge, bazel-...@googlegroups.com
We should really have support for this, except, well, that we should really have other features, too and so far, this has always drawn the short straw. The internal version has a command line option that points to a script that generates this data, and then this is converted to a language-specific format (e.g. properties for Java, a .h file for C++).

The only problem is that we'd have to figure out what to do with the language-specific workspace status logic (that creates e.g. Java properties files / C++ headers files from {stable,volatile}-status.txt .  I think the right approach is to provide language-specific identifier transformation (e.g. GIT_HASH -> #define GIT_HASH in C++ and bazel.git.hash in Java). We already have some infrastructure for this (inherited from the internal version)

On Wed, May 27, 2015 at 12:33 AM, Onath Claridge <clar...@google.com> wrote:
In order to synchronize build information across different processes, we have a generated C file that contains a small amount of git metadata -- the git hash, and an indication of whether the status is clean. Is there a way to make this work? My first thought is to write a genrule that will always rebuild the generated file when it's needed, but I don't see a way to do that.

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/ee60d275-8433-455c-a469-07799c4b1eaf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Lukács T. Berki | Software Engineer | lbe...@google.com | 

Google Germany GmbH | Dienerstrasse 12 | 80331 München | Germany | Geschäftsführer: Graham Law, Christine Elizabeth Flores | Registergericht und -nummer: Hamburg, HRB 86891

Onath Claridge

unread,
May 27, 2015, 1:36:47 PM5/27/15
to bazel-...@googlegroups.com, clar...@google.com
Austin and Jonathan - thanks for your suggestions!

Lukács - does the internal version provide a way to tell whether there are uncommitted changes? We want to compile in the output of `git status --porcelain | grep -v '^??' | wc -l`.

Onath Claridge

unread,
May 28, 2015, 2:57:28 PM5/28/15
to bazel-...@googlegroups.com
From hanwen's internal reply: "For the open-source version,  I don't see problems running "git status". I think it's fast enough."


In the meantime, we'll use a wrapper around the Bazel binary to handle this.
Reply all
Reply to author
Forward
0 new messages