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

QueryDirstate.cpp: new class HgRepoRoot

Caches calls to GetHgRepoRoot

Changeset 1560550fcdc4

Parent 06f4688b4143

by Adrian Buehlmann

Changes to one file · Browse files at 1560550fcdc4 Showing diff from parent 06f4688b4143 Diff from another changeset...

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
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
   // 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 "Dirstatecache.h"  #include "TortoiseUtils.h"  #include "Winstat.h"    #include <shlwapi.h>     +class HgRepoRoot +{ + struct E + { + std::string hgroot_; + unsigned tickcount_; + unsigned hitcount_; + E(): tickcount_(0), hitcount_(0) {} + }; + + // true, if p is a subdir of refp, or identical + static bool is_subdir(const std::string& refp, const std::string& p); + +public: + static const std::string& get(const std::string& path); +}; + + +bool HgRepoRoot::is_subdir(const std::string& refp, const std::string& p) +{ + // refp = "foo\bar" + // p = "foo\bar\ping" -> return true + + if (refp.size() > p.size()) + return false; + + if (p.compare(0, refp.size(), refp) != 0) + return false; + + if (refp.size() == p.size()) + return true; + + // p is longer than refp + + char c = p[refp.size()]; + + // refp = "foo\bar" + // p = "foo\bar2", c is '2' -> return false + + if (c == '\\' || c == '/') + return true; + + return false; +} + + +const std::string& HgRepoRoot::get(const std::string& path) +{ + static E cache; + + if (!cache.hgroot_.empty() + && is_subdir(cache.hgroot_, path)) + { + unsigned tc = GetTickCount(); + if (tc - cache.tickcount_ < 2000) + { + ++cache.hitcount_; + return cache.hgroot_; + } + } + + std::string r = GetHgRepoRoot(path); + + bool show_hitcount = !cache.hgroot_.empty() && cache.hitcount_ > 0; + if (show_hitcount) + TDEBUG_TRACE("HgRepoRoot::get: '" + << cache.hgroot_ << "' had " << cache.hitcount_ << " hits"); + + cache.hitcount_ = 0; + + if (r.empty()) + { + cache.hgroot_.clear(); + cache.tickcount_ = 0; + } + else + { + if (show_hitcount) + { + const char* verb = (r != cache.hgroot_ ? "caching" : "refreshing" ); + TDEBUG_TRACE("HgRepoRoot::get: " << verb << " '" << cache.hgroot_ << "'"); + } + cache.hgroot_ = r; + cache.tickcount_ = GetTickCount(); + } + + return cache.hgroot_; +} + +  int HgQueryDirstate(   const std::string& path, const char& filterStatus, char& outStatus)  {   if (PathIsRoot(path.c_str()))   return 0;   - std::string hgroot = GetHgRepoRoot(path); + std::string hgroot = HgRepoRoot::get(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     if (PathIsDirectory(path.c_str()))   {   DirectoryStatus* pds = DirectoryStatus::get(hgroot);   if (!pds)   return 0;     outStatus = pds->status(relpath);   }   else   {   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;     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;  }