updaterevision/updaterevision.c

changeset 118
e3361cf7cbf4
parent 82
841562f5a32f
child 123
ad33901eb4f6
equal deleted inserted replaced
117:cc1e14d55047 118:e3361cf7cbf4
2 * 2 *
3 * Public domain. This program uses git commands command to get 3 * Public domain. This program uses git commands command to get
4 * various bits of repository status for a particular directory 4 * various bits of repository status for a particular directory
5 * and writes it into a header file so that it can be used for a 5 * and writes it into a header file so that it can be used for a
6 * project's versioning. 6 * project's versioning.
7 *
8 * 2014-03-30: [crimsondusk] added GIT_BRANCH, doubled buffer sizes
7 */ 9 */
8 10
9 #define _CRT_SECURE_NO_DEPRECATE 11 #define _CRT_SECURE_NO_DEPRECATE
10 12
11 #include <stdio.h> 13 #include <stdio.h>
32 } 34 }
33 } 35 }
34 36
35 int main(int argc, char **argv) 37 int main(int argc, char **argv)
36 { 38 {
37 char vertag[64], lastlog[64], lasthash[64], *hash = NULL; 39 char vertag[128], lastlog[128], lasthash[128], branch[128], *hash = NULL;
38 FILE *stream = NULL; 40 FILE *stream = NULL;
39 int gotrev = 0, needupdate = 1; 41 int gotrev = 0, needupdate = 1;
40 42
41 vertag[0] = '\0'; 43 vertag[0] = '\0';
42 lastlog[0] = '\0'; 44 lastlog[0] = '\0';
45 branch[0] = '\0';
43 46
44 if (argc != 2) 47 if (argc != 2)
45 { 48 {
46 fprintf(stderr, "Usage: %s <path to gitinfo.h>\n", argv[0]); 49 fprintf(stderr, "Usage: %s <path to gitinfo.h>\n", argv[0]);
47 return 1; 50 return 1;
49 52
50 // Use git describe --tags to get a version string. If we are sitting directly 53 // Use git describe --tags to get a version string. If we are sitting directly
51 // on a tag, it returns that tag. Otherwise it returns <most recent tag>-<number of 54 // on a tag, it returns that tag. Otherwise it returns <most recent tag>-<number of
52 // commits since the tag>-<short hash>. 55 // commits since the tag>-<short hash>.
53 // Use git log to get the time of the latest commit in ISO 8601 format and its full hash. 56 // Use git log to get the time of the latest commit in ISO 8601 format and its full hash.
54 stream = popen("git describe --tags && git log -1 --format=%ai*%H", "r"); 57 // [crimsondusk] Use git rev-parse --abbrev-ref HEAD to get the branch.
58 stream = popen("git describe --tags && git log -1 --format=%ai*%H && git rev-parse --abbrev-ref HEAD", "r");
55 59
56 if (NULL != stream) 60 if (NULL != stream)
57 { 61 {
58 if (fgets(vertag, sizeof vertag, stream) == vertag && 62 if (fgets(vertag, sizeof vertag, stream) == vertag &&
59 fgets(lastlog, sizeof lastlog, stream) == lastlog) 63 fgets(lastlog, sizeof lastlog, stream) == lastlog &&
64 fgets(branch, sizeof branch, stream) == branch)
60 { 65 {
61 stripnl(vertag); 66 stripnl(vertag);
62 stripnl(lastlog); 67 stripnl(lastlog);
68 stripnl(branch);
63 gotrev = 1; 69 gotrev = 1;
64 } 70 }
65 71
66 pclose(stream); 72 pclose(stream);
67 } 73 }
81 strcpy(vertag, "<unknown version>"); 87 strcpy(vertag, "<unknown version>");
82 lastlog[0] = '\0'; 88 lastlog[0] = '\0';
83 lastlog[1] = '0'; 89 lastlog[1] = '0';
84 lastlog[2] = '\0'; 90 lastlog[2] = '\0';
85 hash = lastlog + 1; 91 hash = lastlog + 1;
92 strcpy(branch, "<unknown branch>");
86 } 93 }
87 94
88 stream = fopen (argv[1], "r"); 95 stream = fopen (argv[1], "r");
89 if (stream != NULL) 96 if (stream != NULL)
90 { 97 {
120 "// This file was automatically generated by the\n" 127 "// This file was automatically generated by the\n"
121 "// updaterevision tool. Do not edit by hand.\n" 128 "// updaterevision tool. Do not edit by hand.\n"
122 "\n" 129 "\n"
123 "#define GIT_DESCRIPTION \"%s\"\n" 130 "#define GIT_DESCRIPTION \"%s\"\n"
124 "#define GIT_HASH \"%s\"\n" 131 "#define GIT_HASH \"%s\"\n"
125 "#define GIT_TIME \"%s\"\n", 132 "#define GIT_TIME \"%s\"\n"
126 hash, vertag, hash, lastlog); 133 "#define GIT_BRANCH \"%s\"\n",
134 hash, vertag, hash, lastlog, branch);
127 fclose(stream); 135 fclose(stream);
128 fprintf(stderr, "%s updated to commit %s.\n", argv[1], vertag); 136 fprintf(stderr, "%s updated to commit %s.\n", argv[1], vertag);
129 } 137 }
130 else 138 else
131 { 139 {

mercurial