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