~ubuntu-branches/ubuntu/trusty/anjuta/trusty

« back to all changes in this revision

Viewing changes to plugins/git/git-status-command.c

  • Committer: Package Import Robot
  • Author(s): Robert Ancell
  • Date: 2012-09-04 10:01:50 UTC
  • mfrom: (1.1.51)
  • Revision ID: package-import@ubuntu.com-20120904100150-d8i99pb0n87fb2bj
Tags: 2:3.5.91-0ubuntu1
* New upstream bugfix release
* debian/patches/00git_sourceview_includes.patch 
  - Applied upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 
25
25
#include "git-status-command.h"
26
26
 
27
 
#define STATUS_REGEX "(modified|new file|deleted|unmerged|both modified|both added|both deleted):   (.*)"
28
 
#define UNTRACKED_FILES_REGEX "(?:#\\t)(.*)"
29
 
#define SECTION_COMMIT_REGEX "Changes to be committed:"
30
 
#define SECTION_NOT_UPDATED_REGEX "Changed but not updated:|Changes not staged for commit:"
31
 
#define SECTION_UNTRACKED_REGEX "Untracked files:"
 
27
#define STATUS_REGEX "((M|A|D|U|\\?|\\s){2}) (.*)"
32
28
 
33
29
struct _GitStatusCommandPriv
34
30
{
35
31
        GQueue *status_queue;
36
 
        GHashTable *path_lookup_table;
37
32
        GitStatusSections sections;
38
 
        GitStatusSections current_section;
39
 
        GRegex *current_section_regex;
 
33
        GHashTable *status_codes;
 
34
        GHashTable *conflict_codes;
40
35
        GRegex *status_regex;
41
 
        GRegex *untracked_files_regex;
42
 
        GRegex *section_commit_regex;
43
 
        GRegex *section_not_updated_regex;
44
 
        GRegex *section_untracked_regex;
45
36
        GFileMonitor *head_monitor;
46
37
        GFileMonitor *index_monitor;
47
38
};
52
43
git_status_command_run (AnjutaCommand *command)
53
44
{
54
45
        git_command_add_arg (GIT_COMMAND (command), "status");
 
46
        git_command_add_arg (GIT_COMMAND (command), "--porcelain");
55
47
        
56
48
        return 0;
57
49
}
66
58
        gchar *path;
67
59
        
68
60
        self = GIT_STATUS_COMMAND (git_command);
69
 
        
70
 
        /* See if the section has changed */
71
 
        if (g_regex_match (self->priv->section_commit_regex, output, 0, NULL))
72
 
        {
73
 
                self->priv->current_section = GIT_STATUS_SECTION_COMMIT;
74
 
                self->priv->current_section_regex = self->priv->status_regex;
75
 
                return;
76
 
        }
77
 
        else if (g_regex_match (self->priv->section_not_updated_regex, output, 0, 
78
 
                                                        NULL))
79
 
        {
80
 
                self->priv->current_section = GIT_STATUS_SECTION_NOT_UPDATED;
81
 
                self->priv->current_section_regex = self->priv->status_regex;
82
 
                return;
83
 
        }
84
 
        else if (g_regex_match (self->priv->section_untracked_regex, output, 0, 
85
 
                                                        NULL))
86
 
        {
87
 
                self->priv->current_section = GIT_STATUS_SECTION_UNTRACKED;
88
 
                self->priv->current_section_regex = self->priv->untracked_files_regex;
89
 
                return;
90
 
        }
91
 
        
92
 
        if (self->priv->sections & self->priv->current_section)
