Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Commit

Permalink
Add git set remote repository method
Browse files Browse the repository at this point in the history
Took 1 hour 15 minutes
  • Loading branch information
seokju-na committed Dec 10, 2018
1 parent 3c2075a commit 1bbe976
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/browser/shared/git.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
GitFindRemoteOptions,
GitGetHistoryOptions,
GitGetHistoryResult,
GitSetRemoteOptions,
GitSyncWithRemoteOptions,
GitSyncWithRemoteResult,
} from '../../core/git';
Expand Down Expand Up @@ -85,6 +86,13 @@ export class GitService implements OnDestroy {
));
}

setRemote(options: GitSetRemoteOptions): Observable<void> {
return from(this.ipcClient.performAction<GitSetRemoteOptions, void>(
'setRemote',
options,
));
}

syncWithRemote(options: GitSyncWithRemoteOptions): Observable<GitSyncWithRemoteResult> {
return from(this.ipcClient.performAction<GitSyncWithRemoteOptions, GitSyncWithRemoteResult>(
'syncWithRemote',
Expand Down
9 changes: 9 additions & 0 deletions src/core/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export function parseGitRemoteUrl(url: string): GitRemoteUrl | null {
export enum GitErrorCodes {
AUTHENTICATION_FAIL = 'git.authenticationFail',
REMOTE_NOT_FOUND = 'git.remoteNotFound',
REMOTE_ALREADY_EXISTS = 'git.remoteAlreadyExists',
MERGE_CONFLICTED = 'git.mergeConflicted',
NETWORK_ERROR = 'git.networkError',
}
Expand All @@ -84,6 +85,7 @@ export enum GitErrorCodes {
export const gitErrorRegexes: { [key: string]: RegExp } = {
[GitErrorCodes.AUTHENTICATION_FAIL]: /authentication required/,
[GitErrorCodes.REMOTE_NOT_FOUND]: /remote '.*' does not exist/,
[GitErrorCodes.REMOTE_ALREADY_EXISTS]: /remote '.*' already exists/,
[GitErrorCodes.NETWORK_ERROR]: /curl error: Could not resolve host:/,
};

Expand Down Expand Up @@ -198,6 +200,13 @@ export interface GitFindRemoteOptions {
}


export interface GitSetRemoteOptions {
workspaceDirPath: string;
remoteName: string;
remoteUrl: string;
}


export interface GitSyncWithRemoteOptions {
workspaceDirPath: string;
remoteName: string;
Expand Down
38 changes: 35 additions & 3 deletions src/main-process/services/git.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,18 +231,50 @@ describe('mainProcess.services.GitService', () => {
});
});

// FIXME LATER : Travis CI fails in this test. Ignore for while.
xdescribe('isRemoteExists', () => {
describe('isRemoteExists', () => {
beforeEach(async () => {
await makeTmpPath(true);
});

it('should return \'false\' if repository has not remote.', async () => {
const result = await git.isRemoteExists({
workspaceDirPath: getFixturePath('origin-not-exists'),
workspaceDirPath: tmpPath,
remoteName: 'origin',
});

expect(result).to.equals(false);
});
});

describe('setRemote', () => {
beforeEach(async () => {
await makeTmpPath(true);
});

it('should remove exists remote and set new remote.', async () => {
const remoteName = 'origin';
const prevRemoteUrl = 'previous_remote_url';
const nextRemoteUrl = 'next_remote_url';

// Set previous remote...
const repo = await _git.Repository.open(tmpPath);
await _git.Remote.create(repo, remoteName, prevRemoteUrl);

await git.setRemote({
workspaceDirPath: tmpPath,
remoteName,
remoteUrl: nextRemoteUrl,
});

// Check changed remote
const remote = await repo.getRemote(remoteName);
expect(remote.url()).to.equals(nextRemoteUrl);

remote.free();
repo.free();
});
});

/**
* NOTE: This tests require network connection.
* If your network is slow, this test is likely to be timed out.
Expand Down
32 changes: 31 additions & 1 deletion src/main-process/services/git.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import {
GitFindRemoteOptions,
GitGetHistoryOptions,
GitGetHistoryResult,
GitMergeConflictedError, GitNetworkError,
GitMergeConflictedError,
GitNetworkError,
GitRemoteNotFoundError,
GitSetRemoteOptions,
GitSyncWithRemoteOptions,
GitSyncWithRemoteResult,
} from '../../core/git';
Expand Down Expand Up @@ -225,6 +227,34 @@ export class GitService extends Service {
}
}

@IpcActionHandler('setRemote')
async setRemote(options: GitSetRemoteOptions): Promise<void> {
const { remoteName, remoteUrl } = options;
const repository = await this.openRepository(options.workspaceDirPath);

// If remote already exists, delete first.
try {
await this.git.Remote.lookup(repository, remoteName);
await this.git.Remote.delete(repository, remoteName);
} catch (error) {
const message = error.message ? error.message : '';

// Only remote not found error accepted. Other should be thrown.
if (!gitErrorRegexes[GitErrorCodes.REMOTE_NOT_FOUND].test(message)) {
repository.free();
throw error;
}
}

try {
await this.git.Remote.create(repository, remoteName, remoteUrl);
} catch (error) {
throw error;
} finally {
repository.free();
}
}

@IpcActionHandler('syncWithRemote')
async syncWithRemote(options: GitSyncWithRemoteOptions): Promise<GitSyncWithRemoteResult> {
const repository = await this.openRepository(options.workspaceDirPath);
Expand Down

0 comments on commit 1bbe976

Please sign in to comment.