Ejemplo n.º 1
0
static GgitRepository *
ide_git_vcs_load (IdeGitVcs  *self,
                  GError    **error)
{
  g_autoptr(GFile) location = NULL;
  GgitRepository *repository = NULL;
  IdeContext *context;
  GFile *project_file;

  g_assert (IDE_IS_GIT_VCS (self));

  context = ide_object_get_context (IDE_OBJECT (self));
  project_file = ide_context_get_project_file (context);

  if (!(location = ggit_repository_discover (project_file, error)))
    return NULL;

  if (!(repository = ggit_repository_open (location, error)))
    return NULL;

  /* Only set once, on load. Not thread-safe otherwise. */
  if (self->working_directory == NULL)
    self->working_directory = ggit_repository_get_workdir (repository);

  return repository;
}
Ejemplo n.º 2
0
static void
ide_git_vcs_list_status_worker (GTask        *task,
                                gpointer      source_object,
                                gpointer      task_data,
                                GCancellable *cancellable)
{
  ListStatus *state = task_data;
  g_autoptr(GListStore) store = NULL;
  g_autoptr(GFile) workdir = NULL;
  g_autoptr(GgitRepository) repository = NULL;
  g_autoptr(GgitStatusOptions) options = NULL;
  g_autoptr(GString) pathspec = NULL;
  g_autoptr(GError) error = NULL;
  g_autofree gchar *relative = NULL;
  gchar *strv[] = { NULL, NULL };

  g_assert (G_IS_TASK (task));
  g_assert (IDE_IS_GIT_VCS (source_object));
  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
  g_assert (state != NULL);
  g_assert (G_IS_FILE (state->repository_location));

  if (!(repository = ggit_repository_open (state->repository_location, &error)))
    {
      g_task_return_error (task, g_steal_pointer (&error));
      return;
    }

  if (!(workdir = ggit_repository_get_workdir (repository)))
    {
      g_task_return_new_error (task,
                               G_IO_ERROR,
                               G_IO_ERROR_FAILED,
                               "Failed to locate working directory");
      return;
    }

  g_set_object (&state->workdir, workdir);

  if (state->directory_or_file != NULL)
    relative = g_file_get_relative_path (workdir, state->directory_or_file);

  strv[0] = relative;
  options = ggit_status_options_new (GGIT_STATUS_OPTION_DEFAULT,
                                     GGIT_STATUS_SHOW_INDEX_AND_WORKDIR,
                                     (const gchar **)strv);

  store = g_list_store_new (IDE_TYPE_VCS_FILE_INFO);
  g_set_object (&state->store, store);

  if (!ggit_repository_file_status_foreach (repository,
                                            options,
                                            ide_git_vcs_list_status_cb,
                                            state,
                                            &error))
    g_task_return_error (task, g_steal_pointer (&error));
  else
    g_task_return_pointer (task, g_steal_pointer (&store), g_object_unref);
}
Ejemplo n.º 3
0
int
main (int argc, char *argv[])
{
	GgitRepository *repository;
	GgitOId *oid;
	GError *error = NULL;
	const gchar hex[] = "82576c09c3fac738a54582c6c04a47684882d1a1";
	gchar *oid_str;
	gchar *repo_path;

	g_type_init ();

	if (argc < 2)
	{
		g_message ("Use: ./general path_to_git_repository");
		return 1;
	}

	repo_path = ggit_repository_discover (argv[1], &error);

	if (error != NULL)
	{
		g_message (error->message);
		return 1;
	}

	g_message ("Path repository: %s", repo_path);
	repository = ggit_repository_open (repo_path, &error);
	g_free (repo_path);

	if (error != NULL)
	{
		g_message (error->message);
		return 1;
	}

	g_message ("Working dir: %s", ggit_repository_get_path (repository,
	                                                        GGIT_REPO_PATH_WORKDIR));

	oid = ggit_oid_new_from_string (hex);
	oid_str = ggit_oid_to_string (oid);

	g_message ("OId str: %s", oid_str);

	g_free (oid_str);
	ggit_oid_free (oid);
	g_object_unref (repository);

	return 0;
}
Ejemplo n.º 4
0
static GgitRepository *
ide_git_vcs_load (IdeGitVcs  *self,
                  GError    **error)
{
  g_autofree gchar *uri = NULL;
  g_autoptr(GFile) location = NULL;
  GgitRepository *repository = NULL;
  IdeContext *context;
  GFile *project_file;

  g_assert (IDE_IS_GIT_VCS (self));
  g_assert (error != NULL);

  context = ide_object_get_context (IDE_OBJECT (self));
  project_file = ide_context_get_project_file (context);

  if (!(location = ide_git_vcs_discover (self, project_file, error)))
    {
      if (g_error_matches (*error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
        return NULL;

      g_clear_error (error);

      /* Fallback to libgit2(-glib) discovery */
      if (!(location = ggit_repository_discover (project_file, error)))
        return NULL;
    }

  uri = g_file_get_uri (location);
  g_debug ("Discovered .git location at “%s”", uri);

  if (!(repository = ggit_repository_open (location, error)))
    return NULL;

  /* Note: Only set this once on load, otherwise not thread-safe. */
  if (self->working_directory == NULL)
    self->working_directory = ggit_repository_get_workdir (repository);

  return repository;
}
Ejemplo n.º 5
0
int
main (int   argc,
      char *argv[])
{
    GFile *file;
    GgitRepository *repo;
    GgitRevisionWalker *revwalker;
    GgitRef *head;
    GgitOId *oid;
    gint64 n_revisions = 10;
    GError *err = NULL;

    ggit_init ();

    if (argc < 2 || argc > 3)
    {
        g_print ("Usage: %s path_to_git_repository [N_REVISIONS]\n",
                 argv[0]);
        return 1;
    }

    if (argc == 3)
    {
        n_revisions = g_ascii_strtoll (argv[2], NULL, 10);

        if (n_revisions < -1)
        {
            g_printerr ("Error: invalid number of "
                        "revisions: %" G_GINT64_FORMAT "\n",
                        n_revisions);
            return 1;
        }
    }

    file = g_file_new_for_path (argv[1]);

    if (!g_file_query_exists (file, NULL))
    {
        g_object_unref (file);
        g_printerr ("Error: invalid git repository: %s\n", argv[1]);
        return 1;
    }


    repo = ggit_repository_open (file, &err);
    g_assert_no_error (err);

    revwalker = ggit_revision_walker_new (repo, &err);
    g_assert_no_error (err);

    ggit_revision_walker_set_sort_mode (revwalker,
                                        GGIT_SORT_TIME |
                                        GGIT_SORT_TOPOLOGICAL);

    head = ggit_repository_get_head (repo, &err);
    g_assert_no_error (err);

    oid = ggit_ref_get_target (head);
    ggit_revision_walker_push (revwalker, oid, &err);
    g_assert_no_error (err);

    ggit_oid_free (oid);
    g_object_unref (head);

    while ((oid = ggit_revision_walker_next (revwalker, &err)) != NULL)
    {
        GgitCommit *commit;
        gchar *oid_str;
        GgitSignature *author;
        GgitSignature *committer;
        gchar *author_str;
        gchar *committer_str;
        const gchar *subject;
        const gchar *message;
        GgitCommitParents *commit_parents;

        commit = GGIT_COMMIT (ggit_repository_lookup (repo, oid,
                              GGIT_TYPE_COMMIT,
                              &err));
        g_assert_no_error (err);

        oid_str = ggit_oid_to_string (oid);

        author = ggit_commit_get_author (commit);
        committer = ggit_commit_get_committer (commit);

        author_str = signature_to_string (author);
        committer_str = signature_to_string (committer);

        subject = ggit_commit_get_subject (commit);
        message = ggit_commit_get_message (commit);

        g_print ("SHA:       %s\n"
                 "Author:    %s\n"
                 "Committer: %s\n"
                 "Subject:   %s\n"
                 "Message:   %s\n",
                 oid_str, author_str, committer_str, subject, message);

        commit_parents = ggit_commit_get_parents (commit);

        if (ggit_commit_parents_get_size (commit_parents) > 0)
        {
            GgitCommit *parent_commit;
            GgitTree *commit_tree;
            GgitTree *parent_tree;
            GgitDiff *diff;

            parent_commit = ggit_commit_parents_get (commit_parents, 0);
            commit_tree = ggit_commit_get_tree (commit);
            parent_tree = ggit_commit_get_tree (parent_commit);

            diff = ggit_diff_new_tree_to_tree (repo,
                                               parent_tree,
                                               commit_tree,
                                               NULL, &err);
            g_assert_no_error (err);

            ggit_diff_print (diff, GGIT_DIFF_FORMAT_PATCH, diff_print_cb, NULL, &err);
            g_assert_no_error (err);

            g_object_unref (diff);
            g_object_unref (parent_tree);
            g_object_unref (commit_tree);
            g_object_unref (parent_commit);
        }

        g_print ("----------------------------------------\n");

        g_object_unref (commit_parents);
        g_free (committer_str);
        g_free (author_str);
        g_object_unref (committer);
        g_object_unref (author);
        g_free (oid_str);
        g_object_unref (commit);
        ggit_oid_free (oid);

        if (n_revisions != -1 && --n_revisions == 0)
        {
            break;
        }
    }

    g_assert_no_error (err);

    g_object_unref (revwalker);
    g_object_unref (repo);
    g_object_unref (file);

    return 0;
}