Skip to content

Commit

Permalink
fix: reduce log size to just last commit (#1243)
Browse files Browse the repository at this point in the history
* fix: reduce log size to just last commit

* feat: defaults maxCount in getGitLog to 1

* chore(GitFileSystemService): add validation tests for getGitLog
  • Loading branch information
timotheeg authored Mar 28, 2024
1 parent af8649f commit d979502
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/services/db/GitFileSystemService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,13 +453,22 @@ export default class GitFileSystemService {
// Get the Git log of a particular branch
getGitLog(
repoName: string,
branchName: string
branchName: string,
maxCount = 1
): ResultAsync<LogResult<DefaultLogFields>, GitFileSystemError> {
const efsVolPath = this.getEfsVolPathFromBranch(branchName)

if (!Number.isInteger(maxCount) || maxCount < 1) {
return errAsync(
new GitFileSystemError(`Invalid maxCount value supplied: ${maxCount}`)
)
}

return ResultAsync.fromPromise(
this.git
.cwd({ path: `${efsVolPath}/${repoName}`, root: false })
.log([branchName]),
.log([`--max-count=${maxCount}`, branchName]),

(error) => {
logger.error(
`Error when getting Git log of "${branchName}" branch: ${error}, when trying to access ${efsVolPath}/${repoName} for ${branchName}`
Expand Down Expand Up @@ -1763,13 +1772,12 @@ export default class GitFileSystemService {
branchName: string
): ResultAsync<GitHubCommitData, GitFileSystemError> {
return this.isLocalBranchPresent(repoName, branchName)
.andThen((isBranchLocallyPresent) => {
if (isBranchLocallyPresent) {
return this.getGitLog(repoName, branchName)
}

return this.getGitLog(repoName, `origin/${branchName}`)
})
.andThen((isBranchLocallyPresent) =>
this.getGitLog(
repoName,
isBranchLocallyPresent ? branchName : `origin/${branchName}`
)
)
.andThen((logSummary) => {
const possibleCommit = logSummary.latest
if (this.isDefaultLogFields(possibleCommit)) {
Expand Down
28 changes: 28 additions & 0 deletions src/services/db/__tests__/GitFileSystemService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,34 @@ describe("GitFileSystemService", () => {

expect(result._unsafeUnwrapErr()).toBeInstanceOf(GitFileSystemError)
})

it("should return GitFileSystemError if maxCount is supplied as a float", async () => {
const result = await GitFileSystemService.getGitLog(
"fake-repo",
"fake-commit-sha",
1.1 // float value
)

expect(result._unsafeUnwrapErr()).toBeInstanceOf(GitFileSystemError)
})

it("should return GitFileSystemError if a maxCount less than 1 is supplied", async () => {
const result1 = await GitFileSystemService.getGitLog(
"fake-repo",
"fake-commit-sha",
0
)

expect(result1._unsafeUnwrapErr()).toBeInstanceOf(GitFileSystemError)

const result2 = await GitFileSystemService.getGitLog(
"fake-repo",
"fake-commit-sha",
-1
)

expect(result2._unsafeUnwrapErr()).toBeInstanceOf(GitFileSystemError)
})
})

describe("rollback", () => {
Expand Down

0 comments on commit d979502

Please sign in to comment.