Documentation
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
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#!/usr/bin/env python3

import hashlib
import inspect
import os
import os.path
import shutil
import subprocess
import tempfile

import git

binary = os.environ["GRM_BINARY"]


def funcname():
    return inspect.stack()[1][3]


def copytree(src, dest):
    shutil.copytree(src, dest, dirs_exist_ok=True)


def get_temporary_directory(dir=None):
    return tempfile.TemporaryDirectory(dir=dir)


def grm(args, cwd=None, is_invalid=False):
    cmd = subprocess.run([binary] + args, cwd=cwd, capture_output=True, text=True)
    if not is_invalid:
        assert "usage" not in cmd.stderr.lower()
    print(f"grmcmd: {args}")
    print(f"stdout:\n{cmd.stdout}")
    print(f"stderr:\n{cmd.stderr}")
    assert "secret-token:" not in cmd.stdout
    assert "secret-token:" not in cmd.stderr
    assert "panicked" not in cmd.stderr
    return cmd


def shell(script):
    script = "set -o errexit\nset -o nounset\nset -o pipefail\n" + script
    cmd = subprocess.run(["bash"], input=script, text=True, capture_output=True)
    if cmd.returncode != 0:
        print(cmd.stdout)
        print(cmd.stderr)
    cmd.check_returncode()


