summaryrefslogtreecommitdiff
path: root/morphlib/gitdir.py
blob: ca4a4c765db9204549519e1e5e2d58c2854c6960 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
# Copyright (C) 2013-2016  Codethink Limited
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# =*= License: GPL-2 =*=


import cliapp
import itertools
import os
import re
import tempfile

import morphlib


class NoGitRepoError(cliapp.AppException):

    def __init__(self, dirname):
        cliapp.AppException.__init__(
            self, 'Directory %s is not a Git repository. ' % dirname)


class NoWorkingTreeError(cliapp.AppException):

    def __init__(self, repo):
        cliapp.AppException.__init__(
            self, 'Git directory %s has no working tree '
                  '(is bare).' % repo.dirname)


class InvalidRefError(cliapp.AppException):
    def __init__(self, repo, ref):
        cliapp.AppException.__init__(
            self, 'Git directory %s has no commit '
                  'at ref %s.' %(repo.dirname, ref))


class ExpectedSha1Error(cliapp.AppException):

    def __init__(self, ref):
        self.ref = ref
        cliapp.AppException.__init__(
            self, 'SHA1 expected, got %s' % ref)


class RefChangeError(cliapp.AppException):
    pass


class RefAddError(RefChangeError):

    def __init__(self, gd, ref, sha1, original_exception):
        self.gd = gd
        self.dirname = dirname = gd.dirname
        self.ref = ref
        self.sha1 = sha1
        self.original_exception = original_exception
        RefChangeError.__init__(self, 'Adding ref %(ref)s '\
            'with commit %(sha1)s failed in git repository '\
            'located at %(dirname)s: %(original_exception)s' % locals())


class RefUpdateError(RefChangeError):

    def __init__(self, gd, ref, old_sha1, new_sha1, original_exception):
        self.gd = gd
        self.dirname = dirname = gd.dirname
        self.ref = ref
        self.old_sha1 = old_sha1
        self.new_sha1 = new_sha1
        self.original_exception = original_exception
        RefChangeError.__init__(self, 'Updating ref %(ref)s '\
            'from %(old_sha1)s to %(new_sha1)s failed in git repository '\
            'located at %(dirname)s: %(original_exception)r' % locals())


class RefDeleteError(RefChangeError):

    def __init__(self, gd, ref, sha1, original_exception):
        self.gd = gd
        self.dirname = dirname = gd.dirname
        self.ref = ref
        self.sha1 = sha1
        self.original_exception = original_exception
        RefChangeError.__init__(self, 'Deleting ref %(ref)s '\
            'expecting commit %(sha1)s failed in git repository '\
            'located at %(dirname)s: %(original_exception)r' % locals())


class InvalidRefSpecError(cliapp.AppException):

    def __init__(self, source, target):
        self.source = source
        self.target = target
        cliapp.AppException.__init__(
            self, 'source or target must be defined, '\
                  'got %(source)r and %(target)r respectively.' % locals())


class PushError(cliapp.AppException):
    pass


class NoRefspecsError(PushError):

    def __init__(self, remote):
        self.remote = remote
        PushError.__init__(
            self, 'Push to remote "%s" was given no refspecs.' % remote)


class PushFailureError(PushError):

    def __init__(self, remote, refspecs, exit, results, stderr):
        self.remote = remote
        self.push_url = push_url = remote.get_push_url()
        self.refspecs = refspecs
        self.exit = exit
        self.results = results
        self.stderr = stderr
        PushError.__init__(self, 'Push to remote "%(remote)s", '
                                 'push url %(push_url)s '
                                 'failed with exit code %(exit)s\n'
                                 'stderr: %(stderr)s' % locals())


