1 // TortoiseGit - a Windows shell extension for easy version control
3 // External Cache Copyright (C) 2005-2006,2008 - TortoiseSVN
4 // Copyright (C) 2008-2012 - TortoiseGit
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include ".\statuscacheentry.h"
22 #include "GitStatus.h"
23 #include "CacheInterface.h"
28 DWORD cachetimeout
= (DWORD
)CRegStdDWORD(_T("Software\\TortoiseGit\\Cachetimeout"), CACHETIMEOUT
);
30 CStatusCacheEntry::CStatusCacheEntry()
32 , m_kind(git_node_unknown
)
33 , m_highestPriorityLocalStatus(git_wc_status_none
)
34 , m_bAssumeValid(false)
35 , m_bSkipWorktree(false)
40 CStatusCacheEntry::CStatusCacheEntry(const git_wc_status_kind status
)
42 , m_kind(git_node_unknown
)
43 , m_highestPriorityLocalStatus(status
)
44 , m_bAssumeValid(false)
45 , m_bSkipWorktree(false)
47 m_GitStatus
.prop_status
=m_GitStatus
.text_status
= status
;
48 m_discardAtTime
= GetTickCount()+cachetimeout
;
51 CStatusCacheEntry::CStatusCacheEntry(const git_wc_status2_t
* pGitStatus
, __int64 lastWriteTime
, bool /*bReadOnly*/, DWORD validuntil
/* = 0*/)
53 , m_kind(git_node_unknown
)
54 , m_highestPriorityLocalStatus(git_wc_status_none
)
56 SetStatus(pGitStatus
);
57 m_lastWriteTime
= lastWriteTime
;
59 m_discardAtTime
= validuntil
;
61 m_discardAtTime
= GetTickCount()+cachetimeout
;
64 bool CStatusCacheEntry::SaveToDisk(FILE * pFile
)
66 #define WRITEVALUETOFILE(x) if (fwrite(&x, sizeof(x), 1, pFile)!=1) return false;
67 #define WRITESTRINGTOFILE(x) if (x.IsEmpty()) {value=0;WRITEVALUETOFILE(value);}else{value=x.GetLength();WRITEVALUETOFILE(value);if (fwrite((LPCSTR)x, sizeof(char), value, pFile)!=value) return false;}
69 unsigned int value
= CACHEVERION
;
70 WRITEVALUETOFILE(value
); // 'version' of this save-format
71 WRITEVALUETOFILE(m_highestPriorityLocalStatus
);
72 WRITEVALUETOFILE(m_lastWriteTime
);
73 WRITEVALUETOFILE(m_bSet
);
74 WRITEVALUETOFILE(m_kind
);
76 // now save the status struct (without the entry field, because we don't use that)
77 WRITEVALUETOFILE(m_GitStatus
.prop_status
);
78 // WRITEVALUETOFILE(m_GitStatus.repos_prop_status);
79 // WRITEVALUETOFILE(m_GitStatus.repos_text_status);
80 WRITEVALUETOFILE(m_GitStatus
.text_status
);
81 WRITEVALUETOFILE(m_GitStatus
.assumeValid
);
82 WRITEVALUETOFILE(m_GitStatus
.skipWorktree
);
86 bool CStatusCacheEntry::LoadFromDisk(FILE * pFile
)
88 #define LOADVALUEFROMFILE(x) if (fread(&x, sizeof(x), 1, pFile)!=1) return false;
91 unsigned int value
= 0;
92 LOADVALUEFROMFILE(value
);
93 if (value
!= CACHEVERION
)
94 return false; // not the correct version
95 LOADVALUEFROMFILE(m_highestPriorityLocalStatus
);
96 LOADVALUEFROMFILE(m_lastWriteTime
);
97 LOADVALUEFROMFILE(m_bSet
);
98 LOADVALUEFROMFILE(m_kind
);
99 SecureZeroMemory(&m_GitStatus
, sizeof(m_GitStatus
));
100 LOADVALUEFROMFILE(m_GitStatus
.prop_status
);
101 // LOADVALUEFROMFILE(m_GitStatus.repos_prop_status);
102 // LOADVALUEFROMFILE(m_GitStatus.repos_text_status);
103 LOADVALUEFROMFILE(m_GitStatus
.text_status
);
104 LOADVALUEFROMFILE(m_GitStatus
.assumeValid
);
105 LOADVALUEFROMFILE(m_GitStatus
.skipWorktree
);
106 // m_GitStatus.entry = NULL;
107 m_discardAtTime
= GetTickCount()+cachetimeout
;
109 catch ( CAtlException
)
116 void CStatusCacheEntry::SetStatus(const git_wc_status2_t
* pGitStatus
)
118 if(pGitStatus
== NULL
)
124 m_highestPriorityLocalStatus
= GitStatus::GetMoreImportant(pGitStatus
->prop_status
, pGitStatus
->text_status
);
125 m_GitStatus
= *pGitStatus
;
126 if (m_kind
!= git_node_dir
)
128 m_bAssumeValid
= pGitStatus
->assumeValid
;
129 m_bSkipWorktree
= pGitStatus
->skipWorktree
;
132 m_discardAtTime
= GetTickCount()+cachetimeout
;
137 void CStatusCacheEntry::SetAsUnversioned()
139 SecureZeroMemory(&m_GitStatus
, sizeof(m_GitStatus
));
140 m_discardAtTime
= GetTickCount()+cachetimeout
;
141 git_wc_status_kind status
= git_wc_status_none
;
142 if (m_highestPriorityLocalStatus
== git_wc_status_missing
)
143 status
= git_wc_status_missing
;
144 if (m_highestPriorityLocalStatus
== git_wc_status_unversioned
)
145 status
= git_wc_status_unversioned
;
146 m_highestPriorityLocalStatus
= status
;
147 m_GitStatus
.prop_status
= git_wc_status_none
;
148 m_GitStatus
.text_status
= status
;
150 m_bAssumeValid
= false;
153 bool CStatusCacheEntry::HasExpired(long now
) const
155 return m_discardAtTime
!= 0 && (now
- m_discardAtTime
) >= 0;
158 void CStatusCacheEntry::BuildCacheResponse(TGITCacheResponse
& response
, DWORD
& responseLength
) const
160 SecureZeroMemory(&response
, sizeof(response
));
161 response
.m_status
= m_GitStatus
;
162 response
.m_bAssumeValid
= m_bAssumeValid
;
163 response
.m_bSkipWorktree
= m_bSkipWorktree
;
164 responseLength
= sizeof(response
);
166 // directories that are empty or only contain unversioned files will be git_wc_status_incomplete, report as unversioned
167 if (response
.m_status
.text_status
== git_wc_status_incomplete
)
169 response
.m_status
.text_status
= response
.m_status
.prop_status
= git_wc_status_unversioned
;
173 bool CStatusCacheEntry::IsVersioned() const
175 return m_highestPriorityLocalStatus
> git_wc_status_unversioned
;
178 bool CStatusCacheEntry::DoesFileTimeMatch(__int64 testTime
) const
180 return m_lastWriteTime
== testTime
;
184 bool CStatusCacheEntry::ForceStatus(git_wc_status_kind forcedStatus
)
186 git_wc_status_kind newStatus
= forcedStatus
;
188 if(newStatus
!= m_highestPriorityLocalStatus
)
190 // We've had a status change
191 m_highestPriorityLocalStatus
= newStatus
;
192 m_GitStatus
.text_status
= newStatus
;
193 m_GitStatus
.prop_status
= newStatus
;
194 m_discardAtTime
= GetTickCount()+cachetimeout
;
201 CStatusCacheEntry::HasBeenSet() const
206 void CStatusCacheEntry::Invalidate()