Passed
Push — main ( 05fcff...04a574 )
by Eran
01:28
created

git_commits   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 13
eloc 44
dl 0
loc 60
rs 10
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A git_commits() 0 8 3
C create_graph_model() 0 35 10
1
import operator
2
from pathlib import Path
3
from tempfile import TemporaryDirectory
4
5
import git
6
7
import graphinate
8
9
10
def create_graph_model(repo: git.Repo):
11
    # Fetch all branches from the remote
12
    repo.git.fetch('--all')
13
14
    graph_model = graphinate.GraphModel(name='Git Repository Graph')
15
16
    @graph_model.node(operator.itemgetter('type'), key=operator.itemgetter('id'), label=operator.itemgetter('label'))
17
    def commit():
18
        for b in repo.remote().refs:
19
            for c in repo.iter_commits(b):
20
                branch = b.name.replace('origin/', '')
21
                for char in '-/. ':
22
                    if char in branch:
23
                        branch = branch.replace(char, '_')
24
25
                yield {'id': c.hexsha,
26
                       'type': branch,
27
                       'branch': b.name,
28
                       'label': c.summary}
29
                for f in c.stats.files:
30
                    yield {'id': f,
31
                           'type': 'file',
32
                           'branch': b.name,
33
                           'label': f}
34
35
    @graph_model.edge()
36
    def branch():
37
        for b in repo.remote().refs:
38
            for c in repo.iter_commits(b):
39
                if c.parents:
40
                    yield {'source': c.parents[0].hexsha, 'target': c.hexsha}
41
                    for f in c.stats.files:
42
                        yield {'source': c.hexsha, 'target': f}
43
44
    return graph_model
45
46
47
def git_commits(repo_url: str):
48
    with TemporaryDirectory() as temp_dir:
49
        repo_path = Path(temp_dir) / 'magika'
50
51
        with git.Repo.clone_from(repo_url, repo_path) as repo:
52
            model = create_graph_model(repo)
53
            schema = graphinate.builders.GraphQLBuilder(model).build()
54
            graphinate.graphql.server(schema)
55
56
57
if __name__ == '__main__':
58
    # git_commits(repo_url='https://github.com/google/magika.git')
59
    git_commits(repo_url='https://github.com/erivlis/mappingtools.git')
60