~ubuntu-branches/ubuntu/lucid/patch/lucid

« back to all changes in this revision

Viewing changes to debian/patches/10_unified-reject-files

  • Committer: Bazaar Package Importer
  • Author(s): Christoph Berg
  • Date: 2009-12-02 10:25:26 UTC
  • mfrom: (5.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20091202102526-5luk0zsqhghu58l2
Tags: 2.6-2
* Update watch file.
* Section: vcs.
* Suggests: diffutils-doc instead of diff-doc, thanks Christoph Anton
  Mitterer for spotting. Closes: #558974.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
Generate unified diff style reject files. Also include the C function names
2
 
in reject files whenever possible.
3
 
 
4
 
        $ cat > f.orig
5
 
        < a() {
6
 
        < 2
7
 
        < 3
8
 
        <
9
 
        < 5
10
 
        < 6
11
 
        < }
12
 
 
13
 
        $ sed -e 's/5/5a/' f.orig > f
14
 
        $ diff -U2 -p f.orig f > f.diff
15
 
        $ sed -e 's/5/5a/' -e 's/6/6x/' f.orig > f
16
 
        $ ./patch -F0 -s --no-backup-if-mismatch f --unified-reject-files < f.diff
17
 
        > 1 out of 1 hunk FAILED -- saving rejects to file f.rej
18
 
 
19
 
        $ cat f.rej
20
 
        > @@ -3,5 +3,5 @@ a() {
21
 
        >  3
22
 
        >  
23
 
        > -5
24
 
        > +5a
25
 
        >  6
26
 
        >  }
27
 
 
28
 
        $ ./patch -F0 -s --no-backup-if-mismatch f < f.diff
29
 
        > 1 out of 1 hunk FAILED -- saving rejects to file f.rej
30
 
 
31
 
        $ cat f.rej
32
 
        > *************** a() {
33
 
        > *** 3,7 ****
34
 
        >   3
35
 
        >   
36
 
        > - 5
37
 
        >   6
38
 
        >   }
39
 
        > --- 3,7 ----
40
 
        >   3
41
 
        >   
42
 
        > + 5a
43
 
        >   6
44
 
        >   }
45
 
 
46
 
        $ diff -Nu -p /dev/null f.orig > f2.diff
47
 
        $ ./patch -F0 -s --no-backup-if-mismatch f --unified-reject-files < f2.diff
48
 
        > Patch attempted to create file f, which already exists.
49
 
        > 1 out of 1 hunk FAILED -- saving rejects to file f.rej
50
 
 
51
 
        $ cat f.rej
52
 
        > @@ -0,0 +1,7 @@
53
 
        > +a() {
54
 
        > +2
55
 
        > +3
56
 
        > +
57
 
        > +5
58
 
        > +6
59
 
        > +}
60
 
 
61
 
        $ rm -f f f.orig f.rej f.diff f2.diff
62
 
 
63
 
diff -urNad patch~/common.h patch/common.h
64
 
--- patch~/common.h     2006-01-28 18:58:09.000000000 +0100
65
 
+++ patch/common.h      2006-01-28 18:58:55.304946176 +0100
66
 
@@ -146,6 +146,7 @@
67
 
 XTERN struct stat instat;
68
 
 XTERN bool dry_run;
69
 
 XTERN bool posixly_correct;
70
 
+XTERN bool unified_reject_files;
71
 
 
72
 
 XTERN char const *origprae;
73
 
 XTERN char const *origbase;
74
 
diff -urNad patch~/patch.c patch/patch.c
75
 
--- patch~/patch.c      2006-01-28 18:58:25.000000000 +0100
76
 
+++ patch/patch.c       2006-01-28 19:16:19.624536664 +0100
77
 
@@ -482,7 +482,7 @@
78
 
     skip_rest_of_patch = false;
79
 
 }
80
 
 
81
 
-static char const shortopts[] = "bB:cd:D:eEfF:g:i:lnNo:p:r:RstTuvV:x:Y:z:Z";
82
 
+static char const shortopts[] = "bB:cd:D:eEfF:g:i:lnNo:p:r:RstTuUvV:x:Y:z:Z";
83
 
 static struct option const longopts[] =
84
 
 {
85
 
   {"backup", no_argument, NULL, 'b'},
86
 
@@ -508,6 +508,7 @@
87
 
   {"batch", no_argument, NULL, 't'},
88
 
   {"set-time", no_argument, NULL, 'T'},
89
 
   {"unified", no_argument, NULL, 'u'},
90
 
+  {"unified-reject-files", no_argument, NULL, 'U'},
91
 
   {"version", no_argument, NULL, 'v'},
92
 
   {"version-control", required_argument, NULL, 'V'},
93
 
   {"debug", required_argument, NULL, 'x'},
94
 
@@ -580,6 +581,7 @@
95
 
 "  --verbose  Output extra information about the work being done.",
96
 
 "  --dry-run  Do not actually change any files; just print what would happen.",
97
 
 "  --posix  Conform to the POSIX standard.",
98
 
+"  -U  --unified-reject-files  Create unified reject files.",
99
 
 "",
100
 
 "  -d DIR  --directory=DIR  Change the working directory to DIR first.",
101
 
 #if HAVE_SETMODE_DOS
102
 
@@ -718,6 +720,9 @@
103
 
            case 'u':
104
 
                diff_type = UNI_DIFF;
105
 
                break;
106
 
+           case 'U':
107
 
+               unified_reject_files = true;
108
 
+               break;
109
 
            case 'v':
110
 
                version();
111
 
                exit (0);
112
 
@@ -927,6 +932,24 @@
113
 
     return 0;
114
 
 }
