From 21459ad372239f38461e413477de8e359eedf334 Mon Sep 17 00:00:00 2001 From: tyankatsu Date: Mon, 28 Sep 2020 23:59:51 +0900 Subject: [PATCH 1/3] feat: add isFirstCommitOnCurrentBranch --- src/util/gitInfo.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/util/gitInfo.ts b/src/util/gitInfo.ts index 36e30e0..74ffdae 100644 --- a/src/util/gitInfo.ts +++ b/src/util/gitInfo.ts @@ -1,5 +1,24 @@ import gitRepoInfo from "git-repo-info"; import simpleGit from "simple-git"; +import { execSync } from "child_process"; + +const getIsFirstCommitOnCurrentBranch = () => { + let isFirstCommitOnCurrentBranch; + try { + const nearestBranchOrTag = execSync( + "git describe --contains --always --all" + ); + const currentBranch = execSync("git rev-parse --abbrev-ref HEAD"); + + isFirstCommitOnCurrentBranch = + nearestBranchOrTag.toString() === currentBranch.toString(); + } catch (error) { + // If not exist remote origin branch, git describe will fail and show error + isFirstCommitOnCurrentBranch = false; + } + + return { isFirstCommitOnCurrentBranch }; +}; export const getGitInfo = async (repoPath: string = process.cwd()) => { const repoInfo = gitRepoInfo(repoPath); @@ -8,6 +27,8 @@ export const getGitInfo = async (repoPath: string = process.cwd()) => { const { not_added, created, deleted, modified, renamed, staged } = status; + const { isFirstCommitOnCurrentBranch } = getIsFirstCommitOnCurrentBranch(); + const gitInfo = { ...repoInfo, not_added, @@ -16,6 +37,7 @@ export const getGitInfo = async (repoPath: string = process.cwd()) => { modified, renamed, staged, + isFirstCommitOnCurrentBranch, }; return { gitInfo }; From 58b122edba07c38b130837f81b4bdb425ef71b49 Mon Sep 17 00:00:00 2001 From: tyankatsu Date: Wed, 30 Sep 2020 00:13:03 +0900 Subject: [PATCH 2/3] feat: add trackingBranch --- src/types.ts | 7 +++++-- src/util/gitInfo.ts | 32 +++++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/types.ts b/src/types.ts index 9f56884..9d86eb7 100644 --- a/src/types.ts +++ b/src/types.ts @@ -5,11 +5,14 @@ import { StatusResult } from "simple-git"; export type CZ = Inquirer; export type Commit = (commitMessage: string) => void; -type GitInfo = GitRepoInfo & +export type GitInfo = GitRepoInfo & Pick< StatusResult, "not_added" | "created" | "deleted" | "modified" | "renamed" | "staged" - >; + > & { + isFirstCommitOnCurrentBranch: boolean; + trackingBranch: string | undefined; + }; export type Config = { questions: ({ diff --git a/src/util/gitInfo.ts b/src/util/gitInfo.ts index 74ffdae..8cb2a5e 100644 --- a/src/util/gitInfo.ts +++ b/src/util/gitInfo.ts @@ -2,7 +2,11 @@ import gitRepoInfo from "git-repo-info"; import simpleGit from "simple-git"; import { execSync } from "child_process"; -const getIsFirstCommitOnCurrentBranch = () => { +import { GitInfo } from "../types"; + +const getIsFirstCommitOnCurrentBranch = (): { + isFirstCommitOnCurrentBranch: GitInfo["isFirstCommitOnCurrentBranch"]; +} => { let isFirstCommitOnCurrentBranch; try { const nearestBranchOrTag = execSync( @@ -11,7 +15,7 @@ const getIsFirstCommitOnCurrentBranch = () => { const currentBranch = execSync("git rev-parse --abbrev-ref HEAD"); isFirstCommitOnCurrentBranch = - nearestBranchOrTag.toString() === currentBranch.toString(); + nearestBranchOrTag.toString().trim() === currentBranch.toString().trim(); } catch (error) { // If not exist remote origin branch, git describe will fail and show error isFirstCommitOnCurrentBranch = false; @@ -20,7 +24,27 @@ const getIsFirstCommitOnCurrentBranch = () => { return { isFirstCommitOnCurrentBranch }; }; -export const getGitInfo = async (repoPath: string = process.cwd()) => { +const getTrackingBranch = (): { + trackingBranch: GitInfo["trackingBranch"]; +} => { + let trackingBranch; + + try { + trackingBranch = execSync( + "git rev-parse --abbrev-ref --symbolic-full-name @{u}" + ) + .toString() + .trim(); + } catch (error) { + trackingBranch = undefined; + } + + return { trackingBranch }; +}; + +export const getGitInfo = async ( + repoPath: string = process.cwd() +): Promise<{ gitInfo: GitInfo }> => { const repoInfo = gitRepoInfo(repoPath); const gitStatus = simpleGit(repoPath); const status = await gitStatus.status(); @@ -28,6 +52,7 @@ export const getGitInfo = async (repoPath: string = process.cwd()) => { const { not_added, created, deleted, modified, renamed, staged } = status; const { isFirstCommitOnCurrentBranch } = getIsFirstCommitOnCurrentBranch(); + const { trackingBranch } = getTrackingBranch(); const gitInfo = { ...repoInfo, @@ -38,6 +63,7 @@ export const getGitInfo = async (repoPath: string = process.cwd()) => { renamed, staged, isFirstCommitOnCurrentBranch, + trackingBranch, }; return { gitInfo }; From f326ba18500d48de9231bc318407ce97f9c620d2 Mon Sep 17 00:00:00 2001 From: tyankatsu Date: Wed, 30 Sep 2020 00:20:34 +0900 Subject: [PATCH 3/3] feat: add countCommitToTrackingBranch --- src/types.ts | 1 + src/util/gitInfo.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/types.ts b/src/types.ts index 9d86eb7..6c50aec 100644 --- a/src/types.ts +++ b/src/types.ts @@ -12,6 +12,7 @@ export type GitInfo = GitRepoInfo & > & { isFirstCommitOnCurrentBranch: boolean; trackingBranch: string | undefined; + countCommitToTrackingBranch: string | undefined; }; export type Config = { diff --git a/src/util/gitInfo.ts b/src/util/gitInfo.ts index 8cb2a5e..9750796 100644 --- a/src/util/gitInfo.ts +++ b/src/util/gitInfo.ts @@ -42,6 +42,30 @@ const getTrackingBranch = (): { return { trackingBranch }; }; +const getCountCommitToTrackingBranch = ( + trackingBranch: GitInfo["trackingBranch"] +): { + countCommitToTrackingBranch: GitInfo["countCommitToTrackingBranch"]; +} => { + let countCommitToTrackingBranch; + if (trackingBranch === undefined) { + countCommitToTrackingBranch = undefined; + return { countCommitToTrackingBranch }; + } + + try { + countCommitToTrackingBranch = execSync( + `git rev-list --count HEAD ^${trackingBranch}` + ) + .toString() + .trim(); + } catch (error) { + countCommitToTrackingBranch = undefined; + } + + return { countCommitToTrackingBranch }; +}; + export const getGitInfo = async ( repoPath: string = process.cwd() ): Promise<{ gitInfo: GitInfo }> => { @@ -53,6 +77,9 @@ export const getGitInfo = async ( const { isFirstCommitOnCurrentBranch } = getIsFirstCommitOnCurrentBranch(); const { trackingBranch } = getTrackingBranch(); + const { countCommitToTrackingBranch } = getCountCommitToTrackingBranch( + trackingBranch + ); const gitInfo = { ...repoInfo, @@ -64,6 +91,7 @@ export const getGitInfo = async ( staged, isFirstCommitOnCurrentBranch, trackingBranch, + countCommitToTrackingBranch, }; return { gitInfo };