Eliminate double for-each-ref call by removing return value from createBranch

Right after we create a branch we refresh the list of branches so we can just use that list rather than issuing a redundant for-each-ref command
This commit is contained in:
Markus Olsson 2020-11-27 14:42:49 +01:00
parent 2e70798475
commit 1b72fd2535
6 changed files with 23 additions and 19 deletions

View file

@ -1,5 +1,4 @@
import { git, gitNetworkArguments } from './core'
import { getBranches } from './for-each-ref'
import { Repository } from '../../models/repository'
import { Branch, BranchType } from '../../models/branch'
import { IGitAccount } from '../../models/git-account'
@ -26,7 +25,7 @@ export async function createBranch(
name: string,
startPoint: string | null,
noTrack?: boolean
): Promise<Branch | null> {
): Promise<void> {
const args =
startPoint !== null ? ['branch', name, startPoint] : ['branch', name]
@ -38,12 +37,6 @@ export async function createBranch(
}
await git(args, repository.path, 'createBranch')
const branches = await getBranches(repository, `refs/heads/${name}`)
if (branches.length > 0) {
return branches[0]
}
return null
}
/** Rename the given branch to a new name. */

View file

@ -3072,7 +3072,7 @@ export class AppStore extends TypedBaseStore<IAppState> {
const gitStore = this.gitStoreCache.get(repository)
const branch = await gitStore.createBranch(name, startPoint, noTrackOption)
if (branch !== null) {
if (branch !== undefined) {
await this._checkoutBranch(repository, branch)
}
}

View file

@ -340,16 +340,19 @@ export class GitStore extends BaseStore {
startPoint: string | null,
noTrackOption: boolean = false
) {
const branch =
(await this.performFailableOperation(() =>
createBranch(this.repository, name, startPoint, noTrackOption)
)) ?? null
const createdBranch = await this.performFailableOperation(async () => {
await createBranch(this.repository, name, startPoint, noTrackOption)
return true
})
if (branch !== null) {
if (createdBranch === true) {
await this.loadBranches()
return this.allBranches.find(
x => x.type === BranchType.Local && x.name === name
)
}
return branch
return undefined
}
public async createTag(name: string, targetCommitSha: string) {

View file

@ -24,6 +24,7 @@ import {
} from '../../../src/lib/git'
import { StatsStore, StatsDatabase } from '../../../src/lib/stats'
import { UiActivityMonitor } from '../../../src/ui/lib/ui-activity-monitor'
import { assertNonNullable } from '../../../src/lib/fatal-error'
describe('git/branch', () => {
let statsStore: StatsStore
@ -176,7 +177,9 @@ describe('git/branch', () => {
it('deletes local branches', async () => {
const name = 'test-branch'
const branch = await createBranch(repository, name, null)
const [branch] = await getBranches(repository, `refs/heads/${name}`)
assertNonNullable(branch, `Could not create branch ${name}`)
const ref = `refs/heads/${name}`
expect(branch).not.toBeNull()

View file

@ -15,8 +15,9 @@ async function createAndCheckout(
repository: Repository,
name: string
): Promise<void> {
const branch = await createBranch(repository, name, null)
if (branch == null) {
await createBranch(repository, name, null)
const [branch] = await getBranches(repository, `refs/heads/${name}`)
if (branch === undefined) {
throw new Error(`Unable to create branch: ${name}`)
}
await checkoutBranch(repository, null, branch)

View file

@ -13,6 +13,7 @@ import {
createCommit,
checkoutBranch,
deleteTag,
getBranches,
} from '../../../src/lib/git'
import {
setupFixtureRepository,
@ -23,6 +24,7 @@ import { getDotComAPIEndpoint } from '../../../src/lib/api'
import { IRemote } from '../../../src/models/remote'
import { findDefaultRemote } from '../../../src/lib/stores/helpers/find-default-remote'
import { getStatusOrThrow } from '../../helpers/status'
import { assertNonNullable } from '../../../src/lib/fatal-error'
describe('git/tag', () => {
let repository: Repository
@ -153,7 +155,9 @@ describe('git/tag', () => {
it('does not return a tag created on a non-pushed branch', async () => {
// Create a tag on a local branch that's not pushed to the remote.
const branch = await createBranch(repository, 'new-branch', 'master')
await createBranch(repository, 'new-branch', 'master')
const [branch] = await getBranches(repository, `refs/heads/${name}`)
assertNonNullable(branch, `Could not create branch ${name}`)
await FSE.writeFile(path.join(repository.path, 'README.md'), 'Hi world\n')
const status = await getStatusOrThrow(repository)