115
 
 
116
 
+static char *
117
 
+format_linerange (char rangebuf[LINENUM_LENGTH_BOUND*2 + 2],
118
 
+                 LINENUM first, LINENUM lines)
119
 
+{
120
 
+    if (lines == 1)
121
 
+      rangebuf = format_linenum (rangebuf, first);
122
 
+    else
123
 
+      {
124
 
+       char *rb;
125
 
+       rangebuf = format_linenum (rangebuf + LINENUM_LENGTH_BOUND + 1, lines);
126
 
+       rb = rangebuf-1;
127
 
+       rangebuf = format_linenum (rangebuf - LINENUM_LENGTH_BOUND - 1,
128
 
+                                  (lines > 0) ? first : 0);
129
 
+       *rb = ',';
130
 
+      }
131
 
+    return rangebuf;
132
 
+}
133
 
+
134
 
 /* We did not find the pattern, dump out the hunk so they can handle it. */
135
 
 
136
 
 static void
137
 
@@ -943,8 +966,83 @@
138
 
       (int) NEW_CONTEXT_DIFF <= (int) diff_type ? " ****" : "";
139
 
     char const *minuses =
140
 
       (int) NEW_CONTEXT_DIFF <= (int) diff_type ? " ----" : " -----";
141
 
+    char const *function = pch_c_function();
142
 
+    if (function == NULL)
143
 
+       function = "";
144
 
 
145
 
-    fprintf(rejfp, "***************\n");
146
 
+    if (unified_reject_files)
147
 
+      {
148
 
+       /* produce unified reject files */
149
 
+       char rangebuf0[LINENUM_LENGTH_BOUND*2 + 2];
150
 
+       char rangebuf1[LINENUM_LENGTH_BOUND*2 + 2];
151
 
+       LINENUM j;
152
 
+
153
 
+       /* Find the beginning of the remove and insert section. */
154
 
+       for (j = 0; j <= pat_end; j++)
155
 
+         if (pch_char (j) == '=')
156
 
+           break;
157
 
+       for (i = j+1; i <= pat_end; i++)
158
 
+         if (pch_char (i) == '^')
159
 
+           break;
160
 
+       if (pch_char (0) != '*' || j > pat_end || i > pat_end+1)
161
 
+         fatal ("internal error in abort_hunk");
162
 
+       i = 1; j++;
163
 
+
164
 
+       /* @@ -from,lines +to,lines @@ */
165
 
+       fprintf (rejfp, "@@ -%s +%s @@%s\n",
166
 
+                format_linerange (rangebuf0, oldfirst, pch_ptrn_lines()),
167
 
+                format_linerange (rangebuf1, newfirst, pch_repl_lines()),
168
 
+                function);
169
 
+
170
 
+       while (   (i <= pat_end && pch_char (i) != '=')
171
 
+              || (j <= pat_end && pch_char (j) != '^'))
172
 
+         {
173
 
+           if (i <= pat_end
174
 
+               && (pch_char (i) == '-' || pch_char (i) == '!'))
175
 
+             {
176
 
+               fputc('-', rejfp);
177
 
+               pch_write_line (i++, rejfp);
178
 
+             }
179
 
+           else if (j <= pat_end
180
 
+                    && (pch_char (j) == '+' || pch_char (j) == '!'))
181
 
+             {
182
 
+               fputc('+', rejfp);
183
 
+               pch_write_line (j++, rejfp);
184
 
+             }
185
 
+           else if ((i <= pat_end
186
 
+                     && (pch_char (i) == ' ' || pch_char (i) == '\n')) &&
187
 
+                    (j > pat_end
188
 
+                     || (pch_char (j) == ' ' || pch_char (j) == '\n')))
189
 
+             {
190
 
+               /* Unless j is already past the end, lines i and j
191
 
+                  must be equal here.  */
192
 
+
193
 
+               if (pch_char (i) == ' ')
194
 
+                 fputc(' ', rejfp);
195
 
+               pch_write_line (i++, rejfp);
196
 
+               if (j <= pat_end)
197
 
+                 j++;
198
 
+             }
199
 
+           else if ((j <= pat_end &&
200
 
+                    (pch_char (j) == ' ' || pch_char (j) == '\n')) &&
201
 
+                    (pch_char (i) == '='))
202
 
+             {
203
 
+               if (pch_char (j) == ' ')
204
 
+                 fputc(' ', rejfp);
205
 
+               pch_write_line (j++, rejfp);
206
 
+             }
207
 
+           else
208
 
+             fatal ("internal error in abort_hunk");
209
 
+         }
210
 
+
211
 
+       if (ferror (rejfp))
212
 
+         write_fatal ();
213
 
+       return;
214
 
+      }
215
 
