2 #include "ATLComTime.h"
\r
6 #include "UnicodeUtils.h"
\r
8 class CException; //Just in case afx.h is not included (cannot be included in every project which uses this file)
\r
10 // provide an ASSERT macro for when compiled without MFC
\r
12 // Don't use _asm here, it isn't supported by x64 version of compiler. In fact, MFC's ASSERT() is the same with _ASSERTE().
\r
13 #define ASSERT(x) _ASSERTE(x)
\r
17 GitRev::GitRev(void)
\r
22 // fetch local machine timezone info
\r
23 if ( GetTimeZoneInformation( &m_TimeZone ) == TIME_ZONE_ID_INVALID )
\r
29 GitRev::~GitRev(void)
\r
34 GitRev::GitRev(GitRev & rev)
\r
37 GitRev& GitRev::operator=(GitRev &rev)
\r
42 void GitRev::Clear()
\r
45 this->m_Files.Clear();
\r
47 this->m_ParentHash.clear();
\r
48 m_CommitterName.Empty();
\r
49 m_CommitterEmail.Empty();
\r
52 m_CommitHash.Empty();
\r
54 m_RefAction.Empty();
\r
58 int GitRev::CopyFrom(GitRev &rev,bool OmitParentAndMark)
\r
60 m_AuthorName =rev.m_AuthorName ;
\r
61 m_AuthorEmail =rev.m_AuthorEmail ;
\r
62 m_AuthorDate =rev.m_AuthorDate ;
\r
63 m_CommitterName =rev.m_CommitterName ;
\r
64 m_CommitterEmail=rev.m_CommitterEmail;
\r
65 m_CommitterDate =rev.m_CommitterDate ;
\r
66 m_Subject =rev.m_Subject ;
\r
67 m_Body =rev.m_Body ;
\r
68 m_CommitHash =rev.m_CommitHash ;
\r
69 m_Files =rev.m_Files ;
\r
70 m_Action =rev.m_Action ;
\r
71 m_IsFull =rev.m_IsFull;
\r
73 if(!OmitParentAndMark)
\r
75 m_ParentHash =rev.m_ParentHash ;
\r
80 int GitRev::ParserFromLog(BYTE_VECTOR &log,int start)
\r
86 BYTE_VECTOR filelist;
\r
89 this->m_Files.Clear();
\r
94 while( pos < log.size() && pos>=0)
\r
97 //one=log.Tokenize(_T("\n"),pos);
\r
98 if(log[pos]==_T('#') && log[pos+1] == _T('<') && log[pos+3] == _T('>'))
\r
100 //text = one.Right(one.GetLength()-4);
\r
102 g_Git.StringAppend(&text,&log[pos+4],CGit::m_LogEncode);
\r
107 case LOG_REV_ITEM_BEGIN:
\r
114 case LOG_REV_AUTHOR_NAME:
\r
115 this->m_AuthorName = text;
\r
117 case LOG_REV_AUTHOR_EMAIL:
\r
118 this->m_AuthorEmail = text;
\r
120 case LOG_REV_AUTHOR_DATE:
\r
121 this->m_AuthorDate =ConverFromString(text);
\r
123 case LOG_REV_COMMIT_NAME:
\r
124 this->m_CommitterName = text;
\r
126 case LOG_REV_COMMIT_EMAIL:
\r
127 this->m_CommitterEmail = text;
\r
129 case LOG_REV_COMMIT_DATE:
\r
130 this->m_CommitterDate =ConverFromString(text);
\r
132 case LOG_REV_COMMIT_SUBJECT:
\r
133 this->m_Subject = text;
\r
135 case LOG_REV_COMMIT_BODY:
\r
136 this->m_Body = text +_T("\n");
\r
138 case LOG_REV_COMMIT_HASH:
\r
139 this->m_CommitHash = text.Right(40);
\r
140 if(text.GetLength()>40)
\r
142 this->m_Mark=text[0];
\r
145 case LOG_REV_COMMIT_PARENT:
\r
146 while(text.GetLength()>0)
\r
148 this->m_ParentHash.insert(this->m_ParentHash.end(),text.Left(40));
\r
149 if(text.GetLength()>40)
\r
150 text=text.Right(text.GetLength()-41);
\r
154 if(m_ParentHash.size()>1)
\r
159 case LOG_REV_COMMIT_FILE:
\r
166 // case LOG_REV_COMMIT_BODY:
\r
167 // this->m_Body += one+_T("\n");
\r
169 case LOG_REV_COMMIT_FILE:
\r
170 //filelist += one +_T("\n");
\r
171 //filelist.append(log,pos,log.find(0,pos));
\r
183 //find next string start
\r
184 pos=log.findNextString(pos);
\r
190 filelist.append(log,filebegin,pos);
\r
191 this->m_Files.ParserFromLog(filelist);
\r
192 this->m_Action=this->m_Files.GetAction();
\r
197 CTime GitRev::ConverFromString(CString input)
\r
199 // pick up date from string
\r
202 COleDateTime tm(_wtoi(input.Mid(0,4)),
\r
203 _wtoi(input.Mid(5,2)),
\r
204 _wtoi(input.Mid(8,2)),
\r
205 _wtoi(input.Mid(11,2)),
\r
206 _wtoi(input.Mid(14,2)),
\r
207 _wtoi(input.Mid(17,2)));
\r
208 if( tm.GetStatus() != COleDateTime::valid )
\r
209 return CTime();//Error parsing time-string
\r
211 // pick up utc offset
\r
212 CString sign = input.Mid(20,1); // + or -
\r
213 int hoursOffset = _wtoi(input.Mid(21,2));
\r
214 int minsOffset = _wtoi(input.Mid(23,2));
\r
215 // convert to a fraction of a day
\r
216 double offset = (hoursOffset*60 + minsOffset) / 1440.0; // 1440 mins = 1 day
\r
221 // we have to subtract this from the time given to get UTC
\r
223 // get utc time as a SYSTEMTIME
\r
224 SYSTEMTIME sysTime;
\r
225 tm.GetAsSystemTime( sysTime );
\r
226 // and convert to users local time
\r
228 if ( SystemTimeToTzSpecificLocalTime( &m_TimeZone, &sysTime, &local ) )
\r
234 ASSERT(false); // this should not happen but leave time in utc if it does
\r
236 // convert to CTime and return
\r
237 return CTime( sysTime, -1 );;
\r
239 catch(CException* e)
\r
241 //Probably the date was something like 1970-01-01 00:00:00. _mktime64() doesnt like this.
\r
242 //Dont let the application crash on this exception
\r
244 #ifdef _AFX //CException classes are only defined when afx.h is included.
\r
245 //When afx.h is not included, the exception is leaked.
\r
246 //This will probably never happen because when CException is not defined, it cannot be thrown.
\r
248 #endif //ifdef _AFX
\r
250 return CTime(); //Return an invalid time
\r
253 int GitRev::SafeFetchFullInfo(CGit *git)
\r
255 if(InterlockedExchange(&m_IsUpdateing,TRUE) == FALSE)
\r
259 BYTE_VECTOR onelog;
\r
260 TCHAR oldmark=this->m_Mark;
\r
261 CString commithash = m_CommitHash;
\r
262 git->GetLog(onelog,commithash,NULL,1,CGit::LOG_INFO_FULL_DIFF|CGit::LOG_INFO_STAT|CGit::LOG_INFO_FILESTATE|CGit::LOG_INFO_DETECT_COPYRENAME|CGit::LOG_INFO_SHOW_MERGEDFILE);
\r
263 CString oldhash=m_CommitHash;
\r
264 GIT_REV_LIST oldlist=this->m_ParentHash;
\r
265 ParserFromLog(onelog);
\r
267 //ASSERT(oldhash==m_CommitHash);
\r
269 this->m_Mark=oldmark; //parser full log will cause old mark overwrited.
\r
270 //So we need keep old bound mark.
\r
271 this->m_ParentHash=oldlist;
\r
272 InterlockedExchange(&m_IsUpdateing,FALSE);
\r
273 InterlockedExchange(&m_IsFull,TRUE);
\r
276 this->m_Files.Clear();
\r
277 git->CheckAndInitDll();
\r
279 GIT_COMMIT_LIST list;
\r
281 if(git_get_commit_from_hash(&commit, this->m_CommitHash.m_hash))
\r
285 bool isRoot = this->m_ParentHash.size()==0;
\r
286 git_get_commit_first_parent(&commit,&list);
\r
287 while(git_get_commit_next_parent(&list,parent) == 0 || isRoot)
\r
292 git_root_diff(git->GetGitDiff(), this->m_CommitHash.m_hash, &file, &count);
\r
294 git_diff(git->GetGitDiff(),parent,commit.m_hash,&file,&count);
\r
299 CString strnewname;
\r
300 CString stroldname;
\r
302 for(int j=0;j<count;j++)
\r
308 strnewname.Empty();
\r
309 stroldname.Empty();
\r
311 int mode,IsBin,inc,dec;
\r
312 git_get_diff_file(git->GetGitDiff(),file,j,&newname,&oldname,
\r
313 &mode,&IsBin,&inc,&dec);
\r
315 git->StringAppend(&strnewname,(BYTE*)newname,CP_ACP);
\r
316 git->StringAppend(&stroldname,(BYTE*)oldname,CP_ACP);
\r
318 path.m_ParentNo = i;
\r
319 path.SetFromGit(strnewname,&stroldname);
\r
320 path.ParserAction((BYTE)mode);
\r
322 this->m_Action|=path.m_Action;
\r
326 path.m_StatAdd=_T("-");
\r
327 path.m_StatDel=_T("-");
\r
330 path.m_StatAdd.Format(_T("%d"),inc);
\r
331 path.m_StatDel.Format(_T("%d"),dec);
\r
333 m_Files.AddPath(path);
\r
335 git_diff_flush(git->GetGitDiff());
\r
339 InterlockedExchange(&m_IsUpdateing,FALSE);
\r
340 InterlockedExchange(&m_IsFull,TRUE);
\r
346 int GitRev::ParserParentFromCommit(GIT_COMMIT *commit)
\r
348 this->m_ParentHash.clear();
\r
349 GIT_COMMIT_LIST list;
\r
352 git_get_commit_first_parent(commit,&list);
\r
353 while(git_get_commit_next_parent(&list,parent)==0)
\r
355 m_ParentHash.push_back(CGitHash((char *)parent));
\r
360 int GitRev::ParserFromCommit(GIT_COMMIT *commit)
\r
362 int encode =CP_UTF8;
\r
364 if(commit->m_Encode != 0 && commit->m_EncodeSize != 0)
\r
367 g_Git.StringAppend(&str, (BYTE*)commit->m_Encode, CP_UTF8, commit->m_EncodeSize);
\r
368 encode = CUnicodeUtils::GetCPCode(str);
\r
371 this->m_AuthorDate = commit->m_Author.Date;
\r
373 this->m_AuthorEmail.Empty();
\r
374 g_Git.StringAppend(&m_AuthorEmail,(BYTE*)commit->m_Author.Email,CP_UTF8,commit->m_Author.EmailSize);
\r
376 this->m_AuthorName.Empty();
\r
377 g_Git.StringAppend(&m_AuthorName,(BYTE*)commit->m_Author.Name,CP_UTF8,commit->m_Author.NameSize);
\r
379 this->m_Body.Empty();
\r
380 g_Git.StringAppend(&m_Body,(BYTE*)commit->m_Body,encode,commit->m_BodySize);
\r
382 this->m_CommitterDate = commit->m_Committer.Date;
\r
384 this->m_CommitterEmail.Empty();
\r
385 g_Git.StringAppend(&m_CommitterEmail, (BYTE*)commit->m_Committer.Email,CP_UTF8, commit->m_Committer.EmailSize);
\r
387 this->m_CommitterName.Empty();
\r
388 g_Git.StringAppend(&m_CommitterName, (BYTE*)commit->m_Committer.Name,CP_UTF8, commit->m_Committer.NameSize);
\r
390 this->m_Subject.Empty();
\r
391 g_Git.StringAppend(&m_Subject, (BYTE*)commit->m_Subject,encode,commit->m_SubjectSize);
\r
396 #define TRACE(x) 1?0:(x)
\r
398 void GitRev::DbgPrint()
\r
400 TRACE(_T("Commit %s\r\n"), this->m_CommitHash.ToString());
\r
401 for(int i=0;i<this->m_ParentHash.size();i++)
\r
403 TRACE(_T("Parent %i %s"),i, m_ParentHash[i].ToString());
\r