Nodegit: How to modify a file and push the changes?

前端 未结 2 537
抹茶落季
抹茶落季 2020-12-14 21:59

Looked around for an example, but couldn\'t find one. The documentation is not explained and I could not figure it out.

How to modify a file (README.md for example),

2条回答
  •  南笙
    南笙 (楼主)
    2020-12-14 22:20

    Rafael's solution works for sure although I might add that there is no needed to perform a .connect into the server. Here is a minimal approach on pushing a repo:

    • Pushing to master using only username and password authentication:

              //Previous calls
      
              }).then(function() {
                  return repo.getRemote("origin"); //Get origin remote
              }).then(function(remote) {
                  return remote.push(["refs/heads/master:refs/heads/master"], {
                      callbacks: {
                          credentials: function(url, userName) {
                              console.log("Requesting creds");
      
                              return NodeGit.Cred.userpassPlaintextNew("[USERNAME]", "[PASSWORD]");
                          }
                      }
                  });
              }).then(function(err) {
                  console.log("Push error number:");
                  console.log(err);
              }).catch(function(err) {
                  console.log(err);
              }).done(function(commitId) {
                  console.log("All done");
              })
      
    • Pushing to myBranch over SSH auth:

              //Previous calls
      
              }).then(function() {
                  return repo.getRemote("origin"); //Get origin remote
              }).then(function(remote) {
                  return remote.push(["refs/heads/myBranch:refs/heads/myBranch"], {
                      callbacks: {
                          credentials: function(url, userName) {
                              console.log("Requesting creds");
                              return NodeGit.Cred.sshKeyFromAgent(userName);
                          }
                      }
                  });
              }).then(function(err) {
                  console.log("Push error number:");
                  console.log(err);
              }).catch(function(err) {
                  console.log(err);
              }).done(function(commitId) {
                  console.log("All done");
              })
      

提交回复
热议问题