comparison mercurial/cmdutil.py @ 34684:5d98674df18a

tersestatus: avoid modifying tersedict Turn dirnode's methods into generators which can be used to update "tersedict" in caller. So instead of passing the "tersedict" to be mutated here and there, it's now clearer where it is updated as it's purely a local variable to tersedir() function. While I was here, I renamed _processtersestatus to tersewalk and _addfilestotersed to iterfilepaths. Differential Revision: https://phab.mercurial-scm.org/D1043
author Denis Laxalde <denis@laxalde.org>
date Fri, 13 Oct 2017 04:02:06 +0530
parents 3d6d4b12128e
children 23eb03f46929
comparison
equal deleted inserted replaced
34683:3d6d4b12128e 34684:5d98674df18a
460 self._addfileindir(filename, status) 460 self._addfileindir(filename, status)
461 461
462 if status not in self.statuses: 462 if status not in self.statuses:
463 self.statuses.add(status) 463 self.statuses.add(status)
464 464
465 def _addfilestotersed(self, tersedict): 465 def iterfilepaths(self):
466 """ 466 """
467 adds files to the their respective status list in the final tersed list 467 adds files to the their respective status list in the final tersed list
468 468
469 path is the path of parent directory of the file 469 path is the path of parent directory of the file
470 files is a list of tuple where each tuple is (filename, status) 470 files is a list of tuple where each tuple is (filename, status)
471 tersedict is a dictonary which contains each status abbreviation as key and
472 list of files and tersed dirs in that status as value
473 """ 471 """
474 for f, st in self.files: 472 for f, st in self.files:
475 tersedict[st].append(os.path.join(self.path, f)) 473 yield st, os.path.join(self.path, f)
476 474
477 def _processtersestatus(self, tersedict, terseargs): 475 def tersewalk(self, terseargs):
478 """ 476 """
479 a recursive function which process status for a certain directory. 477 a recursive function which process status for a certain directory.
480 478
481 self is an oject of dirnode class defined below. each object of dirnode 479 self is an oject of dirnode class defined below. each object of dirnode
482 class has a set of statuses which files in that directory has. This ease 480 class has a set of statuses which files in that directory has. This ease
492 490
493 Following are the cases which can happen: 491 Following are the cases which can happen:
494 492
495 1) All the files in the directory (including all the files in its 493 1) All the files in the directory (including all the files in its
496 subdirectories) share the same status and the user has asked us to terse 494 subdirectories) share the same status and the user has asked us to terse
497 that status. -> we add the directory name to status list and return 495 that status. -> yield (status, dirpath)
498 496
499 2) If '1)' does not happen, we do following: 497 2) If '1)' does not happen, we do following:
500 498
501 a) Add all the files which are in this directory (only the ones in 499 a) Yield (status, filepath) for all the files which are in this
502 this directory, not the subdirs) to their respective status list 500 directory (only the ones in this directory, not the subdirs)
503 501
504 b) Recurse the function on all the subdirectories of this 502 b) Recurse the function on all the subdirectories of this
505 directory 503 directory
506 """ 504 """
507 505
509 onlyst = self.statuses.pop() 507 onlyst = self.statuses.pop()
510 508
511 # Making sure we terse only when the status abbreviation is 509 # Making sure we terse only when the status abbreviation is
512 # passed as terse argument 510 # passed as terse argument
513 if onlyst in terseargs: 511 if onlyst in terseargs:
514 tersedict[onlyst].append(self.path + pycompat.ossep) 512 yield onlyst, self.path + pycompat.ossep
515 return 513 return
516 514
517 # add the files to status list 515 # add the files to status list
518 self._addfilestotersed(tersedict) 516 for st, fpath in self.iterfilepaths():
517 yield st, fpath
519 518
520 #recurse on the subdirs 519 #recurse on the subdirs
521 for dirobj in self.subdirs.values(): 520 for dirobj in self.subdirs.values():
522 dirobj._processtersestatus(tersedict, terseargs) 521 for st, fpath in dirobj.tersewalk(terseargs):
522 yield st, fpath
523 523
524 def tersedir(statuslist, terseargs): 524 def tersedir(statuslist, terseargs):
525 """ 525 """
526 terses the status if all the files in a directory shares the same status 526 terses the status if all the files in a directory shares the same status
527 527
534 stores the information required to know whether we can terse a certain 534 stores the information required to know whether we can terse a certain
535 directory or not. 535 directory or not.
536 536
537 tersedict (defined in the function) is a dictionary which has one word key 537 tersedict (defined in the function) is a dictionary which has one word key
538 for each status and a list of files and dir in that status as the respective 538 for each status and a list of files and dir in that status as the respective
539 value. The dictionary is passed to other helper functions which builds it. 539 value.
540 """ 540 """
541 # the order matters here as that is used to produce final list 541 # the order matters here as that is used to produce final list
542 allst = ('m', 'a', 'r', 'd', 'u', 'i', 'c') 542 allst = ('m', 'a', 'r', 'd', 'u', 'i', 'c')
543 543
544 # checking the argument validity 544 # checking the argument validity
556 for f in getattr(statuslist, attrname): 556 for f in getattr(statuslist, attrname):
557 rootobj.addfile(f, attrname[0]) 557 rootobj.addfile(f, attrname[0])
558 tersedict[attrname[0]] = [] 558 tersedict[attrname[0]] = []
559 559
560 # we won't be tersing the root dir, so add files in it 560 # we won't be tersing the root dir, so add files in it
561 rootobj._addfilestotersed(tersedict) 561 for st, fpath in rootobj.iterfilepaths():
562 tersedict[st].append(fpath)
562 563
563 # process each sub-directory and build tersedict 564 # process each sub-directory and build tersedict
564 for subdir in rootobj.subdirs.values(): 565 for subdir in rootobj.subdirs.values():
565 subdir._processtersestatus(tersedict, terseargs) 566 for st, f in subdir.tersewalk(terseargs):
567 tersedict[st].append(f)
566 568
567 tersedlist = [] 569 tersedlist = []
568 for st in allst: 570 for st in allst:
569 tersedict[st].sort() 571 tersedict[st].sort()
570 tersedlist.append(tersedict[st]) 572 tersedlist.append(tersedict[st])