Skip to content

Commit 160f845

Browse files
authored
Merge pull request #14 from mawi12345/master
Add branch name
2 parents 74faeda + f56157e commit 160f845

File tree

5 files changed

+182
-14
lines changed

5 files changed

+182
-14
lines changed

README.md

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ And a `COMMITHASH` such as:
4040
7c16d8b1abeced419c14eb9908baeb4229ac0542
4141
```
4242

43+
And with the branch option enabled a `BRANCH` such as:
44+
45+
```
46+
master
47+
```
48+
4349
## Configuration
4450

4551
The plugin requires no configuration by default, but it is possible to configure it to support custom git workflows.
@@ -60,6 +66,22 @@ module.exports = {
6066
}
6167
```
6268

69+
### `branch: false`
70+
71+
If you need branch name support, you may turn on `branch` option in this way:
72+
73+
```javascript
74+
var GitRevisionPlugin = require('git-revision-webpack-plugin')
75+
76+
module.exports = {
77+
plugins: [
78+
new GitRevisionPlugin({
79+
branch: true
80+
})
81+
]
82+
}
83+
```
84+
6385
### `commithashCommand: 'rev-parse HEAD'`
6486

6587
To change the default `git` command used to read the value of `COMMITHASH`:
@@ -92,12 +114,29 @@ module.exports = {
92114
}
93115
```
94116

117+
### `branchCommand: 'rev-parse --abbrev-ref HEAD'`
118+
119+
To change the default `git` command used to read the value of `BRANCH`:
120+
121+
```javascript
122+
var GitRevisionPlugin = require('git-revision-webpack-plugin')
123+
124+
module.exports = {
125+
plugins: [
126+
new GitRevisionPlugin({
127+
branchCommand: 'git rev-parse --symbolic-full-name HEAD'
128+
})
129+
]
130+
}
131+
```
132+
95133
## Path Substitutions
96134

97135
It is also possible to use two [path substituitions](http://webpack.github.io/docs/configuration.html#output-filename) on build to get either the revision or version as part of output paths.
98136

99137
- `[git-revision-version]`
100138
- `[git-revision-hash]`
139+
- `[git-revision-branch]` (only with the branch option enabled)
101140

102141
Example:
103142

@@ -112,7 +151,7 @@ module.exports = {
112151

113152
## Plugin API
114153

115-
The `VERSION` and `COMMITHASH` are also exposed through a public API.
154+
The `VERSION`, `COMMITHASH` and `BRANCH` are also exposed through a public API.
116155

117156
Example using the [DefinePlugin](http://webpack.github.io/docs/list-of-plugins.html#defineplugin):
118157

@@ -124,6 +163,7 @@ module.exports = {
124163
new DefinePlugin({
125164
'VERSION': JSON.stringify(gitRevisionPlugin.version()),
126165
'COMMITHASH': JSON.stringify(gitRevisionPlugin.commithash()),
166+
'BRANCH': JSON.stringify(gitRevisionPlugin.branch()),
127167
})
128168
]
129169
}

fixtures/project/webpack.config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ module.exports = {
22
entry: './index.js',
33

44
output: {
5-
publicPath: 'http://cdn.com/assets/[git-revision-version]/[git-revision-hash]',
6-
filename: '[name]-[git-revision-version].js'
5+
publicPath: 'http://cdn.com/assets/[git-revision-branch]/[git-revision-version]/[git-revision-hash]',
6+
filename: '[name]-[git-revision-branch]-[git-revision-version].js'
77
},
88

99
module: {
1010
loaders: [
1111
{
1212
test: /\.(txt)$/,
13-
loader: 'file?name=[name][git-revision-version].[ext]'
13+
loader: 'file?name=[name]-[git-revision-branch]-[git-revision-version].[ext]'
1414
}
1515
]
1616
}

lib/index.integration.spec.js

Lines changed: 85 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ describe('git-revision-webpack-plugin (integration)', function () {
3030
config.context = targetProject
3131
config.output.path = targetBuild
3232
config.plugins = [
33-
new GitRevisionPlugin({ gitWorkTree: targetProject })
33+
new GitRevisionPlugin({
34+
gitWorkTree: targetProject,
35+
branch: true
36+
})
3437
]
3538

3639
webpack(config, function () {
@@ -52,17 +55,24 @@ describe('git-revision-webpack-plugin (integration)', function () {
5255
expect(COMMITHASH.toString()).to.eql('9a15b3ba1f8c347f9db94bcfde9630ed4fdeb1b2')
5356
})
5457

55-
describe('[git-revision-version] and [git-revision-hash] templates', function () {
58+
it('should create the BRANCH file', function () {
59+
var branchPath = path.join(targetBuild, 'BRANCH')
60+
var BRANCH = fs.readFileSync(branchPath)
61+
62+
expect(BRANCH.toString()).to.eql('master')
63+
})
64+
65+
describe('[git-revision-version], [git-revision-hash] and [git-revision-branch] templates', function () {
5666
it('should support templates in the output.filename', function () {
57-
var versionPath = path.join(targetBuild, 'main-v1.0.0-1-g9a15b3b.js')
67+
var versionPath = path.join(targetBuild, 'main-master-v1.0.0-1-g9a15b3b.js')
5868
fs.readFileSync(versionPath)
5969
})
6070

6171
it('should support setting the public path', function () {
62-
var versionPath = path.join(targetBuild, 'main-v1.0.0-1-g9a15b3b.js')
72+
var versionPath = path.join(targetBuild, 'main-master-v1.0.0-1-g9a15b3b.js')
6373
var mainJs = fs.readFileSync(versionPath)
6474

65-
var expectedPublicPath = '__webpack_require__.p = "http://cdn.com/assets/v1.0.0-1-g9a15b3b/9a15b3ba1f8c347f9db94bcfde9630ed4fdeb1b2";'
75+
var expectedPublicPath = '__webpack_require__.p = "http://cdn.com/assets/master/v1.0.0-1-g9a15b3b/9a15b3ba1f8c347f9db94bcfde9630ed4fdeb1b2";'
6676

6777
expect(mainJs.indexOf(expectedPublicPath) !== -1).to.eql(true)
6878
})
@@ -78,6 +88,11 @@ describe('git-revision-webpack-plugin (integration)', function () {
7888
var plugin = new GitRevisionPlugin({ gitWorkTree: targetProject })
7989
expect(plugin.version()).to.eql('v1.0.0-1-g9a15b3b')
8090
})
91+
92+
it('should expose the branch', () => {
93+
var plugin = new GitRevisionPlugin({ gitWorkTree: targetProject })
94+
expect(plugin.branch()).to.eql('master')
95+
})
8196
})
8297
})
8398

@@ -96,7 +111,11 @@ describe('git-revision-webpack-plugin with lightweightTags option', function ()
96111
config.context = targetProject
97112
config.output.path = targetBuild
98113
config.plugins = [
99-
new GitRevisionPlugin({ gitWorkTree: targetProject, lightweightTags: true })
114+
new GitRevisionPlugin({
115+
gitWorkTree: targetProject,
116+
lightweightTags: true,
117+
branch: true
118+
})
100119
]
101120

102121
webpack(config, function () {
@@ -118,17 +137,24 @@ describe('git-revision-webpack-plugin with lightweightTags option', function ()
118137
expect(COMMITHASH.toString()).to.eql('9a15b3ba1f8c347f9db94bcfde9630ed4fdeb1b2')
119138
})
120139

121-
describe('[git-revision-version] and [git-revision-hash] templates', function () {
140+
it('should create the BRANCH file', function () {
141+
var branchPath = path.join(targetBuild, 'BRANCH')
142+
var BRANCH = fs.readFileSync(branchPath)
143+
144+
expect(BRANCH.toString()).to.eql('master')
145+
})
146+
147+
describe('[git-revision-version], [git-revision-hash] and [git-revision-branch] templates', function () {
122148
it('should support templates in the output.filename', function () {
123-
var versionPath = path.join(targetBuild, 'main-v2.0.0-beta.js')
149+
var versionPath = path.join(targetBuild, 'main-master-v2.0.0-beta.js')
124150
fs.readFileSync(versionPath)
125151
})
126152

127153
it('should support setting the public path', function () {
128-
var versionPath = path.join(targetBuild, 'main-v2.0.0-beta.js')
154+
var versionPath = path.join(targetBuild, 'main-master-v2.0.0-beta.js')
129155
var mainJs = fs.readFileSync(versionPath)
130156

131-
var expectedPublicPath = '__webpack_require__.p = "http://cdn.com/assets/v2.0.0-beta/9a15b3ba1f8c347f9db94bcfde9630ed4fdeb1b2";'
157+
var expectedPublicPath = '__webpack_require__.p = "http://cdn.com/assets/master/v2.0.0-beta/9a15b3ba1f8c347f9db94bcfde9630ed4fdeb1b2";'
132158

133159
expect(mainJs.indexOf(expectedPublicPath) !== -1).to.eql(true)
134160
})
@@ -144,6 +170,55 @@ describe('git-revision-webpack-plugin with lightweightTags option', function ()
144170
var plugin = new GitRevisionPlugin({ gitWorkTree: targetProject, lightweightTags: true })
145171
expect(plugin.version()).to.eql('v2.0.0-beta')
146172
})
173+
174+
it('should expose the branch', () => {
175+
var plugin = new GitRevisionPlugin({ gitWorkTree: targetProject, lightweightTags: true })
176+
expect(plugin.branch()).to.eql('master')
177+
})
147178
})
148179
})
149180

181+
describe('git-revision-webpack-plugin without branch option', function () {
182+
beforeEach(function (done) {
183+
fs.emptyDirSync(targetProject)
184+
fs.copySync(sourceProject, targetProject)
185+
186+
fs.emptyDirSync(targetGitRepository)
187+
fs.copySync(sourceGitRepository, targetGitRepository)
188+
189+
fs.remove(targetBuild)
190+
191+
var config = require(targetProjectConfig)
192+
193+
config.context = targetProject
194+
config.output.path = targetBuild
195+
config.plugins = [
196+
new GitRevisionPlugin({
197+
gitWorkTree: targetProject
198+
})
199+
]
200+
201+
webpack(config, function () {
202+
done()
203+
})
204+
})
205+
206+
it('should create the VERSION file', function () {
207+
var versionPath = path.join(targetBuild, 'VERSION')
208+
var VERSION = fs.readFileSync(versionPath)
209+
210+
expect(VERSION.toString()).to.eql('v1.0.0-1-g9a15b3b')
211+
})
212+
213+
it('should create the COMMITHASH file', function () {
214+
var versionPath = path.join(targetBuild, 'COMMITHASH')
215+
var COMMITHASH = fs.readFileSync(versionPath)
216+
217+
expect(COMMITHASH.toString()).to.eql('9a15b3ba1f8c347f9db94bcfde9630ed4fdeb1b2')
218+
})
219+
220+
it('should not create the BRANCH file', function () {
221+
var branchPath = path.join(targetBuild, 'BRANCH')
222+
expect(fs.existsSync(branchPath)).to.eql(false)
223+
})
224+
})

lib/index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var runGitCommand = require('./helpers/run-git-command')
33

44
var COMMITHASH_COMMAND = 'rev-parse HEAD'
55
var VERSION_COMMAND = 'describe --always'
6+
var BRANCH_COMMAND = 'rev-parse --abbrev-ref HEAD'
67

78
function GitRevisionPlugin (options) {
89
options = options || {}
@@ -15,6 +16,11 @@ function GitRevisionPlugin (options) {
1516
this.versionCommand = options.versionCommand ||
1617
VERSION_COMMAND + (options.lightweightTags ? ' --tags' : '')
1718

19+
this.createBranchFile = options.branch || false
20+
21+
this.branchCommand = options.branchCommand ||
22+
BRANCH_COMMAND
23+
1824
if (options.versionCommand && options.lightweightTags) {
1925
throw new Error('lightweightTags can\'t be used together versionCommand')
2026
}
@@ -36,6 +42,16 @@ GitRevisionPlugin.prototype.apply = function (compiler) {
3642
/\[git-revision-version\]/gi,
3743
'VERSION'
3844
)
45+
46+
if (this.createBranchFile) {
47+
buildFile(
48+
compiler,
49+
this.gitWorkTree,
50+
this.branchCommand,
51+
/\[git-revision-branch\]/gi,
52+
'BRANCH'
53+
)
54+
}
3955
}
4056

4157
GitRevisionPlugin.prototype.commithash = function () {
@@ -52,4 +68,11 @@ GitRevisionPlugin.prototype.version = function () {
5268
)
5369
}
5470

71+
GitRevisionPlugin.prototype.branch = function () {
72+
return runGitCommand(
73+
this.gitWorkTree,
74+
this.branchCommand
75+
)
76+
}
77+
5578
module.exports = GitRevisionPlugin

lib/index.spec.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,34 @@ describe('git-revision-webpack-plugin (unit)', function () {
7272
expect(runGitCommand.args[0][1]).to.eql('custom version command')
7373
})
7474
})
75+
76+
describe('on setting custom branch command', function () {
77+
it('should run the build on .apply', function () {
78+
var buildFile = sinon.spy()
79+
GitRevisionPlugin.__set__('buildFile', buildFile)
80+
81+
new GitRevisionPlugin({
82+
branch: true,
83+
branchCommand: 'custom branch command'
84+
}).apply()
85+
86+
var branchCall = buildFile.args.find(function (calls) {
87+
return calls[4] === 'BRANCH'
88+
})
89+
90+
expect(branchCall[2]).to.eql('custom branch command')
91+
})
92+
93+
it('should run the custom git command on .version', function () {
94+
var runGitCommand = sinon.spy()
95+
GitRevisionPlugin.__set__('runGitCommand', runGitCommand)
96+
97+
new GitRevisionPlugin({
98+
branch: true,
99+
branchCommand: 'custom branch command'
100+
}).branch()
101+
102+
expect(runGitCommand.args[0][1]).to.eql('custom branch command')
103+
})
104+
})
75105
})

0 commit comments

Comments
 (0)