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

shellext: new QueryDirstate.h and cpp

Changeset ab2bc226e0db

Parent 27f99dea471b

by Adrian Buehlmann

Changes to 6 files · Browse files at ab2bc226e0db Showing diff from parent 27f99dea471b Diff from another changeset...

 
3
4
5
6
 
7
8
9
 
3
4
5
 
6
7
8
9
@@ -3,7 +3,7 @@
 #include "TortoiseUtils.h"  #include "StringUtils.h"  #include "PipeUtils.h" -#include "dirstate.h" +#include "QueryDirstate.h"    #include <shlwapi.h>  
 
13
14
15
16
 
 
17
18
19
 
13
14
15
 
16
17
18
19
20
@@ -13,7 +13,8 @@
  PipeUtils.o \   ShellUtils2.o \   StringUtils.o \ - dirstate.o + dirstate.o \ + QueryDirstate.o    BASE_LDFLAGS=-lole32 -lkernel32 -luser32 -lgdi32 -lshlwapi -lwininet \   -lwinmm -luuid
Change 1 of 1 Show Entire File tortoise/​shellext/​QueryDirstate.cpp 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
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
184
185
186
187
188
189
190
191
@@ -0,0 +1,191 @@
+ +// 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 "QueryDirstate.h" +#include "dirstate.h" +#include "DirectoryStatus.h" +#include "TortoiseUtils.h" +#include "Winstat.h" + +#include <shlwapi.h> + +#include <vector> +#include <list> + + +#define HASH_LENGTH 20 + + +class Dirstatecache +{ + struct E + { + Dirstate* dstate; + __time64_t dstate_mtime; + + std::string hgroot; + unsigned tickcount; + + E(): dstate(0), dstate_mtime(0), tickcount(0) {} + }; + + typedef std::list<E>::iterator Iter; + + static std::list<E> _cache; + +public: + static Dirstate* get(const std::string& hgroot); +}; + +std::list<Dirstatecache::E> Dirstatecache::_cache; + + +Dirstate* Dirstatecache::get(const std::string& hgroot) +{ + Iter iter = _cache.begin(); + + for (;iter != _cache.end(); ++iter) + { + if (hgroot == iter->hgroot) + break; + } + + bool isnew = false; + + if (iter == _cache.end()) + { + 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; + _cache.push_front(e); + iter = _cache.begin(); + isnew = true; + } + + unsigned tc = GetTickCount(); + + std::string path = hgroot + "\\.hg\\dirstate"; + + Winstat stat; + + bool stat_done = false; + + if (isnew || (tc - iter->tickcount) > 500) + { + if (0 != stat.lstat(path.c_str())) + { + TDEBUG_TRACE("Dirstatecache::get: lstat(" << path <<") failed"); + return 0; + } + iter->tickcount = tc; + stat_done = true; + TDEBUG_TRACE("Dirstatecache::get: lstat(" << path <<") ok "); + } + + if (stat_done && iter->dstate_mtime < stat.mtime) + { + iter->dstate_mtime = stat.mtime; + if (iter->dstate) { + delete iter->dstate; + iter->dstate = 0; + TDEBUG_TRACE("Dirstatecache::get: refreshing " << hgroot); + } else { + TDEBUG_TRACE("Dirstatecache::get: reading " << hgroot); + } + unsigned tc0 = GetTickCount(); + iter->dstate = Dirstate::read(path).release(); + unsigned tc1 = GetTickCount(); + unsigned delta = tc1 - tc0; + TDEBUG_TRACE("Dirstatecache::get: read done in " << delta << " ticks, " + << _cache.size() << " repos in cache"); + } + + return iter->dstate; +} + + +int HgQueryDirstate( + const std::string& path, const char& filterStatus, char& outStatus) +{ + if (PathIsRoot(path.c_str())) + return 0; + + std::string hgroot = GetHgRepoRoot(path); + if (hgroot.empty()) + return 0; + + size_t offset = hgroot.length(); + if (path[offset] == '\\') + offset++; + const char* relpathptr = path.c_str() + offset; + + std::string relpath = relpathptr; + + for (size_t i = 0; i < relpath.size(); ++i) + { + if (relpath[i] == '\\') + relpath[i] = '/'; + } + + if (relpath == ".hg" + || (relpath.size() > 4 && relpath.compare(0, 4, ".hg/") == 0)) + return 0; // don't descend into .hg dir + + Dirstate* pds = Dirstatecache::get(hgroot); + if (!pds) + { + TDEBUG_TRACE("HgQueryDirstate: Dirstatecache::get(" + << hgroot << ") returns no Dirstate"); + return 0; + } + + if (filterStatus == 'A' && pds->num_added() == 0) + return 0; + + if (PathIsDirectory(path.c_str())) + { + std::auto_ptr<DirectoryStatus> pds(new DirectoryStatus()); + if (!pds->read(hgroot)) + return 0; + + outStatus = pds->status(relpath); + } + else + { + const Direntry* e = pds->root().get(relpath); + if (!e) + return 0; + + Winstat stat; + if (0 != stat.lstat(path.c_str())) { + TDEBUG_TRACE("HgQueryDirstate: lstat(" << path << ") failed"); + return 0; + } + + outStatus = e->status(stat); + } + + return 1; +}
Change 1 of 1 Show Entire File tortoise/​shellext/​QueryDirstate.h Stacked
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
@@ -0,0 +1,9 @@
+#ifndef _QUERY_DIRSTATE_H +#define _QUERY_DIRSTATE_H + +#include <string> + +int HgQueryDirstate( + const std::string& path, const char& filterStatus, char& outStatus); + +#endif
 
