Kiln » TortoiseHg » TortoiseHg
Clone URL:  
Dirstatecache.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// 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 "Dirstatecache.h" #include "dirstate.h" #include "Winstat64.h" #include "Thgstatus.h" std::list<Dirstatecache::E>& Dirstatecache::cache() { static std::list<Dirstatecache::E> c; return c; } Dirstate* Dirstatecache::get( const std::string& hgroot, const std::string& cwd, bool& unset, bool usekbfiles) { unset = false; typedef std::list<E>::iterator Iter; Iter iter = cache().begin(); for (;iter != cache().end(); ++iter) { if (hgroot == iter->hgroot && usekbfiles == iter->usekbfiles) break; } Winstat64 stat; std::string path = hgroot + (usekbfiles ? "\\.hg\\kilnbfiles\\dirstate" : "\\.hg\\dirstate"); unsigned tc = GetTickCount(); bool new_stat = false; if (iter == cache().end()) { if (stat.lstat(path.c_str()) != 0) { TDEBUG_TRACE("Dirstatecache::get: lstat(" << path <<") failed"); return 0; } TDEBUG_TRACE("Dirstatecache::get: lstat(" << path <<") ok "); new_stat = true; if (cache().size() >= 10) { TDEBUG_TRACE("Dirstatecache::get: dropping " << cache().back().hgroot); delete cache().back().dstate; cache().back().dstate = 0; cache().pop_back(); } E e; e.hgroot = hgroot; e.usekbfiles = usekbfiles; cache().push_front(e); iter = cache().begin(); iter->tickcount = tc; } if (!new_stat && tc - iter->tickcount > 500) { if (0 != stat.lstat(path.c_str())) { TDEBUG_TRACE("Dirstatecache::get: lstat(" << path <<") failed"); TDEBUG_TRACE("Dirstatecache::get: dropping " << iter->hgroot); delete iter->dstate; iter->dstate = 0; cache().erase(iter); return 0; } iter->tickcount = tc; TDEBUG_TRACE("Dirstatecache::get: lstat(" << path <<") ok "); new_stat = true; } if (iter->dstate) { unset = iter->unset; if (!new_stat) return iter->dstate; if (iter->dstate_mtime == stat.mtime && iter->dstate_size == stat.size) { return iter->dstate; } TDEBUG_TRACE("Dirstatecache::get: refreshing " << hgroot); } else { TDEBUG_TRACE("Dirstatecache::get: reading " << hgroot); } unset = false; unsigned tc0 = GetTickCount(); std::auto_ptr<Dirstate> ds = Dirstate::read(path, unset); unsigned tc1 = GetTickCount(); bool request_thgstatus_update = true; if (unset) { if (iter->unset) { TDEBUG_TRACE( "Dirstatecache::get: **** old and new have unset entries"); request_thgstatus_update = false; } else { TDEBUG_TRACE("Dirstatecache::get: new has unset entries"); } } iter->unset = unset; delete iter->dstate; iter->dstate = ds.release(); unsigned delta = tc1 - tc0; TDEBUG_TRACE("Dirstatecache::get: read done in " << delta << " ticks, " << cache().size() << " repos in cache"); iter->dstate_mtime = stat.mtime; iter->dstate_size = stat.size; if (request_thgstatus_update) { TDEBUG_TRACE("Dirstatecache::get: calling Thgstatus::update"); Thgstatus::update(cwd); } else { TDEBUG_TRACE("Dirstatecache::get: omitting Thgstatus::update"); } return iter->dstate; } void Dirstatecache::invalidate(const std::string& hgroot, bool usekbfiles) { typedef std::list<E>::iterator Iter; if (hgroot.empty()) return; for (Iter i = cache().begin(); i != cache().end(); ++i) { if (hgroot == i->hgroot && usekbfiles == i->usekbfiles) { delete i->dstate; i->dstate = 0; cache().erase(i); TDEBUG_TRACE("Dirstatecache::invalidate(" << hgroot << ")"); break; } } }