
Including Git commit-id as string inside C files - jagrsw
Code: https:&#x2F;&#x2F;github.com&#x2F;google&#x2F;honggfuzz&#x2F;commit&#x2F;2310ab5f6954cbe4ebaca25ca0a52c72bd4103c5<p>Including contents of a text-file as a string in C is not that easy, esp. if it&#x27;s not surrounded by quotes. One cannot simply write<p><pre><code>  static const char str[] = 
  #include &quot;file&quot;
  ;
</code></pre>
and<p><pre><code>  static const char str[] = &quot; 
  #include &quot;file&quot;
  &quot;;
</code></pre>
will not work either.<p>If you know of a better (cleaner&#x2F;shorter&#x2F;less-hacky) way of doing this, and w&#x2F;o external scripts&#x2F;bins like `xxd -i` please let me know.
======
eb0la
If you can use the blob Id for the file:

\- Add this to your .gitattributes file ([https://git-
scm.com/docs/gitattributes](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"

------
ksherlock
static const char str[] = GIT_COMMIT ;

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

