Skip to content

Instantly share code, notes, and snippets.

@brvier
Created October 30, 2014 17:26
Show Gist options
  • Save brvier/9521e1b89837b3382eac to your computer and use it in GitHub Desktop.
Save brvier/9521e1b89837b3382eac to your computer and use it in GitHub Desktop.
int NotesModel::pull() {
/*
git_repository *repo = NULL;
git_strarray remotes = {0};
git_remote *remote = NULL;
git_index *idx = NULL;
git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;
git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT;*/
git_repository *repo = NULL;
git_remote *remote = NULL;
git_merge_head *merge_head = NULL;
try {
//Open Repo
e(git_repository_open(&repo, notesFolder().absolutePath().toUtf8().constData()));
//List repository and add one from Settings if none exists or doesn t have same URI than settings
git_strarray remotes = {0};
git_remote_list(&remotes, repo);
if (remotes.count >= 1) {
for (size_t i = 0; i < remotes.count; i++) {
e(git_remote_load(&remote, repo, remotes.strings[i]));
qDebug() << git_remote_url(remote);
if (QSettings().value("gitRemoteUrl").isValid() && git_remote_url(remote)) {
if (strcmp(git_remote_url(remote),QSettings().value("gitRemoteUrl").toString().toUtf8().constData()) != 0) {
e(git_remote_delete(remote));
e(git_remote_create(&remote, repo, "upstream",
QSettings().value("gitRemoteUrl").toString().toUtf8().constData()));
e(git_remote_save(remote));
}
}
}
} else if (remotes.count == 0) {
e(git_remote_create(&remote, repo, "upstream",
QSettings().value("gitRemoteUrl").toString().toUtf8().constData()));
e(git_remote_save(remote));
}
e(git_remote_load(&remote, repo, "upstream"));
git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
callbacks.credentials = cred_acquire_cb;
e(git_remote_set_callbacks(remote, &callbacks));
e(git_remote_connect(remote, GIT_DIRECTION_FETCH));
int connected = git_remote_connected(remote);
if (connected)
e(git_remote_fetch(remote, NULL, NULL));
git_checkout_options checkout_opt = GIT_CHECKOUT_OPTIONS_INIT;
checkout_opt.checkout_strategy = GIT_CHECKOUT_FORCE;
checkout_opt.file_mode = 0644;
git_merge_options merge_opt = GIT_MERGE_OPTIONS_INIT;
merge_opt.file_favor = GIT_MERGE_FILE_FAVOR_UNION;
const git_remote_head **head = NULL;
size_t size = 0;
e(git_remote_ls(&head, &size, remote));
if (size <= 0)
e(-1);
git_oid oid = head[0]->oid;
e(git_merge_head_from_fetchhead(&merge_head, repo, "master", git_remote_url(remote), &oid));
e(git_merge(repo, (const git_merge_head **)(&merge_head), 1, &merge_opt, &checkout_opt));
//TRY TO COMMIT
/* Create signature */
git_signature *me = NULL;
e(git_signature_now(&me, "sparkleNotes", "sparklenotes@khertan.net"));
//Tree Lookup
git_tree *tree;
git_object *tree_obj;
e(git_revparse_single(&tree_obj, repo, "HEAD^{tree}"));
// Get parent commit
git_oid parentCommitId;
git_commit *parent;
git_oid remoteParentCommitId;
git_commit *remoteParent;
int nparents;
int err;
e(git_reference_name_to_id( &parentCommitId, repo, "ORIG_HEAD" ));
e(git_commit_lookup( &parent, repo, &parentCommitId ));
e(git_reference_name_to_id( &remoteParentCommitId, repo, "MERGE_HEAD" ));
e(git_commit_lookup( &remoteParent, repo, &remoteParentCommitId ));
const git_commit *parents [2] = { parent, remoteParent };
git_oid new_commit_id;
e(git_commit_create(
&new_commit_id,
repo,
"HEAD", /* name of ref to update */
me, /* author */
me, /* committer */
"UTF-8", /* message encoding */
"Modif on notes", /* message */
(git_tree *) tree_obj, /* root tree */
2, /* parent count */
parents)); /* parents */
git_merge_head_free(merge_head);
git_remote_free(remote);
git_repository_free(repo);
}
catch (int error) {
const git_error *err = giterr_last();
if (err != NULL)
qDebug() << QString::number(err->klass) + "\t" + QString(err->message);
giterr_clear();
git_merge_head_free(merge_head);
git_remote_free(remote);
git_repository_free(repo);
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment