debug-manager: use g_spawn_sync() instead of fork() and waitpid()
[anjuta.git] / plugins / git / git-list-tree-command.c
blobda54c2e9589add0773e513de277467fee734203e
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 * anjuta
4 * Copyright (C) James Liggett 2008 <jrliggett@cox.net>
5 *
6 * anjuta is free software.
7 *
8 * You may redistribute it and/or modify it under the terms of the
9 * GNU General Public License, as published by the Free Software
10 * Foundation; either version 2 of the License, or (at your option)
11 * any later version.
13 * anjuta is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 * See the GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with anjuta. If not, write to:
20 * The Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02110-1301, USA.
25 #include "git-list-tree-command.h"
27 #define BLOB_REGEX "blob ([[:xdigit:]]{40})(?:\\t)(.*)"
29 struct _GitListTreeCommandPriv
31 gchar *commit_sha;
32 GRegex *blob_regex;
33 GHashTable *blobs;
36 G_DEFINE_TYPE (GitListTreeCommand, git_list_tree_command, GIT_TYPE_COMMAND);
38 static void
39 git_list_tree_command_init (GitListTreeCommand *self)
41 self->priv = g_new0 (GitListTreeCommandPriv, 1);
43 self->priv->blob_regex = g_regex_new (BLOB_REGEX, 0, 0, NULL);
44 self->priv->blobs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
45 g_free);
48 static void
49 git_list_tree_command_finalize (GObject *object)
51 GitListTreeCommand *self;
53 self = GIT_LIST_TREE_COMMAND (object);
55 g_free (self->priv->commit_sha);
56 g_regex_unref (self->priv->blob_regex);
57 g_hash_table_unref (self->priv->blobs);
59 g_free (self->priv);
61 G_OBJECT_CLASS (git_list_tree_command_parent_class)->finalize (object);
64 static guint
65 git_list_tree_command_run (AnjutaCommand *command)
67 GitListTreeCommand *self;
69 self = GIT_LIST_TREE_COMMAND (command);
71 git_command_add_arg (GIT_COMMAND (command), "ls-tree");
72 git_command_add_arg (GIT_COMMAND (command), self->priv->commit_sha);
74 return 0;
77 static void
78 git_list_tree_command_handle_output (GitCommand *git_command,
79 const gchar *output)
81 GitListTreeCommand *self;
82 GMatchInfo *match_info;
83 gchar *blob_sha;
84 gchar *filename;
86 self = GIT_LIST_TREE_COMMAND (git_command);
87 match_info = NULL;
89 if (g_regex_match (self->priv->blob_regex, output, 0, &match_info))
91 blob_sha = g_match_info_fetch (match_info, 1);
92 filename = g_match_info_fetch (match_info, 2);
94 g_hash_table_insert (self->priv->blobs, g_strdup (filename),
95 g_strdup (blob_sha));
97 g_free (blob_sha);
98 g_free (filename);
101 if (match_info)
102 g_match_info_free (match_info);
105 static void
106 git_list_tree_command_class_init (GitListTreeCommandClass *klass)
108 GObjectClass* object_class = G_OBJECT_CLASS (klass);
109 GitCommandClass* parent_class = GIT_COMMAND_CLASS (klass);
110 AnjutaCommandClass *command_class = ANJUTA_COMMAND_CLASS (klass);
112 object_class->finalize = git_list_tree_command_finalize;
113 parent_class->output_handler = git_list_tree_command_handle_output;
114 command_class->run = git_list_tree_command_run;
118 GitListTreeCommand *
119 git_list_tree_command_new (const gchar *working_directory,
120 const gchar *commit_sha)
122 GitListTreeCommand *self;
124 self = g_object_new (GIT_TYPE_LIST_TREE_COMMAND,
125 "working-directory", working_directory,
126 "single-line-output", TRUE,
127 NULL);
129 self->priv->commit_sha = g_strdup (commit_sha);
131 return self;
134 GHashTable *
135 git_list_tree_command_get_blobs (GitListTreeCommand *self)
137 return g_hash_table_ref (self->priv->blobs);