Skip to content

Commit

Permalink
I like it better without async
Browse files Browse the repository at this point in the history
  • Loading branch information
niik committed Nov 27, 2024
1 parent 9d3eb66 commit 09037ec
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 38 deletions.
10 changes: 10 additions & 0 deletions app/src/lib/git/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,16 @@ export class GitError extends Error {
}
}

export const isGitError = (
e: unknown,
parsedError?: DugiteError
): e is GitError => {
return (
e instanceof GitError &&
(parsedError === undefined || e.result.gitError === parsedError)
)
}

/**
* Shell out to git with the given arguments, at the given path.
*
Expand Down
65 changes: 27 additions & 38 deletions app/src/lib/git/merge-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,46 +41,35 @@ export async function determineMergeability(
ours: Branch,
theirs: Branch
) {
try {
const { stdout, exitCode } = await git(
[
'merge-tree',
'--write-tree',
'--name-only',
'--no-messages',
'-z',
ours.tip.sha,
theirs.tip.sha,
],
repository.path,
'determineMergeability',
{ successExitCodes: new Set([0, 1]) }
return git(
[
'merge-tree',
'--write-tree',
'--name-only',
'--no-messages',
'-z',
ours.tip.sha,
theirs.tip.sha,
],
repository.path,
'determineMergeability',
{ successExitCodes: new Set([0, 1]) }
)
.then<MergeTreeResult>(({ stdout }) => {
// The output will be "<tree-id>\0[<filename>\0]*" so we can get the
// number of conflicted files by counting the number of null bytes and
// subtracting one for the tree id.
const conflictedFiles = (stdout.match(/\0/g)?.length ?? 0) - 1
return conflictedFiles > 0
? { kind: ComputedAction.Conflicts, conflictedFiles }
: { kind: ComputedAction.Clean }
})
.catch<MergeTreeResult>(e =>
isGitError(e, GitError.CannotMergeUnrelatedHistories)
? Promise.resolve({ kind: ComputedAction.Invalid })
: Promise.reject(e)
)

if (exitCode === 0) {
return { kind: ComputedAction.Clean }
}

// The output will be "<tree-id>\0[<filename>\0]*" so we can get the
// number of conflicted files by counting the number of null bytes and
// subtracting one for the tree id
const nulls = stdout.match(/\0/g)?.length ?? 0
const conflictedFiles = nulls - 1

return conflictedFiles > 0
? { kind: ComputedAction.Conflicts, conflictedFiles }
: { kind: ComputedAction.Clean }
} catch (e) {
if (
e instanceof GitError &&
e.result.gitError === DugiteError.CannotMergeUnrelatedHistories
) {
return { kind: ComputedAction.Invalid }
}

throw e
}
}

export function parseMergeTreeResult(stream: NodeJS.ReadableStream) {
return new Promise<MergeTreeResult>(resolve => {
Expand Down

0 comments on commit 09037ec

Please sign in to comment.