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

dirstate: new Directory class

Changeset 470d229b7712

Parent 14ef74069901

by Adrian Buehlmann

Changes to one file · Browse files at 470d229b7712 Showing diff from parent 14ef74069901 Diff from another changeset...

 
57
58
59
 
60
61
62
 
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
 
164
165
166
167
168
169
170
171
172
 
173
174
175
 
182
183
184
185
 
186
187
188
 
195
196
197
198
 
199
200
201
202
203
204
 
205
206
207
 
261
262
263
264
265
266
267
 
270
271
272
273
 
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
 
 
 
 
 
 
323
324
325
326
327
 
328
329
330
331
332
 
 
 
 
333
334
335
336
337
338
339
 
340
341
 
342
343
344
 
371
372
373
374
 
375
376
377
 
416
417
418
 
 
 
 
419
420
421
422
423
424
425
426
427
428
429
430
431
 
 
432
433
434
435
 
436
437
438
 
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
 
347
348
 
 
349
350
351
352
 
 
 
353
354
355
356
357
358
359
 
 
 
 
 
360
361
362
363
364
365
366
367
368
 
369
370
371
372
373
 
413
414
415
 
 
416
417
418
 
419
420
421
422
 
429
430
431
 
432
433
434
435
 
442
443
444
 
445
446
447
448
449
450
 
451
452
453
454
 
508
509
510
 
511
512
513
 
516
517
518
 
519
520
521
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
522
523
524
525
526
527
528
529
530
531
 
532
533
534
 
 
 
535
536
537
538
539
 
 
 
 
 
 
540
541
 
542
543
544
545
 
572
573
574
 
575
576
577
578
 
617
618
619
620
621
622
623
624
625
626
627
628
629
 
 
 
 
 
 
 
630
631
632
633
634
 
