2 #include "GitLogCache.h"
9 CLogCache::~CLogCache()
11 //this->m_IndexFile.Close();
12 //this->m_DataFile.Close();
14 int CLogCache::AddCacheEntry(GitRev
&Rev
)
16 this->m_HashMap
[Rev
.m_CommitHash
] = Rev
;
20 GitRev
* CLogCache::GetCacheData(CGitHash
&hash
)
22 if(this->m_HashMapIndex
.find(hash
)==m_HashMapIndex
.end())
24 if(this->m_HashMap
.IsExist(hash
))
26 return &m_HashMap
[hash
];
28 m_HashMap
[hash
].m_CommitHash
=hash
;
29 return &m_HashMap
[hash
];
34 if(!LoadOneItem(rev
,m_HashMapIndex
[hash
]))
36 m_HashMap
[hash
].CopyFrom(rev
);
37 m_HashMap
[hash
].m_IsFull
=true;
39 return &m_HashMap
[hash
];
42 m_HashMap
[hash
].m_CommitHash
=hash
;
43 return &m_HashMap
[hash
];
45 int CLogCache::FetchCacheIndex(CString GitDir
)
48 if(this->m_IndexFile
.m_hFile
== CFile::hFileNull
)
50 BOOL b
=m_IndexFile
.Open(GitDir
+_T("\\.git\\")+INDEX_FILE_NAME
,
51 CFile::modeRead
|CFile::shareDenyNone
|
52 CFile::modeNoTruncate
|CFile::modeCreate
);
56 //Cache has been fetched.
57 //if(m_GitDir == GitDir)
61 m_IndexFile
.SeekToBegin();
63 SLogCacheIndexHeader header
;
64 UINT count
=m_IndexFile
.Read(&header
,sizeof(SLogCacheIndexHeader
));
65 if(count
!= sizeof(SLogCacheIndexHeader
))
68 if( header
.m_Magic
!= LOG_INDEX_MAGIC
)
71 if( header
.m_Version
!= LOG_INDEX_VERSION
)
77 //for(int i=0;i<header.m_ItemCount;i++)
78 this->m_HashMapIndex
.clear();
82 count
=m_IndexFile
.Read(&Item
,sizeof(SLogCacheItem
));
83 if( count
!= sizeof(SLogCacheItem
) )
86 this->m_HashMapIndex
[Item
.m_Hash
]=Item
.m_Offset
;
92 int CLogCache::SaveOneItem(GitRev
&Rev
,ULONGLONG offset
)
95 SLogCacheRevItemHeader header
;
96 m_DataFile
.Seek(offset
,CFile::begin
);
98 if(this->m_DataFile
.m_hFile
== CFile::hFileNull
)
100 BOOL b
=m_DataFile
.Open(m_GitDir
+_T("\\.git\\")+INDEX_FILE_NAME
,
101 CFile::modeRead
|CFile::shareDenyNone
|
102 CFile::modeNoTruncate
|CFile::modeCreate
);
107 header
.m_Magic
=LOG_DATA_ITEM_MAGIC
;
108 header
.m_Version
=LOG_INDEX_VERSION
;
109 header
.m_FileCount
=Rev
.m_Files
.GetCount();
111 m_DataFile
.Write(&header
,sizeof(SLogCacheRevItemHeader
));
113 CArchive
ar(&m_DataFile
, CArchive::store
);
115 ar
<<Rev
.m_AuthorName
;
116 ar
<<Rev
.m_AuthorEmail
;
117 ar
<<Rev
.m_AuthorDate
;
118 ar
<<Rev
.m_CommitterName
;
119 ar
<<Rev
.m_CommitterEmail
;
120 ar
<<Rev
.m_CommitterDate
;
123 ar
<<Rev
.m_CommitHash
;
126 for(int i
=0;i
<Rev
.m_Files
.GetCount();i
++)
128 SLogCacheRevFileHeader header
;
129 header
.m_Magic
=LOG_DATA_FILE_MAGIC
;
130 header
.m_Version
=LOG_INDEX_VERSION
;
133 ar
<<header
.m_Version
;
134 ar
<<Rev
.m_Files
[i
].GetGitPathString();
135 ar
<<Rev
.m_Files
[i
].GetGitOldPathString();
136 ar
<<Rev
.m_Files
[i
].m_Action
;
137 ar
<<Rev
.m_Files
[i
].m_Stage
;
138 ar
<<Rev
.m_Files
[i
].m_StatAdd
;
139 ar
<<Rev
.m_Files
[i
].m_StatDel
;
145 int CLogCache::LoadOneItem(GitRev
&Rev
,ULONGLONG offset
)
147 SLogCacheRevItemHeader header
;
149 if(this->m_DataFile
.m_hFile
== CFile::hFileNull
)
151 BOOL b
=m_DataFile
.Open(m_GitDir
+_T("\\.git\\")+DATA_FILE_NAME
,
152 CFile::modeRead
|CFile::shareDenyNone
|
153 CFile::modeNoTruncate
|CFile::modeCreate
);
157 m_DataFile
.Seek(offset
,CFile::begin
);
159 UINT count
=m_DataFile
.Read(&header
,sizeof(SLogCacheRevItemHeader
));
160 if( count
!= sizeof(SLogCacheRevItemHeader
))
162 if( !CheckHeader(header
))
166 CArchive
ar(&m_DataFile
, CArchive::load
);
168 ar
>>Rev
.m_AuthorName
;
169 ar
>>Rev
.m_AuthorEmail
;
170 ar
>>Rev
.m_AuthorDate
;
171 ar
>>Rev
.m_CommitterName
;
172 ar
>>Rev
.m_CommitterEmail
;
173 ar
>>Rev
.m_CommitterDate
;
176 ar
>>Rev
.m_CommitHash
;
181 for(int i
=0;i
<header
.m_FileCount
;i
++)
184 CString file
,oldfile
;
186 SLogCacheRevFileHeader header
;
189 ar
>>header
.m_Version
;
191 if( this->CheckHeader(header
) )
195 path
.SetFromGit(file
,&oldfile
);
203 Rev
.m_Files
.AddPath(path
);
207 int CLogCache::RebuildCacheFile()
209 m_IndexFile
.SetLength(0);
210 m_DataFile
.SetLength(0);
212 SLogCacheIndexHeader header
;
213 header
.m_Magic
=LOG_INDEX_MAGIC
;
214 header
.m_Version
=LOG_INDEX_VERSION
;
215 m_IndexFile
.SeekToBegin();
216 m_IndexFile
.Write(&header
,sizeof(SLogCacheIndexHeader
));
220 SLogCacheRevFileHeader header
;
221 header
.m_Magic
=LOG_DATA_MAGIC
;
222 header
.m_Version
=LOG_INDEX_VERSION
;
224 m_DataFile
.SeekToBegin();
225 m_DataFile
.Write(&header
,sizeof(SLogCacheRevFileHeader
));
229 int CLogCache::SaveCache()
231 if( this->m_HashMap
.size() == 0 )
234 if( this->m_GitDir
.IsEmpty())
237 bool bIsRebuild
=false;
238 if(this->m_DataFile
.m_hFile
!= CFile::hFileNull
)
240 if(this->m_IndexFile
.m_hFile
!=CFile::hFileNull
)
243 if(this->m_IndexFile
.m_hFile
== CFile::hFileNull
)
245 BOOL b
=m_IndexFile
.Open(m_GitDir
+_T("\\.git\\")+INDEX_FILE_NAME
,
246 CFile::modeReadWrite
|CFile::shareDenyWrite
|
247 CFile::modeNoTruncate
|CFile::modeCreate
);
252 if(this->m_DataFile
.m_hFile
== CFile::hFileNull
)
254 BOOL b
=m_DataFile
.Open(m_GitDir
+_T("\\.git\\")+DATA_FILE_NAME
,
255 CFile::modeReadWrite
|CFile::shareDenyWrite
|
256 CFile::modeNoTruncate
|CFile::modeCreate
);
267 SLogCacheIndexHeader header
;
268 memset(&header
,0,sizeof(SLogCacheIndexHeader
));
269 UINT count
=m_IndexFile
.Read(&header
,sizeof(SLogCacheIndexHeader
));
270 if(count
!= sizeof(SLogCacheIndexHeader
) || !this->CheckHeader(header
))
279 SLogCacheRevFileHeader header
;
281 UINT count
=m_DataFile
.Read(&header
,sizeof(SLogCacheRevFileHeader
));
282 if( count
!= sizeof(SLogCacheRevFileHeader
) || !CheckHeader(header
))
290 m_DataFile
.SeekToEnd();
291 m_IndexFile
.SeekToEnd();
292 CGitHashMap::iterator i
;
293 for(i
=m_HashMap
.begin();i
!=m_HashMap
.end();i
++)
295 if(this->m_HashMapIndex
.find((*i
).second
.m_CommitHash
) == m_HashMapIndex
.end() || bIsRebuild
)
297 if((*i
).second
.m_IsFull
&& !(*i
).second
.m_CommitHash
.IsEmpty())
299 ULONGLONG offset
= m_DataFile
.GetPosition();
300 this->SaveOneItem((*i
).second
,offset
);
303 item
.m_Hash
= (*i
).second
.m_CommitHash
;
304 item
.m_Offset
=offset
;
306 m_IndexFile
.Write(&item
,sizeof(SLogCacheItem
));
315 int CLogCache::ClearAllParent()
317 CGitHashMap::iterator i
;
318 for(i
=m_HashMap
.begin();i
!=m_HashMap
.end();i
++)
320 (*i
).second
.m_ParentHash
.clear();
321 (*i
).second
.m_Lanes
.clear();