使用download-git-repo下载指定库时,遇到git 128错误,如何定位问题并换向解决呢?
前言我正在搭一个脚手架,希望通过命令行终端自动下载git仓库的代码到本地,首先想到的库便是download-git-repo,部分代码如下所示:const download = require('download-git-repo');download('direct:https://gitee.com/xx/xxx.git', 'test', {clone: true}, (err) =>
前言
我正在搭一个脚手架,希望通过命令行终端自动下载git仓库的代码到本地,首先想到的库便是download-git-repo
,部分代码如下所示:
const download = require('download-git-repo');
download('direct:https://gitee.com/xx/xxx.git', 'test', {clone: true}, (err) => {
if (err) {
console.log('executeDownload error ==', err);
process.exit();
} else {
console.log('success!')
}
});
- 参数
direct:https://gitee.com/xx/xxx.git
:repo
仓库地址 - 参数
test
:拷贝的目的文件夹,若当前路径下没有,会自动创建该文件夹 - 参数
{clone: true}
表示使用git clone
进行下载,若为false
,则直接下载zip
等包,部分download-git-repo
源码如下所示:
var clone = opts.clone || false
if (clone) {
var cloneOptions = {
checkout: repo.checkout,
shallow: repo.checkout === 'master',
...opts
}
gitclone(url, dest, cloneOptions, function (err) {
if (err === undefined) {
rm(dest + '/.git')
fn()
} else {
fn(err)
}
})
} else {
var downloadOptions = {
extract: true,
strip: 1,
mode: '666',
...opts,
headers: {
accept: 'application/zip',
...(opts.headers || {})
}
}
downloadUrl(url, dest, downloadOptions)
}
却不断地遇到如下图的错误:
通过查看download-git-repo
源码,以及该源码库引用的另一git-clone
源码,发现命令行下载git
仓库的终极武器是child_process
,既然如此,我决定直接使用它达到我的目的
child_process
该库提供了spawn
方法,可直接运行各种命令,详情可见
比如,我现在需要下载git
仓库的feature/V1.x
分支,代码如下所示:
var child_process = require('child_process')
const { spawn } = child_process
var g = spawn('git', ['clone', '-b', 'feature/V1.x', 'http://xxx/scm/gis-acc/xxxx.git', 'test'])
g.stdout.on('data', function (s) {
console.log('g stdout: ' + s)
})
g.stderr.on('data', function (data) {
console.log('g stderr: ' + data);
});
第一个参数表示执行的是git
命令,第二个参数为数组,类似于在git bash
终端输入如下命令:
$ git clone -b feature/V1.x http://xxx/scm/gis-acc/xxxx.git
test
表示将该仓库代码下载到该文件夹下(没有该文件夹会直接创建一个,如上面所说)
Duang, Duang~~
,再次执行node bin/sf.js init
,发现在当前项目下面已多出了test
文件夹,如下图所示:
execSync
使用child_process
的execSync
方法,可达到同样的目的,代码如下所示:
child_process.execSync(`git clone -b feature/V1.x http://xxx/scm/gis-acc/xxxx.git test`);
结语
暂时不清楚git 128
错误的根本原因,翻墙去google查找,说是权限问题,试过各种办法,只能弃用'download-git-repo'
,改用nodejs
内置的方法去实现,欢迎感兴趣的小伙伴在下面留言
更多推荐
所有评论(0)