93
 
        {
94
 
                if (g_regex_match (self->priv->current_section_regex, output, 0, 
95
 
                                                   &match_info))
96
 
                {
97
 
                        if (self->priv->current_section_regex == self->priv->status_regex)
98
 
                        {
99
 
                                status = g_match_info_fetch (match_info, 1);
100
 
                                path = g_match_info_fetch (match_info, 2);
 
61
        status_object = NULL;
 
62
 
 
63
        if (g_regex_match (self->priv->status_regex, output, 0, &match_info))
 
64
        {
 
65
                /* Determine which section this entry goes in */
 
66
                status = g_match_info_fetch (match_info, 1);
 
67
                path = g_match_info_fetch (match_info, 3);
 
68
 
 
69
                if (status[0] == ' ')
 
70
                {
 
71
                        /* Changed but not updated */
 
72
                        if (self->priv->sections & GIT_STATUS_SECTION_NOT_UPDATED)
 
73
                        {
 
74
                                status_object = git_status_new(path, 
 
75
                                                               GPOINTER_TO_INT (g_hash_table_lookup (self->priv->status_codes, 
 
76
                                                                                                                         GINT_TO_POINTER (status[1]))));
 
77
                        }
 
78
                }
 
79
                else if (status[1] == ' ')
 
80
                {
 
81
                        /* Added to commit */
 
82
                        if (self->priv->sections & GIT_STATUS_SECTION_COMMIT)
 
83
                        {
 
84
                                status_object = git_status_new(path, 
 
85
                                                               GPOINTER_TO_INT (g_hash_table_lookup (self->priv->status_codes, 
 
86
                                                                                                                 GINT_TO_POINTER (status[0]))));
 
87
                        }
 
88
                }
 
89
                else
 
90
                {
 
91
                        /* File may have been added to the index and then changed again in
 
92
                         * the working tree, or it could be a conflict */
 
93
 
 
94
                        /* Unversioned files */
 
95
                        if (status[0] == '?')
 
96
                        {
 
97
                                if (self->priv->sections & GIT_STATUS_SECTION_UNTRACKED)
 
98
                                {
 
99
                                        status_object = git_status_new(path, 
 
100
                                                                           ANJUTA_VCS_STATUS_UNVERSIONED);
 
101
                                }
 
102
                        }
 
103
                        else if (g_hash_table_lookup_extended (self->priv->conflict_codes, status,
 
104
                                                               NULL, NULL))
 
105
                        {
 
106
                                /* Conflicts are put in the changed but not updated section */
 
107
                                if (self->priv->sections & GIT_STATUS_SECTION_NOT_UPDATED)
 
108
                                {
 
109
                                        status_object = git_status_new (path, 
 
110
                                                                        ANJUTA_VCS_STATUS_CONFLICTED);
 
111
                                }
101
112
                        }
102
113
                        else
103
114
                        {
104
 
                                status = g_strdup ("untracked");
105
 
                                path = g_match_info_fetch (match_info, 1);
106
 
                        }
107
 
                        
108
 
                        /* Git sometimes mentions paths twice in status output. This can
109
 
                         * happen, for example, where there is a conflict, in which case a
110
 
                         * path would show up as both "unmerged" and "modified." */
111
 
                        g_strchug (path);
112
 
                        
113
 
                        if (!g_hash_table_lookup_extended (self->priv->path_lookup_table, 
114
 
                                                                                           path, NULL, NULL))
115
 
                        {
116
 
                                status_object = git_status_new (path, status);
117
 
                                g_queue_push_tail (self->priv->status_queue, status_object);
118
 
                                g_hash_table_insert (self->priv->path_lookup_table,  
119
 
                                                                         g_strdup (path), NULL);
120
 
                                anjuta_command_notify_data_arrived (ANJUTA_COMMAND (git_command));
121
 
                        }
122
 
                        
123
 
                        g_free (status);
124
 
                        g_free (path);
125
 
                }
126
 
 
127
 
                g_match_info_free (match_info);
 
115
                                status_object = git_status_new(path, 
 
116
                                                               GPOINTER_TO_INT(g_hash_table_lookup (self->priv->status_codes, 
 
117
                                                                                                    GINT_TO_POINTER (status[0]))));
 
118
                        }
 
119
                            
 
120
                }
 
121
 
 
122
                
 
123
 
 
124
                g_free (status);
 
125
                g_free (path);
 
126
 
 
127
                if (status_object)
 
128
                {
 
129
                        g_queue_push_tail (self->priv->status_queue, status_object);
 
130
                        anjuta_command_notify_data_arrived (ANJUTA_COMMAND (self));
 
131
                }
 
132
                
128
133
        }
 
134
 
 
135
        g_match_info_free (match_info);
 
136
        
129
137
}
130
138
 
