| Conditions | 11 |
| Total Lines | 63 |
| Lines | 0 |
| Ratio | 0 % |
| 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 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 -*- |
||
| 59 | def clone(repo_url, checkout=None, clone_to_dir='.', no_input=False): |
||
| 60 | """Clone a repo to the current directory. |
||
| 61 | |||
| 62 | :param repo_url: Repo URL of unknown type. |
||
| 63 | :param checkout: The branch, tag or commit ID to checkout after clone. |
||
| 64 | :param clone_to_dir: The directory to clone to. |
||
| 65 | Defaults to the current directory. |
||
| 66 | :param no_input: Suppress all user prompts when calling via API. |
||
| 67 | """ |
||
| 68 | # Ensure that clone_to_dir exists |
||
| 69 | clone_to_dir = os.path.expanduser(clone_to_dir) |
||
| 70 | make_sure_path_exists(clone_to_dir) |
||
| 71 | |||
| 72 | # identify the repo_type |
||
| 73 | repo_type, repo_url = identify_repo(repo_url) |
||
| 74 | |||
| 75 | # check that the appropriate VCS for the repo_type is installed |
||
| 76 | if not is_vcs_installed(repo_type): |
||
| 77 | msg = "'{0}' is not installed.".format(repo_type) |
||
| 78 | raise VCSNotInstalled(msg) |
||
| 79 | |||
| 80 | repo_url = repo_url.rstrip('/') |
||
| 81 | tail = os.path.split(repo_url)[1] |
||
| 82 | if repo_type == 'git': |
||
| 83 | repo_dir = os.path.normpath(os.path.join(clone_to_dir, |
||
| 84 | tail.rsplit('.git')[0])) |
||
| 85 | elif repo_type == 'hg': |
||
| 86 | repo_dir = os.path.normpath(os.path.join(clone_to_dir, tail)) |
||
| 87 | logger.debug('repo_dir is {0}'.format(repo_dir)) |
||
| 88 | |||
| 89 | if os.path.isdir(repo_dir): |
||
| 90 | clone = prompt_and_delete(repo_dir, no_input=no_input) |
||
| 91 | else: |
||
| 92 | clone = True |
||
| 93 | |||
| 94 | if clone: |
||
| 95 | try: |
||
| 96 | subprocess.check_output( |
||
| 97 | [repo_type, 'clone', repo_url], |
||
| 98 | cwd=clone_to_dir, |
||
| 99 | stderr=subprocess.STDOUT, |
||
| 100 | ) |
||
| 101 | if checkout is not None: |
||
| 102 | subprocess.check_output( |
||
| 103 | [repo_type, 'checkout', checkout], |
||
| 104 | cwd=repo_dir, |
||
| 105 | stderr=subprocess.STDOUT, |
||
| 106 | ) |
||
| 107 | except subprocess.CalledProcessError as clone_error: |
||
| 108 | output = clone_error.output.decode('utf-8') |
||
| 109 | if 'not found' in output.lower(): |
||
| 110 | raise RepositoryNotFound( |
||
| 111 | 'The repository {} could not be found, ' |
||
| 112 | 'have you made a typo?'.format(repo_url) |
||
| 113 | ) |
||
| 114 | if any(error in output for error in BRANCH_ERRORS): |
||
| 115 | raise RepositoryCloneFailed( |
||
| 116 | 'The {} branch of repository {} could not found, ' |
||
| 117 | 'have you made a typo?'.format(checkout, repo_url) |
||
| 118 | ) |
||
| 119 | raise |
||
| 120 | |||
| 121 | return repo_dir |
||
| 122 |