635
636
637
638
@@ -57,6 +57,7 @@
  unsigned size;   unsigned mtime;   unsigned length; +   std::string name;     char status(const struct _stat& stat) const; @@ -89,36 +90,284 @@
 }     +class Directory +{ + typedef std::vector<Directory*> DirsT; + typedef std::vector<direntry> FilesT; + + Directory* parent_; + std::string name_; + + DirsT subdirs_; + FilesT files_; + +public: + Directory(Directory* p, const std::string& n): parent_(p), name_(n) {} + ~Directory(); + + std::string path(const std::string& n = "") const; + + int add(const std::string& relpath, direntry& e); + + const direntry* get(const std::string& relpath) const; + Directory* Directory::getdir(const std::string& n); + + char status(const std::string& hgroot); + + void print() const; +}; + + +Directory::~Directory() +{ + for (DirsT::iterator i = subdirs_.begin(); i != subdirs_.end(); ++i) + { + delete *i; + } +} + +int splitbase(const std::string& n, std::string& base, std::string& rest) +{ + if (n.empty()) + return 0; + + size_t x = n.find_first_of ('/'); + if (x == std::string::npos) + { + base.clear(); + rest = n; + return 1; + } + + if (x == 0 || x == n.length()-1) + return 0; + + base = n.substr(0, x); + rest = n.substr(x+1); + + return 1; +} + + +int Directory::add(const std::string& n, direntry& e) +{ + std::string base; + std::string rest; + + if (!splitbase(n, base, rest)) { + TDEBUG_TRACE("Directory(" << path() << ")::add(" << n << "): splitbase returned 0"); + return 0; + } + + if (base.empty()) + { + e.name = n; + files_.push_back(e); + return 1; + } + + Directory* d = 0; + for (DirsT::iterator i = subdirs_.begin(); i != subdirs_.end(); ++i) + { + if ((*i)->name_ == base) { + d = *i; + break; + } + } + + if (!d) + { + d = new Directory(this, base); + subdirs_.push_back(d); + } + + return d->add(rest, e); +} + + +const direntry* Directory::get(const std::string& n) const +{ + std::string base; + std::string rest; + + if (!splitbase(n, base, rest)) + { + TDEBUG_TRACE("Directory(" << path() << ")::get(" << n << "): splitbase returned 0"); + return 0; + } + + if (base.empty()) + { + for (FilesT::const_iterator i = files_.begin(); i != files_.end(); ++i) + { + if (i->name == n) + return &(*i); + } + return 0; + } + + for (DirsT::const_iterator i = subdirs_.begin(); i != subdirs_.end(); ++i) + { + if ((*i)->name_ == base) + return (*i)->get(rest); + } + + TDEBUG_TRACE("Directory(" << path() << ")::get(" << n << "): unknown subdir"); + return 0; +} + + +Directory* Directory::getdir(const std::string& n) +{ + std::string base; + std::string rest; + + if (!splitbase(n, base, rest)) + { + TDEBUG_TRACE("Directory(" << path() << ")::getdir(" << n << "): splitbase returned 0"); + return 0; + } + + const bool leaf = base.empty(); + const std::string& searchstr = (leaf ? n : base); + + for (DirsT::const_iterator i = subdirs_.begin(); i != subdirs_.end(); ++i) + { + if ((*i)->name_ == searchstr) + { + if (leaf) + return *i; + return (*i)->getdir(rest); + } + } + + return 0; +} + + +std::string Directory::path(const std::string& n) const +{ + if (name_.empty()) + return n; + std::string res = name_; + if (!n.empty()) + res += "/" + n; + if (!parent_) + return res; + return parent_->path(res); +} + + +char Directory::status(const std::string& hgroot) +{ + char res = 0; + struct _stat stat; + bool added = false; + + const std::string hrs = hgroot + '\\'; + + for (FilesT::iterator i = files_.begin(); i != files_.end(); ++i) + { + std::string p = hrs + path(i->name); + + if (0 != lstat(p.c_str(), stat)) + { + TDEBUG_TRACE("Directory(" << path() << ")::status: lstat(" << p << ") failed"); + continue; + } + + char s = i->status(stat); + + if (s == 'M') + return 'M'; + if (s == 'A') + added = true; + } + + for (DirsT::iterator i = subdirs_.begin(); i != subdirs_.end(); ++i) + { + char s = (*i)->status(hgroot); + if (s == 'M') + return 'M'; + if (s == 'A') + added = true; + } + + if (added) + return 'A'; + + return 'C'; +} + + +void Directory::print() const +{ + for (DirsT::const_iterator i = subdirs_.begin(); i != subdirs_.end(); ++i) + { + const Directory* d = *i; + if (!d) + { + TDEBUG_TRACE("Directory(" << path() << ")::print: error: d is 0"); + return; + } + d->print(); + } + + std::string base = path(); + + time_t t; + std::string s; + char* ctime_res = 0; + + for (FilesT::const_iterator i = files_.begin(); i != files_.end(); ++i) + { + std::string p = (!base.empty() ? base + "/" + i->name : i->name); + t = i->mtime; + ctime_res = ctime(&t); + if (ctime_res) { + s = ctime_res; + s.resize(s.size() - 1); // strip ending '\n' + } + else { + s = "unset"; + } + printf( + "%c %6o %10u %-24s %s\n", + i->state, i->mode, i->length, s.c_str(), p.c_str() + ); + } +} + +  #define HASH_LENGTH 20      class dirstate  { - typedef std::vector<direntry> EntriesT; + Directory root_;   - EntriesT entries; -   unsigned num_added_; // number of entries that have state 'a' + unsigned num_entries_;    public: - typedef EntriesT::size_type size_type; - typedef EntriesT::const_iterator Iter; -   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 direntry& e) { entries.push_back(e); } - - Iter begin() const { return entries.begin(); } - Iter end() const { return entries.end(); } - size_type size() const { return entries.size(); } + 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(): num_added_(0) {} + dirstate() + : root_(0, ""), num_added_(0), num_entries_(0) {}     static uint32_t ntohl(uint32_t x)   { @@ -164,12 +413,10 @@
  fread(&temp[0], sizeof(char), e.length, f);   temp[e.length] = 0;   - e.name = &temp[0]; -   if (e.state == 'a')   ++pd->num_added_;   - pd->add(e); + pd->add(&temp[0], e);   }     fclose(f); @@ -182,7 +429,7 @@
 {   struct entry   { - const dirstate* dstate; + dirstate* dstate;   __time64_t mtime;   std::string hgroot;   unsigned tickcount; @@ -195,13 +442,13 @@
  static std::list<entry> _cache;    public: - static const dirstate* get(const std::string& hgroot); + static dirstate* get(const std::string& hgroot);  };    std::list<dirstatecache::entry> dirstatecache::_cache;     -const dirstate* dirstatecache::get(const std::string& hgroot) +dirstate* dirstatecache::get(const std::string& hgroot)  {   Iter iter = _cache.begin();   @@ -261,7 +508,6 @@
  }   iter->dstate = dirstate::read(path).release();   TDEBUG_TRACE("dirstatecache::get: " - << iter->dstate->size() << " entries read. "   << _cache.size() << " repos in cache");   }   @@ -270,75 +516,30 @@
     static int HgQueryDirstateDirectory( - const std::string& hgroot, const dirstate& ds, + const std::string& hgroot, dirstate& ds,   const std::string& relpath, char& outStatus)  { - bool added = false; - bool modified = false; - bool empty = true; - - const size_t len = relpath.size(); - const std::string hgroot_slash = hgroot + "/"; - - struct _stat stat; - - for (dirstate::Iter iter = ds.begin(); - iter != ds.end() && !modified; ++iter) - { - const direntry& e = *iter; - - if (e.name.compare(0, len, relpath) != 0) - continue; - - empty = false; - - switch (e.state) - { - case 'n': - { - std::string temp = hgroot_slash + e.name; - if (0 == lstat(temp.c_str(), stat)) - modified = (e.status(stat) == 'M'); - } - break; - case 'm': - modified = true; - break; - case 'a': - added = true; - break; - } - } - - if (modified) - outStatus = 'M'; - else if (added) - outStatus = 'A'; - else if (empty) - outStatus = '?'; - else - outStatus = 'C'; - - return 1; + Directory* dir = ds.root().getdir(relpath); + + if (!dir) + return 0; + + outStatus = dir->status(hgroot);  }      static int HgQueryDirstateFile( - const dirstate& ds, const std::string& relpath, + dirstate& ds, const std::string& relpath,   const struct _stat& stat, char& outStatus)  { - for (dirstate::Iter iter = ds.begin(); iter != ds.end(); ++iter) - { - const direntry& e = *iter; + const direntry* e = ds.root().get(relpath); + + if (!e) + return 0;   - if (relpath == e.name) - { - outStatus = e.status(stat); - return outStatus != '?'; - } - } + outStatus = e->status(stat);   - return 0; + return outStatus != '?';  }     @@ -371,7 +572,7 @@
  || (relpath.size() > 4 && relpath.compare(0, 4, ".hg/") == 0))   return 0; // don't descend into .hg dir   - const dirstate* pds = dirstatecache::get(hgroot); + dirstate* pds = dirstatecache::get(hgroot);   if (!pds)   {   TDEBUG_TRACE("HgQueryDirstate: dirstatecache::get(" << hgroot << ") returns 0"); @@ -416,23 +617,22 @@
 void testread()  {   std::auto_ptr<dirstate> pd = dirstate::read(".hg/dirstate"); + if (!pd.get()) { + printf("error: could not read .hg/dirstate\n"); + return; + }   time_t t;   char *s;   unsigned ix;   printf("parent1: %s\n", revhash_string(pd->parent1));   printf("parent2: %s\n", revhash_string(pd->parent2));   printf("entries: %d\n\n", pd->size()); - for (dirstate::Iter i = pd->begin(); i != pd->end(); ++i) - { - t = i->mtime; - s = ctime(&t); - s[strlen(s) - 1] = '\0'; - printf("%s %s\n", s, i->name.c_str()); - } + + pd->root().print();  }     -#if 0 +#ifdef APPMAIN  int main(int argc, char *argv[])  {   testread();