Conditions | 10 |
Total Lines | 61 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 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 clone() 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 | # -*- coding: utf-8 -*- |
||
90 | def clone(repo_url, checkout=None, clone_to_dir='.', no_input=False): |
||
91 | """ |
||
92 | Clone a repo to the current directory. |
||
93 | |||
94 | :param repo_url: Repo URL of unknown type. |
||
95 | :param checkout: The branch, tag or commit ID to checkout after clone. |
||
96 | :param clone_to_dir: The directory to clone to. |
||
97 | Defaults to the current directory. |
||
98 | :param no_input: Suppress all user prompts when calling via API. |
||
99 | """ |
||
100 | |||
101 | # Ensure that clone_to_dir exists |
||
102 | clone_to_dir = os.path.expanduser(clone_to_dir) |
||
103 | make_sure_path_exists(clone_to_dir) |
||
104 | |||
105 | # identify the repo_type |
||
106 | repo_type, repo_url = identify_repo(repo_url) |
||
107 | |||
108 | # check that the appropriate VCS for the repo_type is installed |
||
109 | if not is_vcs_installed(repo_type): |
||
110 | msg = "'{0}' is not installed.".format(repo_type) |
||
111 | raise VCSNotInstalled(msg) |
||
112 | |||
113 | repo_url = repo_url.rstrip('/') |
||
114 | tail = os.path.split(repo_url)[1] |
||
115 | if repo_type == 'git': |
||
116 | repo_dir = os.path.normpath(os.path.join(clone_to_dir, |
||
117 | tail.rsplit('.git')[0])) |
||
118 | elif repo_type == 'hg': |
||
119 | repo_dir = os.path.normpath(os.path.join(clone_to_dir, tail)) |
||
120 | logging.debug('repo_dir is {0}'.format(repo_dir)) |
||
121 | |||
122 | if os.path.isdir(repo_dir): |
||
123 | prompt_and_delete_repo(repo_dir, no_input=no_input) |
||
124 | |||
125 | try: |
||
126 | subprocess.check_output( |
||
127 | [repo_type, 'clone', repo_url], |
||
128 | cwd=clone_to_dir, |
||
129 | stderr=subprocess.STDOUT, |
||
130 | ) |
||
131 | if checkout is not None: |
||
132 | subprocess.check_output( |
||
133 | [repo_type, 'checkout', checkout], |
||
134 | cwd=repo_dir, |
||
135 | stderr=subprocess.STDOUT, |
||
136 | ) |
||
137 | except subprocess.CalledProcessError as clone_error: |
||
138 | if 'not found' in clone_error.output.lower(): |
||
139 | raise RepositoryNotFound( |
||
140 | 'The repository {} could not be found, ' |
||
141 | 'have you made a typo?'.format(repo_url) |
||
142 | ) |
||
143 | if any(error in clone_error.output for error in BRANCH_ERRORS): |
||
144 | raise RepositoryCloneFailed( |
||
145 | 'The {} branch of repository {} could not found, ' |
||
146 | 'have you made a typo?'.format(checkout, repo_url) |
||
147 | ) |
||
148 | raise |
||
149 | |||
150 | return repo_dir |
||
151 |