Completed
Push — master ( dde524...92e3a2 )
by Sebastian
06:05
created

PrepareCommitMsg::afterAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of CaptainHook.
4
 *
5
 * (c) Sebastian Feldmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace CaptainHook\App\Runner\Hook;
12
13
use CaptainHook\App\Hooks;
14
use CaptainHook\App\Runner\Hook;
15
use SebastianFeldmann\Git;
16
17
/**
18
 *  Hook
19
 *
20
 * @package CaptainHook
21
 * @author  Sebastian Feldmann <[email protected]>
22
 * @link    https://github.com/sebastianfeldmann/captainhook
23
 * @since   Class available since Release 3.1.0
24
 */
25
class PrepareCommitMsg extends Hook
26
{
27
    /**
28
     * Hook to execute
29
     *
30
     * @var string
31
     */
32
    protected $hookName = Hooks::PREPARE_COMMIT_MSG;
33
34
    /**
35
     * @var string
36
     */
37
    private $commentChar;
38
39
    /**
40
     * Path to commit message file
41
     *
42
     * @var string
43
     */
44
    private $file;
45
46
    /**
47
     * Commit mode, empty or [message|template|merge|squash|commit]
48
     *
49
     * @var string
50
     */
51
    private $mode;
52
53
    /**
54
     * Commit hash if mode is commit during -c or --amend
55
     *
56
     * @var string
57
     */
58
    private $hash;
59
60
    /**
61
     * Fetch the original hook arguments and message related config settings
62
     *
63
     * @return void
64
     */
65
    public function beforeHook()
66
    {
67
        $this->commentChar = $this->repository->getConfigOperator()->getSafely('core.commentchar', '#');
68
        $this->file        = (string)$this->arguments->get('file');
69
        $this->mode        = (string)$this->arguments->get('mode');
70
        $this->hash        = (string)$this->arguments->get('hash');
71
72
        if (empty($this->file)) {
73
            throw new \RuntimeException('commit message file argument is missing');
74
        }
75
76
        parent::beforeHook();
77
    }
78
79
    /**
80
     * Read the commit message from file
81
     *
82
     * @return void
83
     */
84
    public function beforeAction()
85
    {
86
        $this->repository->setCommitMsg(Git\CommitMessage::createFromFile($this->file, $this->commentChar));
87
        parent::beforeAction();
88
    }
89
90
    /**
91
     * Write the commit message to disk so git or the next action can proceed further
92
     *
93
     * @return void
94
     */
95
    public function afterAction()
96
    {
97
        file_put_contents($this->file, $this->repository->getCommitMsg()->getRawContent());
98
        parent::afterAction();
99
    }
100
}
101