131
139
static void
133
141
{
134
142
        self->priv = g_new0 (GitStatusCommandPriv, 1);
135
143
        self->priv->status_queue = g_queue_new ();
136
 
        self->priv->path_lookup_table = g_hash_table_new_full (g_str_hash, 
137
 
                                                                                                                   g_str_equal,
138
 
                                                                                                                   g_free, NULL);
 
144
        
139
145
        self->priv->status_regex = g_regex_new (STATUS_REGEX, 0, 0, NULL);
140
 
        self->priv->untracked_files_regex = g_regex_new (UNTRACKED_FILES_REGEX, 
141
 
                                                                                                         0, 0, NULL);
142
 
        self->priv->section_commit_regex = g_regex_new (SECTION_COMMIT_REGEX, 0, 0,
143
 
                                                                                                        NULL);
144
 
        self->priv->section_not_updated_regex = g_regex_new (SECTION_NOT_UPDATED_REGEX,
145
 
                                                                                                                 0, 0, NULL);
146
 
        self->priv->section_untracked_regex = g_regex_new (SECTION_UNTRACKED_REGEX,
147
 
                                                                                                           0, 0, NULL);
 
146
        self->priv->status_codes = g_hash_table_new (g_direct_hash, g_direct_equal);
 
147
        self->priv->conflict_codes = g_hash_table_new (g_str_hash, g_str_equal);
 
148
 
 
149
        /* Initialize status code hash tables */
 
150
        g_hash_table_insert (self->priv->status_codes, 
 
151
                             GINT_TO_POINTER ('M'),
 
152
                             GINT_TO_POINTER (ANJUTA_VCS_STATUS_MODIFIED));
 
153
 
 
154
        g_hash_table_insert (self->priv->status_codes, 
 
155
                             GINT_TO_POINTER ('A'),
 
156
                             GINT_TO_POINTER (ANJUTA_VCS_STATUS_ADDED));
 
157
 
 
158
        g_hash_table_insert (self->priv->status_codes, 
 
159
                             GINT_TO_POINTER ('D'),
 
160
                             GINT_TO_POINTER (ANJUTA_VCS_STATUS_MODIFIED));
 
161
 
 
162
        /* TODO: Handle each conflict case individually so that we can eventually
 
163
         * give the user more information about the conflict */
 
164
        g_hash_table_insert (self->priv->conflict_codes, "DD", NULL);
 
165
        g_hash_table_insert (self->priv->conflict_codes, "AU", NULL);
 
166
        g_hash_table_insert (self->priv->conflict_codes, "UD", NULL);
 
167
        g_hash_table_insert (self->priv->conflict_codes, "UA", NULL);
 
168
        g_hash_table_insert (self->priv->conflict_codes, "DU", NULL);
 
169
        g_hash_table_insert (self->priv->conflict_codes, "AA", NULL);
 
170
        g_hash_table_insert (self->priv->conflict_codes, "UU", NULL);
148
171
}
149
172
 
150
173
static void
253
276
        git_status_command_clear_output (GIT_STATUS_COMMAND (command));
254
277
}
255
278
 
256
 
static void
257
 
git_status_command_finished (AnjutaCommand *command, guint return_code)
258
 
{
259
 
        GitStatusCommand *self;
260
 
 
261
 
        self = GIT_STATUS_COMMAND (command);
262
 
 
263
 
        g_hash_table_remove_all (self->priv->path_lookup_table);
264
 
 
265
 
        ANJUTA_COMMAND_CLASS (git_status_command_parent_class)->command_finished (command, 
266
 
                                                                                  return_code);
267
 
}
268
279
 
269
280
static void
270
281
git_status_command_finalize (GObject *object)
271
282
{
272
283
        GitStatusCommand *self;
273
 
        GList *current_status;
274
284
        
275
285
        self = GIT_STATUS_COMMAND (object);
276
 
        current_status = self->priv->status_queue->head;
277
286
        
278
287
        git_status_command_clear_output (self);
279
288
        git_status_command_stop_automatic_monitor (ANJUTA_COMMAND (self));
280
289
        
281
290
        g_queue_free (self->priv->status_queue);
282
 
        g_hash_table_destroy (self->priv->path_lookup_table);
283
291
        g_regex_unref (self->priv->status_regex);
284
 
        g_regex_unref (self->priv->untracked_files_regex);
285
 
        g_regex_unref (self->priv->section_commit_regex);
286
 
        g_regex_unref (self->priv->section_not_updated_regex);
287
 
        g_regex_unref (self->priv->section_untracked_regex);
288
292
        
289
293
        g_free (self->priv);
290
294
 
302
306
        parent_class->output_handler = git_status_command_handle_output;
303
307
        command_class->run = git_status_command_run;
304
308
        command_class->data_arrived = git_status_command_data_arrived;
305
 
        command_class->command_finished = git_status_command_finished;
306
309
        command_class->start_automatic_monitor = git_status_command_start_automatic_monitor;
307
310
        command_class->stop_automatic_monitor = git_status_command_stop_automatic_monitor;
308
311
}