Skip to content

Commit e5c0a28

Browse files
committed
typescript-ify reduce-path
Signed-off-by: Kipras Melnikovas <[email protected]>
1 parent 4feb64e commit e5c0a28

File tree

3 files changed

+125
-71
lines changed

3 files changed

+125
-71
lines changed

reducePath.spec.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import assert from "assert";
2+
3+
import { reducePath } from "./reducePath";
4+
5+
export default function testcase() {
6+
const obj1 = {
7+
a: "b",
8+
b: "c",
9+
c: "d",
10+
d: "e",
11+
12+
g: "h",
13+
14+
x: "x",
15+
16+
y: "z",
17+
z: "z",
18+
19+
/**
20+
* this might mean that we need to go backwards
21+
* rather than forwards
22+
* (multiple commits can be reported as rewritten into one,
23+
* but i don't think the opposite is possible)
24+
*
25+
* ~~and/or we might need another phase,
26+
* because currently, A -> F,
27+
* and both B and C stay at D.~~
28+
* done
29+
*
30+
*/
31+
A: "D",
32+
B: "D",
33+
C: "D",
34+
D: "E",
35+
E: "F",
36+
};
37+
38+
reducePath(obj1);
39+
console.log(obj1);
40+
41+
assert.deepStrictEqual(obj1, {
42+
a: "e",
43+
44+
g: "h",
45+
46+
x: "x",
47+
48+
y: "z",
49+
50+
A: "F",
51+
B: "F",
52+
C: "F",
53+
});
54+
}

reducePath.ts

Lines changed: 66 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,20 @@
1-
#!/usr/bin/env node
1+
#!/usr/bin/env ts-node-dev
22

33
/* eslint-disable */
44

5-
const assert = require("assert")
6-
const fs = require("fs")
7-
const { execSync } = require("child_process")
5+
import assert from "assert"
6+
import fs from "fs"
7+
import { execSync } from "child_process"
88

