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

feat: add some info to gitInfo #75

Merged
merged 3 commits into from
Sep 29, 2020
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
8 changes: 6 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ 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;
countCommitToTrackingBranch: string | undefined;
};

export type Config<T> = {
questions: ({
Expand Down
78 changes: 77 additions & 1 deletion src/util/gitInfo.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,86 @@
import gitRepoInfo from "git-repo-info";
import simpleGit from "simple-git";
import { execSync } from "child_process";

export const getGitInfo = async (repoPath: string = process.cwd()) => {
import { GitInfo } from "../types";

const getIsFirstCommitOnCurrentBranch = (): {
isFirstCommitOnCurrentBranch: GitInfo["isFirstCommitOnCurrentBranch"];
} => {
let isFirstCommitOnCurrentBranch;
try {
const nearestBranchOrTag = execSync(
"git describe --contains --always --all"
);
const currentBranch = execSync("git rev-parse --abbrev-ref HEAD");

isFirstCommitOnCurrentBranch =
nearestBranchOrTag.toString().trim() === currentBranch.toString().trim();
} catch (error) {
// If not exist remote origin branch, git describe will fail and show error
isFirstCommitOnCurrentBranch = false;
}

return { isFirstCommitOnCurrentBranch };
};

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 };
};

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 }> => {
const repoInfo = gitRepoInfo(repoPath);
const gitStatus = simpleGit(repoPath);
const status = await gitStatus.status();

const { not_added, created, deleted, modified, renamed, staged } = status;

const { isFirstCommitOnCurrentBranch } = getIsFirstCommitOnCurrentBranch();
const { trackingBranch } = getTrackingBranch();
const { countCommitToTrackingBranch } = getCountCommitToTrackingBranch(
trackingBranch
);

const gitInfo = {
...repoInfo,
not_added,
Expand All @@ -16,6 +89,9 @@ export const getGitInfo = async (repoPath: string = process.cwd()) => {
modified,
renamed,
staged,
isFirstCommitOnCurrentBranch,
trackingBranch,
countCommitToTrackingBranch,
};

return { gitInfo };
Expand Down