Kiln » TortoiseHg » TortoiseHg
Clone URL:  
Pushed to one repository · View In Graph Contained in 0.8, 0.8.1, and 0.8.2

shellext: introduce reading .hg/thgstatus

New class DirectoryStatus, which reads its entries from
the new file .hg/thgstatus, which is written by the
new Mercurial extension hgext/thgstatus

Changeset 70fef26106da

Parent 6c34798f128d

by Adrian Buehlmann

Changes to 4 files · Browse files at 70fef26106da Showing diff from parent 6c34798f128d Diff from another changeset...

Change 1 of 1 Show Entire File hgext/​thgstatus.py Stacked
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
@@ -0,0 +1,69 @@
+# thgstatus.py - TortoiseHg status cache extension for Mercurial +# +# 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/>. + +from mercurial.i18n import _ +from mercurial import commands + +def cachefilepath(repo): + return repo.root + "/.hg/thgstatus" + +def dirname(f): + return '/'.join(f.split('/')[:-1]) + +def thgstatus(ui, repo, source='default', **opts): + '''update directory status cache for TortoiseHg + + Caches the information provided by 'hg status' in the file .hg/thgstatus + which can then be used by the TortoiseHg shell extension to display + overlay icons for directories. + The file .hg/thgstatus contains one line for each directory that has + removed, modified or added files (in that order of preference). Each line + consists of one char for the status of the directory (r, m or a), followed + by the relative path of the directory in the repo. + If the file is empty, then the repo is clean. + ''' + + repostate = repo.status() + modified, added, removed, deleted, unknown, ignored, clean = repostate + dirstatus = {} + for fn in added: + dirstatus[dirname(fn)] = 'a' + for fn in modified: + dirstatus[dirname(fn)] = 'm' + for fn in removed: + dirstatus[dirname(fn)] = 'r' + nmodified, nadded, nremoved = 0, 0, 0 + f = open(cachefilepath(repo), 'wb') + for dn in sorted(dirstatus): + s = dirstatus[dn] + if (s == 'm'): + nmodified += 1 + elif (s == 'a'): + nadded += 1 + elif (s == 'r'): + nremoved +=1 + f.write(dirstatus[dn] + dn + '\n') + f.close() + ui.status(_("updated cached directory status: " + "%i modified, %i added, %i removed") % (nmodified, nadded, nremoved)) + +cmdtable = { + 'thgstatus': + (thgstatus, + [ ], + _('hg thgstatus')), +}
 
24
25
26
27
 
28
29
30
 
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
 
24
25
26
 
27
28
29
30
 
