Skip to content

Commit

Permalink
working on diff patches
Browse files Browse the repository at this point in the history
  • Loading branch information
PhotoNomad0 committed Nov 5, 2024
1 parent befaf6d commit 9a3f068
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/utilities/gitUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ export async function modifyRepoFile(server: string, owner: string, repo: string
}
}

async function uploadRepoDiffPatchFile(
export async function uploadRepoDiffPatchFile(
server: string,
owner: string,
repo: string,
Expand Down
40 changes: 34 additions & 6 deletions src/utilities/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
checkDataToTn,
checkDataToTwl,
convertJsonToUSFM,
fetchFileFromRepo,
fetchFromUrl,
flattenGroupData,
getBibleBookFolders,
Expand Down Expand Up @@ -47,6 +48,7 @@ import {
squashMergePullRequest,
updatePullRequest,
UploadFileResponse,
uploadRepoDiffPatchFile,
uploadRepoFileFromPath,
} from "./gitUtils";
// @ts-ignore
Expand All @@ -56,6 +58,7 @@ export const mergeToMasterBranch = 'merge_changes_to_master'
export const mergeFromMasterBranch = 'update_from_master'

export const bibleCheckingTopic = 'bible-checking';
let contextLines = 4;

function sortAndRemoveDuplicates(strings: string[]): string[] {
// Convert the list to a Set to remove duplicates
Expand Down Expand Up @@ -115,19 +118,35 @@ export function getNewBranchName(targetLanguageId:string, targetBibleId:string,
return branchName
}

function getContextLines() {
return contextLines;
}

function incrementContextLines() {
contextLines++;
if (contextLines > 7) {
contextLines = 2
}
}

export async function modifyRepoFileFromPath(server: string, owner: string, repo: string, branch: string, uploadPath: string, sourceFilePath: string, token: string, sha: string, downloadAndDiff = false): Promise<ModifyFileResponse> {
let results:UploadFileResponse

if (!downloadAndDiff) {
const content = fs.readFileSync(sourceFilePath, "UTF-8")?.toString()
results = await modifyRepoFile(server, owner, repo, branch, uploadPath, content, token, sha);
} else {
const fileData = await getFileFromBranch(server, owner, repo, branch, uploadPath, token);
// https://git.door43.org/unfoldingWord/en_ult/raw/branch/auto-pjoakes-TIT/manifest.yaml
// const fileData = await getFileFromBranch(server, owner, repo, branch, uploadPath, token);
const importsFolder = path.join(projectsBasePath, 'imports')
const tempFolder = path.join(importsFolder, 'temp')
const fileData = await fetchFileFromRepo(server, owner, repo, branch, tempFolder, uploadPath)
if (fileData?.error) {
// @ts-ignore
return fileData;
}
const dcsContent = fileData?.content
const tempFile = path.join(tempFolder, uploadPath)
const dcsContent = fs.readFileSync(tempFile, "UTF-8")?.toString()
const content = fs.readFileSync(sourceFilePath, "UTF-8")?.toString()
if (dcsContent === content) { // if no change
// nothing to do
Expand All @@ -138,10 +157,17 @@ export async function modifyRepoFileFromPath(server: string, owner: string, repo
}
} else
if (dcsContent) {
const diffPatch = getPatch(uploadPath, dcsContent, content, false, 4)
const diffPatch = getPatch(uploadPath, dcsContent, content, false, getContextLines())
// @ts-ignore
results = await uploadRepoDiffPatchFile(server, owner, repo, branch, uploadPath, diffPatch, fileData.sha, token)
results = await uploadRepoDiffPatchFile(server, owner, repo, branch, uploadPath, diffPatch, sha, token)
results.content = content
if (results?.error) {
incrementContextLines();
const diffPatch = getPatch(uploadPath, dcsContent, content, false, getContextLines())
// @ts-ignore
results = await uploadRepoDiffPatchFile(server, owner, repo, branch, uploadPath, diffPatch, sha, token)
results.content = content
}
} else {
const content = fs.readFileSync(sourceFilePath, "UTF-8")?.toString()
results = await modifyRepoFile(server, owner, repo, branch, uploadPath, content, token, sha);
Expand Down Expand Up @@ -182,8 +208,10 @@ async function deleteTheBranchAndPR(server: string, owner: string, repo: string,

async function updateFilesInBranch(localFiles: string[], localRepoPath: string, handledFiles: NestedObject, uploadedFiles: NestedObject, server: string, owner: string, repo: string, branch: string, token: string): Promise<GeneralObject> {
let changedFiles = 0;
const importsFolder = path.join(projectsBasePath, 'imports')
fs.emptyDirSync(importsFolder)
for (const localFile of localFiles) {
if ((localFile.includes(dcsStatusFile)) || (localFile === ".DS_Store")) { // skip over DCS data file, and system files
if (localFile.includes(dcsStatusFile) || localFile.includes(".DS_Store")) { // skip over DCS data file, and system files
continue;
}

Expand Down Expand Up @@ -214,7 +242,7 @@ async function updateFilesInBranch(localFiles: string[], localRepoPath: string,
} else {
console.log(`updateFilesInBranch - updating changed file ${localFile}`);
const sha = remoteFileData?.sha || "";
results = await modifyRepoFileFromPath(server, owner, repo, branch, localFile, fullFilePath, token, sha, false);
results = await modifyRepoFileFromPath(server, owner, repo, branch, localFile, fullFilePath, token, sha, true);
}

changedFiles++
Expand Down

0 comments on commit 9a3f068

Please sign in to comment.