You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm using libgit2 to manage installation and update of scripts for use with a program. I clone the remote repository to a local repository and already have code to implement git pull (essentially git fetch and then git merge) based on the example code from the libgit2 repository. That's all working fine.
However I have a "Manual update" button. What I want this to do is fetch from the remote, then show the user a list of changes that would be merged to the local repository if they accept the update. This is in case they have a particular favourite script that has been modified in the remote, so they can save a copy from the local repository or whatever, or just decide not to update.
I have been trying to implement this based on the status.c exemplar from libgit2, but it doesn't seem to work and I don't know why. I'm not even sure if git_status_list_new() does what I want.
My code is shown below. I have set things up so I have 2 local clones of the scripts repository (one for making test changes in and one working repository that is used by the program). The working repository is one commit behind the remote. I have fetched but not merged the new commit from remote. At the command line git status shows:
$ git status
On branch main
Your branch is behind 'origin/main' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)
nothing to commit, working tree clean
So at least the command line version of git can see there is a commit waiting to be merged. However when I run my code using git_status_list_new(), I get nothing shown.
Here's the code:
struct status_opts {
git_status_options statusopt;
char *repodir;
char *pathspec[MAX_PATHSPEC];
int npaths;
int format;
int zterm;
int showbranch;
int showsubmod;
int repeat;
};
int preview_update() {
// Initialize libgit2
git_libgit2_init();
// Local directory where the repository will be cloned
const gchar *local_path = get_scripts_repo_path();
git_repository *repo = NULL;
int error = git_repository_open(&repo, local_path);
if (error < 0) {
log_color_message(_("Error opening repository: %s\n"), "red", giterr_last()->message);
return 1;
}
// Fetch changes
lg2_fetch(repo);
git_status_list *status;
struct status_opts o;
git_status_options_init(&o.statusopt, GIT_STATUS_OPTIONS_VERSION);
o.repodir = g_strdup(local_path);
o.format = FORMAT_LONG;
if (git_repository_is_bare(repo)) {
log_color_message(_("Cannot report status on bare repository"), "red",
git_repository_path(repo));
git_repository_free(repo);
git_libgit2_shutdown();
}
/**
* Run status on the repository
*
* We use `git_status_list_new()` to generate a list of status
* information which lets us iterate over it at our
* convenience and extract the data we want to show out of
* each entry.
*/
check_lg2(git_status_list_new(&status, repo, &o.statusopt),
_("libgit2: could not get status"), NULL);
if (o.showbranch)
show_branch(repo, o.format);
print_long(status); // <---- here is where the status should print the uncommitted changes
git_status_list_free(status);
g_free(o.repodir);
debug_print("%s\n", git_pending_commit_buffer->str);
log_message(_("Completed checking pending commits to scripts repository...\n"));
if (!git_pending_commit_buffer)
log_color_message(_("Local repository is up to date.\n"), "green");
git_repository_free(repo);
git_libgit2_shutdown();
return 0;
}
print_long() is barely modified from the libgit2 examples directory except that it prints its output into a glib GString (git_pending_commit_buffer) which is used later on to populate a GtkTextBuffer. The first couple of lines are:
size_t i, maxi = git_status_list_entrycount(status);
printf("status items: %lu\n", maxi);
But the printf call prints "status items: 0", so the git_status_list appears to have 0 entries which is not what I expected, and then obviously as there are 0 entries to parse print_long() doesn't print anything further.
I know the call to (a slightly modified) lg2_fetch() works because despite not being able to get my list of pending changes, the pull functionality based on lg2_fetch() and then lg2_merge() works fine. Perhaps git_status_list_new() doesn't do what I need, or maybe I've misunderstood the exemplar. Anyway, if someone can advise me on how to use libgit2 to do what I want I'd be eternally grateful. Thanks!
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I'm using libgit2 to manage installation and update of scripts for use with a program. I clone the remote repository to a local repository and already have code to implement
git pull
(essentiallygit fetch
and thengit merge
) based on the example code from the libgit2 repository. That's all working fine.However I have a "Manual update" button. What I want this to do is fetch from the remote, then show the user a list of changes that would be merged to the local repository if they accept the update. This is in case they have a particular favourite script that has been modified in the remote, so they can save a copy from the local repository or whatever, or just decide not to update.
I have been trying to implement this based on the status.c exemplar from libgit2, but it doesn't seem to work and I don't know why. I'm not even sure if git_status_list_new() does what I want.
My code is shown below. I have set things up so I have 2 local clones of the scripts repository (one for making test changes in and one working repository that is used by the program). The working repository is one commit behind the remote. I have fetched but not merged the new commit from remote. At the command line
git status
shows:So at least the command line version of git can see there is a commit waiting to be merged. However when I run my code using git_status_list_new(), I get nothing shown.
Here's the code:
print_long()
is barely modified from the libgit2 examples directory except that it prints its output into a glib GString (git_pending_commit_buffer) which is used later on to populate a GtkTextBuffer. The first couple of lines are:But the printf call prints "status items: 0", so the git_status_list appears to have 0 entries which is not what I expected, and then obviously as there are 0 entries to parse print_long() doesn't print anything further.
I know the call to (a slightly modified)
lg2_fetch()
works because despite not being able to get my list of pending changes, the pull functionality based onlg2_fetch()
and thenlg2_merge()
works fine. Perhapsgit_status_list_new()
doesn't do what I need, or maybe I've misunderstood the exemplar. Anyway, if someone can advise me on how to use libgit2 to do what I want I'd be eternally grateful. Thanks!Beta Was this translation helpful? Give feedback.
All reactions