Conditions | 16 |
Total Lines | 79 |
Code Lines | 54 |
Lines | 0 |
Ratio | 0 % |
Tests | 44 |
CRAP Score | 16.4423 |
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 |
|
59 | def update_files( |
||
60 | self, |
||
61 | 1 | force=False, |
|
62 | 1 | force_interactive=False, |
|
63 | fetch=False, |
||
64 | clean=True, |
||
65 | 1 | skip_changes=False, |
|
66 | 1 | ): |
|
67 | 1 | """Ensure the source matches the specified revision.""" |
|
68 | log.info("Updating source files...") |
||
69 | |||
70 | 1 | # Clone the repository if needed |
|
71 | 1 | assert self.name |
|
72 | 1 | if not os.path.exists(self.name): |
|
73 | git.clone( |
||
74 | self.type, |
||
75 | self.repo, |
||
76 | self.name, |
||
77 | 1 | sparse_paths=self.sparse_paths, |
|
78 | rev=self.rev, |
||
79 | ) |
||
80 | 1 | ||
81 | # Enter the working tree |
||
82 | shell.cd(self.name) |
||
83 | 1 | if not git.valid(): |
|
84 | if force: |
||
85 | 1 | git.rebuild(self.type, self.repo) |
|
86 | fetch = True |
||
87 | 1 | else: |
|
88 | 1 | raise self._invalid_repository |
|
89 | |||
90 | 1 | # Check for uncommitted changes |
|
91 | if not force: |
||
92 | 1 | log.debug("Confirming there are no uncommitted changes...") |
|
93 | if skip_changes: |
||
94 | if git.changes( |
||
95 | self.type, include_untracked=clean, display_status=False |
||
96 | 1 | ): |
|
97 | 1 | common.show( |
|
98 | f'Skipped update due to uncommitted changes in {os.getcwd()}', |
||
99 | 1 | color='git_changes', |
|
100 | 1 | ) |
|
101 | 1 | return |
|
102 | 1 | elif force_interactive: |
|
103 | 1 | if git.changes( |
|
104 | self.type, include_untracked=clean, display_status=False |
||
105 | 1 | ): |
|
106 | 1 | common.show( |
|
107 | f'Uncommitted changes found in {os.getcwd()}', |
||
108 | 1 | color='git_changes', |
|
109 | ) |
||
110 | 1 | ||
111 | 1 | while True: |
|
112 | yn_input = str( |
||
113 | input("Do you want to overwrite? (Y/N)[Y]: ") |
||
114 | 1 | ).rstrip('\r\n') |
|
115 | 1 | ||
116 | if yn_input.lower() == "y" or not yn_input: |
||
117 | break |
||
118 | |||
119 | 1 | if yn_input.lower() == "n": |
|
120 | 1 | common.show( |
|
121 | 1 | f'Skipped update in {os.getcwd()}', color='git_changes' |
|
122 | 1 | ) |
|
123 | return |
||
124 | |||
125 | 1 | else: |
|
126 | 1 | if git.changes(self.type, include_untracked=clean): |
|
127 | 1 | raise exceptions.UncommittedChanges( |
|
128 | 1 | f'Uncommitted changes in {os.getcwd()}' |
|
129 | 1 | ) |
|
130 | 1 | ||
131 | 1 | # Fetch the desired revision |
|
132 | 1 | if fetch or git.is_fetch_required(self.type, self.rev): |
|
133 | git.fetch(self.type, self.repo, self.name, rev=self.rev) |
||
134 | 1 | ||
135 | 1 | # Update the working tree to the desired revision |
|
136 | git.update( |
||
137 | self.type, self.repo, self.name, fetch=fetch, clean=clean, rev=self.rev |
||
138 | 1 | ) |
|
278 |