def checksum_directory(path):
    """
    Gives a "checksum" of a directory that includes all files & directories
    recursively, including owner/group/permissions. Useful to compare that a
    directory did not change after a command was run.

    The following makes it a bit complicated:

    > Whether or not the lists are sorted depends on the file system.

    - https://docs.python.org/3/library/os.html#os.walk

    This means we have to first get a list of all hashes of files and
    directories, then sort the hashes and then create the hash for the whole
    directory.
    """
    path = os.path.realpath(path)

    hashes = []

    if not os.path.exists(path):
        raise f"{path} not found"

    def get_stat_hash(path):
        checksum = hashlib.md5()

        # A note about bytes(). You may think that it converts something to
        # bytes (akin to str()). But it actually creates a list of zero bytes
        # with the length specified by the parameter.
        #
        # This is kinda couterintuitive to me:
        #
        # str(5)   => '5'
        # bytes(5) => b'\x00\x00\x00\x00\x00'
        def int_to_bytes(i):
            return i.to_bytes((i.bit_length() + 7) // 8, byteorder="big")

        # lstat() instead of stat() so symlinks are not followed. So symlinks
        # are treated as-is and will also be checked for changes.
        stat = os.lstat(path)

        # Note that the list of attributes does not include any timings except
        # mtime.
        for s in [
            stat.st_mode,  # type & permission bits
            stat.st_ino,  # inode
            stat.st_uid,
            stat.st_gid,
            # it's a float in seconds, so this gives us ~1us precision
            int(stat.st_mtime * 1e6),
        ]:
            checksum.update(int_to_bytes(s))
        return checksum.digest()

    for root, dirs, files in os.walk(path):
        for file in files:
            checksum = hashlib.md5()
            filepath = os.path.join(root, file)
            checksum.update(str.encode(filepath))
            checksum.update(get_stat_hash(filepath))
            with open(filepath, "rb") as f:
                while True:
                    data = f.read(8192)
                    if not data:
                        break
                    checksum.update(data)
            hashes.append(checksum.digest())

        for d in dirs:
            checksum = hashlib.md5()
            dirpath = os.path.join(root, d)
            checksum.update(get_stat_hash(dirpath))
            hashes.append(checksum.digest())

    checksum = hashlib.md5()
    for c in sorted(hashes):
        checksum.update(c)
    return checksum.hexdigest()


class TempGitRepository:
    def __init__(self, dir=None):
        self.dir = dir

    def __enter__(self):
        self.tmpdir = get_temporary_directory(self.dir)
        self.remote_1 = get_temporary_directory()
        self.remote_2 = get_temporary_directory()
        cmd = f"""
            cd {self.tmpdir.name}
            git -c init.defaultBranch=master init
            echo test > root-commit
            git add root-commit
            git commit -m "root-commit"
            git remote add origin file://{self.remote_1.name}
            git remote add otherremote file://{self.remote_2.name}
        """

        shell(cmd)
        return self.tmpdir.name

    def __exit__(self, exc_type, exc_val, exc_tb):
        pass


class TempGitRemote:
    obj = {}

    def __init__(self, tmpdir, remoteid=None):
        self.tmpdir = tmpdir
        self.remoteid = remoteid

    @classmethod
    def get(cls, cachekey=None, initfunc=None):
        if cachekey is None:
            tmpdir = get_temporary_directory()
            shell(
                f"""
                cd {tmpdir.name}
                git -c init.defaultBranch=master init --bare
            """
            )
            newobj = cls(tmpdir)
            remoteid = None
            if initfunc is not None:
                remoteid = newobj.init(initfunc)
            newobj.remoteid = remoteid
            return newobj, remoteid
        else:
            if cachekey not in cls.obj:
                tmpdir = get_temporary_directory()
                shell(
                    f"""
                    cd {tmpdir.name}
                    git -c init.defaultBranch=master init --bare
                """
                )
                newobj = cls(tmpdir)
                remoteid = newobj.init(initfunc)
                newobj.remoteid = remoteid
                cls.obj[cachekey] = newobj
            return cls.clone(cls.obj[cachekey])

    @classmethod
    def clone(cls, source):
        new_remote = get_temporary_directory()
        copytree(source.tmpdir.name, new_remote.name)
        return cls(new_remote, source.remoteid), source.remoteid

    def init(self, func):
        return func(self.tmpdir.name)

    def __enter__(self):
        return self.tmpdir

    def __exit__(self, exc_type, exc_val, exc_tb):
        pass


class TempGitRepositoryWorktree:
    obj = {}

    def __init__(self, remotes, tmpdir, commit, remote1, remote2, remote1id, remote2id):
        self.remotes = remotes
        self.tmpdir = tmpdir
        self.commit = commit
        self.remote1 = remote1
        self.remote2 = remote2
        self.remote1id = remote1id
        self.remote2id = remote2id

    @classmethod
    def get(cls, cachekey, branch=None, remotes=2, basedir=None, remote_setup=None):
        if cachekey not in cls.obj:
            tmpdir = get_temporary_directory()
            shell(
                f"""
                cd {tmpdir.name}
                git -c init.defaultBranch=master init
                echo test > root-commit-in-worktree-1
                git add root-commit-in-worktree-1
                git commit -m "root-commit-in-worktree-1"
                echo test > root-commit-in-worktree-2
                git add root-commit-in-worktree-2
                git commit -m "root-commit-in-worktree-2"

                git ls-files | xargs rm -rf
                mv .git .git-main-working-tree
                git --git-dir .git-main-working-tree config core.bare true
            """
            )

            repo = git.Repo(f"{tmpdir.name}/.git-main-working-tree")

            commit = repo.head.commit.hexsha
            if branch is not None:
                repo.create_head(branch)

            remote1 = None
            remote2 = None
            remote1id = None
            remote2id = None

            if remotes >= 1:
                cachekeyremote, initfunc = (remote_setup or ((None, None),))[0]
                remote1, remote1id = TempGitRemote.get(
                    cachekey=cachekeyremote, initfunc=initfunc
                )
                remote1 = remote1
                remote1id = remote1id
                shell(
                    f"""
                    cd {tmpdir.name}
                    git --git-dir .git-main-working-tree remote add origin file://{remote1.tmpdir.name}
                """
                )
                repo.remotes.origin.fetch()
                repo.remotes.origin.push("master")

            if remotes >= 2:
                cachekeyremote, initfunc = (remote_setup or (None, (None, None)))[1]
                remote2, remote2id = TempGitRemote.get(
                    cachekey=cachekeyremote, initfunc=initfunc
                )
                remote2 = remote2
                remote2id = remote2id
                shell(
                    f"""
                    cd {tmpdir.name}
                    git --git-dir .git-main-working-tree remote add otherremote file://{remote2.tmpdir.name}
                """
                )
                repo.remotes.otherremote.fetch()
                repo.remotes.otherremote.push("master")

            cls.obj[cachekey] = cls(
                remotes, tmpdir, commit, remote1, remote2, remote1id, remote2id
            )

        return cls.clone(cls.obj[cachekey], remote_setup=remote_setup)

    @classmethod
    def clone(cls, source, remote_setup):
        newdir = get_temporary_directory()

        copytree(source.tmpdir.name, newdir.name)

        remote1 = None
        remote2 = None
        remote1id = None
        remote2id = None
        repo = git.Repo(os.path.join(newdir.name, ".git-main-working-tree"))
        if source.remotes >= 1:
            cachekey, initfunc = (remote_setup or ((None, None),))[0]
            remote1, remote1id = TempGitRemote.get(cachekey=cachekey, initfunc=initfunc)
            if remote1id != source.remote1id:
                repo.remotes.origin.fetch()
                repo.remotes.origin.push("master")
        if source.remotes >= 2:
            cachekey, initfunc = (remote_setup or (None, (None, None)))[1]
            remote2, remote2id = TempGitRemote.get(cachekey=cachekey, initfunc=initfunc)
            if remote2id != source.remote2id:
                repo.remotes.otherremote.fetch()
                repo.remotes.otherremote.push("master")

        return cls(
            source.remotes,
            newdir,
            source.commit,
            remote1,
            remote2,
            remote1id,
            remote2id,
        )

    def __enter__(self):
        return (self.tmpdir.name, self.commit)

    def __exit__(self, exc_type, exc_val, exc_tb):
        pass


class RepoTree:
    def __init__(self):
        pass

    def __enter__(self):
        self.root = get_temporary_directory()
        self.config = tempfile.NamedTemporaryFile()
        with open(self.config.name, "w") as f:
            f.write(
                f"""
                [[trees]]
                root = "{self.root.name}"

                [[trees.repos]]
                name = "test"

                [[trees.repos]]
                name = "test_worktree"
                worktree_setup = true
            """
            )

        cmd = grm(["repos", "sync", "config", "--config", self.config.name])
        assert cmd.returncode == 0
        return (self.root.name, self.config.name, ["test", "test_worktree"])

    def __exit__(self, exc_type, exc_val, exc_tb):
        del self.root
        del self.config


class EmptyDir:
    def __init__(self):
        pass

    def __enter__(self):
        self.tmpdir = get_temporary_directory()
        return self.tmpdir.name

    def __exit__(self, exc_type, exc_val, exc_tb):
        del self.tmpdir


class NonGitDir:
    def __init__(self):
        pass

    def __enter__(self):
        self.tmpdir = get_temporary_directory()
        shell(
            f"""
            cd {self.tmpdir.name}
            mkdir testdir
            touch testdir/test
            touch test2
        """
        )
        return self.tmpdir.name

    def __exit__(self, exc_type, exc_val, exc_tb):
        del self.tmpdir


class TempGitFileRemote:
    def __init__(self):
        pass

    def __enter__(self):
        self.tmpdir = get_temporary_directory()
        shell(
            f"""
            cd {self.tmpdir.name}
            git -c init.defaultBranch=master init
            echo test > root-commit-in-remote-1
            git add root-commit-in-remote-1
            git commit -m "root-commit-in-remote-1"
            echo test > root-commit-in-remote-2
            git add root-commit-in-remote-2
            git commit -m "root-commit-in-remote-2"
            git ls-files | xargs rm -rf
            mv .git/* .
            git config core.bare true
        """
        )
        head_commit_sha = git.Repo(self.tmpdir.name).head.commit.hexsha
        return (self.tmpdir.name, head_commit_sha)

    def __exit__(self, exc_type, exc_val, exc_tb):
        del self.tmpdir


class NonExistentPath:
    def __init__(self):
        pass

    def __enter__(self):
        self.dir = "/doesnotexist"
        if os.path.exists(self.dir):
            raise f"{self.dir} exists for some reason"
        return self.dir

    def __exit__(self, exc_type, exc_val, exc_tb):
        pass