From f21ed7f08b56006f0e5f1215c3738a32c93f5086 Mon Sep 17 00:00:00 2001 From: Shijing Lu Date: Wed, 5 Jun 2019 18:44:11 -0400 Subject: [PATCH] fix #707: failed to set workdir correctly if submodule first opened in bare mode then open with normal mode --- node/lib/util/submodule_config_util.js | 24 ++++++--- node/test/util/submodule_config_util.js | 68 ++++++++++++++++++------- 2 files changed, 65 insertions(+), 27 deletions(-) diff --git a/node/lib/util/submodule_config_util.js b/node/lib/util/submodule_config_util.js index ef3715004..68758c143 100644 --- a/node/lib/util/submodule_config_util.js +++ b/node/lib/util/submodule_config_util.js @@ -548,23 +548,31 @@ exports.initSubmoduleAndRepo = co.wrap(function *(repoUrl, const FLAGS = NodeGit.Repository.INIT_FLAG; + const initRepo = co.wrap(function *() { + return bare ? + yield NodeGit.Repository.init(subRepoDir, 1) : + yield NodeGit.Repository.initExt(subRepoDir, { + workdirPath: exports.computeRelativeWorkDir(name), + flags: FLAGS.NO_DOTGIT_DIR | FLAGS.MKPATH | + FLAGS.RELATIVE_GITLINK | + (null === templatePath ? 0 : FLAGS.EXTERNAL_TEMPLATE), + templatePath: templatePath + }); + }); // See if modules repo exists. let subRepo = null; try { subRepo = bare ? yield NodeGit.Repository.openBare(subRepoDir) : yield NodeGit.Repository.open(subRepoDir); + // re-init if previously opened as bare + if (!bare && subRepo.isBare()) { + subRepo = yield initRepo(); + } } catch (e) { // Or, make it if not. - - subRepo = yield NodeGit.Repository.initExt(subRepoDir, { - workdirPath: bare ? null : exports.computeRelativeWorkDir(name), - flags: FLAGS.NO_DOTGIT_DIR | FLAGS.MKPATH | - FLAGS.RELATIVE_GITLINK | - (null === templatePath ? 0 : FLAGS.EXTERNAL_TEMPLATE), - templatePath: templatePath - }); + subRepo = yield initRepo(); } if (bare) { diff --git a/node/test/util/submodule_config_util.js b/node/test/util/submodule_config_util.js index 5468cfeb9..c6a6883aa 100644 --- a/node/test/util/submodule_config_util.js +++ b/node/test/util/submodule_config_util.js @@ -639,19 +639,11 @@ foo describe("initSubmoduleAndRepo", function () { - const runTest = co.wrap(function *(repo, - subRootRepo, - url, - subName, - originUrl) { - if (undefined === originUrl) { - originUrl = ""; - } + /** Setup a simple meta repo with one submodule (not opened). */ + const setupMeta = co.wrap(function *(subRootRepo, repo, url, subName) { const subHead = yield subRootRepo.getHeadCommit(); - const submodule = yield NodeGit.Submodule.addSetup(repo, - url, - subName, - 1); + const submodule = + yield NodeGit.Submodule.addSetup(repo,url, subName, 1); const subRepo = yield submodule.open(); yield subRepo.fetchAll(); subRepo.setHeadDetached(subHead.id()); @@ -666,14 +658,26 @@ foo sig, "my message"); yield SubmoduleConfigUtil.deinit(repo, [subName]); + return subHead; + }); + + const runTest = co.wrap(function *(repo, + subRootRepo, + url, + subName, + originUrl) { + if (undefined === originUrl) { + originUrl = ""; + } + const subHead = yield setupMeta(subRootRepo, repo, url, subName); const repoPath = repo.workdir(); - const result = yield SubmoduleConfigUtil.initSubmoduleAndRepo( - originUrl, - repo, - subName, - url, - null, - false); + const result = + yield SubmoduleConfigUtil.initSubmoduleAndRepo(originUrl, + repo, + subName, + url, + null, + false); assert.instanceOf(result, NodeGit.Repository); assert(TestUtil.isSameRealPath(result.workdir(), path.join(repoPath, subName))); @@ -722,6 +726,32 @@ foo assert.equal(newUrl, url); })); + it("reset workdir if open in bare first", co.wrap(function *() { + const repo = yield TestUtil.createSimpleRepository(); + const subRootRepo = yield TestUtil.createSimpleRepository(); + const url = subRootRepo.workdir(); + const originUrl = ""; + const subName = "foo"; + yield setupMeta(subRootRepo, repo, url, subName); + const newSub1 = + yield SubmoduleConfigUtil.initSubmoduleAndRepo(originUrl, + repo, + subName, + url, + null, + true); + assert.notExists(newSub1.workdir()); + const newSub2 = + yield SubmoduleConfigUtil.initSubmoduleAndRepo(originUrl, + repo, + subName, + url, + null, + false); + assert.equal(path.relative(repo.workdir(), newSub2.workdir()), + subName); + })); + it("deep name", co.wrap(function *() { const repo = yield TestUtil.createSimpleRepository(); const subRootRepo = yield TestUtil.createSimpleRepository();