Kiln » largefiles » Unity
Clone URL:  

overrides: extract 4x duplicated override_match code into a common function

Changeset 5f2a747353a3

Parent dd4a727edd72

by Profile picture of User 521Andrew Pritchard <andrewp@fogcreek.com>

Changes to one file · Browse files at 5f2a747353a3 Showing diff from parent dd4a727edd72 Diff from another changeset...

Change 1 of 8 Show Entire File overrides.py Stacked
 
26
27
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
30
31
 
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
 
179
180
181
182
 
183
184
185
 
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
 
387
388
389
 
392
393
394
395
396
397
398
399
400
 
401
402
403
 
407
408
409
 
 
 
 
 
 
410
411
412
 
786
787
788
789
 
 
 
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
 
823
824
825
826
 
827
828
829
 
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
 
144
145
146
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
148
149
150
151
152
153
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154
155
156
157
158
159
160
 
171
172
173
 
174
175
176
177
 
352
353
354
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
355
356
357
358
 
361
362
363
 
 
 
 
 
 
364
365
366
367
 
371
372
373
374
375
376
377
378
379
380
381
382
 
756
757
758
 
759
760
761
762
763
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
764
765
766
 
 
 
767
768
769
 
772
773
774
 
775
776
777
778
@@ -26,6 +26,47 @@
 import lfcommands  import proto   +def installnormalfilesmatchfn(manifest): + '''overrides scmutil.match so that the matcher it returns will ignore all + largefiles''' + try: + # Mercurial >= 1.9 + oldmatch = scmutil.match + except ImportError: + # Mercurial <= 1.8 + oldmatch = cmdutil.match + def override_match(repo, pats=[], opts={}, globbed=False, + default='relpath'): + match = oldmatch(repo, pats, opts, globbed, default) + m = copy.copy(match) + notlfile = lambda f: not (lfutil.isstandin(f) or lfutil.standin(f) in + manifest) + m._files = filter(notlfile, m._files) + m._fmap = set(m._files) + orig_matchfn = m.matchfn + m.matchfn = lambda f: notlfile(f) and orig_matchfn(f) or None + return m + setattr(override_match, 'oldmatch', oldmatch) + try: + # Mercurial >= 1.9 + scmutil.match = override_match + except ImportError: + # Mercurial <= 1.8 + cmdutil.match = override_match + +def restorematchfn(): + '''restores scmutil.match to what it was before installnormalfilesmatchfn + was called. no-op if scmutil.match is its original function. + + Note that n calls to installnormalfilesmatchfn will require n calls to + restore matchfn to reverse''' + try: + # Mercurial >= 1.9 + scmutil.match = getattr(scmutil.match, 'oldmatch', scmutil.match) + except ImportError: + # Mercurial <= 1.8 + cmdutil.match = getattr(cmdutil.match, 'oldmatch', cmdutil.match) +  # -- Wrappers: modify existing commands --------------------------------    # Add works by going through the files that the user wanted to add @@ -103,66 +144,17 @@
  finally:   wlock.release()   - try: - # Mercurial >= 1.9 - oldmatch = scmutil.match - except ImportError: - # Mercurial <= 1.8 - oldmatch = cmdutil.match - manifest = repo[None].manifest() - def override_match(repo, pats=[], opts={}, globbed=False, - default='relpath'): - match = oldmatch(repo, pats, opts, globbed, default) - m = copy.copy(match) - notlfile = lambda f: not lfutil.isstandin(f) and lfutil.standin(f) not\ - in manifest - m._files = [f for f in m._files if notlfile(f)] - m._fmap = set(m._files) - orig_matchfn = m.matchfn - m.matchfn = lambda f: notlfile(f) and orig_matchfn(f) or None - return m - try: - # Mercurial >= 1.9 - scmutil.match = override_match - result = orig(ui, repo, *pats, **opts) - scmutil.match = oldmatch - except ImportError: - # Mercurial <= 1.8 - cmdutil.match = override_match - result = orig(ui, repo, *pats, **opts) - cmdutil.match = oldmatch + installnormalfilesmatchfn(repo[None].manifest()) + result = orig(ui, repo, *pats, **opts) + restorematchfn()     return (result == 1 or bad) and 1 or 0    def override_remove(orig, ui, repo, *pats, **opts): - wctx = repo[None].manifest() - try: - # Mercurial >= 1.9 - oldmatch = scmutil.match - except ImportError: - # Mercurial <= 1.8 - oldmatch = cmdutil.match - def override_match(repo, pats=[], opts={}, globbed=False, - default='relpath'): - match = oldmatch(repo, pats, opts, globbed, default) - m = copy.copy(match) - notlfile = lambda f: not lfutil.isstandin(f) and lfutil.standin(f) not\ - in wctx - m._files = [f for f in m._files if notlfile(f)] - m._fmap = set(m._files) - orig_matchfn = m.matchfn - m.matchfn = lambda f: orig_matchfn(f) and notlfile(f) - return m - try: - # Mercurial >= 1.9 - scmutil.match = override_match - orig(ui, repo, *pats, **opts) - scmutil.match = oldmatch - except ImportError: - # Mercurial <= 1.8 - cmdutil.match = override_match - orig(ui, repo, *pats, **opts) - cmdutil.match = oldmatch + manifest = repo[None].manifest() + installnormalfilesmatchfn(manifest) + orig(ui, repo, *pats, **opts) + restorematchfn()     after, force = opts.get('after'), opts.get('force')   if not pats and not after: @@ -179,7 +171,7 @@
  finally:   repo.lfstatus = False   modified, added, deleted, clean = [[f for f in list if lfutil.standin(f) \ - in wctx] for list in [s[0], s[1], s[3], s[6]]] + in manifest] for list in [s[0], s[1], s[3], s[6]]]     def warn(files, reason):   for f in files: @@ -360,30 +352,7 @@
  nonormalfiles = False   nolfiles = False   try: - # Mercurial >= 1.9 - oldmatch = scmutil.match - except ImportError: - # Mercurial <= 1.8 - oldmatch = cmdutil.match - try: - manifest = repo[None].manifest() - def override_match(repo, pats=[], opts={}, globbed=False, - default='relpath'): - match = oldmatch(repo, pats, opts, globbed, default) - m = copy.copy(match) - notlfile = lambda f: not lfutil.isstandin(f) and lfutil.standin(f)\ - not in manifest - m._files = [f for f in m._files if notlfile(f)] - m._fmap = set(m._files) - orig_matchfn = m.matchfn - m.matchfn = lambda f: notlfile(f) and orig_matchfn(f) or None - return m - try: - # Mercurial >= 1.9 - scmutil.match = override_match - except ImportError: - # Mercurial <= 1.8 - cmdutil.match = override_match + installnormalfilesmatchfn(repo[None].manifest())   result = orig(ui, repo, pats, opts, rename)   except util.Abort, e:   if str(e) != 'no files to copy': @@ -392,12 +361,7 @@
  nonormalfiles = True   result = 0   finally: - try: - # Mercurial >= 1.9 - scmutil.match = oldmatch - except ImportError: - # Mercurial <= 1.8 - cmdutil.match = oldmatch + restorematchfn()     # The first rename can cause our current working directory to be removed.   # In that case there is nothing left to copy/rename so just quit. @@ -407,6 +371,12 @@
  return result     try: + # Mercurial >= 1.9 + oldmatch = scmutil.match + except ImportError: + # Mercurial <= 1.8 + oldmatch = cmdutil.match + try:   # When we call orig below it creates the standins but we don't add them   # to the dir state until later so lock during that time.   wlock = repo.wlock() @@ -786,35 +756,14 @@
  return orig(ui, repo, *pats, **opts)    def override_forget(orig, ui, repo, *pats, **opts): - wctx = repo[None].manifest() + installnormalfilesmatchfn(repo[None].manifest()) + orig(ui, repo, *pats, **opts) + restorematchfn()   try:   # Mercurial >= 1.9 - oldmatch = scmutil.match - except ImportError: - # Mercurial <= 1.8 - oldmatch = cmdutil.match - def override_match(repo, pats=[], opts={}, globbed=False, - default='relpath'): - match = oldmatch(repo, pats, opts, globbed, default) - m = copy.copy(match) - notlfile = lambda f: not lfutil.isstandin(f) and lfutil.standin(f) not\ - in wctx - m._files = [f for f in m._files if notlfile(f)] - m._fmap = set(m._files) - orig_matchfn = m.matchfn - m.matchfn = lambda f: orig_matchfn(f) and notlfile(f) - return m - try: - # Mercurial >= 1.9 - scmutil.match = override_match - orig(ui, repo, *pats, **opts) - scmutil.match = oldmatch   m = scmutil.match(repo[None], pats, opts)   except ImportError:   # Mercurial <= 1.8 - cmdutil.match = override_match - orig(ui, repo, *pats, **opts) - cmdutil.match = oldmatch   m = cmdutil.match(repo, pats, opts)     try: @@ -823,7 +772,7 @@
  finally:   repo.lfstatus = False   forget = sorted(s[0] + s[1] + s[3] + s[6]) - forget = [f for f in forget if lfutil.standin(f) in wctx] + forget = [f for f in forget if lfutil.standin(f) in repo[None].manifest()]     for f in forget:   if lfutil.standin(f) not in repo.dirstate and not \