File: diffhelpers.c

package info (click to toggle)
mercurial 1.6.4-1%2Bdeb6u1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze-lts
  • size: 17,928 kB
  • ctags: 6,062
  • sloc: python: 44,238; sh: 20,985; tcl: 3,578; ansic: 2,557; lisp: 1,412; makefile: 176; xml: 15
file content (190 lines) | stat: -rw-r--r-- 4,562 bytes parent folder | download | duplicates (2)
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
/*
 * diffhelpers.c - helper routines for mpatch
 *
 * Copyright 2007 Chris Mason <chris.mason@oracle.com>
 *
 * This software may be used and distributed according to the terms
 * of the GNU General Public License v2, incorporated herein by reference.
 */

#include <Python.h>
#include <stdlib.h>
#include <string.h>

#include "util.h"

static char diffhelpers_doc[] = "Efficient diff parsing";
static PyObject *diffhelpers_Error;


/* fixup the last lines of a and b when the patch has no newline at eof */
static void _fix_newline(PyObject *hunk, PyObject *a, PyObject *b)
{
	int hunksz = PyList_Size(hunk);
	PyObject *s = PyList_GET_ITEM(hunk, hunksz-1);
	char *l = PyBytes_AsString(s);
	int alen = PyList_Size(a);
	int blen = PyList_Size(b);
	char c = l[0];
	PyObject *hline;
	int sz = PyBytes_GET_SIZE(s);

	if (sz > 1 && l[sz-2] == '\r')
		/* tolerate CRLF in last line */
		sz -= 1;

	hline = PyBytes_FromStringAndSize(l, sz-1);

	if (c == ' ' || c == '+') {
		PyObject *rline = PyBytes_FromStringAndSize(l + 1, sz - 2);
		PyList_SetItem(b, blen-1, rline);
	}
	if (c == ' ' || c == '-') {
		Py_INCREF(hline);
		PyList_SetItem(a, alen-1, hline);
	}
	PyList_SetItem(hunk, hunksz-1, hline);
}

/* python callable form of _fix_newline */
static PyObject *
fix_newline(PyObject *self, PyObject *args)
{
	PyObject *hunk, *a, *b;
	if (!PyArg_ParseTuple(args, "OOO", &hunk, &a, &b))
		return NULL;
	_fix_newline(hunk, a, b);
	return Py_BuildValue("l", 0);
}

/*
 * read lines from fp into the hunk.  The hunk is parsed into two arrays
 * a and b.  a gets the old state of the text, b gets the new state
 * The control char from the hunk is saved when inserting into a, but not b
 * (for performance while deleting files)
 */
static PyObject *
addlines(PyObject *self, PyObject *args)
{

	PyObject *fp, *hunk, *a, *b, *x;
	int i;
	int lena, lenb;
	int num;
	int todoa, todob;
	char *s, c;
	PyObject *l;
	if (!PyArg_ParseTuple(args, "OOiiOO", &fp, &hunk, &lena, &lenb, &a, &b))
		return NULL;

	while (1) {
		todoa = lena - PyList_Size(a);
		todob = lenb - PyList_Size(b);
		num = todoa > todob ? todoa : todob;
		if (num == 0)
		    break;
		for (i = 0; i < num; i++) {
			x = PyFile_GetLine(fp, 0);
			s = PyBytes_AsString(x);
			c = *s;
			if (strcmp(s, "\\ No newline at end of file\n") == 0) {
				_fix_newline(hunk, a, b);
				continue;
			}
			if (c == '\n') {
				/* Some patches may be missing the control char
				 * on empty lines. Supply a leading space. */
				Py_DECREF(x);
				x = PyBytes_FromString(" \n");
			}
			PyList_Append(hunk, x);
			if (c == '+') {
				l = PyBytes_FromString(s + 1);
				PyList_Append(b, l);
				Py_DECREF(l);
			} else if (c == '-') {
				PyList_Append(a, x);
			} else {
				l = PyBytes_FromString(s + 1);
				PyList_Append(b, l);
				Py_DECREF(l);
				PyList_Append(a, x);
			}
			Py_DECREF(x);
		}
	}
	return Py_BuildValue("l", 0);
}

/*
 * compare the lines in a with the lines in b.  a is assumed to have
 * a control char at the start of each line, this char is ignored in the
 * compare
 */
static PyObject *
testhunk(PyObject *self, PyObject *args)
{

	PyObject *a, *b;
	long bstart;
	int alen, blen;
	int i;
	char *sa, *sb;

	if (!PyArg_ParseTuple(args, "OOl", &a, &b, &bstart))
		return NULL;
	alen = PyList_Size(a);
	blen = PyList_Size(b);
	if (alen > blen - bstart) {
		return Py_BuildValue("l", -1);
	}
	for (i = 0; i < alen; i++) {
		sa = PyBytes_AsString(PyList_GET_ITEM(a, i));
		sb = PyBytes_AsString(PyList_GET_ITEM(b, i + bstart));
		if (strcmp(sa + 1, sb) != 0)
			return Py_BuildValue("l", -1);
	}
	return Py_BuildValue("l", 0);
}

static PyMethodDef methods[] = {
	{"addlines", addlines, METH_VARARGS, "add lines to a hunk\n"},
	{"fix_newline", fix_newline, METH_VARARGS, "fixup newline counters\n"},
	{"testhunk", testhunk, METH_VARARGS, "test lines in a hunk\n"},
	{NULL, NULL}
};

#ifdef IS_PY3K
static struct PyModuleDef diffhelpers_module = {
	PyModuleDef_HEAD_INIT,
	"diffhelpers",
	diffhelpers_doc,
	-1,
	methods
};

PyMODINIT_FUNC PyInit_diffhelpers(void)
{
	PyObject *m;

	m = PyModule_Create(&diffhelpers_module);
	if (m == NULL)
		return NULL;

	diffhelpers_Error = PyErr_NewException("diffhelpers.diffhelpersError",
											NULL, NULL);
	Py_INCREF(diffhelpers_Error);
	PyModule_AddObject(m, "diffhelpersError", diffhelpers_Error);

	return m;
}
#else
PyMODINIT_FUNC
initdiffhelpers(void)
{
	Py_InitModule3("diffhelpers", methods, diffhelpers_doc);
	diffhelpers_Error = PyErr_NewException("diffhelpers.diffhelpersError",
	                                        NULL, NULL);
}
#endif