Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit c427e2a

Browse files
committed
Initialize and publish empty repositories
1 parent b108eb5 commit c427e2a

File tree

2 files changed

+51
-8
lines changed

2 files changed

+51
-8
lines changed

lib/views/create-dialog.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,22 @@ export async function publishRepository(
4040
{ownerID, name, visibility, protocol, sourceRemoteName},
4141
{repository, relayEnvironment},
4242
) {
43-
let defaultBranchName;
44-
const branchSet = await repository.getBranches();
45-
const branchNames = new Set(branchSet.getNames());
46-
if (branchNames.has('master')) {
43+
let defaultBranchName, wasEmpty;
44+
if (repository.isEmpty()) {
45+
wasEmpty = true;
46+
await repository.init();
4747
defaultBranchName = 'master';
4848
} else {
49-
const head = branchSet.getHeadBranch();
50-
if (head.isPresent()) {
51-
defaultBranchName = head.getName();
49+
wasEmpty = false;
50+
const branchSet = await repository.getBranches();
51+
const branchNames = new Set(branchSet.getNames());
52+
if (branchNames.has('master')) {
53+
defaultBranchName = 'master';
54+
} else {
55+
const head = branchSet.getHeadBranch();
56+
if (head.isPresent()) {
57+
defaultBranchName = head.getName();
58+
}
5259
}
5360
}
5461
if (!defaultBranchName) {
@@ -58,5 +65,7 @@ export async function publishRepository(
5865
const result = await createRepositoryMutation(relayEnvironment, {name, ownerID, visibility});
5966
const sourceURL = result.createRepository.repository[protocol === 'ssh' ? 'sshUrl' : 'url'];
6067
const remote = await repository.addRemote(sourceRemoteName, sourceURL);
61-
await repository.push(defaultBranchName, {remote, setUpstream: true});
68+
if (!wasEmpty) {
69+
await repository.push(defaultBranchName, {remote, setUpstream: true});
70+
}
6271
}

test/views/create-dialog.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,40 @@ describe('CreateDialog', function() {
292292
assert.isTrue(repository.push.calledWith('other-branch', {remote: CREATED_REMOTE, setUpstream: true}));
293293
});
294294

295+
it('initializes an empty repository', async function() {
296+
expectRelayQuery({
297+
name: createRepositoryQuery.operation.name,
298+
variables: {input: {name: 'repo-name', ownerId: 'user0', visibility: 'PUBLIC'}},
299+
}, op => {
300+
return relayResponseBuilder(op)
301+
.createRepository(m => {
302+
m.repository(r => {
303+
r.sshUrl('[email protected]:user0/repo-name.git');
304+
r.url('https://github.com/user0/repo-name');
305+
});
306+
})
307+
.build();
308+
}).resolve();
309+
310+
const empty = await buildRepository(temp.mkdirSync());
311+
assert.isTrue(empty.isEmpty());
312+
313+
sinon.stub(empty, 'addRemote').resolves(CREATED_REMOTE);
314+
sinon.stub(empty, 'push');
315+
316+
await publishRepository({
317+
ownerID: 'user0',
318+
name: 'repo-name',
319+
visibility: 'PUBLIC',
320+
protocol: 'https',
321+
sourceRemoteName: 'origin',
322+
}, {repository: empty, relayEnvironment});
323+
324+
assert.isTrue(empty.isPresent());
325+
assert.isTrue(empty.addRemote.calledWith('origin', 'https://github.com/user0/repo-name'));
326+
assert.isFalse(empty.push.called);
327+
});
328+
295329
it('fails if the source repository has no "master" or current branches', async function() {
296330
await repository.checkout('other-branch', {createNew: true});
297331
await repository.checkout('HEAD^');

0 commit comments

Comments
 (0)