Skip to content

Commit 8a6139b

Browse files
Don't overwrite externally changed msg file
If cli actions are used to change the commit message file the after action did overwrite those changes with the "original" commit message again making sure that all changes were lost. Now the afterAction only overwrites the msg file with the message object if a php action was executed.
1 parent 7fb1c78 commit 8a6139b

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/Runner/Hook/PrepareCommitMsg.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use CaptainHook\App\Config;
1515
use CaptainHook\App\Hooks;
1616
use CaptainHook\App\Runner\Hook;
17+
use CaptainHook\App\Runner\Util;
1718
use RuntimeException;
1819
use SebastianFeldmann\Git;
1920

@@ -35,30 +36,30 @@ class PrepareCommitMsg extends Hook
3536
protected $hook = Hooks::PREPARE_COMMIT_MSG;
3637

3738
/**
38-
* @var string
39+
* @var string|null
3940
*/
40-
private $commentChar;
41+
private ?string $commentChar;
4142

4243
/**
4344
* Path to commit message file
4445
*
4546
* @var string
4647
*/
47-
private $file;
48+
private string $file;
4849

4950
/**
5051
* Commit mode, empty or [message|template|merge|squash|commit]
5152
*
5253
* @var string
5354
*/
54-
private $mode;
55+
private string $mode;
5556

5657
/**
5758
* Commit hash if mode is commit during -c or --amend
5859
*
5960
* @var string
6061
*/
61-
private $hash;
62+
private string $hash;
6263

6364
/**
6465
* Fetch the original hook arguments and message related config settings
@@ -99,7 +100,11 @@ public function beforeAction(Config\Action $action): void
99100
*/
100101
public function afterAction(Config\Action $action): void
101102
{
102-
file_put_contents($this->file, $this->repository->getCommitMsg()->getRawContent());
103+
// only write the commit message to disk if a php action was executed
104+
// and potentially changed the message object
105+
if (Util::getExecType($action->getAction()) !== 'cli') {
106+
file_put_contents($this->file, $this->repository->getCommitMsg()->getRawContent());
107+
}
103108
parent::afterAction($action);
104109
}
105110
}

0 commit comments

Comments
 (0)