// Copyright (C) 2011 Fog Creek Software
// Copyright (C) 2009 Benjamin Pollack
// Copyright (C) 2009 Adrian Buehlmann
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "stdafx.h"
#include "Dirstate.h"
#include "TortoiseUtils.h"
CAutoPtr<CDirstate> CDirstate::Read(const CString& strPath, bool& bUnset)
{
bUnset = false;
CAtlFile file;
if (FAILED(file.Create(strPath, GENERIC_READ,
FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, OPEN_EXISTING)))
{
ATLTRACE("Dirstate::read: can't open '%s'\n", (LPCTSTR)strPath);
return CAutoPtr<CDirstate>(NULL);
}
CAutoPtr<CDirstate> pd(new CDirstate());
file.Read(&pd->m_szParent1, HASH_LENGTH * sizeof(char));
file.Read(&pd->m_szParent2, HASH_LENGTH * sizeof(char));
CDirentry e;
CString strRelPath;
while (e.Read(file, strRelPath))
{
if (e.Unset()) bUnset = true;
if (e.m_chState == 'a') ++pd->m_nNumAdded;
pd->Add(strRelPath, e);
}
file.Close();
return pd;
}
static char *RevHashString(const char achRevHash[HASH_LENGTH])
{
static char szRevString[HASH_LENGTH * 2 + 1];
static LPCTSTR lpszHexVal = "0123456789abcdef";
for (int i = 0; i < HASH_LENGTH; ++i)
{
szRevString[i * 2] = lpszHexVal[(achRevHash[i] >> 4) & 0xf];
szRevString[i * 2 + 1] = lpszHexVal[achRevHash[i] & 0xf];
}
szRevString[HASH_LENGTH * 2] = 0;
return szRevString;
}
void TestRead()
{
bool bUnset;
CAutoPtr<CDirstate> pDirstate = CDirstate::Read(".hg/dirstate", bUnset);
if ((CDirstate*)pDirstate == NULL)
{
printf("error: could not read .hg/dirstate\n");
return;
}
printf("parent1: %s\n", RevHashString(pDirstate->m_szParent1));
printf("parent2: %s\n", RevHashString(pDirstate->m_szParent2));
printf("entries: %d\n\n", pDirstate->Size());
pDirstate->Root().Print();
}
#ifdef APPMAIN
int main(int argc, char *argv[])
{
TestRead();
return 0;
}
#endif
|
Loading...