From 09e910df577b341f823dd23cd9dd412d6e2e7233 Mon Sep 17 00:00:00 2001 From: David Marby Date: Tue, 9 Feb 2021 09:17:47 +0100 Subject: [PATCH] Stop automatically appending .git to git repository URLs (#3054) --- .../app/common/__tests__/database.test.js | 44 +++++++++++++++++++ packages/insomnia-app/app/common/database.js | 21 +++++++++ .../insomnia-app/app/models/git-repository.js | 9 +++- packages/insomnia-app/app/sync/git/git-vcs.js | 14 ++++-- .../app/ui/components/wrapper-home.js | 1 + 5 files changed, 84 insertions(+), 5 deletions(-) diff --git a/packages/insomnia-app/app/common/__tests__/database.test.js b/packages/insomnia-app/app/common/__tests__/database.test.js index 6979d42c3..0bafa5c6d 100644 --- a/packages/insomnia-app/app/common/__tests__/database.test.js +++ b/packages/insomnia-app/app/common/__tests__/database.test.js @@ -443,6 +443,50 @@ describe('_repairDatabase()', () => { expect((await models.apiSpec.getByParentId(w2._id)).fileName).toBe('Workspace 2'); // Should fix expect((await models.apiSpec.getByParentId(w3._id)).fileName).toBe('Unique name'); // should not fix }); + + it('fixes old git uris', async () => { + const oldRepoWithSuffix = await models.gitRepository.create({ + uri: 'https://github.com/foo/bar.git', + uriNeedsMigration: true, + }); + const oldRepoWithoutSuffix = await models.gitRepository.create({ + uri: 'https://github.com/foo/bar', + uriNeedsMigration: true, + }); + const newRepoWithSuffix = await models.gitRepository.create({ + uri: 'https://github.com/foo/bar.git', + }); + const newRepoWithoutSuffix = await models.gitRepository.create({ + uri: 'https://github.com/foo/bar', + }); + + await db._repairDatabase(); + + expect(await db.get(models.gitRepository.type, oldRepoWithSuffix._id)).toEqual( + expect.objectContaining({ + uri: 'https://github.com/foo/bar.git', + uriNeedsMigration: false, + }), + ); + expect(await db.get(models.gitRepository.type, oldRepoWithoutSuffix._id)).toEqual( + expect.objectContaining({ + uri: 'https://github.com/foo/bar.git', + uriNeedsMigration: false, + }), + ); + expect(await db.get(models.gitRepository.type, newRepoWithSuffix._id)).toEqual( + expect.objectContaining({ + uri: 'https://github.com/foo/bar.git', + uriNeedsMigration: false, + }), + ); + expect(await db.get(models.gitRepository.type, newRepoWithoutSuffix._id)).toEqual( + expect.objectContaining({ + uri: 'https://github.com/foo/bar', + uriNeedsMigration: false, + }), + ); + }); }); describe('duplicate()', () => { diff --git a/packages/insomnia-app/app/common/database.js b/packages/insomnia-app/app/common/database.js index 0325adc52..292167eba 100644 --- a/packages/insomnia-app/app/common/database.js +++ b/packages/insomnia-app/app/common/database.js @@ -696,6 +696,10 @@ export async function _repairDatabase() { await _fixMultipleCookieJars(workspace); await _applyApiSpecName(workspace); } + + for (const gitRepository of await find(models.gitRepository.type)) { + await _fixOldGitURIs(gitRepository); + } } /** @@ -789,3 +793,20 @@ async function _fixMultipleCookieJars(workspace) { console.log(`[fix] Merged ${cookieJars.length} cookie jars under ${workspace.name}`); } + +// Append .git to old git URIs to mimic previous isomorphic-git behaviour +async function _fixOldGitURIs(doc: GitRepository) { + if (!doc.uriNeedsMigration) { + return; + } + + if (!doc.uri.endsWith('.git')) { + doc.uri += '.git'; + } + + doc.uriNeedsMigration = false; + + await update(doc); + + console.log(`[fix] Fixed git URI for ${doc._id}`); +} diff --git a/packages/insomnia-app/app/models/git-repository.js b/packages/insomnia-app/app/models/git-repository.js index 495c73d88..0c38f72f3 100644 --- a/packages/insomnia-app/app/models/git-repository.js +++ b/packages/insomnia-app/app/models/git-repository.js @@ -12,6 +12,7 @@ type BaseGitRepository = { name: string, email: string, }, + uriNeedsMigration: boolean, }; export type GitRepository = BaseModel & BaseGitRepository; @@ -31,15 +32,19 @@ export function init(): BaseGitRepository { name: '', email: '', }, + uriNeedsMigration: true, }; } -export function migrate(doc: T): T { +export function migrate(doc: GitRepository): GitRepository { return doc; } export function create(patch: $Shape = {}): Promise { - return db.docCreate(type, patch); + return db.docCreate(type, { + uriNeedsMigration: false, + ...patch, + }); } export function update(repo: GitRepository, patch: $Shape): Promise { diff --git a/packages/insomnia-app/app/sync/git/git-vcs.js b/packages/insomnia-app/app/sync/git/git-vcs.js index f98a94ea2..9e802f6e1 100644 --- a/packages/insomnia-app/app/sync/git/git-vcs.js +++ b/packages/insomnia-app/app/sync/git/git-vcs.js @@ -56,7 +56,7 @@ export const GIT_INSOMNIA_DIR = path.join(GIT_CLONE_DIR, GIT_INSOMNIA_DIR_NAME); export default class GitVCS { _git: Object; - _baseOpts: { dir: string, gitdir?: string }; + _baseOpts: { dir: string, gitdir?: string, noGitSuffix: boolean }; _initialized: boolean; constructor() { @@ -74,7 +74,11 @@ export default class GitVCS { console.log(`[git-event] ${message}`); }); - this._baseOpts = { dir: directory, gitdir: gitDirectory }; + this._baseOpts = { + dir: directory, + gitdir: gitDirectory, + noGitSuffix: true, + }; if (await this._repoExists()) { console.log(`[git] Opened repo for ${gitDirectory}`); @@ -96,7 +100,11 @@ export default class GitVCS { this._git = git; git.plugins.set('fs', fsPlugin); - this._baseOpts = { dir: directory, gitdir: gitDirectory }; + this._baseOpts = { + dir: directory, + gitdir: gitDirectory, + noGitSuffix: true, + }; await git.clone({ ...this._baseOpts, ...creds, url, singleBranch: true }); diff --git a/packages/insomnia-app/app/ui/components/wrapper-home.js b/packages/insomnia-app/app/ui/components/wrapper-home.js index a208b76c7..324786d18 100644 --- a/packages/insomnia-app/app/ui/components/wrapper-home.js +++ b/packages/insomnia-app/app/ui/components/wrapper-home.js @@ -138,6 +138,7 @@ class WrapperHome extends React.PureComponent { url, ...credentials, depth: 1, + noGitSuffix: true, }); } catch (err) { showError({ title: 'Error Cloning Repository', message: err.message, error: err });