-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-fix-issue
More file actions
executable file
·161 lines (128 loc) · 3.83 KB
/
Copy pathgit-fix-issue
File metadata and controls
executable file
·161 lines (128 loc) · 3.83 KB
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
#!/usr/bin/env node
const { spawnSync } = require('child_process');
const { writeFileSync, unlinkSync, readFileSync } = require('fs');
const path = require('path');
let programName = 'git fix-issue';
let isOutputToATerminal = false;
const isGitCola = process.env.GIT_COLA_WRITE_MESSAGE === 'git-fix-issue';
function usage(program = programName) {
process.stderr.write(`Usage: ${program} "https://gitlab.com/.../-/issues/..."\n(or have issue link in clipboard)\n`);
process.exit(1);
}
function run(command, args = [], options = {}) {
const cp = spawnSync(command, args, {
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'pipe'],
...options,
});
if (cp.error) {
throw cp.error;
}
return cp.output;
}
// e.g. https://gitlab.com/marama-labs-software/marama-frontend/-/issues/142
function getFromClipboard() {
const output = run('xclip', ['-o', '-selection', 'clipboard']);
return output.filter(Boolean).join('');
}
function saveToClipboard(data) {
const cp = spawnSync('xclip', ['-i', '-selection', 'clipboard'], {
input: data,
encoding: 'utf8',
stdio: ['pipe', 'ignore', 'ignore'],
});
if (cp.status !== 0) {
throw cp.error || new Error(`xclip -i failed, status ${cp.status}`);
}
}
function gitRoot() {
const output = run('git', ['rev-parse', '--show-toplevel']);
return output.filter(Boolean).join('').trim();
}
function runGitCommit(message) {
const commitMessageFile = `${gitRoot()}/.git/GIT_FIX_ISSUE_COMMIT_MESSAGE`;
try {
writeFileSync(commitMessageFile, message, { encoding: 'utf-8' });
const output = run('git', ['commit', '-F', commitMessageFile, '-e'], { stdio: 'inherit' });
return output.filter(Boolean).map((x) => x.trim());
} finally {
try {
unlinkSync(commitMessageFile);
} catch {
// ignore
}
}
}
function hasStagedFiles() {
const output = run('git', ['diff', '--name-only', '--cached']);
return output.filter(Boolean).length > 0;
}
function getLink(args) {
let [, , link] = args;
if (!link) {
link = getFromClipboard();
}
return link;
}
function getCommitMessage(link, existingText) {
if (link.match(/^fix.*#\d+/)) {
return link;
}
if (!link.match(/^\s*https:\/\/gitlab.com.+issues\/\d+\s*$/)) {
throw new Error('Bad link');
}
const [, issue] = link.match(/https:\/\/gitlab.com\/.+\/issues\/(\d+)/);
if (!issue) {
process.stderr.write(`Malformed issue link: ${link}\n`);
process.exit(1);
}
if (String(existingText).match(/^fix: CHANGEME/)) {
return existingText;
}
return `fix: CHANGEME #${issue}\n\n${existingText ? `${existingText}\n` : ''}Fixes: #${issue}\nIssue-Link: ${link}\n`;
}
function performCommit(commitMessage) {
process.stdout.write(commitMessage);
if (hasStagedFiles()) {
runGitCommit(commitMessage);
}
}
// eslint-disable-next-line consistent-return
function getCommitMessageOrFail(link, existingText = '') {
try {
return getCommitMessage(link, existingText);
} catch (error) {
process.stderr.write(`${error.message}\n`);
usage();
}
}
function getLinkOrFail(args) {
const link = getLink(args);
if (!link) {
usage();
}
return link;
}
function processArgs(args) {
const [, program] = args;
programName = program || programName;
({ isTTY: isOutputToATerminal } = process.stdout);
if (args.find((a) => /^--?h(elp)?/.test(a))) {
usage();
}
}
function main(args) {
processArgs(args);
const link = getLinkOrFail(args);
const msgPath = path.join(gitRoot(), '.git/GIT_COLA_MSG');
const existingText = readFileSync(msgPath, { encoding: 'utf-8' });
const commitMessage = getCommitMessageOrFail(link, existingText);
if (isOutputToATerminal) {
performCommit(commitMessage);
} else if (isGitCola) {
writeFileSync(msgPath, commitMessage, { encoding: 'utf-8' });
} else {
saveToClipboard(commitMessage);
}
}
main(process.argv);