From a79a528fd1de2ed1360ce3c2cc31a165b5183b3c Mon Sep 17 00:00:00 2001 From: joshaber Date: Thu, 7 Jan 2016 16:13:46 -0500 Subject: [PATCH] Make .getRepo() submodule-aware. --- spec/git-repository-async-spec.js | 49 ++++++++++++++++++++++++++----- src/git-repository-async.js | 17 +++++------ 2 files changed, 50 insertions(+), 16 deletions(-) diff --git a/spec/git-repository-async-spec.js b/spec/git-repository-async-spec.js index ccfbfd100..e6084e6ad 100644 --- a/spec/git-repository-async-spec.js +++ b/spec/git-repository-async-spec.js @@ -23,6 +23,17 @@ function copyRepository (name = 'working-dir') { return fs.realpathSync(workingDirPath) } +function copySubmoduleRepository () { + const workingDirectory = copyRepository('repo-with-submodules') + const reGit = (name) => { + fs.renameSync(path.join(workingDirectory, name, 'git.git'), path.join(workingDirectory, name, '.git')) + } + reGit('jstips') + reGit('You-Dont-Need-jQuery') + + return workingDirectory +} + describe('GitRepositoryAsync', () => { let repo @@ -45,6 +56,36 @@ describe('GitRepositoryAsync', () => { }) }) + describe('.getRepo()', () => { + beforeEach(() => { + const workingDirectory = copySubmoduleRepository() + repo = GitRepositoryAsync.open(workingDirectory) + waitsForPromise(() => repo.refreshStatus()) + }) + + it('returns the repository when not given a path', async () => { + const nodeGitRepo1 = await repo.repoPromise + const nodeGitRepo2 = await repo.getRepo() + expect(nodeGitRepo1.workdir()).toBe(nodeGitRepo2.workdir()) + }) + + it('returns the repository when given a non-submodule path', async () => { + const nodeGitRepo1 = await repo.repoPromise + const nodeGitRepo2 = await repo.getRepo('README') + expect(nodeGitRepo1.workdir()).toBe(nodeGitRepo2.workdir()) + }) + + it('returns the submodule repository when given a submodule path', async () => { + const nodeGitRepo1 = await repo.repoPromise + const nodeGitRepo2 = await repo.getRepo('jstips') + expect(nodeGitRepo1.workdir()).not.toBe(nodeGitRepo2.workdir()) + + const nodeGitRepo3 = await repo.getRepo('jstips/README.md') + expect(nodeGitRepo1.workdir()).not.toBe(nodeGitRepo3.workdir()) + expect(nodeGitRepo2.workdir()).toBe(nodeGitRepo3.workdir()) + }) + }) + describe('.openRepository()', () => { it('returns a new repository instance', async () => { repo = openFixture('master.git') @@ -313,7 +354,7 @@ describe('GitRepositoryAsync', () => { describe('in a repository with submodules', () => { beforeEach(() => { - const workingDirectory = copyRepository('repo-with-submodules') + const workingDirectory = copySubmoduleRepository() repo = GitRepositoryAsync.open(workingDirectory) modifiedPath = path.join(workingDirectory, 'jstips', 'README.md') newPath = path.join(workingDirectory, 'You-Dont-Need-jQuery', 'untracked.txt') @@ -321,12 +362,6 @@ describe('GitRepositoryAsync', () => { fs.writeFileSync(newPath, '') fs.writeFileSync(modifiedPath, 'making this path modified') newPath = fs.absolute(newPath) // specs could be running under symbol path. - - const reGit = (name) => { - fs.renameSync(path.join(workingDirectory, name, 'git.git'), path.join(workingDirectory, name, '.git')) - } - reGit('jstips') - reGit('You-Dont-Need-jQuery') }) it('returns status information for all new and modified files', async () => { diff --git a/src/git-repository-async.js b/src/git-repository-async.js index 25aa2fc86..947742a5d 100644 --- a/src/git-repository-async.js +++ b/src/git-repository-async.js @@ -907,9 +907,14 @@ export default class GitRepositoryAsync { }) } + // Get the submodule for the given path. + // + // Returns a {Promise} which resolves to the {GitRepositoryAsync} submodule or + // null if it isn't a submodule path. async _submoduleForPath (_path) { let relativePath = await this.relativizeToWorkingDirectory(_path) - for (const {submodulePath, submoduleRepo} in this.submodules) { + for (const submodulePath in this.submodules) { + const submoduleRepo = this.submodules[submodulePath] if (relativePath === submodulePath) { return submoduleRepo } else if (relativePath.indexOf(`${submodulePath}/`) === 0) { @@ -938,14 +943,8 @@ export default class GitRepositoryAsync { if (!_path) return this.repoPromise - return this.isSubmodule(_path) - .then(isSubmodule => { - if (isSubmodule) { - return Git.Repository.open(_path) - } else { - return this.repoPromise - } - }) + return this._submoduleForPath(_path) + .then(submodule => submodule ? submodule.getRepo() : this.repoPromise) } // Open a new instance of the underlying {NodeGit.Repository}.