Skip to content

Instantly share code, notes, and snippets.

@adityadeshpande
Last active July 6, 2020 17:35
Show Gist options
  • Save adityadeshpande/76adfa5b64967f1262f23e6ef7e0c5f1 to your computer and use it in GitHub Desktop.
Save adityadeshpande/76adfa5b64967f1262f23e6ef7e0c5f1 to your computer and use it in GitHub Desktop.
async function InitSync() {
var remote, repo, count, fileNames, fileContent;
nodegit.Repository.open(repoFolder)
.then(function (repoResult) {
repo = repoResult
count = 0;
fileNames = [];
fileContent = {};
if (twoWaySync) {
debug.info(`TwoWaySync is Enabled, fetching, fast-forwarding and merging changes to local.`)
return PullRepo(repo);
}
}).then(async function () {
/// Adding Files
const files = await repo.getStatusExt();
files.forEach(function (file) {
if ((file.isNew() || file.isModified() || file.isTypechange() || file.isRenamed()) &&
file.inWorkingTree()) {
var status = file.isNew() ? "New" : file.isModified() ? "Modified" : file.isTypechange() ? "Type Changed" : file.isRenamed() ? "Renamed" : " -Unknown. ";
var fileID = file.path();
const path = `${dir}${fileID}`;
if (fs.statSync(path).isFile()) {
data = fs.readFileSync(path);
fileContent[fileID] = data;
debug.verbose(`Adding ${fileID} with status ${status}`);
}
}
});
fileNames = Object.keys(fileContent);
count = fileNames.length;
})
.then(async function () {
return repo.refreshIndex()
.then(async function (index) {
if (count > 0) {
return Promise.all(fileNames.map(function (fileName) {
fse.writeFile(
path.join(repo.workdir(), fileName), fileContent[fileName]);
}))
.then(function () {
// This will add all files to the index
return index.addAll(fileNames, nodegit.Index.ADD_OPTION.ADD_CHECK_PATHSPEC);
})
.then(function () {
// this will write files to the index
return index.write();
})
.then(function () {
return index.writeTree();
})
/// COMMIT
.then(function (oidResult) {
oid = oidResult;
return nodegit.Reference.nameToId(repo, "HEAD");
})
.then(function (head) {
return repo.getCommit(head);
})
.then(function (parent) {
var author = nodegit.Signature.now(signature_name, signature_email);
var committer = nodegit.Signature.now(signature_name, signature_email);
var message = `some commit message that makes sense...`;
return repo.createCommit("HEAD", author, committer, message, oid, [parent]);
})
.then(function (commitId) {
debug.verbose(`New Commit: ${commitId}`);
})
/// PULL if TwoWaySync
.then(function () {
if (twoWaySync) {
debug.info(`TwoWaySync is Enabled. Fetching, fast-forwarding and merging changes to local.`)
return PullRepo(repo);
}
})
/// PUSH
.then(function () {
return repo.getRemote('origin');
})
.then(function (remoteResult) {
remote = remoteResult;
// Create the push object for this remote
return remote.push(
["refs/heads/master:refs/heads/master"], //consider have branch as an parameter, i choose to exploite master <grin>
{ callbacks: credentialsCallback }
);
})
.then(function () {
count = 0;
fileNames = [];
fileContent = {};
debug.verbose('remote Pushed!')
})
.catch(function (reason) {
debug.verbose(reason);
});
}
});
}).done(function () {
debug.verbose(`Successfully pushed to server.`)
});
}
async function PullRepo(repo) {
return repo.fetchAll({
callbacks: credentialsCallback
}).then(function () {
return repo.mergeBranches("master", "origin/master");
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment