簡體   English   中英

nodegit(用於Node.js的libgit2):如何推拉?

[英]nodegit (libgit2 for Node.js): How to push and pull?

我試圖推送或拉動nodegit

包很受歡迎,一切正常,但我似乎無法找到這些基本方法。 我錯過了什么?

還有其他套餐嗎?

試試這個以獲取和拉取:

/**
 * Fetch from remote
 * fileName: pull.js
 *
 * @param {string} repositoryPath - Path to local git repository
 * @param {string} remoteName - Remote name
 * @param {string} branch - Branch to fetch
 */

var Git = require('nodegit');
var open = Git.Repository.open;   

module.exports = function (repositoryPath, remoteName, branch, cb) {
    var repository;
    var remoteBranch = remoteName + '/' + branch;
    open(repositoryPath)
        .then(function (_repository) {
            repository = _repository;
            return repository.fetch(remoteName);
        }, cb)
        .then(function () {
            return repository.mergeBranches(branch, remoteBranch);
        }, cb)
        .then(function (oid) {
            cb(null, oid);
        }, cb);
};

然后你可以這樣使用:

var pull = require('./pull.js');
var remoteRef = 'origin';

pull(repositoryPath, remoteRef, ourBranch, function(errFetch, oid) {
    if (errFetch) {
        return errFetch;
    }
    console.log(oid);
});

並試着推送:

/**
 * Pushes to a remote
 *
 * @param {string} repositoryPath - Path to local git repository
 * @param {string} remoteName - Remote name
 * @param {string} branch - Branch to push
 * @param {doneCallback} cb
 */

var Git = require('nodegit');
var open = Git.Repository.open;

module.exports = function (repositoryPath, remoteName, branch, cb) {

    var repository, remoteResult;

    open(repositoryPath)
        .then(function (_repository) {
            repository = _repository;
            return repository.getRemote(remoteName);
        }, cb)
        .then(function (_remoteResult) {
            remoteResult = _remoteResult;
            return repository.getBranch(branch);
        }, cb)
        .then(function (ref) {
            return remoteResult.push([ref.toString()], new Git.PushOptions());
        }, cb)
        .then(function (number) {
            cb(null, number);
        }, cb);
};

github網站上提供的示例。

對於fetchhttps//github.com/nodegit/nodegit/blob/master/examples/fetch.js

pullhttps//github.com/nodegit/nodegit/issues/341

repo.fetchAll({
  credentials: function(url, userName) {
    return NodeGit.Cred.sshKeyFromAgent(userName);
  }
}).then(function() {
  repo.mergeBranches("master", "origin/master");
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM