@@ -122,16 +122,17 @@ class GitChangelog
122
122
'commitOrder ' => 'ASC ' ,
123
123
];
124
124
/**
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.
126
127
* @see GitChangelog::setFromTag()
127
128
*/
128
129
private $ fromTag ;
129
130
/**
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.
132
133
* @see GitChangelog::setToTag()
133
134
*/
134
- private $ toTag = '' ;
135
+ private $ toTag ;
135
136
/**
136
137
* @var array Contains the tags which exist in the git repository. If the first element's key is an empty string, it
137
138
* refers to the HEAD revision.
@@ -178,7 +179,8 @@ public function __construct()
178
179
* @param bool $force [Optional] Set to true to refresh the cached tags.
179
180
*
180
181
* @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.
182
184
*/
183
185
public function fetchTags ($ force = false ): array
184
186
{
@@ -191,12 +193,14 @@ public function fetchTags($force = false): array
191
193
$ gitPath .= $ this ->gitPath ?? './.git ' ;
192
194
193
195
// 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
+ }
197
201
198
202
// Add HEAD revision as tag.
199
- if ($ this ->toTag === '' ) {
203
+ if ($ this ->toTag === null ) {
200
204
array_unshift ($ this ->gitTags , $ this ->toTag );
201
205
}
202
206
@@ -233,7 +237,8 @@ public function fetchTags($force = false): array
233
237
* @param false $force [Optional] Set to true to refresh the cached tags.
234
238
*
235
239
* @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.
237
242
* @see GitChangelog::processCommitData()
238
243
*/
239
244
public function fetchCommitData ($ force = false ): array
@@ -243,37 +248,36 @@ public function fetchCommitData($force = false): array
243
248
return $ this ->commitData ;
244
249
}
245
250
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 = [];
254
253
255
254
$ gitPath = '--git-dir ' ;
256
255
$ gitPath .= ($ this ->gitPath ?? './ ' ) . '.git ' ;
257
256
258
257
// Get tag dates and commit subjects from git log for each tag.
258
+ $ commandResults = [1 , 1 ];
259
259
$ includeMergeCommits = $ this ->options ['includeMergeCommits ' ] ? '' : '--no-merges ' ;
260
260
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. ' );
277
281
}
278
282
279
283
// Cache commit data and process it.
0 commit comments