git-pull-request
Advanced tools
Comparing version 2.0.2 to 2.1.0
@@ -0,1 +1,11 @@ | ||
# v2.1.0 | ||
## Fixes | ||
- Fallback to git config if invalid package.json found (@joshwooding) | ||
## New features | ||
- SSH support (@joshwooding) | ||
# v2.0.2 | ||
@@ -2,0 +12,0 @@ |
105
gpr.js
@@ -58,46 +58,65 @@ #!/usr/bin/env node | ||
} catch (error) { | ||
console.error(error.output[1]); | ||
console.error(error.output[1]); | ||
} | ||
} | ||
try { | ||
var npmPrefix = execSync('npm prefix', {cwd: process.cwd(), encoding: 'utf8'}).replace(/\s+/g, ''); | ||
} catch (error) {} | ||
function getNpmPrefix(){ | ||
try { | ||
return execSync('npm prefix', {cwd: process.cwd(), encoding: 'utf8'}).replace(/\s+/g, ''); | ||
} catch (error) {} | ||
} | ||
// Read the package.json and extract the repo name | ||
if (fs.existsSync(npmPrefix + '/package.json')) { | ||
function getDetailsFromPackageJson(){ | ||
var npmPrefix = getNpmPrefix(); | ||
var packageFile = JSON.parse(fs.readFileSync(npmPrefix + '/package.json', 'utf8')); | ||
// repo: [ 'https:', '', 'github.com', 'mui-org', 'material-ui.git' ], | ||
var repo = packageFile.repository.url.split('/'); | ||
if(packageFile.repository && packageFile.repository.url){ | ||
// repo: [ 'https:', '', 'github.com', 'mui-org', 'material-ui.git' ], | ||
var repo = packageFile.repository.url.split('/'); | ||
// repoName: 'material-ui' | ||
var repoName = repo[4].slice(0, -4); | ||
// repoName: 'material-ui' | ||
var repoName = repo[4].slice(0, -4); | ||
// path: '/repos/mui-org/material-ui/pulls' | ||
var path = '/repos/' + repo[3]+ '/' + repoName + '/pulls'; | ||
// path: '/repos/mui-org/material-ui/pulls' | ||
var path = '/repos/' + repo[3]+ '/' + repoName + '/pulls'; | ||
// If there's no package.json, try .git config upstream / origin | ||
} else { | ||
return { repoName, path }; | ||
} | ||
return null; | ||
} | ||
function getGitPrefix(){ | ||
try { | ||
var gitPrefix = execSync('git rev-parse --show-toplevel', {cwd: process.cwd(), encoding: 'utf8'}).replace(/\s+/g, ''); | ||
return execSync('git rev-parse --show-toplevel', {cwd: process.cwd(), encoding: 'utf8'}).replace(/\s+/g, ''); | ||
} catch (error) { | ||
exit(error.output[1]); | ||
} | ||
} | ||
if (fs.existsSync(gitPrefix + '/.git/config')) { | ||
var gitConfig = fs.readFileSync(gitPrefix + '/.git/config', 'utf8'); | ||
function getDetailsFromGitConfig(){ | ||
var gitPrefix = getGitPrefix(); | ||
// repo: [ 'https:', '', 'github.com', 'mui-org', 'material-ui.git' ], | ||
var repo = gitConfig.match(/\[remote "upstream"\][\r\n|\r|\n]\s*url\s=\s(.*)/); | ||
var gitConfig = fs.readFileSync(gitPrefix + '/.git/config', 'utf8'); | ||
// repo: [ 'https:', '', 'github.com', 'mui-org', 'material-ui.git' ], | ||
var repo = gitConfig.match(/\[remote "upstream"\][\r\n|\r|\n]\s*url\s=\s(.*)/); | ||
if (!repo) { | ||
repo = gitConfig.match(/\[remote "origin"\][\r\n|\r|\n]\s*url\s=\s(.*)/); | ||
if (!repo) { | ||
repo = gitConfig.match(/\[remote "origin"\][\r\n|\r|\n]\s*url\s=\s(.*)/); | ||
if (!repo) { | ||
exit('No "upstream" or "origin" remote found in .git/config') | ||
} | ||
exit('No "upstream" or "origin" remote found in .git/config') | ||
} | ||
repo = repo[1].split('/'); | ||
} | ||
repo = repo[1].split('/'); | ||
if(repo[0].includes('git@')){ | ||
// repoName: 'material-ui' | ||
var repoName = repo[1].slice(0, -4); | ||
var namespace = repo[0].split(':')[1] | ||
// path: '/repos/mui-org/material-ui/pulls' | ||
var path = '/repos/' + namespace + '/' + repoName + '/pulls'; | ||
} else { | ||
// repoName: 'material-ui' | ||
var repoName = repo[4].slice(0, -4); | ||
@@ -107,3 +126,24 @@ | ||
var path = '/repos/' + repo[3]+ '/' + repoName + '/pulls'; | ||
console.log("git without package") | ||
} | ||
console.log("git without package") | ||
return { repoName, path }; | ||
} | ||
var npmPrefix = getNpmPrefix(); | ||
// Read the package.json and extract the repo name | ||
if (fs.existsSync(npmPrefix + '/package.json')) { | ||
var repoDetails = getDetailsFromPackageJson(); | ||
if(!repoDetails){ | ||
repoDetails = getDetailsFromGitConfig(); | ||
} | ||
// If there's no package.json, try .git config upstream / origin | ||
} else { | ||
var gitPrefix = getGitPrefix(); | ||
if (fs.existsSync(gitPrefix + '/.git/config')) { | ||
var repoDetails = getDetailsFromGitConfig(); | ||
} else { | ||
@@ -124,3 +164,3 @@ exit('No package.json or .git/config found in any ancestor directory') | ||
if (args[3] === '-r' || args[3] === 'remote' || args[3] === 'pr' || args[3] === 'pr') { | ||
break; | ||
break; | ||
} | ||
@@ -141,3 +181,3 @@ execho('git branch --list gpr/*'); | ||
execho('git fetch ' + 'https://github.com/' + ref[0] + '/' + repoName + ' "' + ref[1] + '"'); | ||
execho('git fetch ' + 'https://github.com/' + ref[0] + '/' + repoDetails.repoName + ' "' + ref[1] + '"'); | ||
execho('git checkout FETCH_HEAD'); | ||
@@ -157,3 +197,3 @@ process.exit(); | ||
execho('git push ' + 'https://github.com/' + ref[0] + '/' + repoName + ' "' + 'HEAD:' + ref[1] + '"'); | ||
execho('git push ' + 'https://github.com/' + ref[0] + '/' + repoDetails.repoName + ' "' + 'HEAD:' + ref[1] + '"'); | ||
process.exit(); | ||
@@ -170,5 +210,5 @@ | ||
exit(usage); | ||
}; | ||
} | ||
execho('git push -f ' + 'https://github.com/' + ref[0] + '/' + repoName + ' "' + 'HEAD:' + ref[1] + '"'); | ||
execho('git push -f ' + 'https://github.com/' + ref[0] + '/' + repoDetails.repoName + ' "' + 'HEAD:' + ref[1] + '"'); | ||
process.exit(); | ||
@@ -179,3 +219,4 @@ | ||
if (~~prNumber === 0) { exit(usage + '\n\n <pr> must be a number.'); } | ||
path += '/' + prNumber; | ||
repoDetails.path += '/' + prNumber; | ||
} | ||
@@ -187,3 +228,3 @@ | ||
port: 443, | ||
path: path, | ||
path: repoDetails.path, | ||
headers: { | ||
@@ -190,0 +231,0 @@ 'User-Agent': version |
{ | ||
"name": "git-pull-request", | ||
"version": "2.0.2", | ||
"version": "2.1.0", | ||
"description": "A cli utility to pull a remote branch based on a github PR number", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# git-pull-request [![npm version](https://badge.fury.io/js/git-pull-request.svg)](https://badge.fury.io/js/git-pull-request) | ||
A node based cli utility to pull a remote branch based on a github PR number | ||
A node based cli utility to pull a remote branch based on a github PR number. gpr makes checking out a contribution locally in order to see it in action as simple as `gpr <pr#>`! | ||
@@ -63,3 +63,3 @@ ## NB v2.x.x breaking change | ||
```sh | ||
> grp 123 | ||
> gpr 123 | ||
@@ -66,0 +66,0 @@ ``` |
13881
255