Debian Codegraph @

tests/generate-working-copy-states.py

raw
1 # Helper script used for generating history and working copy files and content.
2 # The file's name corresponds to its history. The number of changesets can
3 # be specified on the command line. With 2 changesets, files with names like
4 # content1_content2_content1-untracked are generated. The first two filename
5 # segments describe the contents in the two changesets. The third segment
6 # ("content1-untracked") describes the state in the working copy, i.e.
7 # the file has content "content1" and is untracked (since it was previously
8 # tracked, it has been forgotten).
9 #
10 # This script generates the filenames and their content, but it's up to the
11 # caller to tell hg about the state.
12 #
13 # There are two subcommands:
14 # filelist <numchangesets>
15 # state <numchangesets> (<changeset>|wc)
16 #
17 # Typical usage:
18 #
19 # $ python $TESTDIR/generate-working-copy-states.py state 2 1
20 # $ hg addremove --similarity 0
21 # $ hg commit -m 'first'
22 #
23 # $ python $TESTDIR/generate-working-copy-states.py state 2 1
24 # $ hg addremove --similarity 0
25 # $ hg commit -m 'second'
26 #
27 # $ python $TESTDIR/generate-working-copy-states.py state 2 wc
28 # $ hg addremove --similarity 0
29 # $ hg forget *_*_*-untracked
30 # $ rm *_*_missing-*
31
32
33 import os
34 import sys
35
36
37 # Generates pairs of (filename, contents), where 'contents' is a list
38 # describing the file's content at each revision (or in the working copy).
39 # At each revision, it is either None or the file's actual content. When not
40 # None, it may be either new content or the same content as an earlier
41 # revisions, so all of (modified,clean,added,removed) can be tested.
42 def generatestates(maxchangesets, parentcontents):
43 depth = len(parentcontents)
44 if depth == maxchangesets + 1:
45 for tracked in (b'untracked', b'tracked'):
46 filename = (
47 b"_".join(
48 [
49 (content is None and b'missing' or content)
50 for content in parentcontents
51 ]
52 )
53 + b"-"
54 + tracked
55 )
56 yield (filename, parentcontents)
57 else:
58 for content in {None, b'content' + (b"%d" % (depth + 1))} | set(
59 parentcontents
60 ):
61 yield from generatestates(maxchangesets, parentcontents + [content])
62
63
64 # retrieve the command line arguments
65 target = sys.argv[1]
66 maxchangesets = int(sys.argv[2])
67 if target == 'state':
68 depth = sys.argv[3]
69
70 # sort to make sure we have stable output
71 combinations = sorted(generatestates(maxchangesets, []))
72
73 # compute file content
74 content = []
75 for filename, states in combinations:
76 if target == 'filelist':
77 print(filename.decode('ascii'))
78 elif target == 'state':
79 if depth == 'wc':
80 # Make sure there is content so the file gets written and can be
81 # tracked. It will be deleted outside of this script.
82 content.append((filename, states[maxchangesets] or b'TOBEDELETED'))
83 else:
84 content.append((filename, states[int(depth) - 1]))
85 else:
86 print("unknown target:", target, file=sys.stderr)
87 sys.exit(1)
88
89 # write actual content
90 for filename, data in content:
91 if data is not None:
92 f = open(filename, 'wb')
93 f.write(data + b'\n')
94 f.close()
95 elif os.path.exists(filename):
96 os.remove(filename)