201
202
203
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
204
205
206
@@ -24,7 +24,7 @@
 Directory::Directory(   Directory* p, const std::string& n, const std::string& basepath  ): - parent_(p), name_(n), tickcount_(0), status_(-1) + parent_(p), name_(n)  {   if (n.empty())   path_ = basepath; @@ -201,73 +201,6 @@
 }     -char Directory::status_imp(const std::string& hgroot) -{ - bool added = false; - - std::vector<Directory*> todo; - todo.push_back(this); - - Winstat stat; - std::string basepath; - - while (!todo.empty()) - { - Directory* const d = todo.back(); - todo.pop_back(); - - //TDEBUG_TRACE("Directory(" << path() << ")::status_imp: " - // "popped '" << d->path() << "'"); - - if (!d->files_.empty()) - { - basepath = hgroot + "/" + d->path() + "/"; - - for (FilesT::iterator i = d->files_.begin(); i != d->files_.end(); ++i) - { - if (i->state == 'r') - return 'M'; // file was removed, report dir as modified - - std::string p = basepath + i->name; - if (0 != stat.lstat(p.c_str())) - return 'M'; // file is missing, report dir as modified - - char s = i->status(stat); - - if (s == 'M') - return 'M'; - if (s == 'A') - added = true; - } - } - - todo.insert(todo.end(), d->subdirs_.begin(), d->subdirs_.end()); - } - - if (added) - return 'A'; - - return 'C'; -} - - -char Directory::status(const std::string& hgroot) -{ - if (status_ != -1) - { - unsigned tc = GetTickCount(); - if (tc - tickcount_ < 3000) { - return status_; - } - } - - status_ = status_imp(hgroot); - tickcount_ = GetTickCount(); - - return status_; -} - -  void Directory::print() const  {   for (DirsT::const_iterator i = subdirs_.begin(); i != subdirs_.end(); ++i)
 
35
36
37
38
39
40
41
42
43
 
50
51
52
53
54
55
56
57
58
59
60
61
 
35
36
37
 
 
 
38
39
40
 
47
48
49
 
 
50
 
 
 
51
52
53
@@ -35,9 +35,6 @@
    DirsT subdirs_;   FilesT files_; - - unsigned tickcount_; - char status_;    public:   Directory(Directory* p, const std::string& n, const std::string& basepath); @@ -50,12 +47,7 @@
  const Direntry* get(const std::string& relpath) const;   Directory* Directory::getdir(const std::string& n);   - char status(const std::string& hgroot); -   void print() const; - -private: - char status_imp(const std::string& hgroot);  };    #endif
 
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
 
133
134
135
136
 
137
138
139
 
160
161
162
163
 
164
165
 
166
167
168
 
178
179
180
181
 
 
 
 
 
 
 
 
 
 
 
 
 
182
183
184
 
212
213
214
215
 
 
 
216
217
218
 
 
219
220
221
 
224
225
226
227
228
 
229
230
 
 
231
232
233
 
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
184
185
186
187
188
189
190
191
192
193
194
195
 
196
197
198
 
199
200
201
202
203
204
205
206
 
207
208
209
210
211
 
212
213
 
214
215
216
 
217
218
219
220
 
221
222
223
 
224
225
226
227
228
 
243
244
245
 
246
247
248
249
 
270
271
272
 
273
274
 
275
276
277
278
 
288
289
290
 
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
 
334
335
336
 
337
338
339
340
341
 
342
343
344
345
346
 
349
350
351
 
 
352
353
 
354
355
356
357
358
@@ -89,30 +89,140 @@
 }     +class DirectoryStatus +{ + struct E + { + std::string path_; + char status_; + + E(): status_(0) {} + }; + + typedef std::vector<E> V; + V v_; + +public: + int read(const std::string& hgroot); + char status(const std::string& relpath) const; +}; + + +char DirectoryStatus::status(const std::string& relpath) const +{ + TDEBUG_TRACE("DirectoryStatus::status(" << relpath << ")"); + + char res = 'C'; + bool added = false; + bool modified = false; + + for (V::const_iterator i = v_.begin(); i != v_.end(); ++i) + { + const E& e = *i; + if (e.path_.compare(0, relpath.length(), relpath) == 0) + { + TDEBUG_TRACE("DirectoryStatus::status(" << relpath << "):" + << " found '" << e.path_ << "'"); + if (e.status_ == 'r' || e.status_ == 'm') + { + modified = true; + break; + } + if (e.status_ == 'a') + added = true; + } + } + + if (modified) + res = 'M'; + else if (added) + res = 'A'; + else + res = 'C'; + + TDEBUG_TRACE("DirectoryStatus::status(" << relpath << "): returns " << res); + return res; +} + + +int DirectoryStatus::read(const std::string& hgroot) +{ + v_.clear(); + + std::string p = hgroot + "\\.hg\\thgstatus"; + + FILE *f = fopen(p.c_str(), "rb"); + if (!f) + { + TDEBUG_TRACE("DirectoryStatus::read: can't open " << p); + return 0; + } + + char state; + std::vector<char> path(MAX_PATH); + + DirectoryStatus::E e; + + while (fread(&state, sizeof(state), 1, f) == 1) + { + e.status_ = state; + + path.clear(); + char t; + while (fread(&t, sizeof(t), 1, f) == 1 && t != '\n') + { + path.push_back(t); + if (path.size() > 1000) + return 0; + } + path.push_back(0); + + e.path_ = &path[0]; + + v_.push_back(e); + } + + fclose(f); + + TDEBUG_TRACE("DirectoryStatus::read(" << hgroot << "): done. " + << v_.size() << " entries read"); + + return 1; +} + +  class Dirstatecache  { - struct entry + struct E   {   Dirstate* dstate; - __time64_t mtime; + __time64_t dstate_mtime; + + DirectoryStatus* tstate; + __time64_t tstate_mtime; +   std::string hgroot;   unsigned tickcount;   - entry(): dstate(0), mtime(0), tickcount(0) {} + E() + : dstate(0), dstate_mtime(0), + tstate(0), tstate_mtime(0), tickcount(0) {}   };   - typedef std::list<entry>::iterator Iter; + typedef std::list<E>::iterator Iter;   - static std::list<entry> _cache; + static std::list<E> _cache;    public: - static Dirstate* get(const std::string& hgroot); + static int get(const std::string& hgroot, + Dirstate*& outDirstate, DirectoryStatus*& outDirectoryStatus);  };   -std::list<Dirstatecache::entry> Dirstatecache::_cache; +std::list<Dirstatecache::E> Dirstatecache::_cache;     -Dirstate* Dirstatecache::get(const std::string& hgroot) +int Dirstatecache::get(const std::string& hgroot, + Dirstate*& outDirstate, DirectoryStatus*& outDirectoryStatus)  {   Iter iter = _cache.begin();   @@ -133,7 +243,7 @@
  _cache.back().dstate = 0;   _cache.pop_back();   } - entry e; + E e;   e.hgroot = hgroot;   _cache.push_front(e);   iter = _cache.begin(); @@ -160,9 +270,9 @@
  TDEBUG_TRACE("Dirstatecache::get: lstat(" << path <<") ok ");   }   - if (stat_done && iter->mtime < stat.mtime) + if (stat_done && iter->dstate_mtime < stat.mtime)   { - iter->mtime = stat.mtime; + iter->dstate_mtime = stat.mtime;   if (iter->dstate) {   delete iter->dstate;   iter->dstate = 0; @@ -178,7 +288,19 @@
  << _cache.size() << " repos in cache");   }   - return iter->dstate; + delete iter->tstate; + iter->tstate = 0; + + { + std::auto_ptr<DirectoryStatus> pds(new DirectoryStatus()); + if (pds->read(hgroot)) + iter->tstate = pds.release(); + } + + outDirstate = iter->dstate; + outDirectoryStatus = iter->tstate; + + return 1;  }     @@ -212,10 +334,13 @@
  || (relpath.size() > 4 && relpath.compare(0, 4, ".hg/") == 0))   return 0; // don't descend into .hg dir   - Dirstate* pds = Dirstatecache::get(hgroot); + Dirstate* pds = 0; + DirectoryStatus* pts = 0; + Dirstatecache::get(hgroot, pds, pts);   if (!pds)   { - TDEBUG_TRACE("HgQueryDirstate: Dirstatecache::get(" << hgroot << ") returns 0"); + TDEBUG_TRACE("HgQueryDirstate: Dirstatecache::get(" + << hgroot << ") returns no Dirstate");   return 0;   }   @@ -224,10 +349,10 @@
    if (PathIsDirectory(path.c_str()))   { - Directory* dir = pds->root().getdir(relpath); - if (!dir) + if (!pts)   return 0; - outStatus = dir->status(hgroot); + + outStatus = pts->status(relpath);   }   else   {