|
2 | 2 | from __future__ import unicode_literals |
3 | 3 |
|
4 | 4 | import collections |
| 5 | +import logging |
5 | 6 | import os.path |
6 | 7 |
|
7 | 8 | from aspy.yaml import ordered_dump |
|
12 | 13 | from pre_commit.clientlib import load_manifest |
13 | 14 | from pre_commit.commands.run import run |
14 | 15 | from pre_commit.store import Store |
| 16 | +from pre_commit.util import cmd_output |
15 | 17 | from pre_commit.util import tmpdir |
16 | 18 |
|
| 19 | +logger = logging.getLogger(__name__) |
17 | 20 |
|
18 | | -def try_repo(args): |
19 | | - ref = args.ref or git.head_rev(args.repo) |
20 | 21 |
|
| 22 | +def _repo_ref(tmpdir, repo, ref): |
| 23 | + # if `ref` is explicitly passed, use it |
| 24 | + if ref: |
| 25 | + return repo, ref |
| 26 | + |
| 27 | + ref = git.head_rev(repo) |
| 28 | + # if it exists on disk, we'll try and clone it with the local changes |
| 29 | + if os.path.exists(repo) and git.has_diff('HEAD', repo=repo): |
| 30 | + logger.warning('Creating temporary repo with uncommitted changes...') |
| 31 | + |
| 32 | + shadow = os.path.join(tmpdir, 'shadow-repo') |
| 33 | + cmd_output('git', 'clone', repo, shadow) |
| 34 | + cmd_output('git', 'checkout', ref, '-b', '_pc_tmp', cwd=shadow) |
| 35 | + idx = git.git_path('index', repo=shadow) |
| 36 | + objs = git.git_path('objects', repo=shadow) |
| 37 | + env = dict(os.environ, GIT_INDEX_FILE=idx, GIT_OBJECT_DIRECTORY=objs) |
| 38 | + cmd_output('git', 'add', '-u', cwd=repo, env=env) |
| 39 | + git.commit(repo=shadow) |
| 40 | + |
| 41 | + return shadow, git.head_rev(shadow) |
| 42 | + else: |
| 43 | + return repo, ref |
| 44 | + |
| 45 | + |
| 46 | +def try_repo(args): |
21 | 47 | with tmpdir() as tempdir: |
| 48 | + repo, ref = _repo_ref(tempdir, args.repo, args.ref) |
| 49 | + |
22 | 50 | store = Store(tempdir) |
23 | 51 | if args.hook: |
24 | 52 | hooks = [{'id': args.hook}] |
25 | 53 | else: |
26 | | - repo_path = store.clone(args.repo, ref) |
| 54 | + repo_path = store.clone(repo, ref) |
27 | 55 | manifest = load_manifest(os.path.join(repo_path, C.MANIFEST_FILE)) |
28 | 56 | manifest = sorted(manifest, key=lambda hook: hook['id']) |
29 | 57 | hooks = [{'id': hook['id']} for hook in manifest] |
30 | 58 |
|
31 | | - items = (('repo', args.repo), ('rev', ref), ('hooks', hooks)) |
| 59 | + items = (('repo', repo), ('rev', ref), ('hooks', hooks)) |
32 | 60 | config = {'repos': [collections.OrderedDict(items)]} |
33 | 61 | config_s = ordered_dump(config, **C.YAML_DUMP_KWARGS) |
34 | 62 |
|
|
0 commit comments