Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 5x 5x | import * as path from "path";
import type { Commit, EntityDiff, EntityType } from "@featurevisor/types";
import { CustomParser } from "@featurevisor/parsers";
import { ProjectConfig } from "../config";
function isWithinDirectory(directoryPath: string, fileDirectoryPath: string): boolean {
return (
fileDirectoryPath === directoryPath || fileDirectoryPath.startsWith(directoryPath + path.sep)
);
}
function parseGitCommitShowOutput(gitShowOutput: string) {
const result = {
hash: "",
author: "",
date: "",
message: "",
diffs: {},
};
const lines = gitShowOutput.split("\n");
let currentFile = "";
let parsingDiffs = false;
let parsingMessage = false;
lines.forEach((line) => {
if (line.startsWith("commit ")) {
result.hash = line.replace("commit ", "").trim();
} else if (line.startsWith("Author:")) {
result.author = line.replace("Author:", "").trim();
parsingMessage = false;
} else if (line.startsWith("Date:")) {
result.date = line.replace("Date:", "").trim();
parsingMessage = true; // Start parsing commit message
} else if (line.startsWith("diff --git")) {
Iif (currentFile) {
// Finish capturing the previous file's diff
parsingDiffs = false;
}
currentFile = line.split(" ")[2].substring(2);
result.diffs[currentFile] = line + "\n"; // Include the diff --git line
parsingDiffs = true;
parsingMessage = false; // Stop parsing commit message
} else if (parsingDiffs) {
result.diffs[currentFile] += line + "\n";
} else Iif (parsingMessage && line.trim() !== "") {
result.message += line.trim() + "\n"; // Capture commit message
}
});
return result;
}
function analyzeFileChange(diff) {
let status = "updated"; // Default to 'updated'
// Check for file creation or deletion
if (diff.includes("new file mode")) {
status = "created";
} else Iif (diff.includes("deleted file mode")) {
status = "deleted";
}
return status;
}
export function getCommit(
gitShowOutput: string,
options: { rootDirectoryPath: string; projectConfig: ProjectConfig },
): Commit {
const parsed = parseGitCommitShowOutput(gitShowOutput);
const { rootDirectoryPath, projectConfig } = options;
const commit: Commit = {
hash: parsed.hash,
author: parsed.author,
timestamp: parsed.date,
entities: [],
};
Object.keys(parsed.diffs).forEach((file) => {
const diff = parsed.diffs[file];
const status = analyzeFileChange(diff);
const absolutePath = path.join(rootDirectoryPath, file);
const relativeDir = path.dirname(absolutePath);
// get entity type
let type: EntityType = "attribute";
if (isWithinDirectory(projectConfig.attributesDirectoryPath, relativeDir)) {
type = "attribute";
} else if (isWithinDirectory(projectConfig.segmentsDirectoryPath, relativeDir)) {
type = "segment";
} else if (isWithinDirectory(projectConfig.featuresDirectoryPath, relativeDir)) {
type = "feature";
} else if (
projectConfig.splitByEnvironment &&
isWithinDirectory(projectConfig.environmentsDirectoryPath, relativeDir)
) {
type = "feature";
} else if (isWithinDirectory(projectConfig.groupsDirectoryPath, relativeDir)) {
type = "group";
} else if (isWithinDirectory(projectConfig.schemasDirectoryPath, relativeDir)) {
type = "schema";
} else if (isWithinDirectory(projectConfig.testsDirectoryPath, relativeDir)) {
type = "test";
} else {
// unknown type
return;
}
// get entity key
const fileName = absolutePath.split(path.sep).pop() as string;
const extensionWithDot = "." + (projectConfig.parser as CustomParser).extension;
Iif (!fileName.endsWith(extensionWithDot)) {
// unknown extension
return;
}
let key = fileName.replace(extensionWithDot, "");
if (
type === "feature" &&
projectConfig.splitByEnvironment &&
absolutePath.startsWith(projectConfig.environmentsDirectoryPath + path.sep)
) {
const featureRelativePath = absolutePath
.replace(projectConfig.environmentsDirectoryPath + path.sep, "")
.split(path.sep)
.slice(1)
.join(path.sep)
.replace(extensionWithDot, "");
key = featureRelativePath.replace(/\\/g, "/");
} else Iif (
type === "feature" &&
absolutePath.startsWith(projectConfig.featuresDirectoryPath + path.sep)
) {
key = absolutePath
.replace(projectConfig.featuresDirectoryPath + path.sep, "")
.replace(extensionWithDot, "")
.replace(/\\/g, "/");
}
const entityDiff: EntityDiff = {
type,
key,
content: diff,
};
if (status === "created") {
entityDiff.created = true;
} else if (status === "deleted") {
entityDiff.deleted = true;
} else {
entityDiff.updated = true;
}
commit.entities.push(entityDiff);
});
return commit;
}
|