Skip to content

Commit 4ddebf3

Browse files
authored
Merge pull request #3 from GrayedFox/20170717
release-20170717
2 parents b83884f + bf6588a commit 4ddebf3

8 files changed

Lines changed: 77 additions & 24 deletions

File tree

README.md

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
1-
This handy little module checks if the currently checked our branch
2-
has been squashed or not (based on the number of commits the branch has).
1+
This handy little module checks if the currently checked out branch
2+
needs squashing. It uses a native git command to count the number of
3+
unique commits the branch has (read: commits not present on develop).
34

4-
Now if you're following the GitFlow branching model you can add a check
5-
to your desired CI pipeline to ensure developers don't merge feature
6-
branches without rebasing/squashing them first!
7-
8-
Install using npm
5+
Install using npm:
96

107
`npm install squashed --save-dev`
118

12-
Then from inside the squashed npm package folder, do
9+
Then from inside the squashed npm package folder
10+
(`node_modules/squashed/`), do:
1311

1412
`npm link`
1513

16-
Now simply navigate to the root folder of the git project and type
17-
`squashed` into your command line. Will output a message and also return
18-
true or false so that it can be easily integrated into a CI task runner.
14+
Now simply type "squashed" into the terminal from any Git folder to see
15+
if you need to squash those commits or not!

app.js

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,64 @@
1+
const fs = require('fs')
12
const execProcess = require('./execProcess.js')
23

3-
execProcess.result('sh node_modules/squashed/countBranchCommits.sh', function(err, response) {
4-
if (err) {
5-
console.log(err)
6-
} else {
7-
if (response > 1) {
8-
console.log('You need to squash your commits! Do a rebase!')
4+
let path = './node_modules/squashed/commands/'
5+
let commitRef = 'HEAD'
6+
7+
// Function which returns a promise so we can chain command execution
8+
const commandHandler = (command) => {
9+
return new Promise((resolve, reject) => {
10+
execProcess.result(`sh ${path}${command}`, (err, response) => {
11+
if (err) {
12+
reject(err)
13+
} else {
14+
resolve(response)
15+
}
16+
})
17+
})
18+
}
19+
20+
// Updates the exitCode and logs errors form command promise rejections
21+
const errorHandler = (error) => {
22+
console.error(error)
23+
process.exit(1)
24+
}
25+
26+
// Log squashed messages in blue so they stand out
27+
const prettyLog = (message) => {
28+
console.log(`\x1b[34m${message}\x1b[0m`)
29+
}
30+
31+
// Initialisation and env setup
32+
const init = () => {
33+
// if not installed via Node Package Manager adjust path to root (for debugging local builds)
34+
try {
35+
(!fs.lstatSync(path).isDirectory())
36+
} catch(err) {
37+
if (err.code === 'ENOENT') {
38+
path = './commands/'
39+
}
40+
}
41+
42+
// if run inside Travis CI we need to ensure we update the commitRef since we are working in a detached head state
43+
if (process.env.TRAVIS_PULL_REQUEST_BRANCH) {
44+
commitRef = process.env.TRAVIS_PULL_REQUEST_SHA
45+
}
46+
console.log(`Commit reference: ${commitRef}`)
47+
}
48+
49+
init()
50+
51+
Promise.all([commandHandler(`getBranchCommitCount.sh ${commitRef}`), commandHandler('getFormattedBranchCommits.sh')]).then(
52+
([commitCount, formattedBranchCommits]) => {
53+
if (commitCount > 1) {
54+
console.log(`
55+
${formattedBranchCommits}`)
56+
prettyLog(`
57+
You need to squash those ${+commitCount} commits!
58+
`)
959
process.exit(1)
1060
} else {
11-
console.log('Merge away McLoven')
61+
prettyLog('Merge away McLoven :)')
1262
process.exit(0)
1363
}
14-
}
15-
})
64+
}, errorHandler)

bin/squashed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#! /usr/bin/env node
22

33
squashedResult = require('../app.js')
4+
45
squashedResult

commands/getBranchCommitCount.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
3+
git rev-list $1 --not origin/develop --count
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
3+
git log --first-parent origin/develop --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n'' %C(white)%s%C(reset) %C(dim white)- %an%C(reset)'

commands/getStatus.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
3+
git status

countBranchCommits.sh

Lines changed: 0 additions & 3 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "squashed",
3-
"version": "1.0.4",
3+
"version": "1.1.0",
44
"description": "A nifty tool to check if your branch needs to be rebased/squashed",
55
"main": "app.js",
66
"bin": {

0 commit comments

Comments
 (0)