| Conditions | 10 |
| Total Lines | 35 |
| Code Lines | 29 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Complex classes like git_commits.create_graph_model() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | import operator |
||
| 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 | |||
| 60 |