class Config(object):
    '''Class representing Git repo configuration.

    The 'git config' format is reusable outside of Git repos, and this class
    can be used for these files too.

    This class doesn't take any notice of updates by other processes to the
    config file, at present. Updates are cached in memory, so changes made from
    outside the process may be ignored.

    '''
    def __init__(self, config_file, runcmd=None):
        '''Creates a Config instance.

        The 'filepath' parameter can be None if 'runcmd' is passed. Calls to
        'git config' will be passed through 'runcmd', which can set the 'cwd'
        parameter to a git repo.

        '''
        assert runcmd or config_file

        if runcmd:
            self.runcmd = runcmd
            self.extra_args = []
        else:
            self.runcmd = cliapp.runcmd
            self.extra_args = ['-f', config_file]

        self.cache = dict()

    def __setitem__(self, key, value):
        '''Set a git repository configuration variable.

        The key must have at least one period in it: foo.bar for example,
        not just foo. The part before the first period is interpreted
        by git as a section name.

        '''
        self.cache[key] = value
        self.runcmd(['git', 'config'] + self.extra_args + [key, value])

    def __getitem__(self, key):
        '''Return value for a git repository configuration variable.

        If the variable is unset, this will raise cliapp.AppException.

        '''
        if key not in self.cache:
            value = self.runcmd(
                ['git', 'config'] + self.extra_args + ['-z', key])
            self.cache[key] = value.rstrip('\0')
        return self.cache[key]


class RefSpec(object):
    '''Class representing how to push or pull a ref.

    `source` is a reference to the local commit/tag you want to push to
    the remote.
    `target` is the ref on the remote you want to push to.
    `require` is the value that the remote is expected to currently be.
    Currently `require` is only used to provide a reverse of the respec,
    but future versions of Git will support requiring the value of
    `target` on the remote to be at a certain commit, or fail.
    `force` defaults to false, and if set adds the flag to push even if
    it's non-fast-forward.

    If `source` is not provided, but `target` is, then the refspec will
    delete `target` on the remote.
    If `source` is provided, but `target` is not, then `source` is used
    as the `target`, since if you specify a ref for the `source`, you
    can push the same local branch to the same remote branch.

    '''

    def __init__(self, source=None, target=None, require=None, force=False):
        if source is None and target is None:
            raise InvalidRefSpecError(source, target)
        self.source = source
        self.target = target
        self.require = require
        self.force = force
        if target is None:
            # Default to source if target not given, source must be a
            # branch name, or when this refspec is pushed it will fail.
            self.target = target = source
        if source is None: # Delete if source not given
            self.source = source = '0' * 40

    @property
    def push_args(self):
        '''Arguments to pass to push to push this ref.

        Returns an iterable of the arguments that would need to be added
        to a push command to push this ref spec.

        This currently returns a single-element tuple, but it may expand
        to multiple arguments, e.g.
        1.  tags expand to `tag "$name"`
        2.  : expands to all the matching refs
        3.  When Git 1.8.5 becomes available,
            `"--force-with-lease=$target:$required"  "$source:$target"`.

        '''

        # TODO: Use require parameter when Git 1.8.5 is available,
        #       to allow the push to fail if the target ref is not at
        #       that commit by using the --force-with-lease option.
        return ('%(force)s%(source)s:%(target)s' % {
            'force': '+' if self.force else '',
            'source': self.source,
            'target': self.target
        }),

    def revert(self):
        '''Create a respec which will undo the effect of pushing this one.

        If `require` was not specified, the revert refspec will delete
        the branch.

        '''

        return self.__class__(source=(self.require or '0' * 40),
                              target=self.target, require=self.source,
                              force=self.force)


PUSH_FORMAT = re.compile(r'''
# Match flag, this is the eventual result in a nutshell
(?P<flag>[- +*=!])\t
# The refspec is colon separated and separated from the rest by another tab.
(?P<from>[^:]*):(?P<to>[^\t]*)\t
# Two possible formats remain, so separate the two with a capture group
(?:
    # Summary is an arbitrary string, separated from the reason by a space
    (?P<summary>.*)[ ]
    # Reason is enclosed in parenthesis and ends the line
    \((?P<reason>.*)\)
    # The reason is optional, so we may instead only have the summary
    |  (?P<summary_only>.*)
)
''', re.VERBOSE)


