Skip to content

Commit

Permalink
fix: cloning a local path should clone the currently checked out branch
Browse files Browse the repository at this point in the history
  • Loading branch information
ZauberNerd committed May 5, 2020
1 parent 2aae319 commit 53b7b70
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 11 deletions.
24 changes: 24 additions & 0 deletions src/__tests__/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,30 @@ describe('command execution', () => {
);
});

it('should not add `--branch` argument when branch is empty', () => {
cloneRepositories(
partialConfig({
repositories: [
partialRepositoryConfig({
url: '.',
directory: 'canarist',
branch: '',
}),
],
}),
'/cwd'
);

expect(execSync).toHaveBeenCalledWith(
[
'git clone . /some/directory/canarist',
'--single-branch',
'--no-tags',
].join(' '),
{ stdio: 'pipe', cwd: '/cwd' }
);
});

it('should throw an error if cloning failed', () => {
(execSync as jest.Mock).mockImplementationOnce(execSyncError);
const spy = consoleErrorSpy();
Expand Down
44 changes: 44 additions & 0 deletions src/__tests__/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,19 @@ describe('normalize config', () => {
expect(config.repositories[0].directory).toBe('canarist');
});

it('should leave branch name blank when cloning from a local path', () => {
// $ canarist -r .
const config = normalizeConfig(
{
_: [],
repository: ['.'],
},
null
);

expect(config.repositories[0].branch).toBe('');
});

it('should accept target directory', () => {
// $ canarist -r . /some/dir
const config = normalizeConfig(
Expand Down Expand Up @@ -306,6 +319,37 @@ describe('normalize config', () => {
});
});

it('should leave branch blank when cloning from a local path', () => {
// $ canarist
const config = normalizeConfig(
{ _: [] },
{
filepath: '',
config: {
repositories: [
{
url: '.',
},
],
},
}
);

expect(config).toEqual({
repositories: [
{
url: '.',
branch: '',
directory: 'canarist',
commands: ['yarn test'],
},
],
rootManifest: {},
targetDirectory: '/tmp/canarist-XXXXXX',
yarnArguments: '',
});
});

it('should throw an error if no project is selected', () => {
// $ canarist
expect(() => {
Expand Down
8 changes: 6 additions & 2 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,17 @@ export function cloneRepositories(
`git clone ${url} ${target}`,
`--single-branch`,
`--no-tags`,
`--branch ${branch}`,
branch ? `--branch ${branch}` : '',
isLocalFilePath ? '' : '--depth 1',
]
.join(' ')
.trim();

console.log('[canarist] cloning "%s#%s" into "%s"', url, branch, target);
console.log(
`[canarist] cloning "${url}${
branch ? '#' + branch : ''
}" into "${target}"`
);
if (!execute(command, cwd, debug)) {
throw new Error('Failed to clone repositories');
}
Expand Down
25 changes: 16 additions & 9 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,35 +72,42 @@ function tryParse(input: string): Record<string, unknown> {
}
}

function normalizeDirectoryName(url: string): string {
const { name } = gitUrlParse(url);
return name === '.' ? basename(resolve(name)) : name;
}

function normalizeRepository(
input: string | RepositoryArguments | Partial<RepositoryConfig>
): RepositoryConfig {
const defaultBranch = 'master';
const defaultCommands = ['yarn test'];

if (typeof input === 'string') {
const { name } = gitUrlParse(input);
return {
url: input,
branch: defaultBranch,
directory: normalizeDirectoryName(input),
branch: name === '.' ? '' : defaultBranch,
directory: name === '.' ? basename(resolve(name)) : name,
commands: defaultCommands,
};
}

const url = Array.isArray((input as RepositoryArguments)._)
? (input as RepositoryArguments)._[0]
: input.url;
const branch = input.branch || defaultBranch;
const { name } = gitUrlParse(url);
const directory =
typeof input.directory === 'string'
? input.directory
: name === '.'
? basename(resolve(name))
: name;
const branch =
typeof input.branch === 'string'
? input.branch
: name === '.'
? ''
: defaultBranch;
const command =
typeof (input as RepositoryArguments).command === 'undefined'
? input.commands
: (input as RepositoryArguments).command;
const directory = normalizeDirectoryName(input.directory || url);
const commands = Array.isArray(command)
? command
: typeof command === 'string'
Expand Down

0 comments on commit 53b7b70

Please sign in to comment.