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_NewCacheEntry
.push_back(Rev
);
20 int CLogCache::GetCacheData(GitRev
&Rev
)
22 if(this->m_HashMapIndex
.find(Rev
.m_CommitHash
)==m_HashMapIndex
.end())
24 for(int i
=0;i
<this->m_NewCacheEntry
.size();i
++)
26 if(m_NewCacheEntry
[i
].m_CommitHash
==Rev
.m_CommitHash
)
28 Rev
.CopyFrom(m_NewCacheEntry
[i
],true);
36 return LoadOneItem(Rev
,m_HashMapIndex
[Rev
.m_CommitHash
]);
40 int CLogCache::FetchCacheIndex(CString GitDir
)
43 if(this->m_IndexFile
.m_hFile
== CFile::hFileNull
)
45 BOOL b
=m_IndexFile
.Open(GitDir
+_T("\\.git\\")+INDEX_FILE_NAME
,
46 CFile::modeRead
|CFile::shareDenyNone
|
47 CFile::modeNoTruncate
|CFile::modeCreate
);
51 //Cache has been fetched.
52 //if(m_GitDir == GitDir)
56 m_IndexFile
.SeekToBegin();
58 SLogCacheIndexHeader header
;
59 UINT count
=m_IndexFile
.Read(&header
,sizeof(SLogCacheIndexHeader
));
60 if(count
!= sizeof(SLogCacheIndexHeader
))
63 if( header
.m_Magic
!= LOG_INDEX_MAGIC
)
66 if( header
.m_Version
!= LOG_INDEX_VERSION
)
72 //for(int i=0;i<header.m_ItemCount;i++)
73 this->m_HashMapIndex
.clear();
77 count
=m_IndexFile
.Read(&Item
,sizeof(SLogCacheItem
));
78 if( count
!= sizeof(SLogCacheItem
) )
82 g_Git
.StringAppend(&str
,Item
.m_Hash
,CP_UTF8
,40);
83 this->m_HashMapIndex
[str
]=Item
.m_Offset
;
89 int CLogCache::SaveOneItem(GitRev
&Rev
,ULONGLONG offset
)
92 SLogCacheRevItemHeader header
;
93 m_DataFile
.Seek(offset
,CFile::begin
);
95 if(this->m_DataFile
.m_hFile
== CFile::hFileNull
)
97 BOOL b
=m_DataFile
.Open(m_GitDir
+_T("\\.git\\")+INDEX_FILE_NAME
,
98 CFile::modeRead
|CFile::shareDenyNone
|
99 CFile::modeNoTruncate
|CFile::modeCreate
);
104 header
.m_Magic
=LOG_DATA_ITEM_MAGIC
;
105 header
.m_Version
=LOG_INDEX_VERSION
;
106 header
.m_FileCount
=Rev
.m_Files
.GetCount();
108 m_DataFile
.Write(&header
,sizeof(SLogCacheRevItemHeader
));
110 CArchive
ar(&m_DataFile
, CArchive::store
);
112 ar
<<Rev
.m_AuthorName
;
113 ar
<<Rev
.m_AuthorEmail
;
114 ar
<<Rev
.m_AuthorDate
;
115 ar
<<Rev
.m_CommitterName
;
116 ar
<<Rev
.m_CommitterEmail
;
117 ar
<<Rev
.m_CommitterDate
;
120 ar
<<Rev
.m_CommitHash
;
123 for(int i
=0;i
<Rev
.m_Files
.GetCount();i
++)
125 SLogCacheRevFileHeader header
;
126 header
.m_Magic
=LOG_DATA_FILE_MAGIC
;
127 header
.m_Version
=LOG_INDEX_VERSION
;
130 ar
<<header
.m_Version
;
131 ar
<<Rev
.m_Files
[i
].GetGitPathString();
132 ar
<<Rev
.m_Files
[i
].GetGitOldPathString();
133 ar
<<Rev
.m_Files
[i
].m_Action
;
134 ar
<<Rev
.m_Files
[i
].m_Stage
;
135 ar
<<Rev
.m_Files
[i
].m_StatAdd
;
136 ar
<<Rev
.m_Files
[i
].m_StatDel
;
142 int CLogCache::LoadOneItem(GitRev
&Rev
,ULONGLONG offset
)
144 SLogCacheRevItemHeader header
;
146 if(this->m_DataFile
.m_hFile
== CFile::hFileNull
)
148 BOOL b
=m_DataFile
.Open(m_GitDir
+_T("\\.git\\")+DATA_FILE_NAME
,
149 CFile::modeRead
|CFile::shareDenyNone
|
150 CFile::modeNoTruncate
|CFile::modeCreate
);
154 m_DataFile
.Seek(offset
,CFile::begin
);
156 UINT count
=m_DataFile
.Read(&header
,sizeof(SLogCacheRevItemHeader
));
157 if( count
!= sizeof(SLogCacheRevItemHeader
))
159 if( !CheckHeader(header
))
163 CArchive
ar(&m_DataFile
, CArchive::load
);
165 ar
>>Rev
.m_AuthorName
;
166 ar
>>Rev
.m_AuthorEmail
;
167 ar
>>Rev
.m_AuthorDate
;
168 ar
>>Rev
.m_CommitterName
;
169 ar
>>Rev
.m_CommitterEmail
;
170 ar
>>Rev
.m_CommitterDate
;
173 ar
>>Rev
.m_CommitHash
;
178 for(int i
=0;i
<header
.m_FileCount
;i
++)
181 CString file
,oldfile
;
183 SLogCacheRevFileHeader header
;
186 ar
>>header
.m_Version
;
188 if( this->CheckHeader(header
) )
192 path
.SetFromGit(file
,&oldfile
);
200 Rev
.m_Files
.AddPath(path
);
204 int CLogCache::RebuildCacheFile()
206 m_IndexFile
.SetLength(0);
207 m_DataFile
.SetLength(0);
209 SLogCacheIndexHeader header
;
210 header
.m_Magic
=LOG_INDEX_MAGIC
;
211 header
.m_Version
=LOG_INDEX_VERSION
;
212 m_IndexFile
.SeekToBegin();
213 m_IndexFile
.Write(&header
,sizeof(SLogCacheIndexHeader
));
217 SLogCacheRevFileHeader header
;
218 header
.m_Magic
=LOG_DATA_MAGIC
;
219 header
.m_Version
=LOG_INDEX_VERSION
;
221 m_DataFile
.SeekToBegin();
222 m_DataFile
.Write(&header
,sizeof(SLogCacheRevFileHeader
));
226 int CLogCache::SaveCache()
228 if( this->m_NewCacheEntry
.size() == 0 )
231 bool bIsRebuild
=false;
232 if(this->m_DataFile
.m_hFile
!= CFile::hFileNull
)
234 if(this->m_IndexFile
.m_hFile
!=CFile::hFileNull
)
237 if(this->m_IndexFile
.m_hFile
== CFile::hFileNull
)
239 BOOL b
=m_IndexFile
.Open(m_GitDir
+_T("\\.git\\")+INDEX_FILE_NAME
,
240 CFile::modeReadWrite
|CFile::shareDenyWrite
|
241 CFile::modeNoTruncate
|CFile::modeCreate
);
246 if(this->m_DataFile
.m_hFile
== CFile::hFileNull
)
248 BOOL b
=m_DataFile
.Open(m_GitDir
+_T("\\.git\\")+DATA_FILE_NAME
,
249 CFile::modeReadWrite
|CFile::shareDenyWrite
|
250 CFile::modeNoTruncate
|CFile::modeCreate
);
261 SLogCacheIndexHeader header
;
262 memset(&header
,0,sizeof(SLogCacheIndexHeader
));
263 UINT count
=m_IndexFile
.Read(&header
,sizeof(SLogCacheIndexHeader
));
264 if(count
!= sizeof(SLogCacheIndexHeader
) || !this->CheckHeader(header
))
273 SLogCacheRevFileHeader header
;
275 UINT count
=m_DataFile
.Read(&header
,sizeof(SLogCacheRevFileHeader
));
276 if( count
!= sizeof(SLogCacheRevFileHeader
) || !CheckHeader(header
))
284 m_DataFile
.SeekToEnd();
285 m_IndexFile
.SeekToEnd();
286 for(int i
=0;i
<this->m_NewCacheEntry
.size();i
++)
288 if(this->m_HashMapIndex
.find(m_NewCacheEntry
[i
].m_CommitHash
) == m_HashMapIndex
.end() || bIsRebuild
)
290 ULONGLONG offset
= m_DataFile
.GetPosition();
291 this->SaveOneItem(m_NewCacheEntry
[i
],offset
);
294 for(int j
=0; j
<40;j
++)
295 item
.m_Hash
[j
]=(BYTE
)m_NewCacheEntry
[i
].m_CommitHash
[j
];
296 item
.m_Offset
=offset
;
298 m_IndexFile
.Write(&item
,sizeof(SLogCacheItem
));