Skip to content

Commit

Permalink
Merge pull request #12 from tkskto/feature/add-branches
Browse files Browse the repository at this point in the history
chore: add branch.
  • Loading branch information
tkskto authored Aug 1, 2021
2 parents 6307aa8 + 2e05ba8 commit 81c1d84
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 8 deletions.
5 changes: 3 additions & 2 deletions __tests__/spec/authors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ describe('authors test', () => {
]);
} else {
expect(authorData).toStrictEqual([
{name: 'kato takeshi', email: '[email protected]', commitCount: 1},
{name: 'tkskto', email: '[email protected]', commitCount: authorData[1].commitCount}, // TODO
{name: 'Renovate Bot', email: '[email protected]', commitCount: authorData[0].commitCount},
{name: 'kato takeshi', email: '[email protected]', commitCount: authorData[1].commitCount},
{name: 'tkskto', email: '[email protected]', commitCount: authorData[2].commitCount},
]);
}
});
Expand Down
45 changes: 45 additions & 0 deletions __tests__/spec/branches.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {branches} from '../../src';

const isCI = process.argv[2] === '--ci';

describe('branch test', () => {
test('local', async () => {
const branch = await branches(false);

if (isCI) {
expect(branch.local.length).toStrictEqual(1);
expect(branch.remote).toBeUndefined();
} else {
expect(branch).toStrictEqual({
local: [
'feature/add-branches',
'feature/add-remote-details',
'main'
],
});
expect(branch.remote).toBeUndefined();
}
});

test('remote', async () => {
const branch = await branches(true);

if (isCI) {
expect(branch.local.length).toStrictEqual(1);
if (branch.remote && branch.remote.pull) {
expect(branch.remote.pull.length).toStrictEqual(1);
}
} else {
expect(branch).toStrictEqual({
local: [
'feature/add-branches',
'feature/add-remote-details',
'main'
],
remote: {
origin: [ 'feature/add-branches', 'main' ],
},
});
}
});
});
39 changes: 39 additions & 0 deletions src/Branch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {Branches} from 'git-log-nodejs';
import {getBranch} from './Execs';
import {makeErrorMessage} from './ErrorLogFactory';

export const branches = async (withRemote = true): Promise<Branches> => {
try {
const local: string = await getBranch(false);
const lines: string[] = local.split(/\n|\r\n/g).map((name) => name.replace('* ', '').trim());
const result: Branches = {
local: lines,
};

if (withRemote) {
const remote: string = await getBranch(true);
const lines: string[] = remote.replace(local,'').split(/\n|\r\n/g);
const remotes: Record<string, string[]> = {};

lines.forEach((branch: string) => {
if (branch.includes('HEAD -> ')) {
return;
}

const [remote, ...name] = branch.replace('remotes/', '').trim().split(/\//);

if (!Object.prototype.hasOwnProperty.call(remotes, remote)) {
remotes[remote] = [];
}

remotes[remote].push(name.join('/'));
});

result.remote = remotes;
}

return result;
} catch (err) {
throw new Error(makeErrorMessage('authors in Author.ts', err.message));
}
};
10 changes: 9 additions & 1 deletion src/Execs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const commonFunction = (command: string, option = ['']): Promise<string> => {

const makeOption = (...params: string[]): string[] => {
return params.filter((item) => item); // filtering false
}
};

export const getCommits = ({count, withFile, branch}: LogOption): Promise<string> => {
const option = makeOption(
Expand Down Expand Up @@ -66,6 +66,14 @@ export const getAuthor = ({branch}: LogOption): Promise<string> => {
return commonFunction('shortlog', option);
};

export const getBranch = (withRemote: boolean): Promise<string> => {
const option = makeOption(
'--list',
withRemote ? '-r' : '',
);
return commonFunction('branch', option);
};

export const getConfig = (): Promise<string> => {
return commonFunction('config', ['--list']);
};
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {authors} from './Author';
import {branches} from './Branch';
import {commits} from './Commit';
import {configs} from './Config';
import {remotes} from './Remote';

export {
authors,
branches,
commits,
configs,
remotes,
Expand Down
9 changes: 4 additions & 5 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ declare module 'git-log-nodejs' {
commitCount: number;
}

export type Branch = {
name: string;
remote: string;
hash: string;
export type Branches = {
local: string[];
remote?: Record<string, string[]>;
}

export type Tag = {
Expand All @@ -55,7 +54,7 @@ declare module 'git-log-nodejs' {

export function authors(params?: LogOption): Promise<Author[]>;

export function branches(): Branch[];
export function branches(withRemote: boolean): Promise<Branches>;

export function tags(): Tag[];

Expand Down

0 comments on commit 81c1d84

Please sign in to comment.