~ubuntu-branches/ubuntu/lucid/giggle/lucid

« back to all changes in this revision

Viewing changes to src/giggle-git-log.c

  • Committer: Bazaar Package Importer
  • Author(s): Andrea Corradi
  • Date: 2007-05-09 21:16:35 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20070509211635-p0wst0b2b7qdns12
Tags: 0.3-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 
2
/*
 
3
 * Copyright (C) 2007 Imendio AB
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU General Public License as
 
7
 * published by the Free Software Foundation; either version 2 of the
 
8
 * License, or (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
 * General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public
 
16
 * License along with this program; if not, write to the
 
17
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
18
 * Boston, MA 02111-1307, USA.
 
19
 */
 
20
 
 
21
#include <config.h>
 
22
#include <string.h>
 
23
 
 
24
#include "giggle-git-log.h"
 
25
#include "giggle-revision.h"
 
26
 
 
27
typedef struct GiggleGitLogPriv GiggleGitLogPriv;
 
28
 
 
29
struct GiggleGitLogPriv {
 
30
        GiggleRevision *revision;
 
31
        gchar          *log;
 
32
};
 
33
 
 
34
static void     git_log_finalize            (GObject           *object);
 
35
static void     git_log_get_property        (GObject           *object,
 
36
                                             guint              param_id,
 
37
                                             GValue            *value,
 
38
                                             GParamSpec        *pspec);
 
39
static void     git_log_set_property        (GObject           *object,
 
40
                                             guint              param_id,
 
41
                                             const GValue      *value,
 
42
                                             GParamSpec        *pspec);
 
43
 
 
44
static gboolean git_log_get_command_line    (GiggleJob         *job,
 
45
                                             gchar            **command_line);
 
46
static void     git_log_handle_output       (GiggleJob         *job,
 
47
                                             const gchar       *output_str,
 
48
                                             gsize              output_len);
 
49
 
 
50
enum {
 
51
        PROP_0,
 
52
        PROP_REVISION,
 
53
};
 
54
 
 
55
G_DEFINE_TYPE (GiggleGitLog, giggle_git_log, GIGGLE_TYPE_JOB)
 
56
 
 
57
#define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GIGGLE_TYPE_GIT_LOG, GiggleGitLogPriv))
 
58
 
 
59
static void
 
60
giggle_git_log_class_init (GiggleGitLogClass *class)
 
