aboutsummaryrefslogtreecommitdiffstats
path: root/library/kzykhys/git/src/PHPGit/Command/DescribeCommand.php
blob: affdd009b2c28e8593aee1f360e6454f30469580 (plain) (blame)
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
<?php

namespace PHPGit\Command;

use PHPGit\Command;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

/**
 * Show the most recent tag that is reachable from a commit - `git describe`
 *
 * @author Kazuyuki Hayashi <hayashi@valnur.net>
 */
class DescribeCommand extends Command
{

    /**
     * Returns the most recent tag that is reachable from a commit
     *
     * ``` php
     * $git = new PHPGit\Git();
     * $git->setRepository('/path/to/repo');
     * $git->tag->create('v1.0.0');
     * $git->commit('Fixes #14');
     * echo $git->describe('HEAD', ['tags' => true]);
     * ```
     *
     * ##### Output Example
     *
     * ```
     * v1.0.0-1-g7049efc
     * ```
     *
     * ##### Options
     *
     * - **all**    (_boolean_) Enables matching any known branch, remote-tracking branch, or lightweight tag
     * - **tags**   (_boolean_) Enables matching a lightweight (non-annotated) tag
     * - **always** (_boolean_) Show uniquely abbreviated commit object as fallback
     *
     * @param string $committish [optional] Committish object names to describe.
     * @param array  $options    [optional] An array of options {@see DescribeCommand::setDefaultOptions}
     *
     * @return string
     */
    public function __invoke($committish = null, array $options = array())
    {
        $options = $this->resolve($options);
        $builder = $this->git->getProcessBuilder()
            ->add('describe');

        $this->addFlags($builder, $options, array());

        if ($committish) {
            $builder->add($committish);
        }

        return trim($this->git->run($builder->getProcess()));
    }

    /**
     * Equivalent to $git->describe($committish, ['tags' => true]);
     *
     * @param string $committish [optional] Committish object names to describe.
     * @param array  $options    [optional] An array of options {@see DescribeCommand::setDefaultOptions}
     *
     * @return string
     */
    public function tags($committish = null, array $options = array())
    {
        $options['tags'] = true;

        return $this->__invoke($committish, $options);
    }

    /**
     * {@inheritdoc}
     *
     * - **all**    (_boolean_) Enables matching any known branch, remote-tracking branch, or lightweight tag
     * - **tags**   (_boolean_) Enables matching a lightweight (non-annotated) tag
     * - **always** (_boolean_) Show uniquely abbreviated commit object as fallback
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'all'    => false,
            'tags'   => false,
            'always' => false,
        ));
    }

}