class Remote(object):
    '''Represent a remote git repository.

    This can either be nascent or concrete, depending on whether the
    name is given.

    Changes to a concrete remote's config are written-through to git's
    config files, while a nascent remote keeps changes in-memory.

    '''

    def __init__(self, gd, name=None):
        self.gd = gd
        self.name = name
        self.push_url = None
        self.fetch_url = None

    def __str__(self):
        return self.name or '(nascent remote)'

    def set_fetch_url(self, url):
        self.fetch_url = url
        if self.name is not None:
            morphlib.git.gitcmd(self.gd._runcmd, 'remote', 'set-url',
                                self.name, url)
        if self.name is None and self.push_url is None:
            self.set_push_url(url)

    def set_push_url(self, url):
        self.push_url = url
        if self.name is not None:
            morphlib.git.gitcmd(self.gd._runcmd, 'remote', 'set-url',
                                '--push', self.name, url)
        if self.name is None and self.fetch_url is None:
            self.set_fetch_url(url)

    def _get_remote_url(self, remote_name, kind):
        # As distasteful as it is to parse the output of porcelain
        # commands, this is the best option.
        # Git config can be used to get the raw value, but this is
        # incorrect when url.*.insteadof rules are involved.
        # Re-implementing the rewrite logic in morph is duplicated effort
        # and more work to keep it in sync.
        # It's possible to get the fetch url with `git ls-remote --get-url
        # <remote>`, but this will just print the remote's name if it
        # is not defined.
        # It is only possible to use git to get the push url by parsing
        # `git remote -v` or `git remote show -n <remote>`, and `git
        # remote -v` is easier to parse.
        output = morphlib.git.gitcmd(self.gd._runcmd, 'remote', '-v')
        for line in output.splitlines():
            words = line.split()
            if (len(words) == 3 and
                words[0] == remote_name and
                words[2] == '(%s)' % kind):
                return words[1]

        return None

    def get_fetch_url(self):
        if self.name is None:
            return self.fetch_url
        return self._get_remote_url(self.name, 'fetch')

    def get_push_url(self):
        if self.name is None:
            return self.push_url or self.get_fetch_url()
        return self._get_remote_url(self.name, 'push')

    @staticmethod
    def _parse_ls_remote_output(output): # pragma: no cover
        for line in output.splitlines():
            sha1, refname = line.split(None, 1)
            yield sha1, refname

    def ls(self): # pragma: no cover
        out = morphlib.git.gitcmd(self.gd._runcmd, 'ls-remote',
                                  self.get_fetch_url())
        return self._parse_ls_remote_output(out)

    @staticmethod
    def _parse_push_output(output):
        for line in output.splitlines():
            m = PUSH_FORMAT.match(line)
            # Push may output lines that are not related to the status,
            # so ignore any that don't match the status format.
            if m is None:
                continue
            # Ensure the same number of arguments
            ret = list(m.group('flag', 'from', 'to'))
            ret.append(m.group('summary') or m.group('summary_only'))
            ret.append(m.group('reason'))
            yield tuple(ret)

    def push(self, *refspecs):
        '''Push given refspecs to the remote and return results.

        If no refspecs are given, an exception is raised.

        Returns an iterable of (flag, from_ref, to_ref, summary, reason)

        If the push fails, a PushFailureError is raised, from which the
        result can be retrieved with the `results` field.

        '''

        if not refspecs:
            raise NoRefspecsError(self)
        push_name = self.name or self.get_push_url()
        cmdline = ['push', '--porcelain', push_name]
        cmdline.extend(itertools.chain.from_iterable(
            rs.push_args for rs in refspecs))
        exit, out, err = morphlib.git.gitcmd(self.gd._runcmd_unchecked,
                                             *cmdline)
        if exit != 0:
            raise PushFailureError(self, refspecs, exit,
                                   self._parse_push_output(out), err)
        return self._parse_push_output(out)

    def pull(self, branch=None): # pragma: no cover
        if branch:
            repo = self.get_fetch_url()
            ret = morphlib.git.gitcmd(self.gd._runcmd, 'pull', repo, branch)
        else:
            ret = morphlib.git.gitcmd(self.gd._runcmd, 'pull')
        return ret


