Skip to content

Commit

Permalink
First commit.
Browse files Browse the repository at this point in the history
This version works, although there is only one line of documentation/debug.
  • Loading branch information
SysadminJeroen committed Jan 17, 2016
0 parents commit b374397
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions git-hash.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <stdio.h>
#include <string.h>

char static str[137];

int main()
{
if(!is_in_git_repo())
return 0;
strcpy(str, "#if defined _INC_GIT_HASH\n");
strcat(str, " #endinput\n");
strcat(str, "#else\n");
strcat(str, " #define _INC_GIT_HASH\n");
strcat(str, "#endif\n\n");
strcat(str, "#define GitHash ");
getGitSha(str);
store_data("githash.inc", str);
//printf("%d\n", strlen(str)); //Debug function, to count the string size if there are any changes to 'str'.
}

getGitSha(char *target)
{
FILE *sha = popen("git rev-parse --verify HEAD -q", "r");
char buf[40];
while (fgets(buf, sizeof(buf), sha) != 0) {
strcat(target, buf);
}
pclose(sha);
}

int is_in_git_repo()
{
if(system("git rev-parse --verify HEAD -q"))
return 0;
return 1;
}

store_data(const char *filepath, const char *data)
{
FILE *fp = fopen(filepath, "w");
if (fp != NULL)
{
fputs(data, fp);
fclose(fp);
}
}

0 comments on commit b374397

Please sign in to comment.