-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mateusz Duda
committed
May 22, 2024
1 parent
7c35981
commit c1ec1e7
Showing
6 changed files
with
196 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -106,7 +106,6 @@ export class GitRepository { | |
} | ||
|
||
public getFileDataUsingNativeGitCommand(commit: Commit): FileData[] { | ||
|
||
/** | ||
* Has the following format: | ||
* M src/Git/GitRepository.ts | ||
|
@@ -134,8 +133,14 @@ export class GitRepository { | |
`cd ${this._root} && git update-index && git diff-tree --no-commit-id --numstat -r ${commit.sha()}` | ||
).toString().trim().split('\n'); | ||
|
||
if (nameStatusOutput.length !== numstatOutput.length) { | ||
throw new Error(`Output of git-diff --name-status does not match one for --no-commit-id for commit ${commit.sha()}`); | ||
if (nameStatusOutput.length === 0) { | ||
console.debug(nameStatusOutput); | ||
throw new Error(`Output of git-diff --name-status is empty for commit ${commit.sha()}`); | ||
} | ||
|
||
if (numstatOutput.length === 0) { | ||
console.debug(numstatOutput); | ||
throw new Error(`Output of git-diff --numstat is empty for commit ${commit.sha()}`); | ||
} | ||
|
||
const statusesToGitDeltaTypeMap = new Map<string, GitDeltaType>([ | ||
|
@@ -154,19 +159,27 @@ export class GitRepository { | |
|
||
const fileDataArray: FileData[] = []; | ||
|
||
numstatOutput.forEach((numStatLine: string, i: number) => { | ||
nameStatusOutput.forEach((nameStatusLine: string, i: number) => { | ||
const [changeType, relatedFilePath, optionalRenamedPath] = nameStatusLine.split('\t'); | ||
|
||
const numStatLine = numstatOutput.find(output => output.includes(relatedFilePath)); | ||
|
||
if (!numStatLine) { | ||
throw new Error(`No matching git-diff --name-status for file ${relatedFilePath}, output is ${nameStatusOutput}`); | ||
} | ||
|
||
const [linesAdded, linesRemoved, filePath] = numStatLine.split('\t'); | ||
|
||
const [changeType, relatedFilePath, optionalRenamedPath] = nameStatusOutput[i].split('\t'); | ||
let ourChangeType = statusesToGitDeltaTypeMap.get(changeType[0]) || GitDeltaType.UNREADABLE; | ||
|
||
if (filePath !== relatedFilePath) { | ||
throw new Error(`Error while processing git-diff: File path '${filePath}' does not match one of '${relatedFilePath}'`); | ||
if (optionalRenamedPath) { | ||
ourChangeType = GitDeltaType.RENAMED; | ||
} | ||
|
||
fileDataArray.push({ | ||
oldPath: filePath, | ||
newPath: optionalRenamedPath || filePath, | ||
change: statusesToGitDeltaTypeMap.get(changeType[0]) || GitDeltaType.UNREADABLE, | ||
change: ourChangeType, | ||
linesAdded: parseInt(linesAdded), | ||
linesRemoved: parseInt(linesRemoved), | ||
commitedIn: commit | ||
|
@@ -251,27 +264,12 @@ export class GitRepository { | |
|
||
// Mostly from https://github.com/nodegit/nodegit/blob/master/examples/add-and-commit.js | ||
public async commitFiles(commitMessage: string, filePaths: string[]): Promise<Oid> { | ||
const { execSync } = require('child_process'); | ||
|
||
const repository = await this._getRepository(); | ||
// const index = await repository.refreshIndex(); | ||
|
||
// for (const filePath of filePaths) { | ||
// await index.addByPath(filePath); | ||
// } | ||
|
||
// await index.write(); | ||
|
||
// const oid = await index.writeTree(); | ||
|
||
// const parent = await repository.getHeadCommit(); | ||
const author = Signature.now("Scott Chacon", "[email protected]"); | ||
const committer = Signature.now("Scott A Chacon", "[email protected]"); | ||
|
||
const commitId = await repository.createCommitOnHead(filePaths, author, committer, commitMessage); | ||
|
||
repository.createCommitOnHead | ||
|
||
return commitId; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { GitRepository } from "../../src/Git/GitRepository"; | ||
import { GitDeltaType } from "../../src/Git/Types"; | ||
import { FileTagsDatabase } from "../../src/Scope/FileTagsDatabase"; | ||
import { cloneMockRepositoryToFolder, commitFilesUsingGitNatively, deleteFiles, makeUniqueFolderForTest, renameFiles } from "../utils/utils"; | ||
|
||
describe("Database file", () => { | ||
it("After files are deleted, the information about them is purged from database", async () => { | ||
const FOLDER_PATH = makeUniqueFolderForTest(); | ||
const REPO_PATH = cloneMockRepositoryToFolder(FOLDER_PATH); | ||
|
||
const savedDatabase = new FileTagsDatabase(REPO_PATH); | ||
|
||
savedDatabase.load(); | ||
|
||
const filesToDelete = [ | ||
"src/tagged-file.js", | ||
"src/file-ignored-by-database.js" | ||
]; | ||
|
||
deleteFiles(filesToDelete, REPO_PATH); | ||
commitFilesUsingGitNatively(filesToDelete, REPO_PATH, "delete commit"); | ||
|
||
const repository = new GitRepository(REPO_PATH); | ||
|
||
const unpushedCommits = await repository.getUnpushedCommits(); | ||
expect(unpushedCommits.length).toBe(1); | ||
|
||
const commit = unpushedCommits[0]; | ||
|
||
const fileDataArray = await repository.getFileDataForCommits([commit], true); | ||
|
||
expect(fileDataArray.length).toBe(2); | ||
|
||
expect(fileDataArray[0].change).toBe(GitDeltaType.DELETED); | ||
expect(fileDataArray[1].change).toBe(GitDeltaType.DELETED); | ||
|
||
const newDatabase = new FileTagsDatabase(REPO_PATH); | ||
|
||
newDatabase.load(); | ||
newDatabase.updateDatabaseBasedOnChanges(fileDataArray); | ||
|
||
// Check if the files were deleted | ||
|
||
expect(savedDatabase.isFileInDatabase("src/tagged-file.js")).toBe(true); | ||
expect(newDatabase.isFileInDatabase("src/tagged-file.js")).toBe(false); | ||
|
||
expect(savedDatabase.isIgnored("src/file-ignored-by-database.js")).toBe(true); | ||
expect(newDatabase.isIgnored("src/file-ignored-by-database.js")).toBe(false); | ||
|
||
expect(newDatabase.fileCount).toBe(savedDatabase.fileCount - 1); | ||
expect(newDatabase.ignoredFilesCount).toBe(savedDatabase.ignoredFilesCount - 1); | ||
}); | ||
|
||
it("After files are renamed, the information about them is purged from database", async () => { | ||
const FOLDER_PATH = makeUniqueFolderForTest(); | ||
const REPO_PATH = cloneMockRepositoryToFolder(FOLDER_PATH); | ||
|
||
const savedDatabase = new FileTagsDatabase(REPO_PATH); | ||
|
||
savedDatabase.load(); | ||
|
||
const filesToRename = [ | ||
["src/tagged-file.js", "src/tagged-file-renamed.js"], | ||
["src/file-ignored-by-database.js", "src/file-ignored-by-database-renamed.js"], | ||
]; | ||
|
||
renameFiles(filesToRename, REPO_PATH); | ||
commitFilesUsingGitNatively(filesToRename.map(files => files[1]), REPO_PATH, "rename commit"); | ||
|
||
const repository = new GitRepository(REPO_PATH); | ||
|
||
const unpushedCommits = await repository.getUnpushedCommits(); | ||
expect(unpushedCommits.length).toBe(1); | ||
|
||
const commit = unpushedCommits[0]; | ||
|
||
const fileDataArray = await repository.getFileDataForCommits([commit], true); | ||
|
||
expect(fileDataArray.length).toBe(2); | ||
|
||
expect(fileDataArray[0].change).toBe(GitDeltaType.RENAMED); | ||
expect(fileDataArray[1].change).toBe(GitDeltaType.RENAMED); | ||
|
||
const newDatabase = new FileTagsDatabase(REPO_PATH); | ||
|
||
newDatabase.load(); | ||
newDatabase.updateDatabaseBasedOnChanges(fileDataArray); | ||
|
||
// Check if the files were deleted | ||
|
||
expect(savedDatabase.isFileInDatabase("src/tagged-file.js")).toBe(true); | ||
expect(savedDatabase.isFileInDatabase("src/tagged-file-renamed.js")).toBe(false); | ||
expect(newDatabase.isFileInDatabase("src/tagged-file.js")).toBe(false); | ||
expect(newDatabase.isFileInDatabase("src/tagged-file-renamed.js")).toBe(true); | ||
|
||
expect(savedDatabase.isIgnored("src/file-ignored-by-database.js")).toBe(true); | ||
expect(savedDatabase.isIgnored("src/file-ignored-by-database-renamed.js")).toBe(false); | ||
expect(newDatabase.isIgnored("src/file-ignored-by-database.js")).toBe(false); | ||
expect(newDatabase.isIgnored("src/file-ignored-by-database-renamed.js")).toBe(true); | ||
|
||
expect(newDatabase.fileCount).toBe(savedDatabase.fileCount); | ||
expect(newDatabase.ignoredFilesCount).toBe(savedDatabase.ignoredFilesCount); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters