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