Skip to content

Commit db53998

Browse files
committed
Add Git helper functions
1 parent 5e475b7 commit db53998

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

src/actions-util.ts

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,111 @@ export const determineBaseBranchHeadCommitOid = async function (
162162
}
163163
};
164164

165+
/**
166+
* Deepen the git history of the given ref by one level. Errors are logged.
167+
*
168+
* This function uses the `checkout_path` to determine the repository path and
169+
* works only when called from `analyze` or `upload-sarif`.
170+
*/
171+
export const deepenGitHistory = async function () {
172+
try {
173+
await runGitCommand(
174+
getOptionalInput("checkout_path"),
175+
["fetch", "--no-tags", "--deepen=1"],
176+
"Cannot deepen the shallow repository.",
177+
);
178+
} catch {
179+
// Errors are already logged by runGitCommand()
180+
}
181+
};
182+
183+
/**
184+
* Fetch the given remote branch. Errors are logged.
185+
*
186+
* This function uses the `checkout_path` to determine the repository path and
187+
* works only when called from `analyze` or `upload-sarif`.
188+
*/
189+
export const gitFetch = async function (branch: string, extraFlags: string[]) {
190+
try {
191+
await runGitCommand(
192+
getOptionalInput("checkout_path"),
193+
["fetch", "--no-tags", ...extraFlags, "origin", `${branch}:${branch}`],
194+
`Cannot fetch ${branch}.`,
195+
);
196+
} catch {
197+
// Errors are already logged by runGitCommand()
198+
}
199+
};
200+
201+
/**
202+
* Compute the all merge bases between the given refs. Returns an empty array
203+
* if no merge base is found, or if there is an error.
204+
*
205+
* This function uses the `checkout_path` to determine the repository path and
206+
* works only when called from `analyze` or `upload-sarif`.
207+
*/
208+
export const getAllGitMergeBases = async function (
209+
refs: string[],
210+
): Promise<string[]> {
211+
try {
212+
const stdout = await runGitCommand(
213+
getOptionalInput("checkout_path"),
214+
["merge-base", "--all", ...refs],
215+
`Cannot get merge base of ${refs}.`,
216+
);
217+
return stdout.trim().split("\n");
218+
} catch {
219+
return [];
220+
}
221+
};
222+
223+
/**
224+
* Compute the diff hunk headers between the two given refs.
225+
*
226+
* This function uses the `checkout_path` to determine the repository path and
227+
* works only when called from `analyze` or `upload-sarif`.
228+
*
229+
* @returns an array of diff hunk headers (one element per line), or undefined
230+
* if the action was not triggered by a pull request, or if the diff could not
231+
* be determined.
232+
*/
233+
export const getGitDiffHunkHeaders = async function (
234+
fromRef: string,
235+
toRef: string,
236+
): Promise<string[] | undefined> {
237+
let stdout = "";
238+
try {
239+
stdout = await runGitCommand(
240+
getOptionalInput("checkout_path"),
241+
[
242+
"-c",
243+
"core.quotePath=false",
244+
"diff",
245+
"--no-renames",
246+
"--irreversible-delete",
247+
"-U0",
248+
fromRef,
249+
toRef,
250+
],
251+
`Cannot get diff from ${fromRef} to ${toRef}.`,
252+
);
253+
} catch {
254+
return undefined;
255+
}
256+
257+
const headers: string[] = [];
258+
for (const line of stdout.split("\n")) {
259+
if (
260+
line.startsWith("--- ") ||
261+
line.startsWith("+++ ") ||
262+
line.startsWith("@@ ")
263+
) {
264+
headers.push(line);
265+
}
266+
}
267+
return headers;
268+
};
269+
165270
/**
166271
* Decode, if necessary, a file path produced by Git. See
167272
* https://git-scm.com/docs/git-config#Documentation/git-config.txt-corequotePath

0 commit comments

Comments
 (0)