-
-
Notifications
You must be signed in to change notification settings - Fork 141
/
diffUtils.ts
44 lines (41 loc) · 1.72 KB
/
diffUtils.ts
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
import * as core from "@actions/core"
import * as path from "path"
// If needed alter diff file to be compatible with working directory
export function alterDiffFile(diffFile: string): string {
let workingDirectory = core.getInput(`working-directory`)
if (workingDirectory) {
const workspace = process.env["GITHUB_WORKSPACE"] || ""
const relativeFile = path.relative(workspace, workingDirectory)
workingDirectory = relativeFile
const diffLines = diffFile.split("\n")
let ignore = false
const filteredDiffLines = []
for (const line of diffLines) {
if (line.startsWith("diff --git")) {
if (line.includes(`a/${workingDirectory}/`)) {
ignore = false
filteredDiffLines.push(line.replace(` a/${workingDirectory}/`, " a/").replace(` b/${workingDirectory}/`, " b/"))
} else {
ignore = true
}
} else {
if (!ignore) {
if (line.startsWith(`--- a/${workingDirectory}/`)) {
filteredDiffLines.push(line.replace(`--- a/${workingDirectory}/`, "--- a/"))
} else if (line.startsWith(`+++ a/${workingDirectory}/`)) {
filteredDiffLines.push(line.replace(`+++ a/${workingDirectory}/`, "+++ a/"))
} else if (line.startsWith(`--- b/${workingDirectory}/`)) {
filteredDiffLines.push(line.replace(`--- b/${workingDirectory}/`, "--- b/"))
} else if (line.startsWith(`+++ b/${workingDirectory}/`)) {
filteredDiffLines.push(line.replace(`+++ b/${workingDirectory}/`, "+++ b/"))
} else {
filteredDiffLines.push(line)
}
}
}
}
// Join the modified lines back into a diff string
diffFile = filteredDiffLines.join("\n")
}
return diffFile
}