77
77
*/
78
78
class GitChangelog
79
79
{
80
- /**
81
- * @var string Format of tag strings. {tag} is replaced by the tags found in the git log, {date} is replaced by the
82
- * corresponding tag date.
83
- */
84
- public $ formatTag = "## {tag} ({date}) \n\n" ;
85
- /**
86
- * @var string Format of hashes. {hashes} is replaced by the concatenated commit hashes.
87
- */
88
- public $ formatHashes = "({hashes}) " ;
89
- /**
90
- * @var string Format of subjects. {subject} is replaced by commit subjects, {hashes} is replaced by the formatted
91
- * commit hashes.
92
- */
93
- public $ formatSubject = "* {subject} {hashes} \n" ;
94
80
/**
95
81
* @var string Path to a base (changelog) file. The generated changelog can be prepend this file.
96
82
*/
97
83
public $ baseFile ;
98
- /**
99
- * @var string Format of a single commit hash. {hash} is replaced by the commit hash.
100
- */
101
- public $ formatHash = '{hash} ' ;
102
84
/**
103
85
* @var string Path to local git repository. Leave null for repository at current folder.
104
86
*/
105
87
public $ gitPath ;
106
- /**
107
- * @var string Value of the oldest tag to include into the generated changelog.
108
- * @see GitChangelog::setFromTag()
109
- */
110
- protected $ fromTag ;
111
- /**
112
- * @var string Value of the newest tag to include into the generated changelog.
113
- * @see GitChangelog::setToTag()
114
- */
115
- protected $ toTag = 'HEAD ' ;
116
- /**
117
- * @var array Contains the tags which exist in the git repository.
118
- * @see GitChangelog::fetchTags();
119
- */
120
- protected $ gitTags ;
121
88
/**
122
89
* @var string The generated changelog.
123
90
* @see GitChangelog::build()
124
91
*/
125
92
protected $ changelog ;
126
- /**
127
- * @var string[] Contains the labels to filter the commit subjects. All subjects which do not start with any of
128
- * these labels will not be listed. To disable this filtering, remove all labels from this variable.
129
- */
130
- protected $ labels = [
131
- // 'Add', // Create a capability e.g. feature, test, dependency.
132
- // 'Cut', // Remove a capability e.g. feature, test, dependency.
133
- // 'Fix', // Fix an issue e.g. bug, typo, accident, misstatement.
134
- // 'Bump', // Increase the version of something e.g. dependency.
135
- // 'Make', // Change the build process, or tooling, or infra.
136
- // 'Start', // Begin doing something; e.g. create a feature flag.
137
- // 'Stop', // End doing something; e.g. remove a feature flag.
138
- // 'Refactor', // A code change that MUST be just a refactoring.
139
- // 'Reformat', // Refactor of formatting, e.g. omit whitespace.
140
- // 'Optimize', // Refactor of performance, e.g. speed up code.
141
- // 'Document', // Refactor of documentation, e.g. help files.
142
- ];
143
93
/**
144
94
* @var array Contains the (processed) information which is fetched from the git repository.
145
95
*/
@@ -161,7 +111,7 @@ class GitChangelog
161
111
* @see https://git-scm.com/docs/git-for-each-ref
162
112
*/
163
113
protected $ options = [
164
- 'logHeader ' => " # Changelog\n\n" ,
114
+ 'logHeader ' => ' Changelog ' ,
165
115
'headSubject ' => 'Upcoming changes ' ,
166
116
'nextTagDate ' => 'Undetermined ' ,
167
117
'noChangesMessage ' => 'No changes. ' ,
@@ -171,6 +121,38 @@ class GitChangelog
171
121
'tagOrderDesc ' => true ,
172
122
'commitOrder ' => 'ASC ' ,
173
123
];
124
+ /**
125
+ * @var string Value of the oldest tag to include into the generated changelog.
126
+ * @see GitChangelog::setFromTag()
127
+ */
128
+ private $ fromTag ;
129
+ /**
130
+ * @var string Value of the newest tag to include into the generated changelog.
131
+ * @see GitChangelog::setToTag()
132
+ */
133
+ private $ toTag = 'HEAD ' ;
134
+ /**
135
+ * @var array Contains the tags which exist in the git repository.
136
+ * @see GitChangelog::fetchTags();
137
+ */
138
+ private $ gitTags ;
139
+ /**
140
+ * @var string[] Contains the labels to filter the commit subjects. All subjects which do not start with any of
141
+ * these labels will not be listed. To disable this filtering, remove all labels from this variable.
142
+ */
143
+ private $ labels = [
144
+ // 'Add', // Create a capability e.g. feature, test, dependency.
145
+ // 'Cut', // Remove a capability e.g. feature, test, dependency.
146
+ // 'Fix', // Fix an issue e.g. bug, typo, accident, misstatement.
147
+ // 'Bump', // Increase the version of something e.g. dependency.
148
+ // 'Make', // Change the build process, or tooling, or infra.
149
+ // 'Start', // Begin doing something; e.g. create a feature flag.
150
+ // 'Stop', // End doing something; e.g. remove a feature flag.
151
+ // 'Refactor', // A code change that MUST be just a refactoring.
152
+ // 'Reformat', // Refactor of formatting, e.g. omit whitespace.
153
+ // 'Optimize', // Refactor of performance, e.g. speed up code.
154
+ // 'Document', // Refactor of documentation, e.g. help files.
155
+ ];
174
156
175
157
/**
176
158
* GitChangelog constructor.
@@ -223,66 +205,6 @@ public function fetchTags($force = false): array
223
205
return $ this ->gitTags ;
224
206
}
225
207
226
-
227
- /**
228
- * Generate the changelog.
229
- *
230
- * The generated changelog will be stored into a class property.
231
- *
232
- * @throws Exception When the defined From- or To-tag doesn't exist in the git repository.
233
- * @see GitChangelog::changelog
234
- */
235
- public function build (): void
236
- {
237
- $ logContent = $ this ->options ['logHeader ' ];
238
- $ commitData = $ this ->fetchCommitData ();
239
-
240
- if (!$ commitData ) {
241
- $ logContent .= $ this ->options ['noChangesMessage ' ];
242
- $ this ->changelog = $ logContent . "\n" ;
243
-
244
- return ;
245
- }
246
-
247
- if (!$ this ->options ['tagOrderDesc ' ]) {
248
- $ commitData = array_reverse ($ commitData );
249
- }
250
-
251
- // Build changelog.
252
- foreach ($ commitData as $ tag => &$ data ) {
253
- // Add tag header and date.
254
- $ tagData = [$ tag , $ data ['date ' ]];
255
- if ($ tag == 'HEAD ' ) {
256
- $ tagData = [$ this ->options ['headSubject ' ], $ this ->options ['nextTagDate ' ]];
257
- }
258
-
259
- $ logContent .= str_replace (['{tag} ' , '{date} ' ], $ tagData , $ this ->formatTag );
260
-
261
- // No subjects present for this tag.
262
- if (!$ data ['subjects ' ]) {
263
- $ subject = $ this ->options ['noChangesMessage ' ];
264
- $ logContent .= str_replace (['{subject} ' , '{hashes} ' ], [$ subject , '' ], $ this ->formatSubject );
265
- $ logContent .= "\n" ;
266
- continue ;
267
- }
268
-
269
- // Sort commit subjects.
270
- Utilities::natSort ($ data ['subjects ' ], $ this ->options ['commitOrder ' ]);
271
-
272
- // Add commit subjects.
273
- foreach ($ data ['subjects ' ] as $ subjectKey => &$ subject ) {
274
- $ logContent .= str_replace (
275
- ['{subject} ' , '{hashes} ' ],
276
- [$ subject , $ this ->formatHashes ($ data ['hashes ' ][$ subjectKey ])],
277
- $ this ->formatSubject
278
- );
279
- }
280
- $ logContent .= "\n" ;
281
- }
282
-
283
- $ this ->changelog = trim ($ logContent ) . "\n" ;
284
- }
285
-
286
208
/**
287
209
* Fetch the commit data from the git repository.
288
210
*
@@ -395,34 +317,6 @@ private function processCommitData(): void
395
317
}
396
318
}
397
319
398
- /**
399
- * Format the hashes of a commit subject into a string.
400
- *
401
- * Each hash is formatted as defined by property formatHash.
402
- * After formatting, all hashes are concatenated to a single line, comma separated.
403
- * Finally this line is formatted as defined by property formatHashes.
404
- *
405
- * @param array $hashes Hashes to format
406
- *
407
- * @return string Formatted hash string.
408
- * @see GitChangelog::$formatHash
409
- * @see GitChangelog::$formatHashes
410
- */
411
- protected function formatHashes (array $ hashes ): string
412
- {
413
- if (!$ this ->options ['addHashes ' ]) {
414
- return '' ;
415
- }
416
-
417
- foreach ($ hashes as &$ hash ) {
418
- $ hash = str_replace ('{hash} ' , $ hash , $ this ->formatHash );
419
- }
420
- unset($ hash );
421
- $ hashes = implode (', ' , $ hashes );
422
-
423
- return str_replace ('{hashes} ' , $ hashes , $ this ->formatHashes );
424
- }
425
-
426
320
/**
427
321
* Save the generated changelog to a file.
428
322
*
0 commit comments