File: giggle-git-diff.c

package info (click to toggle)
giggle 0.7-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,084 kB
  • sloc: ansic: 23,243; sh: 11,642; makefile: 442; xml: 81
file content (348 lines) | stat: -rw-r--r-- 8,662 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2007 Imendio AB
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"
#include "giggle-git-diff.h"

typedef struct GiggleGitDiffPriv GiggleGitDiffPriv;

struct GiggleGitDiffPriv {
	GiggleRevision *rev1;
	GiggleRevision *rev2;

	GList          *files;
	GiggleRevision *patch_format;

	gchar          *result;
};

static void     git_diff_finalize            (GObject           *object);
static void     git_diff_get_property        (GObject           *object,
					      guint              param_id,
					      GValue            *value,
					      GParamSpec        *pspec);
static void     git_diff_set_property        (GObject           *object,
					      guint              param_id,
					      const GValue      *value,
					      GParamSpec        *pspec);

static gboolean git_diff_get_command_line    (GiggleJob         *job,
					      gchar            **command_line);
static void     git_diff_handle_output       (GiggleJob         *job,
					      const gchar       *output_str,
					      gsize              output_len);

G_DEFINE_TYPE (GiggleGitDiff, giggle_git_diff, GIGGLE_TYPE_JOB)

#define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GIGGLE_TYPE_GIT_DIFF, GiggleGitDiffPriv))

enum {
	PROP_0,
	PROP_REV1,
	PROP_REV2,
	PROP_FILES,
	PROP_PATCH_FORMAT
};

static void
giggle_git_diff_class_init (GiggleGitDiffClass *class)
{
	GObjectClass   *object_class = G_OBJECT_CLASS (class);
	GiggleJobClass *job_class    = GIGGLE_JOB_CLASS (class);

	object_class->finalize     = git_diff_finalize;
	object_class->get_property = git_diff_get_property;
	object_class->set_property = git_diff_set_property;

	job_class->get_command_line = git_diff_get_command_line;
	job_class->handle_output    = git_diff_handle_output;

	g_object_class_install_property (object_class,
					 PROP_REV1,
					 g_param_spec_object ("revision1",
							      "Revision 1",
							      "Revision 1 to make diff on",
							      GIGGLE_TYPE_REVISION,
							      G_PARAM_READWRITE));
	g_object_class_install_property (object_class,
					 PROP_REV2,
					 g_param_spec_object ("revision2",
							      "Revision 2",
							      "Revision 2 to make diff on",
							      GIGGLE_TYPE_REVISION,
							      G_PARAM_READWRITE));
	g_object_class_install_property (object_class,
					 PROP_FILES,
					 g_param_spec_pointer ("files",
							       "Files",
							       "Files list to make diff on",
							       G_PARAM_READWRITE));
	g_object_class_install_property (object_class,
					 PROP_PATCH_FORMAT,
					 g_param_spec_object ("patch-format",
							      "Patch format",
							      "The revision to output a patch format for",
							      GIGGLE_TYPE_REVISION,
							      G_PARAM_READWRITE));

	g_type_class_add_private (object_class, sizeof (GiggleGitDiffPriv));
}

static void
giggle_git_diff_init (GiggleGitDiff *dummy)
{
}

static void
git_diff_finalize (GObject *object)
{
	GiggleGitDiffPriv *priv;

	priv = GET_PRIV (object);

	if (priv->rev1) {
		g_object_unref (priv->rev1);
	}

	if (priv->rev2) {
		g_object_unref (priv->rev2);
	}

	g_free (priv->result);

	g_list_free_full (priv->files, g_free);

	if (priv->patch_format) {
		g_object_unref (priv->patch_format);
	}

	G_OBJECT_CLASS (giggle_git_diff_parent_class)->finalize (object);
}

static void
git_diff_get_property (GObject    *object,
		       guint       param_id,
		       GValue     *value,
		       GParamSpec *pspec)
{
	GiggleGitDiffPriv *priv;

	priv = GET_PRIV (object);

	switch (param_id) {
	case PROP_REV1:
		g_value_set_object (value, priv->rev1);
		break;
	case PROP_REV2:
		g_value_set_object (value, priv->rev2);
		break;
	case PROP_FILES:
		g_value_set_pointer (value, priv->files);
		break;
	case PROP_PATCH_FORMAT:
		g_value_set_object (value, priv->patch_format);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
		break;
	}
}

