-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit_actions.js
executable file
·134 lines (110 loc) · 3.74 KB
/
git_actions.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
#!/usr/bin/env node
const exec = require("child_process").exec;
const args = process.argv.slice(2);
const commands = {
// Review the changes made to a specific file since the last shared commit
// of the current branch and the branch we're rebasing
rebaseOnto: (branch) => {
exec(
`git rebase --onto=${branch} $(git merge-base ${branch} HEAD)`,
(err, res) => {
if (err) throw err;
const matches = res.match(
/You are currently rebasing branch '([^']*)'/
);
if (!matches) {
process.stdout.write('echo "Not currently rebasing"');
return;
}
const finalCommand = `git log -p $(git merge-base ${matches[1]} HEAD)..HEAD ${file}`;
process.stdout.write(finalCommand);
}
);
},
// View all changes to the specified file since the current branch and the
// rebasing branch have diverged
reviewMergeFile: (file) => {
exec("git status", (err, res) => {
if (err) throw err;
const matches = res.match(/You are currently rebasing branch '([^']*)'/);
if (!matches) {
process.stdout.write('echo "Not currently rebasing"');
return;
}
const finalCommand = `git log -p $(git merge-base ${matches[1]} HEAD)..HEAD ${file}`;
process.stdout.write(finalCommand);
});
},
searchBlame: (searchTerm) => {
exec(
`git grep -n "${searchTerm}" | while IFS=: read i j k; do git blame -f -L $j,$j $i; done`,
(err, res) => {
if (err) throw err;
const formattedResponse = `* ${res.split("\n").join(`\n* `)}`;
return process.stdout.write(formattedResponse);
}
);
},
pushNewBranch: () => {
exec("git changes");
exec(`git symbolic-ref --short HEAD`, (err, res) => {
if (err) throw err;
const branch = res.trim();
exec(`git push -u origin ${branch} --no-verify`, (err) => {
if (err) throw err;
exec(`git push`, (err) => {
if (err) throw err;
return process.stdout.write(`Pushy push complete.\n`);
});
return process.stdout.write(`Pushed branch ${branch}\n`);
});
return process.stdout.write(`Pushing ${branch}\n`);
});
},
deleteMergedBranches: (masterBranch) => {
masterBranch = masterBranch === "sh" ? "master" : masterBranch;
process.stdout.write(`${masterBranch} is master\n`);
exec(`git branch`, async (err, res) => {
if (err) throw err;
const branches = res.split("\n");
let deletedBranches = 0;
await Promise.all(
branches.map((br, index) => {
return new Promise((resolve, reject) => {
const branch = br.trim();
if (!branch) {
resolve();
return;
}
if (branch.indexOf("*") >= 0) {
process.stdout.write(`Skipping active branch ${branch}\n`);
resolve();
} else {
exec(
`git branch ${masterBranch} --contains=${branch}`,
(err, res) => {
if (err) throw err;
if (res.indexOf(masterBranch) >= 0) {
exec(`git branch -D ${branch}`, (err, res) => {
if (err) throw err;
process.stdout.write(`Deleted branch: ${branch}\n`);
deletedBranches++;
resolve();
});
} else {
resolve();
}
}
);
}
});
})
);
return process.stdout.write(`Deleted ${deletedBranches} branches\n`);
});
},
};
if (typeof commands[args[0]] === "function") {
const [functionName, ...rest] = args;
commands[functionName](...rest);
}