61
{
 
62
        GObjectClass   *object_class = G_OBJECT_CLASS (class);
 
63
        GiggleJobClass *job_class    = GIGGLE_JOB_CLASS (class);
 
64
 
 
65
        object_class->finalize     = git_log_finalize;
 
66
        object_class->get_property = git_log_get_property;
 
67
        object_class->set_property = git_log_set_property;
 
68
 
 
69
        job_class->get_command_line = git_log_get_command_line;
 
70
        job_class->handle_output    = git_log_handle_output;
 
71
 
 
72
        g_object_class_install_property (object_class,
 
73
                                         PROP_REVISION,
 
74
                                         g_param_spec_object ("revision",
 
75
                                                              "revision",
 
76
                                                              "Revision",
 
77
                                                              GIGGLE_TYPE_REVISION,
 
78
                                                              G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
 
79
 
 
80
        g_type_class_add_private (object_class, sizeof (GiggleGitLogPriv));
 
81
}
 
82
 
 
83
static void
 
84
giggle_git_log_init (GiggleGitLog *log)
 
85
{
 
86
}
 
87
 
 
88
static void
 
89
git_log_finalize (GObject *object)
 
90
{
 
91
        GiggleGitLogPriv *priv;
 
92
 
 
93
        priv = GET_PRIV (object);
 
94
        g_object_unref (priv->revision);
 
95
        g_free (priv->log);
 
96
 
 
97
        G_OBJECT_CLASS (giggle_git_log_parent_class)->finalize (object);
 
98
}
 
99
 
 
100
static void
 
101
git_log_get_property (GObject    *object,
 
102
                      guint       param_id,
 
103
                      GValue     *value,
 
104
                      GParamSpec *pspec)
 
105
{
 
106
        GiggleGitLogPriv *priv;
 
107
 
 
108
        priv = GET_PRIV (object);
 
109
 
 
110
        switch (param_id) {
 
111
        case PROP_REVISION:
 
112
                g_value_set_object (value, priv->revision);
 
113
                break;
 
114
        default:
 
115
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
 
116
                break;
 
117
        }
 
118
}
 
119
 
 
120
static void
 
121
git_log_set_property (GObject      *object,
 
122
                      guint         param_id,
 
123
                      const GValue *value,
 
124
                      GParamSpec   *pspec)
 
125
{
 
126
        GiggleGitLogPriv *priv;
 
127
 
 
128
        priv = GET_PRIV (object);
 
129
 
 
130
        switch (param_id) {
 
131
        case PROP_REVISION:
 
132
#if GLIB_MAJOR_VERSION > 2 || GLIB_MINOR_VERSION > 12
 
133
                priv->revision = g_value_dup_object (value);
 
134
#else
 
135
                priv->revision = GIGGLE_REVISION (g_value_dup_object (value));
 
136
#endif
 
137
                break;
 
138
        default:
 
139
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
 
140
                break;
 
141
        }
 
142
}
 
143
 
 
144
static gboolean
 
145
git_log_get_command_line (GiggleJob  *job,
 
146
                          gchar     **command_line)
 
147
{
 
148
        GiggleGitLogPriv *priv;
 
149
        GString          *str;
 
150
        const gchar      *sha;
 
151
 
 
152
        priv = GET_PRIV (job);
 
153
        sha = giggle_revision_get_sha (priv->revision);
 
154
        str = g_string_new (GIT_COMMAND);
 
155
        g_string_append_printf (str, " rev-list --pretty=raw %s^..%s", sha, sha);
 
156
 
 
157
        *command_line = g_string_free (str, FALSE);
 
158
        return TRUE;
 
159
}
 
160
 
 
161
static gchar *
 
162
git_log_parse_log (const gchar *output)
 
163
{
 
164
        gint      i = 0;
 
165
        gchar   **lines;
 
166
        GString  *long_log;
 
167
 
 
168
        lines = g_strsplit (output, "\n", -1);
 
169
        long_log = g_string_new ("");
 
170
 
 
171
        while (lines[i]) {
 
172
                gchar* converted = NULL;
 
173
 
 
174
                if (g_utf8_validate (lines[i], -1, NULL)) {
 
175
                        converted = g_strdup (lines[i]);
 
176
                }
 
177
 
 
178
                if (!converted) {
 
179
                        converted = g_locale_to_utf8 (lines[i], -1,
 
180
                                                      NULL, NULL,
 
181
                                                      NULL); // FIXME: add GError
 
182
                }
 
183
 
 
184
                if (!converted) {
 
185
                        converted = g_filename_to_utf8 (lines[i], -1,
 
186
                                                        NULL, NULL,
 
187
                                                        NULL); // FIXME: add GError
 
188
                }
 
189
 
 
190
                if (!converted) {
 
191
                        converted = g_convert (lines[i], -1,
 
192
                                               "UTF-8", "ISO-8859-15",
 
193
                                               NULL, NULL, NULL); // FIXME: add GError
 
194
                }
 
195
 
 
196
                if (!converted) {
 
197
                        converted = g_strescape (lines[i], "\n\r\\\"\'");
 
198
                }
 
199
 
 
200
                if (!converted) {
 
201
                        g_warning ("Error while converting string");
 
202
                        // FIXME: fallback
 
203
                }
 
204
 
 
205
                if (g_str_has_prefix (converted, " ")) {
 
206
                        g_strstrip (converted);
 
207
                        g_string_append_printf (long_log, "%s\n", converted);
 
208
                }
 
209
 
 
210
                g_free (converted);
 
211
 
 
212
                i++;
 
213
        }
 
214
 
 
215
        return g_string_free (long_log, FALSE);
 
216
}
 
217
 
 
218
static void
 
219
git_log_handle_output (GiggleJob   *job,
 
220
                       const gchar *output_str,
 
221
                       gsize        output_len)
 
222
{
 
223
        GiggleGitLogPriv  *priv;
 
224
        gchar            **lines;
 
225
 
 
226
        priv = GET_PRIV (job);
 
227
 
 
228
        lines = g_strsplit (output_str, "\n", -1);
 
229
        priv->log = git_log_parse_log (output_str);
 
230
        g_strfreev (lines);
 
231
}
 
232
 
 
233
GiggleJob *
 
234
giggle_git_log_new (GiggleRevision *revision)
 
235
{
 
236
        g_return_val_if_fail (GIGGLE_IS_REVISION (revision), NULL);
 
237
 
 
238
        return g_object_new (GIGGLE_TYPE_GIT_LOG,
 
239
                             "revision", revision,
 
240
                             NULL);
 
241
}
 
242
 
 
243
G_CONST_RETURN gchar *
 
244
giggle_git_log_get_log (GiggleGitLog *log)
 
245
{
 
246
        GiggleGitLogPriv *priv;
 
247
 
 
248
        g_return_val_if_fail (GIGGLE_IS_GIT_LOG (log), NULL);
 
249
 
 
250
        priv = GET_PRIV (log);
 
251
 
 
252
        return priv->log;
 
253
}