Skip to content

Commit

Permalink
feat(fetch) Add updated/deleted branches to fetch response.
Browse files Browse the repository at this point in the history
Parses more of the git fetch response, notably updates + deletes.
This returns the to/from commit of each branch.
When `--prune` is passed, git will also return deleted branches.
  • Loading branch information
modelbitjason committed Jul 21, 2022
1 parent 61d4ee0 commit dffe6f4
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 10 deletions.
44 changes: 34 additions & 10 deletions simple-git/src/lib/parsers/parse-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,40 @@ const parsers: LineParser<FetchResult>[] = [
new LineParser(/From (.+)$/, (result, [remote]) => {
result.remote = remote;
}),
new LineParser(/\* \[new branch]\s+(\S+)\s*-> (.+)$/, (result, [name, tracking]) =>{
result.branches.push({
name,
tracking,
new LineParser(
/\* \[new branch]\s+(\S+)\s*-> (.+)$/,
(result, [name, tracking]) => {
result.branches.push({
name,
tracking
});
}
),
new LineParser(
/\* \[new tag]\s+(\S+)\s*-> (.+)$/,
(result, [name, tracking]) => {
result.tags.push({
name,
tracking
});
}
),
new LineParser(/- \[deleted]\s+\S+\s*-> (.+)$/, (result, [tracking]) => {
result.deleted.push({
tracking
});
}),
new LineParser(/\* \[new tag]\s+(\S+)\s*-> (.+)$/, (result, [name, tracking]) => {
result.tags.push({
name,
tracking,
});
})
new LineParser(
/\s*([^.]+)\.\.(\S+)\s+(\S+)\s*-> (.+)$/,
(result, [from, to, name, tracking]) => {
result.updated.push({
name,
tracking,
to,
from
});
}
)
];

export function parseFetchResult (stdOut: string, stdErr: string): FetchResult {
Expand All @@ -25,6 +47,8 @@ export function parseFetchResult (stdOut: string, stdErr: string): FetchResult {
remote: null,
branches: [],
tags: [],
updated: [],
deleted: []
};
return parseStringResponse(result, parsers, [stdOut, stdErr]);
}
93 changes: 93 additions & 0 deletions simple-git/test/integration/fetch.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// ./simple-git/test/integration/fetch.spec.ts

import {
createTestContext,
newSimpleGit,
setUpInit,
SimpleGitTestContext
} from '../__fixtures__';

describe('fetch', () => {
let context: SimpleGitTestContext;
let upstream: string;
let local: string;

beforeEach(async () => (context = await createTestContext()));
beforeEach(async () => {
upstream = await context.dir('upstream');
local = context.path('local');
await context.file(['upstream', 'file']);

await givenRemote(upstream);
await givenLocal(upstream, local);
});

it('fetches updates', async () => {
const git = newSimpleGit(local);
const bravoPriorHead = await git.revparse('origin/bravo');
await givenRemoteChanges(upstream);

const result = await git.fetch(['--prune']);

const bravoCurrentHead = await git.revparse('origin/bravo');

expect(result).toEqual({
branches: [
{
name: 'delta',
tracking: 'origin/delta'
}
],
deleted: [{ tracking: 'origin/charlie' }],
raw: '',
remote: upstream,
tags: [
{
name: 'alpha',
tracking: 'alpha'
}
],
updated: [
{
from: bravoPriorHead.substring(0, 7),
name: 'bravo',
to: bravoCurrentHead.substring(0, 7),
tracking: 'origin/bravo'
}
]
});
});

/**
* Sets up the repo to be used as a local - by cloning the remote
*/
async function givenLocal(upstream: string, local: string) {
await newSimpleGit(context.root).clone(upstream, local);
await setUpInit({ git: newSimpleGit(local) });
}

/**
* Sets up the repo to be used as a remote
*/
async function givenRemote(upstream: string) {
const git = newSimpleGit(upstream);
await setUpInit({ git });
await git.add('.');
await git.commit('first');
await git.raw('checkout', '-b', 'bravo');
await git.raw('checkout', '-b', 'charlie');
}
/**
* Configure the remote with changes to be retrieved when using fetch on the local
*/
async function givenRemoteChanges(upstream: string) {
const git = newSimpleGit(upstream);
await git.raw('tag', 'alpha');
await git.raw('checkout', 'bravo');
await context.file(['upstream', 'another-file']);
await git.add('.');
await git.commit('second');
await git.raw('checkout', '-b', 'delta');
await git.raw('branch', '-d', 'charlie');
}
});
9 changes: 9 additions & 0 deletions simple-git/typings/response.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,15 @@ export interface FetchResult {
name: string;
tracking: string;
}[];
updated: {
name: string;
tracking: string;
to: string;
from: string;
}[];
deleted: {
tracking: string;
}[];
}

/** Represents the response to git.grep */
Expand Down

0 comments on commit dffe6f4

Please sign in to comment.