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