Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #143 - Support parent-less commits in diffs #147

Merged
merged 2 commits into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/git/src/GitDiff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,27 @@ describe('Git', () => {
})
})
})

describe('corner cases', () => {
it('gets diff for parent-less commit', async () => {
const sha = await getBaseCommit()
const git = new Git(dir)

const changes = await git.diff.getByShas(sha)
expect(changes!).toEqual<DiffResult>({
files: [
{
oldName: null,
newName: 'onlyfile',
isDeleted: false,
isModified: false,
isNew: true,
isRenamed: false,
},
],
})
})
})
})

describe('getDiffFromIndex', () => {
Expand Down
64 changes: 61 additions & 3 deletions packages/git/src/GitDiff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,31 @@ export class GitDiff {
}
}

// Just support git syntax here, but it does need converting internally
if (sha?.includes('~')) {
const [childSha] = sha.split('~')
sha = await this.getShaParent(childSha)
}

const fileType = path.extname(filePath)

let plainText = null
if (sha) {
const cmd = ['show', `${sha}:${filePath}`]
plainText = await spawn(cmd)
try {
plainText = await spawn(cmd)
} catch (err) {
if (
/[(' does not exist in ')|( exists on disk, but not in )]/.test(
String(err),
)
) {
// If the file is not in the commit, that's fine, it just didn't exist yet and so is empty
plainText = ''
} else {
throw err
}
}
} else {
const absoluteFilePath = path.join(this._cwd, filePath)
plainText = await new Promise<string>((resolve, reject) => {
Expand Down Expand Up @@ -72,9 +91,12 @@ export class GitDiff {
return null
}

if (!shaOld) {
shaOld = `${shaNew}~1`
// While sha~1 syntax can go back a commit for free, it will error for parent-less commits.
// We look up the parent commit properly so parent-less commits can be compared to the empty tree instead
if (shaOld == null) {
shaOld = await this.getShaParent(shaNew)
}

const cmd = [
'diff',
shaOld,
Expand Down Expand Up @@ -153,4 +175,40 @@ export class GitDiff {
}),
}
}

private emptyTreeShaMemo: string | null = null
private getEmptyTreeSha = async (): Promise<string> => {
const spawn = (await this._getSpawn())!

if (this.emptyTreeShaMemo == null) {
this.emptyTreeShaMemo = await spawn([
'hash-object',
'-t',
'tree',
'/dev/null',
])
this.emptyTreeShaMemo = this.emptyTreeShaMemo.trim()
}

return this.emptyTreeShaMemo
}

private getShaParent = async (sha: string): Promise<string> => {
const spawn = (await this._getSpawn())!

const output = await spawn(['rev-list', '--parents', '-n', '1', sha])

const shas = output.split(/[\s]+/)
if (shas[0] != sha) {
throw `getShaParent failed. Unexpected output: "${output}" for input "${sha}"`
}

if (!!shas[1]) {
// Merge commits will have a 3rd element, but but we always want the mainline parent for diffs
return shas[1]
} else {
// Unparented commits will... not have a parent
return await this.getEmptyTreeSha()
}
}
}
2 changes: 1 addition & 1 deletion packages/giterm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "giterm",
"version": "0.20.0",
"version": "0.20.1",
"description": "A git gui for terminal lovers",
"homepage": "https://github.com/Nick-Lucas/giterm",
"main": "init.js",
Expand Down