[7297] Fixed profession spells sorting in trainer spell list at client.
[getmangos.git] / src / tools / genrevision / genrevision.cpp
blobace3a0bbea1dbe10bec8e973da33e3584e1cf2cd
1 /*
2 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <fstream>
20 #include <sstream>
21 #include <time.h>
22 #include <stdio.h>
23 #include <string.h>
25 #pragma warning(disable:4996)
27 struct RawData
29 char rev_str[200];
30 char date_str[200];
31 char time_str[200];
34 void extractDataFromSvn(FILE* EntriesFile, bool url, RawData& data)
36 char buf[200];
38 char repo_str[200];
39 char num_str[200];
41 fgets(buf,200,EntriesFile);
42 fgets(buf,200,EntriesFile);
43 fgets(buf,200,EntriesFile);
44 fgets(buf,200,EntriesFile); sscanf(buf,"%s",num_str);
45 fgets(buf,200,EntriesFile); sscanf(buf,"%s",repo_str);
46 fgets(buf,200,EntriesFile);
47 fgets(buf,200,EntriesFile);
48 fgets(buf,200,EntriesFile);
49 fgets(buf,200,EntriesFile);
50 fgets(buf,200,EntriesFile); sscanf(buf,"%10sT%8s",data.date_str,data.time_str);
52 if(url)
53 sprintf(data.rev_str,"%s at %s",num_str,repo_str);
54 else
55 strcpy(data.rev_str,num_str);
58 void extractDataFromGit(FILE* EntriesFile, std::string path, bool url, RawData& data)
60 char buf[200];
62 char hash_str[200];
63 char branch_str[200];
64 char url_str[200];
66 bool found = false;
67 while(fgets(buf,200,EntriesFile))
69 if(sscanf(buf,"%s\t\tbranch %s of %s",hash_str,branch_str,url_str)==3)
71 found = true;
72 break;
76 if(!found)
78 strcpy(data.rev_str,"*");
79 strcpy(data.date_str,"*");
80 strcpy(data.time_str,"*");
81 return;
84 if(url)
86 char* host_str = NULL;
87 char* acc_str = NULL;
88 char* repo_str = NULL;
90 // parse URL like git@github.com:mangos/mangos
91 char url_buf[200];
92 int res = sscanf(url_str,"git@%s",url_buf);
93 if(res)
95 host_str = strtok(url_buf,":");
96 acc_str = strtok(NULL,"/");
97 repo_str = strtok(NULL," ");
99 else
101 res = sscanf(url_str,"git://%s",url_buf);
102 if(res)
104 host_str = strtok(url_buf,"/");
105 acc_str = strtok(NULL,"/");
106 repo_str = strtok(NULL,".");
110 // can generate nice link
111 if(res)
112 sprintf(data.rev_str,"http://%s/%s/%s/commit/%s",host_str,acc_str,repo_str,hash_str);
113 // unknonw URL format, use as-is
114 else
115 sprintf(data.rev_str,"%s at %s",hash_str,url_str);
117 else
118 strcpy(data.rev_str,hash_str);
120 time_t rev_time = 0;
121 // extracting date/time
122 FILE* LogFile = fopen((path+".git/logs/HEAD").c_str(), "r");
123 if(LogFile)
125 while(fgets(buf,200,LogFile))
127 char buf2[200];
128 char new_hash[200];
129 int unix_time = 0;
130 int res2 = sscanf(buf,"%s %s %s %s %i",buf2,new_hash,buf2,buf2,&unix_time);
131 if(res2!=5)
132 continue;
134 if(strcmp(hash_str,new_hash))
135 continue;
137 rev_time = unix_time;
138 break;
141 fclose(LogFile);
143 if(rev_time)
145 tm* aTm = localtime(&rev_time);
146 // YYYY year
147 // MM month (2 digits 01-12)
148 // DD day (2 digits 01-31)
149 // HH hour (2 digits 00-23)
150 // MM minutes (2 digits 00-59)
151 // SS seconds (2 digits 00-59)
152 sprintf(data.date_str,"%04d-%02d-%02d",aTm->tm_year+1900,aTm->tm_mon+1,aTm->tm_mday);
153 sprintf(data.time_str,"%02d:%02d:%02d",aTm->tm_hour,aTm->tm_min,aTm->tm_sec);
155 else
157 strcpy(data.date_str,"*");
158 strcpy(data.time_str,"*");
161 else
163 strcpy(data.date_str,"*");
164 strcpy(data.time_str,"*");
168 bool extractDataFromSvn(std::string filename, bool url, RawData& data)
170 FILE* EntriesFile = fopen(filename.c_str(), "r");
171 if(!EntriesFile)
172 return false;
174 extractDataFromSvn(EntriesFile,url,data);
175 fclose(EntriesFile);
176 return true;
179 bool extractDataFromGit(std::string filename, std::string path, bool url, RawData& data)
181 FILE* EntriesFile = fopen(filename.c_str(), "r");
182 if(!EntriesFile)
183 return false;
185 extractDataFromGit(EntriesFile,path,url,data);
186 fclose(EntriesFile);
187 return true;
190 std::string generateHeader(char const* rev_str, char const* date_str, char const* time_str)
192 std::ostringstream newData;
193 newData << "#ifndef __REVISION_H__" << std::endl;
194 newData << "#define __REVISION_H__" << std::endl;
195 newData << " #define REVISION_ID \"" << rev_str << "\"" << std::endl;
196 newData << " #define REVISION_DATE \"" << date_str << "\"" << std::endl;
197 newData << " #define REVISION_TIME \"" << time_str << "\""<< std::endl;
198 newData << "#endif // __REVISION_H__" << std::endl;
199 return newData.str();
202 int main(int argc, char **argv)
204 bool use_url = false;
205 bool svn_prefered = false;
206 std::string path;
208 // Call: tool {options} [path]
209 // -g use git prefered (default)
210 // -s use svn prefered
211 // -r use only revision (without repo URL) (default)
212 // -u include repositire URL as commit URL or "rev at URL"
213 for(int k = 1; k <= argc; ++k)
215 if(!argv[k] || !*argv[k])
216 break;
218 if(argv[k][0]!='-')
220 path = argv[k];
221 if(path.size() > 0 && (path[path.size()-1]!='/' || path[path.size()-1]!='\\'))
222 path += '/';
223 break;
226 switch(argv[k][1])
228 case 'g':
229 svn_prefered = false;
230 continue;
231 case 'r':
232 use_url = false;
233 continue;
234 case 's':
235 svn_prefered = true;
236 continue;
237 case 'u':
238 use_url = true;
239 continue;
240 default:
241 printf("Unknown option %s",argv[k]);
242 return 1;
246 /// new data extraction
247 std::string newData;
250 RawData data;
252 bool res = false;
254 if(svn_prefered)
256 /// SVN data
257 res = extractDataFromSvn(path+".svn/entries",use_url,data);
258 if (!res)
259 res = extractDataFromSvn(path+"_svn/entries",use_url,data);
260 // GIT data
261 if (!res)
262 res = extractDataFromGit(path+".git/FETCH_HEAD",path,use_url,data);
264 else
266 // GIT data
267 res = extractDataFromGit(path+".git/FETCH_HEAD",path,use_url,data);
268 /// SVN data
269 if (!res)
270 res = extractDataFromSvn(path+".svn/entries",use_url,data);
271 if (!res)
272 res = extractDataFromSvn(path+"_svn/entries",use_url,data);
275 if(res)
276 newData = generateHeader(data.rev_str,data.date_str,data.time_str);
277 else
278 newData = generateHeader("*", "*", "*");
281 /// get existed header data for compare
282 std::string oldData;
284 if(FILE* HeaderFile = fopen("revision.h","rb"))
286 while(!feof(HeaderFile))
288 int c = fgetc(HeaderFile);
289 if(c < 0)
290 break;
291 oldData += (char)c;
294 fclose(HeaderFile);
297 /// update header only if different data
298 if(newData != oldData)
300 if(FILE* OutputFile = fopen("revision.h","wb"))
302 fprintf(OutputFile,"%s",newData.c_str());
303 fclose(OutputFile);
307 return 0;