1
2
 
3
4
5
 
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
 
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
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
 
1
2
3
4
5
6
 
18
19
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
22
23
 
50
51
52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
54
55
@@ -1,5 +1,6 @@
   // 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 @@ -17,47 +18,6 @@
 #include "stdafx.h"    #include "dirstate.h" -#include "Directory.h" -#include "DirectoryStatus.h" -#include "TortoiseUtils.h" -#include "Winstat.h" - -#include <shlwapi.h> - -#include <vector> -#include <list> - - -#define HASH_LENGTH 20 - - -class Dirstate -{ - Directory root_; - - unsigned num_added_; // number of entries that have state 'a' - unsigned num_entries_; - -public: - char parent1[HASH_LENGTH]; - char parent2[HASH_LENGTH]; - - static std::auto_ptr<Dirstate> read(const std::string& path); - - Directory& root() { return root_; } - - void add(const std::string& relpath, Direntry& e) { - root_.add(relpath, e); - ++num_entries_; - } - - unsigned num_added() const { return num_added_; } - unsigned size() const { return num_entries_; } - -private: - Dirstate() - : root_(0, "", ""), num_added_(0), num_entries_(0) {} -};      std::auto_ptr<Dirstate> Dirstate::read(const std::string& path) @@ -90,165 +50,6 @@
 }     -class Dirstatecache -{ - struct E - { - Dirstate* dstate; - __time64_t dstate_mtime; - - std::string hgroot; - unsigned tickcount; - - E(): dstate(0), dstate_mtime(0), tickcount(0) {} - }; - - typedef std::list<E>::iterator Iter; - - static std::list<E> _cache; - -public: - static Dirstate* get(const std::string& hgroot); -}; - -std::list<Dirstatecache::E> Dirstatecache::_cache; - - -Dirstate* Dirstatecache::get(const std::string& hgroot) -{ - Iter iter = _cache.begin(); - - for (;iter != _cache.end(); ++iter) - { - if (hgroot == iter->hgroot) - break; - } - - bool isnew = false; - - if (iter == _cache.end()) - { - 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; - _cache.push_front(e); - iter = _cache.begin(); - isnew = true; - } - - unsigned tc = GetTickCount(); - - std::string path = hgroot + "\\.hg\\dirstate"; - - Winstat stat; - - bool stat_done = false; - - if (isnew || (tc - iter->tickcount) > 500) - { - if (0 != stat.lstat(path.c_str())) - { - TDEBUG_TRACE("Dirstatecache::get: lstat(" << path <<") failed"); - return 0; - } - iter->tickcount = tc; - stat_done = true; - TDEBUG_TRACE("Dirstatecache::get: lstat(" << path <<") ok "); - } - - if (stat_done && iter->dstate_mtime < stat.mtime) - { - iter->dstate_mtime = stat.mtime; - if (iter->dstate) { - delete iter->dstate; - iter->dstate = 0; - TDEBUG_TRACE("Dirstatecache::get: refreshing " << hgroot); - } else { - TDEBUG_TRACE("Dirstatecache::get: reading " << hgroot); - } - unsigned tc0 = GetTickCount(); - iter->dstate = Dirstate::read(path).release(); - unsigned tc1 = GetTickCount(); - unsigned delta = tc1 - tc0; - TDEBUG_TRACE("Dirstatecache::get: read done in " << delta << " ticks, " - << _cache.size() << " repos in cache"); - } - - return iter->dstate; -} - - -int HgQueryDirstate( - const std::string& path, const char& filterStatus, char& outStatus) -{ - if (PathIsRoot(path.c_str())) - return 0; - - std::string hgroot = GetHgRepoRoot(path); - if (hgroot.empty()) - return 0; - - size_t offset = hgroot.length(); - if (path[offset] == '\\') - offset++; - const char* relpathptr = path.c_str() + offset; - - std::string relpath = relpathptr; - - for (size_t i = 0; i < relpath.size(); ++i) - { - if (relpath[i] == '\\') - relpath[i] = '/'; - } - - if (relpath == ".hg" - || (relpath.size() > 4 && relpath.compare(0, 4, ".hg/") == 0)) - return 0; // don't descend into .hg dir - - Dirstate* pds = Dirstatecache::get(hgroot); - if (!pds) - { - TDEBUG_TRACE("HgQueryDirstate: Dirstatecache::get(" - << hgroot << ") returns no Dirstate"); - return 0; - } - - if (filterStatus == 'A' && pds->num_added() == 0) - return 0; - - if (PathIsDirectory(path.c_str())) - { - std::auto_ptr<DirectoryStatus> pds(new DirectoryStatus()); - if (!pds->read(hgroot)) - return 0; - - outStatus = pds->status(relpath); - } - else - { - const Direntry* e = pds->root().get(relpath); - if (!e) - return 0; - - Winstat stat; - if (0 != stat.lstat(path.c_str())) { - TDEBUG_TRACE("HgQueryDirstate: lstat(" << path << ") failed"); - return 0; - } - - outStatus = e->status(stat); - } - - return 1; -} - -  static char *revhash_string(const char revhash[HASH_LENGTH])  {   unsigned ix;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
 
 
4
5
6
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
9
 
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
@@ -1,9 +1,56 @@
+ +// 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/>. +  #ifndef _DIRSTATE_H  #define _DIRSTATE_H   +#include "Directory.h" +  #include <string>   -int HgQueryDirstate( - const std::string& path, const char& filterStatus, char& outStatus); + +#define HASH_LENGTH 20 + +class Dirstate +{ + Directory root_; + + unsigned num_added_; // number of entries that have state 'a' + unsigned num_entries_; + +public: + char parent1[HASH_LENGTH]; + char parent2[HASH_LENGTH]; + + static std::auto_ptr<Dirstate> read(const std::string& path); + + Directory& root() { return root_; } + + void add(const std::string& relpath, Direntry& e) { + root_.add(relpath, e); + ++num_entries_; + } + + unsigned num_added() const { return num_added_; } + unsigned size() const { return num_entries_; } + +private: + Dirstate() + : root_(0, "", ""), num_added_(0), num_entries_(0) {} +};    #endif