static void
git_diff_set_property (GObject      *object,
		       guint         param_id,
		       const GValue *value,
		       GParamSpec   *pspec)
{
	GiggleGitDiffPriv *priv;

	priv = GET_PRIV (object);

	switch (param_id) {
	case PROP_REV1:
		if (priv->rev1)
			g_object_unref (priv->rev1);

		priv->rev1 = GIGGLE_REVISION (g_value_dup_object (value));
		break;
	case PROP_REV2:
		if (priv->rev2)
			g_object_unref (priv->rev2);

		priv->rev2 = g_value_dup_object (value);
		break;
	case PROP_FILES:
		priv->files = g_value_get_pointer (value);
		break;
	case PROP_PATCH_FORMAT:
		priv->patch_format = g_value_get_object (value);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
		break;
	}
}

static gboolean
git_diff_get_command_line (GiggleJob *job, gchar **command_line)
{
	GiggleGitDiffPriv *priv;
	GString           *str;
	GList             *files;

	priv = GET_PRIV (job);
	files = priv->files;

	/* NOTE: 
	 * Git currently (20071009) v1.5.2.1 only supports:
	 *
	 *   $ git format-patch
	 *
	 * for COMPLETE changes sets to a branch, you can't use this
	 * command for a selection of files you have changed, also, it
	 * only works for committed work, not uncommitted work AFAICS. 
	 */
	if (priv->patch_format) {
		str = g_string_new (GIT_COMMAND " format-patch");

		g_string_append_printf (str, " %s -1", 
					giggle_revision_get_sha (priv->patch_format));
	} else {
		str = g_string_new (GIT_COMMAND);

		if (priv->rev1) {
			g_string_append_printf (str, " diff-tree -p %s",
						giggle_revision_get_sha (priv->rev1));
		} else if (priv->rev2) {
			g_string_append_printf (str, " diff-tree -p %s^",
						giggle_revision_get_sha (priv->rev2));
		} else {
			g_string_append (str, " diff-index -p HEAD");
		}
		
		if (priv->rev2)
			g_string_append_printf (str, " %s", giggle_revision_get_sha (priv->rev2));

		while (files) {
			g_string_append_printf (str, " %s", (gchar *) files->data);
			files = files->next;
		}
	}

	*command_line = g_string_free (str, FALSE);
	return TRUE;
}

static void
git_diff_handle_output (GiggleJob   *job,
			const gchar *output_str,
			gsize        output_len)
{
	GiggleGitDiffPriv *priv;

	priv = GET_PRIV (job);

	priv->result = g_strdup (output_str);
}

GiggleJob *
giggle_git_diff_new (void)
{
	return g_object_new (GIGGLE_TYPE_GIT_DIFF, NULL);
}

void
giggle_git_diff_set_revisions (GiggleGitDiff  *diff,
			       GiggleRevision *rev1,
			       GiggleRevision *rev2)
{
	g_return_if_fail (GIGGLE_IS_GIT_DIFF (diff));
	g_return_if_fail (!rev1 || GIGGLE_IS_REVISION (rev1));
	g_return_if_fail (!rev2 || GIGGLE_IS_REVISION (rev2));

	g_object_set (diff,
		      "revision1", rev1,
		      "revision2", rev2,
		      NULL);
}

void
giggle_git_diff_set_files (GiggleGitDiff *diff,
			   GList         *files)
{
	GiggleGitDiffPriv *priv;

	g_return_if_fail (GIGGLE_IS_GIT_DIFF (diff));

	priv = GET_PRIV (diff);

	if (priv->files) {
		g_warning ("You have the 'patch-format' property set to TRUE. "
			   "Use of the git-format-patch command does not allow specific files. "
			   "These files will be ignored while 'patch-format' is TRUE.");
	}

	g_object_set (diff,
		      "files", files,
		      NULL);
}

void
giggle_git_diff_set_patch_format (GiggleGitDiff  *diff,
				  GiggleRevision *rev)
{
	GiggleGitDiffPriv *priv;

	g_return_if_fail (GIGGLE_IS_GIT_DIFF (diff));
	g_return_if_fail (GIGGLE_IS_REVISION (rev));

	priv = GET_PRIV (diff);

	if (priv->files) {
		g_warning ("Use of the git-format-patch command does not allow specific files. "
			   "You have files set for this GiggleGitDiff which will be ignored.");
	}

	g_object_set (diff,
		      "patch-format", rev,
		      NULL);
}

GiggleRevision *
giggle_git_diff_get_patch_format (GiggleGitDiff *diff)
{
	GiggleGitDiffPriv *priv;

	g_return_val_if_fail (GIGGLE_IS_GIT_DIFF (diff), NULL);

	priv = GET_PRIV (diff);

	return priv->patch_format;
}

const gchar *
giggle_git_diff_get_result (GiggleGitDiff *diff)
{
	GiggleGitDiffPriv *priv;

	g_return_val_if_fail (GIGGLE_IS_GIT_DIFF (diff), NULL);

	priv = GET_PRIV (diff);

	return priv->result;
}