Fixed issue #221: After resolving all merge conflicts, it's not obvious the project...
[TortoiseGit.git] / src / Git / GitRev.cpp
blob1883ac7b9222f1e4079cb4453a688f66826f490f
1 #include "StdAfx.h"
2 #include "ATLComTime.h"
3 #include "GitRev.h"
4 #include "Git.h"
5 #include "GitDLL.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
11 #if !defined ASSERT
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)
14 #endif
17 GitRev::GitRev(void)
19 m_Action=0;
20 m_IsFull = 0;
21 m_IsUpdateing = 0;
22 // fetch local machine timezone info
23 if ( GetTimeZoneInformation( &m_TimeZone ) == TIME_ZONE_ID_INVALID )
25 ASSERT(false);
29 GitRev::~GitRev(void)
33 #if 0
34 GitRev::GitRev(GitRev & rev)
37 GitRev& GitRev::operator=(GitRev &rev)
39 return *this;
41 #endif
42 void GitRev::Clear()
44 this->m_Action=0;
45 this->m_Files.Clear();
46 this->m_Action=0;
47 this->m_ParentHash.clear();
48 m_CommitterName.Empty();
49 m_CommitterEmail.Empty();
50 m_Body.Empty();
51 m_Subject.Empty();
52 m_CommitHash.Empty();
53 m_Ref.Empty();
54 m_RefAction.Empty();
55 m_Mark=0;
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 ;
67 m_Body =rev.m_Body ;
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 ;
76 m_Mark =rev.m_Mark;
78 return 0;
80 int GitRev::ParserFromLog(BYTE_VECTOR &log,int start)
82 int pos=start;
83 CString one;
84 CString key;
85 CString text;
86 BYTE_VECTOR filelist;
87 BYTE mode=0;
88 CTGitPath path;
89 this->m_Files.Clear();
90 m_Action=0;
91 int begintime=0;
92 int filebegin=-1;
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);
101 text.Empty();
102 g_Git.StringAppend(&text,&log[pos+4],CGit::m_LogEncode);
103 mode = log[pos+2];
105 switch(mode)
107 case LOG_REV_ITEM_BEGIN:
108 begintime++;
109 if(begintime>1)
110 break;
111 else
112 this->Clear();
113 break;
114 case LOG_REV_AUTHOR_NAME:
115 this->m_AuthorName = text;
116 break;
117 case LOG_REV_AUTHOR_EMAIL:
118 this->m_AuthorEmail = text;
119 break;
120 case LOG_REV_AUTHOR_DATE:
121 this->m_AuthorDate =ConverFromString(text);
122 break;
123 case LOG_REV_COMMIT_NAME:
124 this->m_CommitterName = text;
125 break;
126 case LOG_REV_COMMIT_EMAIL:
127 this->m_CommitterEmail = text;
128 break;
129 case LOG_REV_COMMIT_DATE:
130 this->m_CommitterDate =ConverFromString(text);
131 break;
132 case LOG_REV_COMMIT_SUBJECT:
133 this->m_Subject = text;
134 break;
135 case LOG_REV_COMMIT_BODY:
136 this->m_Body = text +_T("\n");
137 break;
138 case LOG_REV_COMMIT_HASH:
139 this->m_CommitHash = text.Right(40);
140 if(text.GetLength()>40)
142 this->m_Mark=text[0];
144 break;
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);
151 else
152 break;
154 if(m_ParentHash.size()>1)
156 int a=1;
158 break;
159 case LOG_REV_COMMIT_FILE:
160 break;
162 }else
164 switch(mode)
166 // case LOG_REV_COMMIT_BODY:
167 // this->m_Body += one+_T("\n");
168 // break;
169 case LOG_REV_COMMIT_FILE:
170 //filelist += one +_T("\n");
171 //filelist.append(log,pos,log.find(0,pos));
172 if(filebegin<0)
173 filebegin=pos;
174 break;
178 if(begintime>1)
180 break;
183 //find next string start
184 pos=log.findNextString(pos);
187 if(filebegin>=0)
190 filelist.append(log,filebegin,pos);
191 this->m_Files.ParserFromLog(filelist);
192 this->m_Action=this->m_Files.GetAction();
194 return pos;
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
217 if ( sign == "-" )
219 offset = -offset;
221 // we have to subtract this from the time given to get UTC
222 tm -= offset;
223 // get utc time as a SYSTEMTIME
224 SYSTEMTIME sysTime;
225 tm.GetAsSystemTime( sysTime );
226 // and convert to users local time
227 SYSTEMTIME local;
228 if ( SystemTimeToTzSpecificLocalTime( &m_TimeZone, &sysTime, &local ) )
230 sysTime = local;
232 else
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 );;
239 catch(CException* e)
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.
247 e->Delete();
248 #endif //ifdef _AFX
250 return CTime(); //Return an invalid time
253 int GitRev::SafeFetchFullInfo(CGit *git)
255 if(InterlockedExchange(&m_IsUpdateing,TRUE) == FALSE)
257 #if 0
258 //GitRev rev;
259 BYTE_VECTOR onelog;
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);
268 if(oldmark!=0)
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);
274 return 0;
275 #endif
276 this->m_Files.Clear();
277 git->CheckAndInitDll();
278 GIT_COMMIT commit;
279 GIT_COMMIT_LIST list;
280 GIT_HASH parent;
281 if(git_get_commit_from_hash(&commit, this->m_CommitHash.m_hash))
282 return -1;
284 int i=0;
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)
289 GIT_FILE file;
290 int count;
291 if(isRoot)
292 git_root_diff(git->GetGitDiff(), this->m_CommitHash.m_hash, &file, &count);
293 else
294 git_diff(git->GetGitDiff(),parent,commit.m_hash,&file,&count);
296 isRoot = false;
298 CTGitPath path;
299 CString strnewname;
300 CString stroldname;
302 for(int j=0;j<count;j++)
304 path.Reset();
305 char *newname;
306 char *oldname;
308 strnewname.Empty();
309 stroldname.Empty();
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);
318 path.m_ParentNo = i;
319 path.SetFromGit(strnewname,&stroldname);
320 path.ParserAction((BYTE)mode);
322 this->m_Action|=path.m_Action;
324 if(IsBin)
326 path.m_StatAdd=_T("-");
327 path.m_StatDel=_T("-");
328 }else
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());
336 i++;
339 InterlockedExchange(&m_IsUpdateing,FALSE);
340 InterlockedExchange(&m_IsFull,TRUE);
343 return -1;
346 int GitRev::ParserParentFromCommit(GIT_COMMIT *commit)
348 this->m_ParentHash.clear();
349 GIT_COMMIT_LIST list;
350 GIT_HASH parent;
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));
357 return 0;
360 int GitRev::ParserFromCommit(GIT_COMMIT *commit)
362 int encode =CP_UTF8;
364 if(commit->m_Encode != 0 && commit->m_EncodeSize != 0)
366 CString str;
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);
393 return 0;
395 #ifndef TRACE
396 #define TRACE(x) 1?0:(x)
397 #endif
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());
405 TRACE(_T("\n"));
408 int GitRev::GetCommitFromHash(CGitHash &hash)
410 g_Git.CheckAndInitDll();
412 GIT_COMMIT commit;
413 if(git_get_commit_from_hash( &commit, hash.m_hash))
414 return -1;
416 this->ParserFromCommit(&commit);
417 git_free_commit(&commit);
419 this->m_CommitHash=hash;
421 return 0;
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");
434 return 0;
436 CStringA rev;
437 rev= CUnicodeUtils::GetUTF8(refname);
438 GIT_HASH sha;
440 if(git_get_sha1(rev.GetBuffer(),sha))
441 return -1;
443 GetCommitFromHash(CGitHash((char*)sha));
444 return 0;