class GitDirectory(object):

    '''Represents a local Git repository.

    This class represents a directory that is the result of a `git clone` or
    `git init`. It includes both the .git subdirectory and the working tree.
    It is a thin abstraction, meant to make it easier to do certain git
    operations.

    In the case of bare repositories, there is no working tree. These are
    supported, but a NoWorkingTree exception will be raised if you try to
    perform any operations that require a working tree.

    The repository must already exist on disk. Use gitdir.init() to create a
    new repo, or gitdir.clone_from_cached_repo() if you want to clone a repo
    that Morph has cached locally.

    '''

    def __init__(self, dirname, search_for_root=False, allow_missing=False):
        '''Set up a GitDirectory instance for the repository at 'dirname'.

        If 'search_for_root' is set to True, 'dirname' may point to a
        subdirectory inside the working tree of repository. Otherwise 'dirname'
        must be the top directory.

        '''

        if search_for_root:
            dirname = morphlib.util.find_root(dirname, '.git')

        self.dirname = dirname
        self.config = Config(config_file=None, runcmd=self._runcmd)

        if not allow_missing:
            self._ensure_is_git_repo()

    def __str__(self):
        return self.dirname

    def _runcmd(self, argv, **kwargs):
        '''Run a command at the root of the git directory.

        See cliapp.runcmd for arguments.

        Do NOT use this from outside the class. Add more public
        methods for specific git operations instead.

        '''
        return cliapp.runcmd(argv, cwd=self.dirname, **kwargs)

    def _runcmd_unchecked(self, *args, **kwargs):
        return cliapp.runcmd_unchecked(*args, cwd=self.dirname, **kwargs)

    def _ensure_is_git_repo(self):
        try:
            self._runcmd(['git', 'rev-parse', '--git-dir'])
        except cliapp.AppException as e:
            # Exact error is logged already by the runcmd() function.
            raise NoGitRepoError(self.dirname)

    def checkout(self, branch_name): # pragma: no cover
        '''Check out a git branch.'''
        morphlib.git.gitcmd(self._runcmd, 'checkout', branch_name)
        if self.has_fat():
            self.fat_init()
            self.fat_pull()

    def reset_workdir(self): # pragma: no cover
        '''Removes any differences between the current commit
           and the status of the working directory '''

        morphlib.git.reset_workdir(cliapp.runcmd, self.dirname)

    def branch(self, new_branch_name, base_ref): # pragma: no cover
        '''Create a git branch based on an existing ref.

        This does not automatically check out the branch.

        base_ref may be None, in which case the current branch is used.

        '''

        argv = ['branch', new_branch_name]
        if base_ref is not None:
            argv.append(base_ref)
        morphlib.git.gitcmd(self._runcmd, *argv)

    def get_file_from_ref(self, ref, filename): # pragma: no cover
        '''Get file contents from git by ref and filename.

        `ref` should be a tree-ish e.g. HEAD, master, refs/heads/master,
        refs/tags/foo, though SHA1 tag, commit or tree IDs are also valid.

        `filename` is the path to the file object from the base of the
        git directory.

        Returns the contents of the referred to file as a string.

        '''

        # Blob ID is left as the git revision, rather than SHA1, since
        # we know get_blob_contents will accept it
        blob_id = '%s:%s' % (ref, filename)
        return self.get_blob_contents(blob_id)

    def get_blob_contents(self, blob_id): # pragma: no cover
        '''Get file contents from git by ID'''
        return morphlib.git.gitcmd(self._runcmd, 'cat-file', 'blob', blob_id)

    def get_commit_contents(self, commit_id): # pragma: no cover
        '''Get commit contents from git by ID'''
        return morphlib.git.gitcmd(self._runcmd, 'cat-file', 'commit',
                                   commit_id)

    def update_submodules(self, app): # pragma: no cover
        '''Change .gitmodules URLs, and checkout submodules.'''
        morphlib.git.update_submodules(app, self.dirname)

    def set_config(self, key, value):
        '''Set a git repository configuration variable.

        The key must have at least one period in it: foo.bar for example,
        not just foo. The part before the first period is interpreted
        by git as a section name.

        '''
        self.config[key] = value

    def get_config(self, key):
        '''Return value for a git repository configuration variable.

        If the variable is unset, this will raise cliapp.AppException.

        '''
        return self.config[key]

    def get_remote(self, *args, **kwargs):
        '''Get a remote for this Repository.

        Gets a previously configured remote if a remote name is given.
        Otherwise a nascent one is created.

        '''
        return Remote(self, *args, **kwargs)

    def update_remotes(self, echo_stderr=False): # pragma: no cover
        '''Run "git remote update --prune".'''
        morphlib.git.gitcmd(self._runcmd, 'remote', 'update', '--prune',
                            echo_stderr=echo_stderr)

    def is_bare(self):
        '''Determine whether the repository has no work tree (is bare)'''
        return self.get_config('core.bare') == 'true'

    def list_files(self, ref=None, recurse=True):
        '''Return an iterable of the files in the repository.

        If `ref` is specified, list files at that ref, otherwise
        use the working tree.

        If this is a bare repository and no ref is specified, raises
        an exception.

        '''
        if ref is None and self.is_bare():
            raise NoWorkingTreeError(self)
        if ref is None:
            return self._list_files_in_work_tree(recurse)
        else:
            return self._list_files_in_ref(ref, recurse)

    def _rev_parse(self, ref):
        try:
            return morphlib.git.gitcmd(self._runcmd, 'rev-parse',
                                       '--verify', ref).strip()
        except cliapp.AppException as e:
            raise InvalidRefError(self, ref)

    def get_upstream_of_branch(self, branch): # pragma: no cover
        try:
            out = morphlib.git.gitcmd(
                self._runcmd, 'rev-parse', '--abbrev-ref',
                '%s@{upstream}' % branch).strip()
            return out
        except cliapp.AppException as e:
            emsg = str(e)
            if 'does not point to a branch' in emsg:
                # ref wasn't a branch, can't have upstream
                # treat it the same as no upstream for convenience
                return None
            elif 'No such branch' in emsg:
                # Same as above
                return None
            elif 'No upstream configured for branch' in emsg:
                return None
            raise

    def resolve_ref_to_commit(self, ref):
        return self._rev_parse('%s^{commit}' % ref)

    def resolve_ref_to_tree(self, ref):
        return self._rev_parse('%s^{tree}' % ref)

    def ref_exists(self, ref):
        try:
            self._rev_parse('%s^{commit}' % ref)
            return True
        except InvalidRefError:
            return False

    def _list_files_in_work_tree(self, recurse=True):
        for dirpath, subdirs, filenames in os.walk(self.dirname):
            if not recurse:  # pragma: no cover
                subdirs[:] = []
            elif dirpath == self.dirname and '.git' in subdirs:
                subdirs.remove('.git')
            for filename in filenames:
                filepath = os.path.join(dirpath, filename)
                yield os.path.relpath(filepath, start=self.dirname)

    def _list_files_in_ref(self, ref, recurse=True):
        tree = self.resolve_ref_to_tree(ref)

        command = ['ls-tree', '--name-only', '-z']
        if recurse:
            command.append('-r')
        command.append(tree)

        output = morphlib.git.gitcmd(self._runcmd, *command)
        # ls-tree appends \0 instead of interspersing, so we need to
        # strip the trailing \0 before splitting
        paths = output.strip('\0').split('\0')
        return paths

    def read_file(self, filename, ref=None):
        '''Attempts to read a file, from the working tree or a given ref.

        Raises an InvalidRefError if the ref is not found in the repository.
        Raises an IOError if the requested file is not found in the ref.

        '''

        if ref is None and self.is_bare():
            raise NoWorkingTreeError(self)
        if ref is None:
            with open(os.path.join(self.dirname, filename)) as f:
                return f.read()
        tree = self.resolve_ref_to_tree(ref)
        try:
            return self.get_file_from_ref(tree, filename)
        except cliapp.AppException:
            raise IOError('File %s does not exist in ref %s of repo %s' %
                          (filename, ref, self))

    def is_symlink(self, filename, ref=None):
        if ref is None and self.is_bare():
            raise NoWorkingTreeError(self)
        if ref is None:
            filepath = os.path.join(self.dirname, filename.lstrip('/'))
            return os.path.islink(filepath)
        tree_entry = morphlib.git.gitcmd(self._runcmd, 'ls-tree', ref,
                                         filename)
        file_mode = tree_entry.split(' ', 1)[0]
        return file_mode == '120000'

    @property
    def HEAD(self):
        output = morphlib.git.gitcmd(self._runcmd, 'rev-parse',
                                     '--abbrev-ref', 'HEAD')
        return output.strip()

    def get_index(self, index_file=None):
        return morphlib.gitindex.GitIndex(self, index_file)

    def store_blob(self, blob_contents):
        '''Hash `blob_contents`, store it in git and return the sha1.

        `blob_contents` must either be a string or a value suitable to
        pass to subprocess.Popen i.e. a file descriptor or file object
        with fileno() method.

        '''
        return self._store_object(contents=blob_contents, type='blob')

    def _store_object(self, contents, type):
        if isinstance(contents, basestring):
            kwargs = {'feed_stdin': contents}
        else:
            kwargs = {'stdin': contents}
        return morphlib.git.gitcmd(self._runcmd, 'hash-object', '-t', type,
                                   '-w', '--stdin', **kwargs).strip()

    def commit_tree(self, tree, parent, message, **kwargs):
        '''Create a commit'''
        # NOTE: Will need extension for 0 or N parents.
        env = {}
        for who, info in itertools.product(('committer', 'author'),
                                           ('name', 'email')):
            argname = '%s_%s' % (who, info)
            envname = 'GIT_%s_%s' % (who.upper(), info.upper())
            if argname in kwargs:
                env[envname] = kwargs[argname]
        for who in ('committer', 'author'):
            argname = '%s_date' % who
            envname = 'GIT_%s_DATE' % who.upper()
            if argname in kwargs:
                env[envname] = kwargs[argname].isoformat()
        return morphlib.git.gitcmd(self._runcmd, 'commit-tree', tree,
                                   '-p', parent, '-m', message,
                                   env=env).strip()

    @staticmethod
    def _check_is_sha1(string):
        if not morphlib.git.is_valid_sha1(string):
            raise ExpectedSha1Error(string)

    def _check_ref_exists(self, ref):
        self._rev_parse('%s^{commit}' % ref)

    def _gitcmd_output_list(self, *args):
        output = morphlib.git.gitcmd(self._runcmd, *args)
        separated = [l.strip() for l in output.splitlines()]
        prefix = '* '
        for i, l in enumerate(separated):
            if l.startswith(prefix):
                separated[i] = l[len(prefix):]
        return separated

    def tags_containing_sha1(self, ref): # pragma: no cover
        self._check_is_sha1(ref)
        self._check_ref_exists(ref)

        args = ['tag', '--contains', ref]
        return self._gitcmd_output_list(*args)

    def branches_containing_sha1(self, ref):
        self._check_is_sha1(ref)
        self._check_ref_exists(ref)

        args = ['branch', '--contains', ref]
        return self._gitcmd_output_list(*args)

    def version_guess(self, ref): # pragma: no cover
        self._check_ref_exists(ref)

        args = ['describe', '--tags', '--always', ref]
        return morphlib.git.gitcmd(self._runcmd, *args).strip()

    def _update_ref(self, ref_args, message):
        args = ['update-ref']
        # No test coverage, since while this functionality is useful,
        # morph does not need an API for inspecting the reflog, so
        # it existing purely to test ref updates is a tad overkill.
        if message is not None: # pragma: no cover
            args.extend(('-m', message))
        args.extend(ref_args)
        morphlib.git.gitcmd(self._runcmd, *args)

    def add_ref(self, ref, sha1, message=None):
        '''Create a ref called `ref` in the repository pointing to `sha1`.

        `message` is a string to add to the reflog about this change
        `ref` must not already exist, if it does, use `update_ref`
        `sha1` must be a 40 character hexadecimal string representing
        the SHA1 of the commit or tag this ref will point to, this is
        the result of the commit_tree or resolve_ref_to_commit methods.

        '''
        self._check_is_sha1(sha1)
        # 40 '0' characters is code for no previous value
        # this ensures it will fail if the branch already exists
        try:
            return self._update_ref((ref, sha1, '0' * 40), message)
        except Exception as e:
            raise RefAddError(self, ref, sha1, e)

    def update_ref(self, ref, sha1, old_sha1, message=None):
        '''Change the commit the ref `ref` points to, to `sha1`.

        `message` is a string to add to the reflog about this change
        `sha1` and `old_sha` must be 40 character hexadecimal strings
        representing the SHA1 of the commit or tag this ref will point
        to and currently points to respectively. This is the result of
        the commit_tree or resolve_ref_to_commit methods.
        `ref` must exist, and point to `old_sha1`.
        This is to avoid unexpected results when multiple processes
        attempt to change refs.

        '''
        self._check_is_sha1(sha1)
        self._check_is_sha1(old_sha1)
        try:
            return self._update_ref((ref, sha1, old_sha1), message)
        except Exception as e:
            raise RefUpdateError(self, ref, old_sha1, sha1, e)

    def delete_ref(self, ref, old_sha1, message=None):
        '''Remove the ref `ref`.

        `message` is a string to add to the reflog about this change
        `old_sha1` must be a 40 character hexadecimal string representing
        the SHA1 of the commit or tag this ref will point to, this is
        the result of the commit_tree or resolve_ref_to_commit methods.
        `ref` must exist, and point to `old_sha1`.
        This is to avoid unexpected results when multiple processes
        attempt to change refs.

        '''
        self._check_is_sha1(old_sha1)
        try:
            return self._update_ref(('-d', ref, old_sha1), message)
        except Exception as e:
            raise RefDeleteError(self, ref, old_sha1, e)

    def describe(self):
        version = morphlib.git.gitcmd(self._runcmd, 'describe',
                                      '--always', '--dirty=-unreproducible')
        return version.strip()

    def fat_init(self): # pragma: no cover
        return morphlib.git.gitcmd(self._runcmd, 'fat', 'init')

    def fat_push(self): # pragma: no cover
        return morphlib.git.gitcmd(self._runcmd, 'fat', 'push')

    def fat_pull(self): # pragma: no cover
        return morphlib.git.gitcmd(self._runcmd, 'fat', 'pull')

    def has_fat(self): # pragma: no cover
        return os.path.isfile(self.join_path('.gitfat'))

    def join_path(self, path): # pragma: no cover
        return os.path.join(self.dirname, path)

    def get_relpath(self, path): # pragma: no cover
        return os.path.relpath(path, self.dirname)

    def extract_commit(self, ref, target_dir):
        '''Extract files from a given commit into target_dir.

        This is different to a checkout: a checkout assumes a working tree
        associated with a repository, where here we just copy the files out so
        they can be read quickly.

        Use read_file() if you only want to read one or two files.

        This approach is marginally quicker than doing a shallow clone. Running
        `morph list-artifacts` 10 times gave an average time of 1.334s
        using `git clone --depth 1` and an average time of 1.261s using
        this code.

        '''
        if not os.path.exists(target_dir):
            os.makedirs(target_dir)

        with tempfile.NamedTemporaryFile() as index_file:
            index = self.get_index(index_file=index_file.name)
            index.set_to_tree(ref)
            index.checkout(working_tree=target_dir)


