Including Git commit-id as string inside C files

4 points by jagrsw ↗ HN
Code: https://github.com/google/honggfuzz/commit/2310ab5f6954cbe4ebaca25ca0a52c72bd4103c5

Including contents of a text-file as a string in C is not that easy, esp. if it's not surrounded by quotes. One cannot simply write

  static const char str[] = 
  #include "file"
  ;
and

  static const char str[] = " 
  #include "file"
  ";
will not work either.

If you know of a better (cleaner/shorter/less-hacky) way of doing this, and w/o external scripts/bins like `xxd -i` please let me know.

2 comments

[ 1.8 ms ] story [ 18.1 ms ] thread
static const char str[] = GIT_COMMIT ;

cc -D GIT_COMMIT=\"`git log --format="%H"`\" test.c

If you can use the blob Id for the file:

- Add this to your .gitattributes file (https://git-scm.com/docs/gitattributes):

   .c ident
- Add this string to the file:

   static const char str[] = "$Id$"
- Commit the change AND check out the file.

- The string will be changed to:

   static const char str[] = "$Id: 0400..(40-byte-blob-id-for-this-C-file...213"