-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-resolve
executable file
·153 lines (118 loc) · 3.66 KB
/
git-resolve
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
#!/usr/bin/env node
"use strict";
const execSync = require("child_process").execSync,
exec = require("child_process").exec,
fs = require("fs"),
path = require("path"),
readline = require("readline"),
spawn = require("child_process").spawn,
stream = require("stream"),
util = require("util");
// TODO use chalk
const clc = require("cli-color"),
tmp = require("tmp");
// squelch EPIPE errors
require("epipebomb")();
let editor;
try {
editor = execSync("git config core.editor");
} catch(err) {
editor = process.env.VISUAL || process.env.EDITOR || "vi";
}
const BinarySplitter = require("./lib/binary-splitter");
const file = process.argv.slice(-1).pop();
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const input = fs.createReadStream(path.resolve(file), "utf8").pipe(new BinarySplitter()),
output = fs.createWriteStream(path.resolve(file + ".resolved"), "utf8");
class ConflictTracker extends stream.Transform {
constructor() {
super();
this.conflict = "";
}
_transform(chunk, encoding, callback) {
const line = chunk.toString();
switch (true) {
case line.startsWith("<<<<<<< "):
this.state = "ours";
this[this.state] = "";
this.header = line;
// re-initialize conflict body
this.conflict = line;
break;
case line.startsWith("======="):
this.state = "theirs";
this[this.state] = "";
this.conflict += line;
this.middle = line;
break;
case line.startsWith(">>>>>>> "):
this.conflict += line;
this.footer = line;
// reset state
this.state = null;
// TODO this is line-based not feature-based
// TODO track entity ids (unless the first line of the conflict is an entity)
// line.match(/^\s*\<node/)
// TODO display context above (using a ring buffer)
process.stdout.write(clc.blackBright(this.header));
process.stdout.write(clc.cyan(this.ours));
process.stdout.write(clc.blackBright(this.middle));
process.stdout.write(clc.magenta(this.theirs));
process.stdout.write(clc.blackBright(this.footer));
return rl.question(clc.blueBright.bold("Resolve this hunk [l,r,b,e,q,?]? "), answer => {
switch (answer) {
case "l":
return callback(null, this.ours);
case "r":
// TODO mangle version number
return callback(null, this.theirs);
case "b":
// both
return callback(null, this.ours + this.theirs);
case "e":
const tmpFile = tmp.fileSync();
fs.writeSync(tmpFile.fd, this.conflict);
fs.closeSync(tmpFile.fd);
rl.pause();
const args = editor.split(" ").concat([tmpFile.name]),
edit = spawn(args[0], args.slice(1), {
stdio: "inherit"
});
edit.on("error", err => {
console.warn(err.stack);
process.exit(1);
});
edit.on("exit", code => {
rl.resume();
return callback(null, fs.readFileSync(tmpFile.name, "utf8"));
});
break;
case "q":
// TODO cleanup or write the rest of the file out, conflicts included
process.exit(1);
case "?":
default:
// ask the question again
console.log("Uhhh");
return callback();
}
});
break;
default:
if (this.state) {
this[this.state] += line;
this.conflict += line;
} else {
return callback(null, line);
}
}
return callback();
}
_flush(callback) {
return callback();
}
}
input.pipe(new ConflictTracker()).pipe(output);