-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
177 lines (157 loc) · 5.19 KB
/
index.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
/**
* @copyright Copyright 2016-2020 Kevin Locke <[email protected]>
* @license MIT
* @module git-branch-is
*/
'use strict';
const { execFile } = require('node:child_process');
/** Options for {@link gitBranchIs}.
*
* @typedef {{
* cwd: (?string|undefined),
* gitArgs: (Array|undefined),
* gitDir: (?string|undefined),
* gitPath: (string|undefined)
* }} GitBranchIsOptions
* @property {?string=} cwd Current working directory where the branch name is
* tested.
* @property {Array=} gitArgs Extra arguments to pass to git.
* @property {?string=} gitDir Path to the repository (i.e.
* <code>--git-dir=</code> option to <code>git</code>).
* @property {string=} gitPath Git binary name or path to use (default:
* <code>'git'</code>).
*/
const GitBranchIsOptions = {
cwd: '',
gitArgs: [],
gitDir: '',
gitPath: 'git',
};
/** Checks that the current branch of a git repository has a given name.
*
* @param {string|function(string)} branchNameOrTest Expected name of
* current branch or a test function to apply to the branch name.
* @param {?GitBranchIsOptions=} options Options.
* @param {?function(Error, boolean=)=} callback Callback function called
* with the return value of <code>branchNameOrTest</code> if it is a function,
* or the result of identity checking <code>branchNameOrTest</code> to the
* current branch name.
* @returns {Promise|undefined} If <code>callback</code> is not given, a
* <code>Promise</code> with the return value of <code>branchNameOrTest</code>
* if it is a function, or the result of identity checking
* <code>branchNameOrTest</code> to the current branch name.
*/
function gitBranchIs(branchNameOrTest, options, callback) {
if (!callback && typeof options === 'function') {
callback = options;
options = undefined;
}
if (!callback) {
return new Promise((resolve, reject) => {
gitBranchIs(branchNameOrTest, options, (err, result) => {
if (err) { reject(err); } else { resolve(result); }
});
});
}
if (typeof callback !== 'function') {
throw new TypeError('callback must be a function');
}
if (options !== undefined && typeof options !== 'object') {
queueMicrotask(() => {
callback(new TypeError('options must be an object'));
});
return undefined;
}
gitBranchIs.getBranch(options, (err, currentBranch) => {
if (err) {
callback(err);
return;
}
let result;
try {
result = currentBranch === branchNameOrTest
|| (typeof branchNameOrTest === 'function'
&& branchNameOrTest(currentBranch));
} catch (errTest) {
callback(errTest);
return;
}
callback(null, result); // eslint-disable-line unicorn/no-null
});
return undefined;
}
/** Gets the name of the current (i.e. checked out) branch of a git repository.
*
* @param {?GitBranchIsOptions=} options Options.
* @param {?function(Error, string=)=} callback Callback function called
* with the current branch name, empty string if not on a branch, or
* <code>Error</code> if there was an error determining the branch name.
* @returns {Promise|undefined} If <code>callback</code> is not given, a
* <code>Promise</code> with the current branch name, empty string if not on a
* branch, or <code>Error</code> if there was an error determining the branch
* name.
*/
gitBranchIs.getBranch = function getBranch(options, callback) {
if (!callback && typeof options === 'function') {
callback = options;
options = undefined;
}
if (!callback) {
return new Promise((resolve, reject) => {
getBranch(options, (err, result) => {
if (err) { reject(err); } else { resolve(result); }
});
});
}
if (typeof callback !== 'function') {
throw new TypeError('callback must be a function');
}
if (options && typeof options !== 'object') {
queueMicrotask(() => {
callback(new TypeError('options must be an Object'));
});
return undefined;
}
const combinedOpts = {
...GitBranchIsOptions,
...options,
};
const gitArgs = combinedOpts.gitArgs
? Array.prototype.slice.call(combinedOpts.gitArgs, 0)
: [];
if (combinedOpts.gitDir) {
gitArgs.unshift(`--git-dir=${combinedOpts.gitDir}`);
}
// Note: --quiet causes symbolic-ref to exit with code 1 and no error
// instead of code 128 and "ref %s is not a symbolic ref" when not on a
// branch.
gitArgs.push('symbolic-ref', '--quiet', '--short', 'HEAD');
try {
execFile(
combinedOpts.gitPath,
gitArgs,
{ cwd: combinedOpts.cwd },
(errExec, stdout, stderr) => {
if (errExec) {
if (errExec.code === 1 && !stdout && !stderr) {
// Not on a branch
callback(null, ''); // eslint-disable-line unicorn/no-null
} else {
callback(errExec);
}
return;
}
// Note: ASCII space and control characters are forbidden in names
// https://www.kernel.org/pub/software/scm/git/docs/git-check-ref-format.html
callback(null, stdout.trimEnd()); // eslint-disable-line unicorn/no-null
},
);
} catch (errExec) {
queueMicrotask(() => {
callback(errExec);
});
return undefined;
}
return undefined;
};
module.exports = gitBranchIs;