-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-time-travel-cli.js
190 lines (167 loc) · 4.95 KB
/
git-time-travel-cli.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
#!/usr/bin/env node
import { exec } from "child_process";
import fs from "fs";
import chalk from "chalk";
import tempfile from "tempfile";
import readline from "readline";
import ora from "ora";
import { isGitRepo } from "./utils/isGitRepo.js";
import { showSignature } from "./utils/signature.js";
import { showHelp } from "./utils/showHelp.js";
import { rewriteGitHistory } from "./utils/rewriteGitHistory.js";
let COMMITS = 5;
let LIMITCHUNKS = 20;
let DEBUG = false;
let ALL = false;
/**
* Determines if the current working directory is inside a Git repository.
*/
isGitRepo();
/**
* Show signature
*/
showSignature();
const args = process.argv.slice(2);
for (let i = 0; i < args.length; i++) {
switch (args[i]) {
case "-c":
case "--commits":
COMMITS = args[i + 1] || COMMITS;
i++;
break;
case "-l":
case "--limit":
LIMITCHUNKS = args[i + 1] || LIMITCHUNKS;
i++;
break;
case "-d":
case "--debug":
DEBUG = true;
break;
case "-a":
case "--all":
ALL = true;
break;
default:
// unknown option
break;
}
}
if (args.includes("-h") || args.includes("--help")) {
showHelp();
process.exit(0);
}
let sh_file = `#!/bin/sh
export FILTER_BRANCH_SQUELCH_WARNING=1
`;
let datefmt = "%cI";
let ITER = 0;
let COLITER = 0;
const COLLECTION = [];
exec(`git log -n1 --pretty=format:"${datefmt}"`, (error, stdout) => {
if (stdout === datefmt) {
datefmt = "%ci";
}
let cmd = `git log -n ${COMMITS} --pretty=format:"${datefmt} | %H | %s"`;
if (ALL) {
cmd = `git log --pretty=format:"${datefmt} | %H | %s"`;
}
exec(cmd, (error, stdout) => {
const tmpfile = tempfile("gitblah-");
fs.writeFileSync(tmpfile, stdout);
const editor = "code";
exec(`${editor} ${tmpfile}`, (error, s) => {
if (error) {
console.error(`Error: ${error}`);
process.exit(1);
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question(
"Please update the commit date in the code editor and press enter to continue...",
() => {
console.log(
`Please wait while we adjust the dates of your commits and enjoy the ${chalk.red.bold(
"TIME TRAVEL..."
)}`
);
const contents = fs.readFileSync(tmpfile, "utf-8");
const commits = contents.split("\n");
let count = 0;
for (const commit of commits) {
count++;
if (count > (ALL ? commits : COMMITS)) break;
const [date, hash, message] = commit.split("|");
const DATE_NO_SPACE = date.trim();
const commitEnv = `
if [ \$GIT_COMMIT = ${hash.trim()} ];
then
export GIT_AUTHOR_DATE="${DATE_NO_SPACE}"
export GIT_COMMITTER_DATE="${DATE_NO_SPACE}"
fi;
`;
ITER++;
if (DEBUG && ITER % LIMITCHUNKS === LIMITCHUNKS - 1) {
console.log(`Chunk ${COLITER} Finished`);
}
if (ITER % LIMITCHUNKS === 0) {
COLITER++;
if (DEBUG) {
console.log(`Chunk ${COLITER} Started`);
}
}
COLLECTION[COLITER] = (COLLECTION[COLITER] || "") + commitEnv;
if (DEBUG) {
console.log(`Commit ${ITER}/${commits.length} Collected`);
}
}
let spinner;
for (let i = 0; i < COLLECTION.length; i++) {
const each = COLLECTION[i];
let cmd = "";
if (ALL) {
cmd = `git filter-branch -f --env-filter '${each}' -- --all`;
} else {
cmd = `git filter-branch -f --env-filter '${each}' HEAD~${COMMITS}..HEAD`;
}
sh_file += `${cmd}\n`;
fs.appendFileSync(`filter_branch_commands_${i}.sh`, sh_file);
sh_file = `#!/bin/sh
export FILTER_BRANCH_SQUELCH_WARNING=1
`;
if (DEBUG) {
spinner = ora(
`Chunk ${i + 1}/${COLLECTION.length} Started...`
).start();
rewriteGitHistory(
`filter_branch_commands_${i}.sh`,
i,
COLLECTION.length,
spinner
);
} else {
spinner = ora(
`Chunk ${i + 1}/${COLLECTION.length} Started...`
).start();
rewriteGitHistory(
`filter_branch_commands_${i}.sh`,
i,
COLLECTION.length,
spinner
);
}
}
spinner.succeed(
chalk.green.bold(
"Git commit dates have been adjusted. To push your changes, do 'git push -f BRANCH NAME'."
)
);
rl.close();
process.exit(1);
}
);
});
});
});