Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 57 additions & 38 deletions lib/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,18 @@ Git.prototype.push = function (remote, branch, force) {
* @return {Promise<string>} A promise for the remote URL.
*/
Git.prototype.getRemoteUrl = function (remote) {
return this.exec('config', '--get', 'remote.' + remote + '.url')
.then((git) => {
const repo = git.output && git.output.split(/[\n\r]/).shift();
if (repo) {
return repo;
} else {
throw new Error(
'Failed to get repo URL from options or current directory.',
);
}
})
const cleanOutput = (git) => {
const repo = git.output && git.output.split(/[\n\r]/).shift();
if (repo) {
return repo;
} else {
throw new Error(
'Failed to get repo URL from options or current directory.',
);
}
};
const pullURL = this.exec('config', '--get', 'remote.' + remote + '.url')
.then(cleanOutput)
.catch((err) => {
throw new Error(
'Failed to get remote.' +
Expand All @@ -221,6 +222,12 @@ Git.prototype.getRemoteUrl = function (remote) {
'or must be configured with the "repo" option).',
);
});
return Promise.all([
pullURL,
this.exec('config', '--get', 'remote.' + remote + '.pushurl')
.then(cleanOutput)
.catch(() => pullURL),
]);
};

/**
Expand All @@ -235,7 +242,7 @@ Git.prototype.deleteRef = function (branch) {

/**
* Clone a repo into the given dir if it doesn't already exist.
* @param {string} repo Repository URL.
* @param {[string, string]} repo Repository URL.
* @param {string} dir Target directory.
* @param {string} branch Branch name.
* @param {options} options All options.
Expand All @@ -246,32 +253,44 @@ Git.clone = function clone(repo, dir, branch, options) {
if (exists) {
return Promise.resolve(new Git(dir, options.git));
} else {
return fs.mkdirp(path.dirname(path.resolve(dir))).then(() => {
const args = [
'clone',
repo,
dir,
'--branch',
branch,
'--single-branch',
'--origin',
options.remote,
'--depth',
options.depth,
];
return spawn(options.git, args)
.catch((err) => {
// try again without branch or depth options
return spawn(options.git, [
'clone',
repo,
dir,
'--origin',
options.remote,
]);
})
.then(() => new Git(dir, options.git));
});
return fs
.mkdirp(path.dirname(path.resolve(dir)))
.then(() => {
const args = [
'clone',
repo[0],
dir,
'--branch',
branch,
'--single-branch',
'--origin',
options.remote,
'--depth',
options.depth,
];
return spawn(options.git, args)
.catch((err) => {
// try again without branch or depth options
return spawn(options.git, [
'clone',
repo[0],
dir,
'--origin',
options.remote,
]);
})
.then(() => new Git(dir, options.git));
})
.then(
repo[0] !== repo[1]
? (g) =>
spawn(
options.git,
['remote', 'set-url', '--push', options.remote, repo[1]],
dir,
).then(() => g)
: undefined,
);
}
});
};
Expand Down
6 changes: 3 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,13 @@ exports.publish = function publish(basePath, config, callback) {
getRepo(options)
.then((repo) => {
repoUrl = repo;
const clone = getCacheDir(repo);
log('Cloning %s into %s', repo, clone);
const clone = getCacheDir(repo[0]);
log('Cloning %s into %s', repo[0], clone);
return Git.clone(repo, clone, options.branch, options);
})
.then((git) => {
return git.getRemoteUrl(options.remote).then((url) => {
if (url !== repoUrl) {
if (url[0] !== repoUrl[0] || url[1] !== repoUrl[1]) {
const message =
'Remote url mismatch. Got "' +
url +
Expand Down
2 changes: 1 addition & 1 deletion test/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function assertContentsMatch(dir, url, branch) {
.then((root) => {
const clone = path.join(root, 'repo');
const options = {git: 'git', remote: 'origin', depth: 1};
return Git.clone(url, clone, branch, options);
return Git.clone([url, url], clone, branch, options);
})
.then((git) => {
const comparison = compare(dir, git.cwd, {excludeFilter: '.git'});
Expand Down
Loading