Fix compile error in TortoiseShell build.
[TortoiseGit.git] / src / Git / GitRev.cpp
blobc428f9c3ec36bc6452940f6ec304b6e1f520a78c
1 #include "StdAfx.h"
2 #include "ATLComTime.h"
3 #include "GitRev.h"
4 #include "Git.h"
6 class CException; //Just in case afx.h is not included (cannot be included in every project which uses this file)
8 // provide an ASSERT macro for when compiled without MFC
9 #if !defined ASSERT
10 // Don't use _asm here, it isn't supported by x64 version of compiler. In fact, MFC's ASSERT() is the same with _ASSERTE().
11 #define ASSERT(x) _ASSERTE(x)
12 #endif
15 GitRev::GitRev(void)
17 m_Action=0;
18 m_IsFull = 0;
19 m_IsUpdateing = 0;
20 // fetch local machine timezone info
21 if ( GetTimeZoneInformation( &m_TimeZone ) == TIME_ZONE_ID_INVALID )
23 ASSERT(false);
27 GitRev::~GitRev(void)
31 #if 0
32 GitRev::GitRev(GitRev & rev)
35 GitRev& GitRev::operator=(GitRev &rev)
37 return *this;
39 #endif
40 void GitRev::Clear()
42 this->m_Action=0;
43 this->m_Files.Clear();
44 this->m_Action=0;
45 this->m_ParentHash.clear();
46 m_CommitterName.Empty();
47 m_CommitterEmail.Empty();
48 m_Body.Empty();
49 m_Subject.Empty();
50 m_CommitHash.Empty();
51 m_Mark=0;
54 int GitRev::CopyFrom(GitRev &rev,bool OmitParentAndMark)
56 m_AuthorName =rev.m_AuthorName ;
57 m_AuthorEmail =rev.m_AuthorEmail ;
58 m_AuthorDate =rev.m_AuthorDate ;
59 m_CommitterName =rev.m_CommitterName ;
60 m_CommitterEmail=rev.m_CommitterEmail;
61 m_CommitterDate =rev.m_CommitterDate ;
62 m_Subject =rev.m_Subject ;
63 m_Body =rev.m_Body ;
64 m_CommitHash =rev.m_CommitHash ;
65 m_Files =rev.m_Files ;
66 m_Action =rev.m_Action ;
68 if(!OmitParentAndMark)
70 m_ParentHash =rev.m_ParentHash ;
71 m_Mark =rev.m_Mark;
73 return 0;
75 int GitRev::ParserFromLog(BYTE_VECTOR &log,int start)
77 int pos=start;
78 CString one;
79 CString key;
80 CString text;
81 BYTE_VECTOR filelist;
82 BYTE mode=0;
83 CTGitPath path;
84 this->m_Files.Clear();
85 m_Action=0;
86 int begintime=0;
87 int filebegin=-1;
89 while( pos < log.size() && pos>=0)
92 //one=log.Tokenize(_T("\n"),pos);
93 if(log[pos]==_T('#') && log[pos+1] == _T('<') && log[pos+3] == _T('>'))
95 //text = one.Right(one.GetLength()-4);
96 text.Empty();
97 g_Git.StringAppend(&text,&log[pos+4],CGit::m_LogEncode);
98 mode = log[pos+2];
100 switch(mode)
102 case LOG_REV_ITEM_BEGIN:
103 begintime++;
104 if(begintime>1)
105 break;
106 else
107 this->Clear();
108 break;
109 case LOG_REV_AUTHOR_NAME:
110 this->m_AuthorName = text;
111 break;
112 case LOG_REV_AUTHOR_EMAIL:
113 this->m_AuthorEmail = text;
114 break;
115 case LOG_REV_AUTHOR_DATE:
116 this->m_AuthorDate =ConverFromString(text);
117 break;
118 case LOG_REV_COMMIT_NAME:
119 this->m_CommitterName = text;
120 break;
121 case LOG_REV_COMMIT_EMAIL:
122 this->m_CommitterEmail = text;
123 break;
124 case LOG_REV_COMMIT_DATE:
125 this->m_CommitterDate =ConverFromString(text);
126 break;
127 case LOG_REV_COMMIT_SUBJECT:
128 this->m_Subject = text;
129 break;
130 case LOG_REV_COMMIT_BODY:
131 this->m_Body = text +_T("\n");
132 break;
133 case LOG_REV_COMMIT_HASH:
134 this->m_CommitHash = text.Right(40);
135 if(text.GetLength()>40)
137 this->m_Mark=text[0];
139 break;
140 case LOG_REV_COMMIT_PARENT:
141 while(text.GetLength()>0)
143 this->m_ParentHash.insert(this->m_ParentHash.end(),text.Left(40));
144 if(text.GetLength()>40)
145 text=text.Right(text.GetLength()-41);
146 else
147 break;
149 if(m_ParentHash.size()>1)
151 int a=1;
153 break;
154 case LOG_REV_COMMIT_FILE:
155 break;
157 }else
159 switch(mode)
161 // case LOG_REV_COMMIT_BODY:
162 // this->m_Body += one+_T("\n");
163 // break;
164 case LOG_REV_COMMIT_FILE:
165 //filelist += one +_T("\n");
166 //filelist.append(log,pos,log.find(0,pos));
167 if(filebegin<0)
168 filebegin=pos;
169 break;
173 if(begintime>1)
175 break;
178 //find next string start
179 pos=log.findNextString(pos);
182 if(filebegin>=0)
185 filelist.append(log,filebegin,pos);
186 this->m_Files.ParserFromLog(filelist);
187 this->m_Action=this->m_Files.GetAction();
189 return pos;
192 CTime GitRev::ConverFromString(CString input)
194 // pick up date from string
197 COleDateTime tm(_wtoi(input.Mid(0,4)),
198 _wtoi(input.Mid(5,2)),
199 _wtoi(input.Mid(8,2)),
200 _wtoi(input.Mid(11,2)),
201 _wtoi(input.Mid(14,2)),
202 _wtoi(input.Mid(17,2)));
203 if( tm.GetStatus() != COleDateTime::valid )
204 return CTime();//Error parsing time-string
206 // pick up utc offset
207 CString sign = input.Mid(20,1); // + or -
208 int hoursOffset = _wtoi(input.Mid(21,2));
209 int minsOffset = _wtoi(input.Mid(23,2));
210 // convert to a fraction of a day
211 double offset = (hoursOffset*60 + minsOffset) / 1440.0; // 1440 mins = 1 day
212 if ( sign == "-" )
214 offset = -offset;
216 // we have to subtract this from the time given to get UTC
217 tm -= offset;
218 // get utc time as a SYSTEMTIME
219 SYSTEMTIME sysTime;
220 tm.GetAsSystemTime( sysTime );
221 // and convert to users local time
222 SYSTEMTIME local;
223 if ( SystemTimeToTzSpecificLocalTime( &m_TimeZone, &sysTime, &local ) )
225 sysTime = local;
227 else
229 ASSERT(false); // this should not happen but leave time in utc if it does
231 // convert to CTime and return
232 return CTime( sysTime, -1 );;
234 catch(CException* e)
236 //Probably the date was something like 1970-01-01 00:00:00. _mktime64() doesnt like this.
237 //Dont let the application crash on this exception
239 #ifdef _AFX //CException classes are only defined when afx.h is included.
240 //When afx.h is not included, the exception is leaked.
241 //This will probably never happen because when CException is not defined, it cannot be thrown.
242 e->Delete();
243 #endif //ifdef _AFX
245 return CTime(); //Return an invalid time
248 int GitRev::SafeFetchFullInfo(CGit *git)
250 if(InterlockedExchange(&m_IsUpdateing,TRUE) == FALSE)
252 //GitRev rev;
253 BYTE_VECTOR onelog;
254 TCHAR oldmark=this->m_Mark;
256 git->GetLog(onelog,m_CommitHash,NULL,1,CGit::LOG_INFO_STAT|CGit::LOG_INFO_FILESTATE|CGit::LOG_INFO_DETECT_COPYRENAME|CGit::LOG_INFO_SHOW_MERGEDFILE);
257 CString oldhash=m_CommitHash;
258 GIT_REV_LIST oldlist=this->m_ParentHash;
259 ParserFromLog(onelog);
261 //ASSERT(oldhash==m_CommitHash);
262 if(oldmark!=0)
263 this->m_Mark=oldmark; //parser full log will cause old mark overwrited.
264 //So we need keep old bound mark.
265 this->m_ParentHash=oldlist;
266 InterlockedExchange(&m_IsUpdateing,FALSE);
267 InterlockedExchange(&m_IsFull,TRUE);
268 return 0;
270 return -1;