我正在尝试使用nodegit npm包将更改推送到bitbucket repo。
下面是代码
async function createCommit1(commitMsg) {
try {
const repo = await NodeGit.Repository.open(bb.repoPath);
const index = await repo.refreshIndex(); // read latest
const files = await repo.getStatus(); // get status of all files
console.log("file: nodegit.js:212 ~ createCommit1 ~ files:", files);
files.forEach(file => index.addByPath(file.path())); // stage each file
await index.write(); // flush changes to index
const changes = await index.writeTree(); // get reference to a set of changes
const head = await NodeGit.Reference.nameToId(repo, "HEAD"); // get reference to the current state
const parent = await repo.getCommit(head); // get the commit for current state
const author = NodeGit.Signature.now(bb.name, bb.email); // build auth/committer
const committer = NodeGit.Signature.now(bb.name, bb.email);
// combine all info into commit and return hash
const commitId = await repo.createCommit("HEAD", author, committer, commitMsg, changes, [parent]);
console.log('commitId', commitId);
return commitId;
} catch (err) {
console.log("file: nodegit.js:224 ~ createCommit1 ~ err:", err);
throw err;
}
}
function pushChanges1(commitMsg) {
createCommit1(commitMsg)
.then(() => {
let repository;
NodeGit.Repository.open(bb.repoPath)
.then(function (repo) {
repository = repo;
return repository.getRemote("origin");
})
.then(function (remote) {
return remote.push(
["refs/heads/main:refs/heads/main"],
{
callbacks: {
credentials: function (_url, _userName) {
return NodeGit.Cred.userpassPlaintextNew(cred.userName, cred.password);
}
}
}
);
})
.then(() => {
console.log("Local changes pushed successfully!");
})
.catch((err) => {
console.log("file: nodegit.js:197 ~ pushChanges1 ~ err:", err);
console.log("file: nodegit.js:199 ~ pushChanges1 ~ err.errno:", err.errno);
if (err.errno === NodeGit.Error.CODE.EUPDFAIL) {
// Push failed, pull changes
console.log("Push failed, pulling changes...");
return pullChanges3();
} else if (err.errno === NodeGit.Error.CODE.ENOTFOUND) {
// Remote branch not found, clone repository
console.log("Remote branch not found, cloning repository...");
return cloneRepo();
} else {
console.log("Error pushing changes:", err);
}
});
});
}
它正在进行提交和推送(也在bitbucket网站上检查),但没有更新文件内容。在VS代码上,同一文件正在过渡和更改中。
有什么问题?
1条答案
按热度按时间t2a7ltrp1#
我终于得到了解决方案,它正在工作