From c6122438fd835a43b8630cd4cac0628be1b43755 Mon Sep 17 00:00:00 2001 From: Yiteng Guo Date: Mon, 30 Nov 2020 01:02:44 -0500 Subject: [PATCH 1/3] feat: implement a better way to resolve remote and upstream branch --- src/git.ts | 47 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/src/git.ts b/src/git.ts index 755f98d4..70c27c63 100644 --- a/src/git.ts +++ b/src/git.ts @@ -3,6 +3,30 @@ import * as path from 'path' import { log } from './log' import { getRemoteUrlReplacements } from './config' +/** + * Returns [remote, upstream branch]. + * Empty remote is returned if the upstream branch points to a local branch. + * Empty upstream branch is returned if there is no upstream branch. + */ +async function gitRemoteBranch(repoDir: string): Promise<[string, string]> { + try { + const { stdout } = await execa('git', ['rev-parse', '--abbrev-ref', 'HEAD@{upstream}'], { cwd: repoDir }) + const remoteAndBranch = stdout.split('/') + if (remoteAndBranch.length == 2) { + const [remote, branch] = remoteAndBranch + return [remote, branch] + } else if (remoteAndBranch.length == 1) { + // The upstream branch points to a local branch. + return ['', remoteAndBranch[0]] + } else { + return ['', ''] + } + } catch (error) { + return ['', ''] + } + +} + /** * Returns the names of all git remotes, e.g. ["origin", "foobar"] */ @@ -29,9 +53,16 @@ async function gitRemoteURL(repoDirectory: string, remoteName: string): Promise< } /** - * Returns the remote URL of the first Git remote found. + * Returns the remote URL. */ async function gitDefaultRemoteURL(repoDirectory: string): Promise { + const [remote,] = await gitRemoteBranch(repoDirectory) + if (remote !== '') { + return await gitRemoteURL(repoDirectory, remote) + } + + // If we cannot find the remote name deterministically, we use the first + // Git remote found. const remotes = await gitRemotes(repoDirectory) if (remotes.length === 0) { throw new Error('no configured git remotes') @@ -52,12 +83,18 @@ async function gitRootDirectory(repoDirectory: string): Promise { } /** - * Returns either the current branch name of the repository OR in all - * other cases (e.g. detached HEAD state), it returns "HEAD". + * Returns either the current remote branch name of the repository OR in all + * other cases (e.g. detached HEAD state, upstream branch points to a local + * branch), it returns "HEAD". */ async function gitBranch(repoDirectory: string): Promise { - const { stdout } = await execa('git', ['rev-parse', '--abbrev-ref', 'HEAD'], { cwd: repoDirectory }) - return stdout + const [origin, branch] = await gitRemoteBranch(repoDirectory) + if (origin !== '') { + // The remote branch exists. + return branch + } else { + return "HEAD" + } } /** From 72c492ab99fa6ab4d7c891bc1f0725ad238e7102 Mon Sep 17 00:00:00 2001 From: Yiteng Guo Date: Mon, 30 Nov 2020 01:59:03 -0500 Subject: [PATCH 2/3] fix: eslint and prettier --- src/git.ts | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/git.ts b/src/git.ts index 70c27c63..4f83efb9 100644 --- a/src/git.ts +++ b/src/git.ts @@ -5,26 +5,25 @@ import { getRemoteUrlReplacements } from './config' /** * Returns [remote, upstream branch]. - * Empty remote is returned if the upstream branch points to a local branch. + * Empty remote is returned if the upstream branch points to a local branch. * Empty upstream branch is returned if there is no upstream branch. */ async function gitRemoteBranch(repoDir: string): Promise<[string, string]> { try { const { stdout } = await execa('git', ['rev-parse', '--abbrev-ref', 'HEAD@{upstream}'], { cwd: repoDir }) const remoteAndBranch = stdout.split('/') - if (remoteAndBranch.length == 2) { + if (remoteAndBranch.length === 2) { const [remote, branch] = remoteAndBranch return [remote, branch] - } else if (remoteAndBranch.length == 1) { + } + if (remoteAndBranch.length === 1) { // The upstream branch points to a local branch. return ['', remoteAndBranch[0]] - } else { - return ['', ''] } + return ['', ''] } catch (error) { return ['', ''] } - } /** @@ -56,7 +55,7 @@ async function gitRemoteURL(repoDirectory: string, remoteName: string): Promise< * Returns the remote URL. */ async function gitDefaultRemoteURL(repoDirectory: string): Promise { - const [remote,] = await gitRemoteBranch(repoDirectory) + const [remote] = await gitRemoteBranch(repoDirectory) if (remote !== '') { return await gitRemoteURL(repoDirectory, remote) } @@ -92,9 +91,8 @@ async function gitBranch(repoDirectory: string): Promise { if (origin !== '') { // The remote branch exists. return branch - } else { - return "HEAD" } + return 'HEAD' } /** From b0aa50145b631a4618a7c9b27d796833eb633eec Mon Sep 17 00:00:00 2001 From: Yiteng Guo Date: Thu, 17 Dec 2020 23:39:10 -0500 Subject: [PATCH 3/3] fix: prettier check --- src/git.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/git.ts b/src/git.ts index 4f83efb9..ea892745 100644 --- a/src/git.ts +++ b/src/git.ts @@ -8,9 +8,9 @@ import { getRemoteUrlReplacements } from './config' * Empty remote is returned if the upstream branch points to a local branch. * Empty upstream branch is returned if there is no upstream branch. */ -async function gitRemoteBranch(repoDir: string): Promise<[string, string]> { +async function gitRemoteBranch(repoDirectory: string): Promise<[string, string]> { try { - const { stdout } = await execa('git', ['rev-parse', '--abbrev-ref', 'HEAD@{upstream}'], { cwd: repoDir }) + const { stdout } = await execa('git', ['rev-parse', '--abbrev-ref', 'HEAD@{upstream}'], { cwd: repoDirectory }) const remoteAndBranch = stdout.split('/') if (remoteAndBranch.length === 2) { const [remote, branch] = remoteAndBranch @@ -21,7 +21,7 @@ async function gitRemoteBranch(repoDir: string): Promise<[string, string]> { return ['', remoteAndBranch[0]] } return ['', ''] - } catch (error) { + } catch { return ['', ''] } } @@ -57,7 +57,7 @@ async function gitRemoteURL(repoDirectory: string, remoteName: string): Promise< async function gitDefaultRemoteURL(repoDirectory: string): Promise { const [remote] = await gitRemoteBranch(repoDirectory) if (remote !== '') { - return await gitRemoteURL(repoDirectory, remote) + return gitRemoteURL(repoDirectory, remote) } // If we cannot find the remote name deterministically, we use the first