Skip to content

Commit 1283cb7

Browse files
authored
Merge pull request #36 from tartinesKiller/master
Implement LASTCOMMITDATETIME
2 parents cb366f8 + 80f3491 commit 1283cb7

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ It is also possible to use [path substitutions](https://webpack.js.org/configura
5353
- `[git-revision-version]`
5454
- `[git-revision-hash]`
5555
- `[git-revision-branch]` (only [when branch is enabled](#branch-false))
56+
- `[git-revision-last-commit-datetime]`
5657

5758
Example:
5859

@@ -67,7 +68,7 @@ module.exports = {
6768

6869
## Plugin API
6970

70-
The `VERSION`, `COMMITHASH` and `BRANCH` are also exposed through a public API.
71+
The `VERSION`, `COMMITHASH`, `LASTCOMMITDATETIME` and `BRANCH` are also exposed through a public API.
7172

7273
Example using the [DefinePlugin](https://webpack.js.org/plugins/define-plugin/#usage):
7374

@@ -83,6 +84,7 @@ module.exports = {
8384
'VERSION': JSON.stringify(gitRevisionPlugin.version()),
8485
'COMMITHASH': JSON.stringify(gitRevisionPlugin.commithash()),
8586
'BRANCH': JSON.stringify(gitRevisionPlugin.branch()),
87+
'LASTCOMMITDATETIME': JSON.stringify(gitRevisionPlugin.lastcommitdatetime()),
8688
})
8789
]
8890
}
@@ -172,6 +174,22 @@ module.exports = {
172174
}
173175
```
174176

177+
### `lastCommitDateTimeCommand: 'log -1 --format=%cI'`
178+
179+
To change the default `git` command used to read the value of `LASTCOMMITDATETIME`:
180+
181+
```javascript
182+
var GitRevisionPlugin = require('git-revision-webpack-plugin')
183+
184+
module.exports = {
185+
plugins: [
186+
new GitRevisionPlugin({
187+
branchCommand: 'log -1 --format=%ci'
188+
})
189+
]
190+
}
191+
```
192+
175193
## Outdated webpack
176194

177195
If your project is **not using webpack version 4 or greater**, you will need to install an older version of this package:

lib/index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var runGitCommand = require('./helpers/run-git-command')
44
var COMMITHASH_COMMAND = 'rev-parse HEAD'
55
var VERSION_COMMAND = 'describe --always'
66
var BRANCH_COMMAND = 'rev-parse --abbrev-ref HEAD'
7+
var LASTCOMMITDATETIME_COMMAND = 'log -1 --format=%cI'
78

89
function GitRevisionPlugin (options) {
910
options = options || {}
@@ -21,6 +22,9 @@ function GitRevisionPlugin (options) {
2122
this.branchCommand = options.branchCommand ||
2223
BRANCH_COMMAND
2324

25+
this.lastCommitDateTimeCommand = options.lastCommitDateTimeCommand ||
26+
LASTCOMMITDATETIME_COMMAND
27+
2428
if (options.versionCommand && options.lightweightTags) {
2529
throw new Error('lightweightTags can\'t be used together versionCommand')
2630
}
@@ -43,6 +47,14 @@ GitRevisionPlugin.prototype.apply = function (compiler) {
4347
asset: 'VERSION'
4448
})
4549

50+
buildFile({
51+
compiler: compiler,
52+
gitWorkTree: this.gitWorkTree,
53+
command: this.lastCommitDateTimeCommand,
54+
replacePattern: /\[git-revision-last-commit-datetime\]/gi,
55+
asset: 'LASTCOMMITDATETIME'
56+
})
57+
4658
if (this.createBranchFile) {
4759
buildFile({
4860
compiler: compiler,
@@ -75,4 +87,11 @@ GitRevisionPlugin.prototype.branch = function () {
7587
)
7688
}
7789

90+
GitRevisionPlugin.prototype.lastcommitdatetime = function () {
91+
return runGitCommand(
92+
this.gitWorkTree,
93+
this.lastCommitDateTimeCommand
94+
)
95+
}
96+
7897
module.exports = GitRevisionPlugin

lib/index.spec.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,32 @@ describe('git-revision-webpack-plugin (unit)', function () {
102102
expect(runGitCommand.args[0][1]).to.eql('custom branch command')
103103
})
104104
})
105+
106+
describe('on setting custom last commit date time command', function () {
107+
it('should run the build on .apply', function () {
108+
var buildFile = sinon.spy()
109+
GitRevisionPlugin.__set__('buildFile', buildFile)
110+
111+
new GitRevisionPlugin({
112+
lastCommitDateTimeCommand: 'custom last commit date time command'
113+
}).apply()
114+
115+
var lastCommitDateTimeCall = buildFile.args.find(function (calls) {
116+
return calls[0].asset === 'LASTCOMMITDATETIME'
117+
})
118+
119+
expect(lastCommitDateTimeCall[0].command).to.eql('custom last commit date time command')
120+
})
121+
122+
it('should run the custom git command on .lastcommitdatetime', function () {
123+
var runGitCommand = sinon.spy()
124+
GitRevisionPlugin.__set__('runGitCommand', runGitCommand)
125+
126+
new GitRevisionPlugin({
127+
lastCommitDateTimeCommand: 'custom last commit date time command'
128+
}).lastcommitdatetime()
129+
130+
expect(runGitCommand.args[0][1]).to.eql('custom last commit date time command')
131+
})
132+
})
105133
})

0 commit comments

Comments
 (0)