+
216
 
+    /* produce context type reject files */
217
 
+   
218
 
+    fprintf(rejfp, "***************%s\n", function);
219
 
     for (i=0; i<=pat_end; i++) {
220
 
        char numbuf0[LINENUM_LENGTH_BOUND + 1];
221
 
        char numbuf1[LINENUM_LENGTH_BOUND + 1];
222
 
diff -urNad patch~/patch.man patch/patch.man
223
 
--- patch~/patch.man    2006-01-28 18:58:25.000000000 +0100
224
 
+++ patch/patch.man     2006-01-28 19:14:48.146443448 +0100
225
 
@@ -517,6 +517,9 @@
226
 
 .B \&.rej
227
 
 file.
228
 
 .TP
229
 
+\fB\-U\fP  or  \fB\*=unified\-reject\-files\fP
230
 
+Produce unified reject files. The default is to produce context type reject files.
231
 
+.TP
232
 
 \fB\-R\fP  or  \fB\*=reverse\fP
233
 
 Assume that this patch was created with the old and new files swapped.
234
 
 (Yes, I'm afraid that does happen occasionally, human nature being what it
235
 
diff -urNad patch~/pch.c patch/pch.c
236
 
--- patch~/pch.c        2006-01-28 18:58:04.000000000 +0100
237
 
+++ patch/pch.c 2006-01-28 18:58:55.303946328 +0100
238
 
@@ -68,6 +68,7 @@
239
 
 static LINENUM p_hunk_beg;             /* line number of current hunk */
240
 
 static LINENUM p_efake = -1;           /* end of faked up lines--don't free */
241
 
 static LINENUM p_bfake = -1;           /* beg of faked up lines */
242
 
+static char *p_c_function;             /* the C function a hunk is in */
243
 
 
244
 
 enum nametype { OLD, NEW, INDEX, NONE };
245
 
 
246
 
@@ -888,6 +889,19 @@
247
 
            next_intuit_at(line_beginning,p_input_line);
248
 
            return chars_read == (size_t) -1 ? -1 : 0;
249
 
        }
250
 
+       s = buf;
251
 
+       while (*s == '*')
252
 
+           s++;
253
 
+       if (*s == ' ')
254
 
+         {
255
 
+           p_c_function = s;
256
 
+           while (*s != '\n')
257
 
+               s++;
258
 
+           *s = '\0';
259
 
+           p_c_function = savestr (p_c_function);
260
 
+         }
261
 
+       else
262
 
+           p_c_function = NULL;
263
 
        p_hunk_beg = p_input_line + 1;
264
 
        while (p_end < p_max) {
265
 
            chars_read = get_line ();
266
 
@@ -1277,8 +1291,18 @@
267
 
        else
268
 
            p_repl_lines = 1;
269
 
        if (*s == ' ') s++;
270
 
-       if (*s != '@')
271
 
+       if (*s++ != '@')
272
 
            malformed ();
273
 
+       if (*s++ == '@' && *s == ' ' && *s != '\0')
274
 
+         {
275
 
+           p_c_function = s;
276
 
+           while (*s != '\n')
277
 
+               s++;
278
 
+           *s = '\0';
279
 
+           p_c_function = savestr (p_c_function);
280
 
+         }
281
 
+       else
282
 
+           p_c_function = NULL;
283
 
        if (!p_ptrn_lines)
284
 
            p_first++;                  /* do append rather than insert */
285
 
        if (!p_repl_lines)
286
 
@@ -1884,6 +1908,12 @@
287
 
     return p_hunk_beg;
288
 
 }
289
 
 
290
 
+char const *
291
 
+pch_c_function (void)
292
 
+{
293
 
+    return p_c_function;
294
 
+}
295
 
+
296
 
 /* Is the newline-terminated line a valid `ed' command for patch
297
 
    input?  If so, return the command character; if not, return 0.
298
 
    This accepts accepts just a subset of the valid commands, but it's
299
 
diff -urNad patch~/pch.h patch/pch.h
300
 
--- patch~/pch.h        2006-01-28 18:58:09.000000000 +0100
301
 
+++ patch/pch.h 2006-01-28 18:58:55.303946328 +0100
302
 
@@ -25,6 +25,7 @@
303
 
 LINENUM pch_end (void);
304
 
 LINENUM pch_first (void);
305
 
 LINENUM pch_hunk_beg (void);
306
 
+char const *pch_c_function (void);
307
 
 LINENUM pch_newfirst (void);
308
 
 LINENUM pch_prefix_context (void);
309
 
 LINENUM pch_ptrn_lines (void);