This repository was archived by the owner on May 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgit.js
More file actions
135 lines (123 loc) · 3.14 KB
/
Copy pathgit.js
File metadata and controls
135 lines (123 loc) · 3.14 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
import child_process from "child_process";
import {
printEmptyLine,
printFailure,
printSuggestion,
setPrintToStdErr,
setPrintToStdOut,
} from "./print";
import { isTestMode } from "../tests/test-utils";
/**
* Executes a git command with the given args in that order
* @param {string[]} args
*/
export function executeGitCommand(args) {
try {
const resp = child_process.execSync(`git ${args.join(" ")}`, {
encoding: "utf8",
});
return resp;
} catch (err) {
// console.debug(err)
return null;
}
}
/**
* Gets the git directory
* @returns the directory location or null if there isn't one
*/
export function getGitDirectory() {
// if we're in test mode we'll skip this check
if (isTestMode) return "./tests/fixtures";
// now look for a git repository
const rootDirectory = executeGitCommand(["rev-parse", "--show-toplevel"]);
if (rootDirectory) {
return rootDirectory.split("\n").join("");
} else {
return null;
}
}
/**
* Gets the git directory
* @returns the directory location or exits if there isn't one
*/
export function getGitDirectoryRequired() {
// if we're in test mode we'll skip this check
if (isTestMode) return "./tests/fixtures";
// now look for a git repository
const rootDirectory = executeGitCommand(["rev-parse", "--show-toplevel"]);
if (rootDirectory) {
return rootDirectory.split("\n").join("");
} else {
setPrintToStdErr();
printEmptyLine();
printFailure(
"Unable to execute a git command because you're not in a git repository"
);
printEmptyLine();
setPrintToStdOut();
process.exit(1);
}
}
/**
* Gets the current git branch
* @returns {string?} branch
*/
export function getCurrentBranch() {
const currentBranch = executeGitCommand([
"rev-parse",
"--abbrev-ref",
"HEAD",
]);
if (currentBranch) {
return currentBranch.split("\n").join("");
} else {
return null;
}
}
/**
* Gets the HEAD git branch
* @returns {string?} branch
*/
export function getMainBranch() {
const output = executeGitCommand(["remote", "show", "origin"]);
let branch = null;
if (output) {
const splitOutput = output.split("\n");
for (let i = 0; i < splitOutput.length; i++) {
const line = splitOutput[i].replace(/\n/g, "");
// we've found our targeted branch
if (line.includes("HEAD branch:")) {
// extract the branch name now
const temp = line.split(":").map((x) => x.trim());
branch = temp.length > 0 ? temp[1] : null;
break;
}
}
}
return branch;
}
export function findClosestSha() {
if (isTestMode) return null;
const currentBranch = getCurrentBranch();
const mainBranch = getMainBranch();
let closestSha = null;
if (!currentBranch || !mainBranch) {
console.debug("cannot find the closest SHA (issue when finding branches)");
return null;
}
const output = executeGitCommand([
"merge-base",
"-a",
mainBranch,
currentBranch,
]);
if (output) {
closestSha = output.replace(/\n/g, "").trim();
printSuggestion(
`Closest SHA found between ${currentBranch} and ${mainBranch}`,
closestSha
);
}
return closestSha;
}