aboutsummaryrefslogtreecommitdiffstats
path: root/library/kzykhys/git/src/PHPGit/Command/CommitCommand.php
blob: a4f2bdd956757e3f95bd5a8990852edd3946a7ac (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
<?php

namespace PHPGit\Command;

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

/**
 * Record changes to the repository - `git commit`
 *
 * @author Kazuyuki Hayashi <hayashi@valnur.net>
 */
class CommitCommand extends Command
{

    /**
     * Record changes to the repository
     *
     * ``` php
     * $git = new PHPGit\Git();
     * $git->clone('https://github.com/kzykhys/PHPGit.git', '/path/to/repo');
     * $git->setRepository('/path/to/repo');
     * $git->add('README.md');
     * $git->commit('Fixes README.md');
     * ```
     *
     * ##### Options
     *
     * - **all**           (_boolean_) Stage files that have been modified and deleted
     * - **reuse-message** (_string_)  Take an existing commit object, and reuse the log message and the authorship information (including the timestamp) when creating the commit
     * - **squash**        (_string_)  Construct a commit message for use with rebase --autosquash
     * - **author**        (_string_)  Override the commit author
     * - **date**          (_string_)  Override the author date used in the commit
     * - **cleanup**       (_string_)  Can be one of verbatim, whitespace, strip, and default
     * - **amend**         (_boolean_) Used to amend the tip of the current branch
     *
     * @param string $message Use the given <$msg> as the commit message
     * @param array  $options [optional] An array of options {@see CloneCommand::setDefaultOptions}
     *
     * @throws GitException
     * @return bool
     */
    public function __invoke($message, array $options = array())
    {
        $options = $this->resolve($options);
        $builder = $this->git->getProcessBuilder()
            ->add('commit')
            ->add('-m')->add($message);

        $this->addFlags($builder, $options, array('all', 'amend'));
        $this->addValues($builder, $options, array('reuse-message', 'squash', 'author', 'date', 'cleanup'));

        $this->git->run($builder->getProcess());

        return true;
    }

    /**
     * {@inheritdoc}
     *
     * - **all**           (_boolean_) Stage files that have been modified and deleted
     * - **reuse-message** (_string_)  Take an existing commit object, and reuse the log message and the authorship information (including the timestamp) when creating the commit
     * - **squash**        (_string_)  Construct a commit message for use with rebase --autosquash
     * - **author**        (_string_)  Override the commit author
     * - **date**          (_string_)  Override the author date used in the commit
     * - **cleanup**       (_string_)  Can be one of verbatim, whitespace, strip, and default
     * - **amend**         (_boolean_) Used to amend the tip of the current branch
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'all'           => false,
            'reuse-message' => null,
            'squash'        => null,
            'author'        => null,
            'date'          => null,
            'cleanup'       => null,
            'amend'         => false
        ));

        $resolver->setAllowedValues(array(
            'cleanup' => array(null, 'default', 'verbatim', 'whitespace', 'strip')
        ));
    }

}