9-
const obj1 = {
10-
"a": "b",
11-
"b": "c",
12-
"c": "d",
13-
"d": "e",
9+
export type StringFromToMap = { [key: string]: string }
1410

15-
"g": "h",
16-
17-
"x": "x",
18-
19-
"y": "z",
20-
"z": "z",
21-
22-
/**
23-
* this might mean that we need to go backwards
24-
* rather than forwards
25-
* (multiple commits can be reported as rewritten into one,
26-
* but i don't think the opposite is possible)
27-
*
28-
* ~~and/or we might need another phase,
29-
* because currently, A -> F,
30-
* and both B and C stay at D.~~
31-
* done
32-
*
33-
*/
34-
"A": "D",
35-
"B": "D",
36-
"C": "D",
37-
"D": "E",
38-
"E": "F",
39-
}
40-
41-
reducePath(obj1)
42-
console.log(obj1)
43-
assert.deepStrictEqual(obj1, {
44-
"a": "e",
45-
46-
"g": "h",
47-
48-
"x": "x",
49-
50-
"y": "z",
51-
52-
"A": "F",
53-
"B": "F",
54-
"C": "F",
55-
})
56-
57-
function reducePath(obj) {
58-
let prevSize = -Infinity
59-
let entries
60-
let keysMarkedForDeletion = new Set()
11+
/**
12+
* mutates `obj` and returns it too
13+
*/
14+
export function reducePath(obj: StringFromToMap): StringFromToMap {
15+
let prevSize : number = -Infinity
16+
let entries : [string, string][]
17+
let keysMarkedForDeletion: Set<string> = new Set<string>()
6118

6219
// as long as it continues to improve
6320
while (keysMarkedForDeletion.size > prevSize) {
@@ -100,30 +57,61 @@ function reducePath(obj) {
10057
return obj
10158
}
10259

103-
function combineRewrittenLists(rewrittenListFile) {
60+
export type RewrittenListBlockBase = {
61+
mapping: StringFromToMap
62+
}
63+
export type RewrittenListBlockAmend = RewrittenListBlockBase & {
64+
type: "amend"
65+
}
66+
export type RewrittenListBlockRebase = RewrittenListBlockBase & {
67+
type: "rebase"
68+
}
69+
export type RewrittenListBlock = RewrittenListBlockAmend | RewrittenListBlockRebase
70+
71+
export type CombineRewrittenListsRet = {
72+
/**
73+
* notice that this only includes rebases, no amends --
74+
* that's the whole point.
75+
*
76+
* further, probably only the 1st one is necessary,
77+
* because it's likely that we'll start creating separate files for new rebases,
78+
* or that might not be needed at all, because we might be able to
79+
* --apply after every rebase, no matter if the user exited or not,
80+
* thus we'd always have only 1 "rebase" block in the rewritten list.
81+
*/
82+
mergedReducedRewrittenLists: RewrittenListBlockRebase[],
83+
84+
/**
85+
* the git's standard represantation of the rewritten-list
86+
* (no extras of ours)
87+
*/
88+
combinedRewrittenList: string,
89+
}
90+
export function combineRewrittenLists(rewrittenListFileContent: string): CombineRewrittenListsRet {
10491
/**
10592
* $1 (amend/rebase)
10693
*/
107-
const extraOperatorLineCount = 1
94+
const extraOperatorLineCount = 1 as const
10895

109-
const rewrittenLists = rewrittenListFile
96+
const rewrittenLists: RewrittenListBlock[] = rewrittenListFileContent
11097
.split("\n\n")
11198
.map(lists => lists.split("\n"))
11299
.map(list => list[list.length - 1] === "" ? list.slice(0, -1) : list)
113100
// .slice(0, -1)
114101
.filter(list => list.length > extraOperatorLineCount)
115-
.map(list => ({
116-
type: list[0],
117-
mapping: Object.fromEntries(
118-
list.slice(1).map(line => line.split(" "))
102+
.map((list): RewrittenListBlock => ({
103+
type: list[0] as RewrittenListBlock["type"],
104+
mapping: Object.fromEntries<string>(
105+
list.slice(1).map(line => line.split(" ") as [string, string])
119106
)
120107
})
121108
)
122109
// .map(list => Object.fromEntries(list))
123110
console.log("rewrittenLists", rewrittenLists)
124111

125-
let prev = []
126-
let mergedReducedRewrittenLists = []
112+
let prev : RewrittenListBlockAmend[] = []
113+
let mergedReducedRewrittenLists: RewrittenListBlockRebase[] = []
114+
127115
for (const list of rewrittenLists) {
128116
if (list.type === "amend") {
129117
prev.push(list)
@@ -169,7 +157,7 @@ function combineRewrittenLists(rewrittenListFile) {
169157
reducePath(list.mapping)
170158
mergedReducedRewrittenLists.push(list)
171159
} else {
172-
throw new Error(`invalid list type (got "${list.type}")`)
160+
throw new Error(`invalid list type (got "${(list as any).type}")`)
173161
}
174162
}
175163
/**
@@ -181,7 +169,7 @@ function combineRewrittenLists(rewrittenListFile) {
181169
console.log("mergedReducedRewrittenLists", mergedReducedRewrittenLists)
182170

183171
const combinedRewrittenList = Object.entries(mergedReducedRewrittenLists[0].mapping).map(([k, v]) => k + " " + v).join("\n") + "\n"
184-
fs.writeFileSync("rewritten-list", combinedRewrittenList)
172+
// fs.writeFileSync("rewritten-list", combinedRewrittenList)
185173

186174
return {
187175
mergedReducedRewrittenLists,
@@ -198,13 +186,21 @@ if (!module.parent) {
198186

199187
const b4 = Object.keys(mergedReducedRewrittenLists[0].mapping)
200188
const after = Object.values(mergedReducedRewrittenLists[0].mapping)
189+
190+
const path = require("path")
191+
const os = require("os")
192+
const dir = path.join(os.tmpdir(), "gsr-reduce-path")
193+
fs.mkdirSync(dir, { recursive: true })
201194

202-
fs.writeFileSync("b4", b4.join("\n") + "\n")
203-
fs.writeFileSync("after", after.join("\n") + "\n")
195+
const b4path = path.join(dir, "b4")
196+
const afterpath = path.join(dir, "after")
197+
fs.writeFileSync(b4path , b4 .join("\n") + "\n")
198+
fs.writeFileSync(afterpath, after.join("\n") + "\n")
204199

205200
const N = after.length
206201
console.log({ N })
207202

208-
execSync(`git log --pretty=format:"%H" | head -n ${N} | tac - > curr`)
209-
execSync(`diff -us curr after`, { stdio: "inherit" })
203+
const currpath = path.join(dir, "curr")
204+
execSync(`git log --pretty=format:"%H" | head -n ${N} | tac - > ${currpath}`)
205+
execSync(`diff -us ${currpath} ${afterpath}`, { stdio: "inherit" })
210206
}

test/run.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
#!/usr/bin/env ts-node-dev
22

33
import { testCase } from "./experiment.spec";
4+
import reducePathTC from "../reducePath.spec";
45

56
main();
67
function main() {
7-
testCase()
8+
Promise.all([
9+
testCase(), //
10+
reducePathTC(),
11+
])
812
.then(() => process.stdout.write("\nsuccess\n\n"))
913
.catch((e) => {
1014
process.stderr.write("\nfailure: " + e + "\n\n");

0 commit comments

Comments
 (0)