-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-hash.c
86 lines (75 loc) · 1.58 KB
/
git-hash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <stdio.h>
#include <string.h>
#include <time.h>
//#define DEBUG
#ifdef __unix__
#elif defined(_WIN32) || defined(WIN32)
#ifdef DEBUG
#define OS_Windows
#endif
#define popen _popen
#define pclose _pclose
#endif
char static str[281];
int main()
{
if(!IsCurrentlyInGitRepo())
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, "/*\n");
strcat(str, " * git-hash 1.1.2 (https://github.com/Jeroen52/samp-git-hash)\n");
strcat(str, " * This file has been generated at ");
AppendCurrentTimeToString(str);
strcat(str, " */\n\n");
strcat(str, "#define _INC_GIT_HASH_GIT_HASH \"");
AppendGitShaToString(str);
str[(strlen(str)-1)] = '\0';
strcat(str, "\"");
store_data("git-hash.inc", str);
#ifdef DEBUG
printf("%d\n", strlen(str));
#ifdef OS_Windows
getchar();
#endif
#endif
}
AppendGitShaToString(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);
return 1;
}
int IsCurrentlyInGitRepo()
{
if(system("git rev-parse --verify HEAD -q"))
return 0;
return 1;
}
AppendCurrentTimeToString(char *target)
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
strcat(str, asctime (timeinfo));
return 1;
}
store_data(const char *filepath, const char *data)
{
FILE *fp = fopen(filepath, "w");
if (fp != NULL)
{
fputs(data, fp);
fclose(fp);
return 1;
}
return 0;
}