-
Notifications
You must be signed in to change notification settings - Fork 0
/
git.js
242 lines (224 loc) · 8.03 KB
/
git.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*!
* thoughtful-release <https://github.com/nknapp/thoughtful-release>
*
* Copyright (c) 2015 Nils Knappmeier.
* Released under the MIT license.
*/
'use strict'
var cpp = require('../lib/q-child-process')
var qfs = require('q-io/fs')
var path = require('path')
var moment = require('moment')
var debug = require('debug')('thoughtful-release:git')
var _ = {
defaults: require('lodash.defaults'),
isString: require('lodash.isstring'),
toArray: require('lodash.toarray')
}
/**
* Create a git-object for a directory
* @param {string} dir the current directory
* @returns {Git} the git object
*/
module.exports = (dir) => new Git(dir)
/**
* Path to a node-script to be used instead of the real 'git' for test-cases
* @type {undefined}
*/
module.exports.mockCmd = undefined
/**
* The Git class for accessing git repositories
* @param {string} dir the working directory
* @constructor
*/
function Git (dir) {
var env = _.defaults({
'LANG': 'en_US'
}, process.env)
var gitcmd = module.exports.mockCmd ? [ 'node', module.exports.mockCmd ] : 'git'
/**
* Execute git with a list of cmd-line arguments in the working directory
* and with LANG=en_US
* <br/>
* This method has dynamic arguments (the cli-arguments to pass to git)
*
* @returns {*}
*/
this.run = function runGit () {
debug(`Running in ${dir}`, gitcmd, _.toArray(arguments))
return cpp.execFile(gitcmd, _.toArray(arguments), {env: env, cwd: dir})
}
/**
* Spawn git, keeping stdio connected to the parents stdio. The is needed for commands
* that need a tty to work
* @returns {*}
*/
this.spawn = function spawnGit () {
debug(`Running in ${dir}`, gitcmd, _.toArray(arguments))
return cpp.spawn(gitcmd, _.toArray(arguments), {env: env, cwd: dir, stdio: 'inherit'})
}
/**
* Return true, if the working directory is a git-repository (i.e. has ".git"-subdirectory)
*
* @return {Promise<boolean>} true, if the working directory is a git-repository
*/
this.isRepo = function isRepo () {
return qfs.stat(path.join(dir, '.git')).then((stat) => {
// .git directory must be a directory
return stat.isDirectory()
}).catch((error) => {
/* istanbul ignore else The else path is very unlikely and would need a setup with changed permissions to test it */
if (error.code === 'ENOENT') {
// Not .git directory
return false
} else {
throw error
}
})
}
/**
* Answer the latest release tag or `null` if no release tag exists
*/
this.lastRelease = function lastReleaseTag () {
return this.run('describe', '--long', '--match', 'v*')
.then((output) => {
if (output.stderr && output.stderr.trim()) {
throw new Error(output.stderr)
}
var match = output.stdout.trim().match(/^(v.+)-(\d+)-g([\da-f]+)$/)
if (match) {
return {
tag: match[1],
newCommits: Number(match[2])
}
} else {
throw new Error(`No version tag found in git output: ${output.stdout}`)
}
}, (err) => {
if (err.message && err.message.indexOf('fatal: No names found, cannot describe anything.') >= 0) {
return {
tag: null,
newCommits: null
}
}
throw err
})
}
/**
* Generate changelog entries for a span between two tags
* @param {string} from the start tag
* @param {object=} options optional parameters
* @param {string=} options.url repository remote url
* @param {string=} options.to the end tag. This defaults to the current commit if not set.
*/
this.changes = function changes (from, options) {
var to = ''
var commitPattern = '%h'
if (options) {
if (options.to) {
to = options.to
}
commitPattern = markdownLinkPattern(options.url)
}
var args = ['log', '--no-merges', `--pretty=tformat:* ${commitPattern} %s - %an`]
if (from || to) {
args.push(`${from}..${to}`)
}
return this.run.apply(this, args).then(function (output) {
if (output.stderr && output.stderr.trim()) {
throw new Error(output.stderr)
}
return output.stdout
})
}
/**
* Return the currently checked-out branch
* @returns {Promise.<string>} the branch name
*/
this.currentBranch = function currentBranch () {
return this.run('symbolic-ref', '--short', 'HEAD')
.then((output) => output.stdout.trim())
}
/**
* Replace all "pick" commits of an interactive rebase by "squash" (except the first)
* @param todoFile the "git-rebase-todo" file
*/
this.squashRebaseTodos = function squashRebaseTodos (todoFile) {
return qfs.read(todoFile)
.then((todo) => {
var counter = 0
if (todo.match(/^pick/gm).length > 1) {
// Replace every "pick" by "squash" except the first one.
return todo.replace(/^pick/gm, () => counter++ === 0 ? 'pick' : 'squash')
} else {
return todo.replace(/^pick/m, 'reword')
}
})
.then((todo) => qfs.write(todoFile, todo))
}
/**
* Perform a rebase of the current (topic-)branch onto a target-branch, condensing the
* whole branch into a single commit.
* This is done in two steps: First perform an interactive rebase on the `merge-base` to squash the branch.
* Then rebase the whole branch on top of the target branch.
*
* This workflow has reduces the overhead from conflict management, when conflicts occur in multiple commits and different files on the branch.
* (see `manual-testing/prepare-repo-for-rebase.sh` as an example where conflict handling works smoother if the branch is squashed first)
*
* @param {object=} options options to this function
* @param {string=} options.thoughtful the command to invoke "thoughtful"
* @param {string=} options.targetBranch the branch to rebase the current branch upon (default: master)
*/
this.cleanupHistory = function cleanupHistory (options) {
var targetBranch = (options && options.targetBranch) || 'master'
var thoughtful = process.argv[1]
/* istanbul ignore else
The default value for "thoughtful" is the current process. It
is hardly possible to use this default value in a unit-test */
if (options && options.thoughtful) {
thoughtful = options.thoughtful
}
const timestamp = moment().format('YYYY-MM-DDTHH-mm-ss')
return this.run('tag', '-f', `thoughtful-backup/cleanup-history-${timestamp}`) // Make a backup...
.then((tagOutput) => {
return this.run('merge-base', 'HEAD', targetBranch) // Get merge-base
// Squash
.then((mergeBase) => this.spawn('-c', `sequence.editor='${thoughtful}' sequence-editor`, 'rebase', '--interactive', mergeBase.stdout.trim()))
// Rebase on target branch
.then(() => this.run('rebase', targetBranch).invoke('appendTo', tagOutput.appendTo('')))
})
}
/**
* Add (stage) files to the git repository
* @param {string|string[]} file a single file or an array of files
* @return {Promise<*>} a promise for the end of the operation
*/
this.add = function add (file) {
debug('Staging"' + file + '" to repository at "' + dir + '"')
var files = _.isString(file) ? [file] : file
files = files.map((file) => path.relative(dir, file))
return this.run.apply(this, ['add'].concat(files))
.then(function (output) {
return null
})
}
}
/**
* Convert a repository url like "git://github.com/nknapp/bootprint" and "git@github.com/nknapp/bootprint" to
* a tformat-pattern for git creating a markdown link to the current commit
* like "[%h](https://github.com/nknapp/bootprint/commit/%h)".
* If the url is not github, '%h' will be returned to default.
* Support for other git-providers (like gitlab) could be added here.
*/
function markdownLinkPattern (url) {
if (!_.isString(url)) {
return '%h'
}
var match = url.match(/.*?(:\/\/|@)github\.com[/:](.*?)(#.*?)?$/)
if (match) {
var repoPath = match[2].replace(/\.git$/, '')
return `[%h](https://github.com/${repoPath}/commit/%h)`
} else {
return '%h'
}
}