File: repo_comparator.py

package info (click to toggle)
git-ubuntu 1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,688 kB
  • sloc: python: 13,378; sh: 480; makefile: 2
file content (56 lines) | stat: -rw-r--r-- 1,933 bytes parent folder | download
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
from enum import Enum
import tempfile

import pygit2

class RepoComparisonType(Enum):
    Tree = 1
    Commit = 2

def equals(repoA, repoB, test_refs, comparison_type=RepoComparisonType.Commit):
    """Compare two pygit2.Repository objects for equality

    :param pygit2.Repository repoA The first repository to compare
    :param gitubuntu.repo_builder.Repo repoB The second repository to compare
    :param list(str) test_refs String reference names that should be
        compared for equality
    :param RepoComparisonType comparison_type What underlying Git object
        should be compared for equality

    :rtype bool
    :returns Whether @repoA and @repoB contain the matching references and
    reachable Git commits
    """

    test_refs = set(test_refs)

    with tempfile.TemporaryDirectory() as tmp_dir:
        pygit2_repoB = pygit2.init_repository(tmp_dir)
        repoB.write(pygit2_repoB)

        repoA_refs = set(repoA.listall_references())
        if not test_refs.issubset(repoA_refs):
            return False
        repoA_refs = repoA_refs.intersection(test_refs)
        repoB_refs = set(pygit2_repoB.listall_references())
        if not test_refs.issubset(repoB_refs):
            return False
        repoB_refs = repoB_refs.intersection(test_refs)

        if repoA_refs.symmetric_difference(repoB_refs):
            return False

        if comparison_type == RepoComparisonType.Tree:
            peel_type = pygit2.Tree
        elif comparison_type == RepoComparisonType.Commit:
            peel_type = pygit2.Commit
        else:
            raise TypeError("Unknown RepoComparisonType: %r" % comparison_type)

        for ref in repoA_refs:
            refA_peeled = repoA.lookup_reference(ref).peel(peel_type)
            refB_peeled = pygit2_repoB.lookup_reference(ref).peel(peel_type)
            if str(refA_peeled.id) != str(refB_peeled.id):
                return False

        return True