updaterevision/updaterevision.c

Wed, 05 Mar 2014 18:42:26 +0200

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Wed, 05 Mar 2014 18:42:26 +0200
changeset 651
747e84062279
parent 632
63e6243b880c
child 659
06c4477675e4
permissions
-rw-r--r--

- doxygenified Version.h

632
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 /* updaterevision.c
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 *
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
3 * Public domain. This program uses git commands command to get
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4 * various bits of repository status for a particular directory
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 * and writes it into a header file so that it can be used for a
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 * project's versioning.
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 */
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 #define _CRT_SECURE_NO_DEPRECATE
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 #include <stdio.h>
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 #include <stdlib.h>
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 #include <string.h>
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
14 #include <ctype.h>
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 #include <errno.h>
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17 #ifdef _WIN32
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18 #define popen _popen
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 #define pclose _pclose
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 #endif
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22 // Used to strip newline characters from lines read by fgets.
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23 void stripnl(char *str)
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24 {
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25 if (*str != '\0')
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26 {
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27 size_t len = strlen(str);
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28 if (str[len - 1] == '\n')
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29 {
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30 str[len - 1] = '\0';
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 }
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32 }
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33 }
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 int main(int argc, char **argv)
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36 {
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37 char vertag[64], lastlog[64], lasthash[64], *hash = NULL;
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 FILE *stream = NULL;
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39 int gotrev = 0, needupdate = 1;
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41 vertag[0] = '\0';
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 lastlog[0] = '\0';
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44 if (argc != 2)
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45 {
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46 fprintf(stderr, "Usage: %s <path to gitinfo.h>\n", argv[0]);
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47 return 1;
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48 }
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
50 // Use git describe --tags to get a version string. If we are sitting directly
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51 // on a tag, it returns that tag. Otherwise it returns <most recent tag>-<number of
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52 // commits since the tag>-<short hash>.
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 // Use git log to get the time of the latest commit in ISO 8601 format and its full hash.
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54 stream = popen("git describe --tags && git log -1 --format=%ai*%H", "r");
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56 if (NULL != stream)
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57 {
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 if (fgets(vertag, sizeof vertag, stream) == vertag &&
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59 fgets(lastlog, sizeof lastlog, stream) == lastlog)
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60 {
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61 stripnl(vertag);
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62 stripnl(lastlog);
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63 gotrev = 1;
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 }
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66 pclose(stream);
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67 }
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69 if (gotrev)
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70 {
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71 hash = strchr(lastlog, '*');
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72 if (hash != NULL)
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73 {
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74 *hash = '\0';
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75 hash++;
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76 }
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77 }
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
78 if (hash == NULL)
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79 {
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80 fprintf(stderr, "Failed to get commit info: %s\n", strerror(errno));
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81 strcpy(vertag, "<unknown version>");
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82 lastlog[0] = '\0';
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83 lastlog[1] = '0';
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84 lastlog[2] = '\0';
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85 hash = lastlog + 1;
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
86 }
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88 stream = fopen (argv[1], "r");
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89 if (stream != NULL)
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90 {
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91 if (!gotrev)
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 { // If we didn't get a revision but the file does exist, leave it alone.
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93 fclose (stream);
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94 return 0;
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95 }
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
96 // Read the revision that's in this file already. If it's the same as
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97 // what we've got, then we don't need to modify it and can avoid rebuilding
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
98 // dependant files.
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
99 if (fgets(lasthash, sizeof lasthash, stream) == lasthash)
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
100 {
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
101 stripnl(lasthash);
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102 if (strcmp(hash, lasthash + 3) == 0)
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
103 {
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
104 needupdate = 0;
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
105 }
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
106 }
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
107 fclose (stream);
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
108 }
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
109
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
110 if (needupdate)
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
111 {
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
112 stream = fopen (argv[1], "w");
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
113 if (stream == NULL)
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
114 {
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
115 return 1;
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
116 }
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
117 fprintf(stream,
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
118 "// %s\n"
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
119 "//\n"
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
120 "// This file was automatically generated by the\n"
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
121 "// updaterevision tool. Do not edit by hand.\n"
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
122 "\n"
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
123 "#define GIT_DESCRIPTION \"%s\"\n"
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
124 "#define GIT_HASH \"%s\"\n"
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
125 "#define GIT_TIME \"%s\"\n",
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
126 hash, vertag, hash, lastlog);
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
127 fclose(stream);
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
128 fprintf(stderr, "%s updated to commit %s.\n", argv[1], vertag);
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
129 }
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
130 else
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
131 {
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
132 fprintf (stderr, "%s is up to date at commit %s.\n", argv[1], vertag);
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
133 }
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
134
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
135 return 0;
63e6243b880c - migrated to CMake again
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
136 }

mercurial