Make .getRepo() submodule-aware.

This commit is contained in:
joshaber 2016-01-07 16:13:46 -05:00
parent 957c8557cc
commit a79a528fd1
2 changed files with 50 additions and 16 deletions

View File

@ -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 () => {

View File

@ -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}.