Conditions | 16 |
Total Lines | 79 |
Code Lines | 54 |
Lines | 0 |
Ratio | 0 % |
Tests | 44 |
CRAP Score | 16.2716 |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like gitman.models.source.Source.update_files() 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 | 1 | import os |
|
50 | 1 | def update_files( |
|
51 | 1 | self, |
|
52 | force=False, |
||
53 | 1 | force_interactive=False, |
|
54 | 1 | fetch=False, |
|
55 | clean=True, |
||
56 | 1 | skip_changes=False, |
|
57 | ): |
||
58 | 1 | """Ensure the source matches the specified revision.""" |
|
59 | log.info("Updating source files...") |
||
60 | |||
61 | 1 | # Clone the repository if needed |
|
62 | 1 | assert self.name |
|
63 | if not os.path.exists(self.name): |
||
64 | git.clone( |
||
65 | 1 | self.type, |
|
66 | 1 | self.repo, |
|
67 | 1 | self.name, |
|
68 | sparse_paths=self.sparse_paths, |
||
69 | rev=self.rev, |
||
70 | 1 | ) |
|
71 | 1 | ||
72 | 1 | # Enter the working tree |
|
73 | shell.cd(self.name) |
||
74 | if not git.valid(): |
||
75 | if force: |
||
76 | git.rebuild(self.type, self.repo) |
||
77 | 1 | fetch = True |
|
78 | else: |
||
79 | raise self._invalid_repository |
||
80 | 1 | ||
81 | # Check for uncommitted changes |
||
82 | if not force: |
||
83 | 1 | log.debug("Confirming there are no uncommitted changes...") |
|
84 | if skip_changes: |
||
85 | 1 | if git.changes( |
|
86 | self.type, include_untracked=clean, display_status=False |
||
87 | 1 | ): |
|
88 | 1 | common.show( |
|
89 | f'Skipped update due to uncommitted changes in {os.getcwd()}', |
||
90 | 1 | color='git_changes', |
|
91 | ) |
||
92 | 1 | return |
|
93 | elif force_interactive: |
||
94 | if git.changes( |
||
95 | self.type, include_untracked=clean, display_status=False |
||
96 | 1 | ): |
|
97 | 1 | common.show( |
|
98 | f'Uncommitted changes found in {os.getcwd()}', |
||
99 | 1 | color='git_changes', |
|
100 | 1 | ) |
|
101 | 1 | ||
102 | 1 | while True: |
|
103 | 1 | yn_input = str( |
|
104 | input("Do you want to overwrite? (Y/N)[Y]: ") |
||
105 | 1 | ).rstrip('\r\n') |
|
106 | 1 | ||
107 | if yn_input.lower() == "y" or not yn_input: |
||
108 | 1 | break |
|
109 | |||
110 | 1 | if yn_input.lower() == "n": |
|
111 | 1 | common.show( |
|
112 | f'Skipped update in {os.getcwd()}', color='git_changes' |
||
113 | ) |
||
114 | 1 | return |
|
115 | 1 | ||
116 | else: |
||
117 | if git.changes(self.type, include_untracked=clean): |
||
118 | raise exceptions.UncommittedChanges( |
||
119 | 1 | f'Uncommitted changes in {os.getcwd()}' |
|
120 | 1 | ) |
|
121 | 1 | ||
122 | 1 | # Fetch the desired revision |
|
123 | if fetch or git.is_fetch_required(self.type, self.rev): |
||
124 | git.fetch(self.type, self.repo, self.name, rev=self.rev) |
||
125 | 1 | ||
126 | 1 | # Update the working tree to the desired revision |
|
127 | 1 | git.update( |
|
128 | 1 | self.type, self.repo, self.name, fetch=fetch, clean=clean, rev=self.rev |
|
129 | 1 | ) |
|
267 |