def init(dirname):
    '''Initialise a new git repository.'''

    morphlib.git.gitcmd(cliapp.runcmd, 'init', cwd=dirname)
    gd = GitDirectory(dirname)
    return gd


def checkout_from_cached_repo(cached_repo, ref, target_dir,
                              runcmd_cb=cliapp.runcmd):  # pragma: no cover
    '''Clones the repository to a directory and checks out a given ref.

    This doesn't actually call `git clone`. The approach used is to `cp -a`
    the repo in place, then tweak it so it appears to be a clone. This seems
    *slightly* faster than using `git clone`, as of Git version 2.3.0.

    I tested this by checking out a ref of linux.git, having cleared my
    system's caches. It took 7m06s using `git clone`, and 6m53s using the
    'cp' approach.

    '''
    if not os.path.exists(target_dir):
        os.mkdir(target_dir)

    # Note, we copy instead of cloning because it's much faster in the case
    # that the target is on a different filesystem from the cache. We then
    # take care to turn the copy into something as good as a real clone.
    try:
        morphlib.git.copy_repository(
            runcmd_cb, cached_repo.dirname, target_dir, cached_repo.is_mirror)
    except cliapp.AppException:
        raise CopyError(cached_repo.original_name, target_dir)

    gitdir = morphlib.gitdir.GitDirectory(target_dir)
    try:
        gitdir.checkout(ref)
    except cliapp.AppException:
        raise CheckoutError(cached_repo.original_name, ref, target_dir)

    if gitdir.has_fat():
        gitdir.fat_init()
        gitdir.fat_pull()

    return gitdir