1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4 * Copyright (C) James Liggett 2008 <jrliggett@cox.net>
6 * anjuta is free software.
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)
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-status-command.h"
27 #define STATUS_REGEX "(modified|new file|deleted|unmerged): (.*)"
28 #define SECTION_COMMIT_REGEX "Changes to be committed:"
29 #define SECTION_NOT_UPDATED_REGEX "Changed but not updated:"
31 struct _GitStatusCommandPriv
34 GHashTable
*path_lookup_table
;
35 GitStatusSections sections
;
36 GitStatusSections current_section
;
38 GRegex
*section_commit_regex
;
39 GRegex
*section_not_updated_regex
;
42 G_DEFINE_TYPE (GitStatusCommand
, git_status_command
, GIT_TYPE_COMMAND
);
45 git_status_command_run (AnjutaCommand
*command
)
47 git_command_add_arg (GIT_COMMAND (command
), "status");
53 git_status_command_handle_output (GitCommand
*git_command
, const gchar
*output
)
55 GitStatusCommand
*self
;
56 GMatchInfo
*match_info
;
57 GitStatus
*status_object
;
61 self
= GIT_STATUS_COMMAND (git_command
);
63 /* See if the section has changed */
64 if (g_regex_match (self
->priv
->section_commit_regex
, output
, 0, NULL
))
66 self
->priv
->current_section
= GIT_STATUS_SECTION_COMMIT
;
68 else if (g_regex_match (self
->priv
->section_not_updated_regex
, output
, 0,
71 self
->priv
->current_section
= GIT_STATUS_SECTION_NOT_UPDATED
;
74 if (self
->priv
->sections
& self
->priv
->current_section
)
76 if (g_regex_match (self
->priv
->status_regex
, output
, 0, &match_info
))
78 /* Git sometimes mentions paths twice in status output. This can
79 * happen, for example, where there is a conflict, in which case a
80 * path would show up as both "unmerged" and "modified." */
82 status
= g_match_info_fetch (match_info
, 1);
83 path
= g_match_info_fetch (match_info
, 2);
86 if (!g_hash_table_lookup_extended (self
->priv
->path_lookup_table
,
89 status_object
= git_status_new (path
, status
);
90 g_queue_push_tail (self
->priv
->status_queue
, status_object
);
91 g_hash_table_insert (self
->priv
->path_lookup_table
,
92 g_strdup (path
), NULL
);
93 anjuta_command_notify_data_arrived (ANJUTA_COMMAND (git_command
));
98 g_match_info_free (match_info
);
104 git_status_command_init (GitStatusCommand
*self
)
106 self
->priv
= g_new0 (GitStatusCommandPriv
, 1);
107 self
->priv
->status_queue
= g_queue_new ();
108 self
->priv
->path_lookup_table
= g_hash_table_new_full (g_str_hash
,
111 self
->priv
->status_regex
= g_regex_new (STATUS_REGEX
, 0, 0, NULL
);
112 self
->priv
->section_commit_regex
= g_regex_new (SECTION_COMMIT_REGEX
, 0, 0,
114 self
->priv
->section_not_updated_regex
= g_regex_new (SECTION_NOT_UPDATED_REGEX
,
119 git_status_command_finalize (GObject
*object
)
121 GitStatusCommand
*self
;
122 GList
*current_status
;
124 self
= GIT_STATUS_COMMAND (object
);
125 current_status
= self
->priv
->status_queue
->head
;
127 while (current_status
)
129 g_object_unref (current_status
->data
);
130 current_status
= g_list_next (current_status
);
133 g_queue_free (self
->priv
->status_queue
);
134 g_hash_table_destroy (self
->priv
->path_lookup_table
);
135 g_regex_unref (self
->priv
->status_regex
);
136 g_regex_unref (self
->priv
->section_commit_regex
);
137 g_regex_unref (self
->priv
->section_not_updated_regex
);
141 G_OBJECT_CLASS (git_status_command_parent_class
)->finalize (object
);
145 git_status_command_class_init (GitStatusCommandClass
*klass
)
147 GObjectClass
* object_class
= G_OBJECT_CLASS (klass
);
148 GitCommandClass
* parent_class
= GIT_COMMAND_CLASS (klass
);
149 AnjutaCommandClass
* command_class
= ANJUTA_COMMAND_CLASS (klass
);
151 object_class
->finalize
= git_status_command_finalize
;
152 parent_class
->output_handler
= git_status_command_handle_output
;
153 command_class
->run
= git_status_command_run
;
158 git_status_command_new (const gchar
*working_directory
,
159 GitStatusSections sections
)
161 GitStatusCommand
*self
;
163 self
= g_object_new (GIT_TYPE_STATUS_COMMAND
,
164 "working-directory", working_directory
,
165 "single-line-output", TRUE
,
168 self
->priv
->sections
= sections
;
174 git_status_command_get_status_queue (GitStatusCommand
*self
)
176 return self
->priv
->status_queue
;