Skip to content

Commit fc79a58

Browse files
committed
Optimize Git execution and Fix docBlocks
- Changed shell_exec function calls to exec function call where multiline output is expected. This avoid the overhead of exploding the single output string of shell_exec. - GitChangelog::$setToTag now refers to head revision of repository with value null instead of empty string. - Fixed thrown exceptions at docBlocks.
1 parent d4e352e commit fc79a58

File tree

1 file changed

+38
-34
lines changed

1 file changed

+38
-34
lines changed

src/GitChangelog.php

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,17 @@ class GitChangelog
122122
'commitOrder' => 'ASC',
123123
];
124124
/**
125-
* @var string Value of the oldest tag to include into the generated changelog.
125+
* @var string Value of the oldest tag to include into the generated changelog. If the value is null it refers to
126+
* the oldest commit.
126127
* @see GitChangelog::setFromTag()
127128
*/
128129
private $fromTag;
129130
/**
130-
* @var string Value of the newest tag to include into the generated changelog. If the value = an empty string, it
131-
* refers to the HEAD revision.
131+
* @var string Value of the newest tag to include into the generated changelog. If the value is null, it refers to
132+
* the HEAD revision.
132133
* @see GitChangelog::setToTag()
133134
*/
134-
private $toTag = '';
135+
private $toTag;
135136
/**
136137
* @var array Contains the tags which exist in the git repository. If the first element's key is an empty string, it
137138
* refers to the HEAD revision.
@@ -178,7 +179,8 @@ public function __construct()
178179
* @param bool $force [Optional] Set to true to refresh the cached tags.
179180
*
180181
* @return array The cached tags.
181-
* @throws Exception When the defined From- or To-tag doesn't exist in the git repository.
182+
* @throws InvalidArgumentException When the defined From- or To-tag doesn't exist in the git repository.
183+
* @throws RuntimeException When executing the git command fails.
182184
*/
183185
public function fetchTags($force = false): array
184186
{
@@ -191,12 +193,14 @@ public function fetchTags($force = false): array
191193
$gitPath .= $this->gitPath ?? './.git';
192194

193195
// Get all git tags.
194-
// TODO: Change shell_exec to exec.
195-
$this->gitTags = explode("\n", shell_exec("git $gitPath tag --sort=-{$this->options['tagOrderBy']}"));
196-
array_pop($this->gitTags); // Remove empty trailing element.
196+
$commandResult = 1;
197+
exec("git $gitPath tag --sort=-{$this->options['tagOrderBy']}", $this->gitTags, $commandResult);
198+
if ($commandResult !== 0) {
199+
throw new RuntimeException('An error occurred while fetching the tags from the repository!');
200+
}
197201

198202
// Add HEAD revision as tag.
199-
if ($this->toTag === '') {
203+
if ($this->toTag === null) {
200204
array_unshift($this->gitTags, $this->toTag);
201205
}
202206

@@ -233,7 +237,8 @@ public function fetchTags($force = false): array
233237
* @param false $force [Optional] Set to true to refresh the cached tags.
234238
*
235239
* @return array Commit data.
236-
* @throws Exception When the defined From- or To-tag doesn't exist in the git repository.
240+
* @throws InvalidArgumentException When the defined From- or To-tag doesn't exist in the git repository.
241+
* @throws RuntimeException When executing a git command fails.
237242
* @see GitChangelog::processCommitData()
238243
*/
239244
public function fetchCommitData($force = false): array
@@ -243,37 +248,36 @@ public function fetchCommitData($force = false): array
243248
return $this->commitData;
244249
}
245250

246-
$gitTags = $this->fetchTags();
247-
$previousTag = $this->toTag;
248-
$commitData = [];
249-
250-
// Add empty tag to get commits upto first tag or HEAD revision.
251-
if ($this->fromTag === null) {
252-
$gitTags[] = '';
253-
}
251+
$gitTags = $this->fetchTags();
252+
$commitData = [];
254253

255254
$gitPath = '--git-dir ';
256255
$gitPath .= ($this->gitPath ?? './') . '.git';
257256

258257
// Get tag dates and commit subjects from git log for each tag.
258+
$commandResults = [1, 1];
259259
$includeMergeCommits = $this->options['includeMergeCommits'] ? '' : '--no-merges';
260260
foreach ($gitTags as $tag) {
261-
$tagRange = $tag == '' ? $previousTag : "$tag..$previousTag";
262-
263-
// TODO: Change shell_exec to exec.
264-
$commitData[$previousTag]['date'] =
265-
shell_exec("git $gitPath log -1 --pretty=format:%ad --date=short $previousTag");
266-
$commitData[$previousTag]['subjects'] =
267-
explode(
268-
"\n",
269-
shell_exec("git $gitPath log $tagRange $includeMergeCommits --pretty=format:%s") ?? ''
270-
);
271-
$commitData[$previousTag]['hashes'] =
272-
explode(
273-
"\n",
274-
shell_exec("git $gitPath log $tagRange $includeMergeCommits --pretty=format:%h") ?? ''
275-
);
276-
$previousTag = $tag;
261+
$rangeStart = next($gitTags);
262+
$tagRange = $rangeStart !== false ? "$rangeStart..$tag" : "$tag^";
263+
264+
$commitData[$tag]['date'] =
265+
shell_exec("git $gitPath log -1 --pretty=format:%ad --date=short $tag") ?? 'Error';
266+
267+
exec(
268+
"git $gitPath log $tagRange $includeMergeCommits --pretty=format:%s",
269+
$commitData[$tag]['subjects'],
270+
$commandResults[0]
271+
);
272+
exec(
273+
"git $gitPath log $tagRange $includeMergeCommits --pretty=format:%h",
274+
$commitData[$tag]['hashes'],
275+
$commandResults[1]
276+
);
277+
}
278+
279+
if (array_sum($commandResults)) {
280+
throw new RuntimeException('An error occurred while fetching the commit data from the repository.');
277281
}
278282

279283
// Cache commit data and process it.

0 commit comments

Comments
 (0)