Skip to content

Commit

Permalink
➕ Add Support for Short SHAs
Browse files Browse the repository at this point in the history
  • Loading branch information
say25 committed Jan 21, 2019
1 parent a229d89 commit 0fde421
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 20 deletions.
17 changes: 10 additions & 7 deletions app/src/lib/git/for-each-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export async function getBranches(
'%(refname:short)',
'%(upstream:short)',
'%(objectname)', // SHA
'%(objectname:short)', // short SHA
'%(author)',
'%(committer)',
'%(parent)', // parent SHAs
Expand Down Expand Up @@ -76,29 +77,31 @@ export async function getBranches(
const name = pieces[1]
const upstream = pieces[2]
const sha = pieces[3]
const shortSha = pieces[4]

const authorIdentity = pieces[4]
const authorIdentity = pieces[5]
const author = CommitIdentity.parseIdentity(authorIdentity)

if (!author) {
throw new Error(`Couldn't parse author identity ${authorIdentity}`)
}

const committerIdentity = pieces[5]
const committerIdentity = pieces[6]
const committer = CommitIdentity.parseIdentity(committerIdentity)

if (!committer) {
throw new Error(`Couldn't parse committer identity ${committerIdentity}`)
}

const parentSHAs = pieces[6].split(' ')
const symref = pieces[7]
const summary = pieces[8]
const body = pieces[9]
const trailers = parseRawUnfoldedTrailers(pieces[10], trailerSeparators)
const parentSHAs = pieces[7].split(' ')
const symref = pieces[8]
const summary = pieces[9]
const body = pieces[10]
const trailers = parseRawUnfoldedTrailers(pieces[11], trailerSeparators)

const tip = new Commit(
sha,
shortSha,
summary,
body,
author,
Expand Down
15 changes: 9 additions & 6 deletions app/src/lib/git/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export async function getCommits(
const delimiterString = String.fromCharCode(parseInt(delimiter, 16))
const prettyFormat = [
'%H', // SHA
'%h', // short SHA
'%s', // summary
'%b', // body
// author identity string, matching format of GIT_AUTHOR_IDENT.
Expand Down Expand Up @@ -117,14 +118,15 @@ export async function getCommits(
const commits = lines.map(line => {
const pieces = line.split(delimiterString)
const sha = pieces[0]
const summary = pieces[1]
const body = pieces[2]
const authorIdentity = pieces[3]
const committerIdentity = pieces[4]
const shaList = pieces[5]
const shortSha = pieces[1]
const summary = pieces[2]
const body = pieces[3]
const authorIdentity = pieces[4]
const committerIdentity = pieces[5]
const shaList = pieces[6]

const parentSHAs = shaList.length ? shaList.split(' ') : []
const trailers = parseRawUnfoldedTrailers(pieces[6], trailerSeparators)
const trailers = parseRawUnfoldedTrailers(pieces[7], trailerSeparators)

const author = CommitIdentity.parseIdentity(authorIdentity)

Expand All @@ -140,6 +142,7 @@ export async function getCommits(

return new Commit(
sha,
shortSha,
summary,
body,
author,
Expand Down
2 changes: 2 additions & 0 deletions app/src/models/commit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export class Commit {

/**
* @param sha The commit's SHA.
* @param shortSha The commit's shortSHA.
* @param summary The first line of the commit message.
* @param body The commit message without the first line and CR.
* @param author Information about the author of this commit.
Expand All @@ -65,6 +66,7 @@ export class Commit {
*/
public constructor(
public readonly sha: string,
public readonly shortSha: string,
public readonly summary: string,
public readonly body: string,
public readonly author: CommitIdentity,
Expand Down
2 changes: 1 addition & 1 deletion app/src/ui/history/commit-summary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ export class CommitSummary extends React.Component<
const fileCount = this.props.files.length
const filesPlural = fileCount === 1 ? 'file' : 'files'
const filesDescription = `${fileCount} changed ${filesPlural}`
const shortSHA = this.props.commit.sha.slice(0, 7)
const shortSHA = this.props.commit.shortSha

const className = classNames({
expanded: this.props.isExpanded,
Expand Down
1 change: 1 addition & 0 deletions app/src/ui/lib/configure-git-user.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export class ConfigureGitUser extends React.Component<
)
const dummyCommit = new Commit(
this.state.name,
this.state.name.slice(0, 7),
'Fix all the things',
'',
author,
Expand Down
1 change: 1 addition & 0 deletions app/test/unit/create-branch-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const stubAuthor = {

const stubTip = {
sha: 'deadbeef',
shortSha: 'dead',
summary: 'some commit',
body: '',
coAuthors: [],
Expand Down
1 change: 1 addition & 0 deletions app/test/unit/git/branch-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ describe('git/branch', () => {
expect(onBranch.branch.tip.sha).toEqual(
'dfa96676b65e1c0ed43ca25492252a5e384c8efd'
)
expect(onBranch.branch.tip.shortSha).toEqual('dfa9667')
})

it('returns non-origin remote', async () => {
Expand Down
1 change: 1 addition & 0 deletions app/test/unit/git/checkout-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('git/checkout', () => {
type: BranchType.Local,
tip: {
sha: '',
shortSha: '',
summary: '',
body: '',
author: {
Expand Down
12 changes: 6 additions & 6 deletions app/test/unit/git/commit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe('git/commit', () => {
expect(commit).not.toBeNull()
expect(commit!.summary).toEqual('Special commit')
expect(commit!.body).toEqual('# this is a comment\n')
expect(commit!.sha.substring(0, 7)).toEqual(sha)
expect(commit!.shortSha).toEqual(sha)
})

it('can commit for empty repository', async () => {
Expand Down Expand Up @@ -188,7 +188,7 @@ describe('git/commit', () => {
const newTip = (await getCommits(repository!, 'HEAD', 1))[0]
expect(newTip.sha).not.toEqual(previousTip.sha)
expect(newTip.summary).toEqual('title')
expect(newTip.sha.substring(0, 7)).toEqual(sha)
expect(newTip.shortSha).toEqual(sha)

// verify that the contents of this new commit are just the new file
const changedFiles = await getChangedFiles(repository!, newTip.sha)
Expand Down Expand Up @@ -295,7 +295,7 @@ describe('git/commit', () => {
const newTip = (await getCommits(repository!, 'HEAD', 1))[0]
expect(newTip.sha).not.toEqual(previousTip.sha)
expect(newTip.summary).toEqual('title')
expect(newTip.sha.substring(0, 7)).toEqual(sha)
expect(newTip.shortSha).toEqual(sha)

// verify that the contents of this new commit are just the modified file
const changedFiles = await getChangedFiles(repository!, newTip.sha)
Expand Down Expand Up @@ -341,7 +341,7 @@ describe('git/commit', () => {
const newTip = (await getCommits(repository!, 'HEAD', 1))[0]
expect(newTip.sha).not.toEqual(previousTip.sha)
expect(newTip.summary).toEqual('title')
expect(newTip.sha.substring(0, 7)).toEqual(sha)
expect(newTip.shortSha).toEqual(sha)

// verify that the contents of this new commit are just the modified file
const changedFiles = await getChangedFiles(repository!, newTip.sha)
Expand Down Expand Up @@ -512,7 +512,7 @@ describe('git/commit', () => {

const commits = await getCommits(repo, 'HEAD', 5)
expect(commits[0].parentSHAs.length).toEqual(2)
expect(commits[0]!.sha.substring(0, 7)).toEqual(sha)
expect(commits[0]!.shortSha).toEqual(sha)
})
})

Expand Down Expand Up @@ -675,7 +675,7 @@ describe('git/commit', () => {
const commit = await getCommit(repo, 'HEAD')
expect(commit).not.toBeNull()
expect(commit!.summary).toEqual('commit again!')
expect(commit!.sha.substring(0, 7)).toEqual(sha)
expect(commit!.shortSha).toEqual(sha)
})
})
})
3 changes: 3 additions & 0 deletions app/test/unit/git/for-each-ref-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe('git/for-each-ref', () => {
expect(commitWithBody.tip.sha).toBe(
'dfa96676b65e1c0ed43ca25492252a5e384c8efd'
)
expect(commitWithBody.tip.shortSha).toBe('dfa9667')
expect(commitWithBody.tip.summary).toBe('this is a commit title')
expect(commitWithBody.tip.body).toContain('lucky last')
expect(commitWithBody.tip.parentSHAs).toHaveLength(1)
Expand All @@ -39,6 +40,7 @@ describe('git/for-each-ref', () => {
expect(commitNoBody.tip.sha).toBe(
'49ec1e05f39eef8d1ab6200331a028fb3dd96828'
)
expect(commitNoBody.tip.shortSha).toBe('49ec1e0')
expect(commitNoBody.tip.summary).toBe('this is a commit title')
expect(commitNoBody.tip.body).toHaveLength(0)
expect(commitNoBody.tip.parentSHAs).toHaveLength(1)
Expand All @@ -47,6 +49,7 @@ describe('git/for-each-ref', () => {
expect(master.name).toBe('master')
expect(master.upstream).toBeNull()
expect(master.tip.sha).toBe('b9ccfc3307240b86447bca2bd6c51a4bb4ade493')
expect(master.tip.shortSha).toBe('b9ccfc3')
expect(master.tip.summary).toBe('stubbed a README')
expect(master.tip.parentSHAs).toHaveLength(1)
})
Expand Down
2 changes: 2 additions & 0 deletions app/test/unit/git/log-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('git/log', () => {
const firstCommit = commits[commits.length - 1]
expect(firstCommit.summary).toBe('first')
expect(firstCommit.sha).toBe('7cd6640e5b6ca8dbfd0b33d0281ebe702127079c')
expect(firstCommit.shortSha).toBe('7cd6640')
})

it('handles repository with HEAD file on disk', async () => {
Expand All @@ -43,6 +44,7 @@ describe('git/log', () => {

expect(commits).toHaveLength(1)
expect(commits[0].sha).toBe('415e4987158c49c383ce7114e0ef00ebf4b070c1')
expect(commits[0].shortSha).toBe('415e498')
})
})

Expand Down
1 change: 1 addition & 0 deletions app/test/unit/group-branches-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ describe('Branches grouping', () => {
const author = new CommitIdentity('Hubot', 'hubot@github.com', new Date())

const commit = new Commit(
'300acef',
'300acef',
'summary',
'body',
Expand Down
1 change: 1 addition & 0 deletions app/test/unit/infer-comparison-branch-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { ComparisonCache } from '../../src/lib/comparison-cache'
function createTestCommit(sha: string) {
return new Commit(
sha,
sha.slice(0, 7),
'',
'',
new CommitIdentity('tester', 'tester@test.com', new Date()),
Expand Down

0 comments on commit 0fde421

Please sign in to comment.