home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume14 / unidiff / part01 / gnudiff.uni next >
Encoding:
Text File  |  1990-08-30  |  7.6 KB  |  286 lines

  1. Index: version.c
  2. Prereq: 1.14";
  3. @@ -1,3 +1,3 @@
  4. =/* Version number of GNU diff.  */
  5. =
  6. -char *version_string = "1.14";
  7. +char *version_string = "1.14u";
  8. Index: analyze.c
  9. @@ -751,5 +751,5 @@
  10. =    {
  11. =      setup_output (files[0].name, files[1].name, depth);
  12. -      if (output_style == OUTPUT_CONTEXT)
  13. +      if (output_style == OUTPUT_CONTEXT || output_patch_flag)
  14. =    print_context_header (files);
  15. =
  16. Index: context.c
  17. @@ -22,4 +22,5 @@
  18. =
  19. =static void pr_context_hunk ();
  20. +static void pr_unidiff_hunk ();
  21. =static struct change *find_hunk ();
  22. =static void mark_ignorable ();
  23. @@ -38,8 +39,19 @@
  24. =     struct file_data *inf;
  25. ={
  26. -  fprintf (outfile, "*** %s\t%s", inf[0].name,
  27. +  if (output_patch_flag)
  28. +    {
  29. +      char *cp = strlen (inf[0].name) <= strlen (inf[1].name)
  30. +        ? inf[0].name : inf[1].name;
  31. +      if (cp[0] == '.' && cp[1] == '/')
  32. +    cp += 2;
  33. +      fprintf (outfile, "Index: %s\n", cp);
  34. +    }
  35. +  else
  36. +    {
  37. +      fprintf (outfile, "*** %s\t%s", inf[0].name,
  38. =       ctime (&inf[0].stat.st_mtime));
  39. -  fprintf (outfile, "--- %s\t%s", inf[1].name,
  40. +      fprintf (outfile, "--- %s\t%s", inf[1].name,
  41. =       ctime (&inf[1].stat.st_mtime));
  42. +    }
  43. =}
  44. =
  45. @@ -62,5 +74,8 @@
  46. =  find_function_last_match = -1;
  47. =
  48. -  print_script (script, find_hunk, pr_context_hunk);
  49. +  if (unidiff_flag)
  50. +    print_script (script, find_hunk, pr_unidiff_hunk);
  51. +  else
  52. +    print_script (script, find_hunk, pr_context_hunk);
  53. =}
  54. =
  55. @@ -189,4 +204,125 @@
  56. =
  57. =      print_1_line (prefix, &files[1].linbuf[i]);
  58. +    }
  59. +    }
  60. +}
  61. +
  62. +/* Print a pair of line numbers with a comma, translated for file FILE.
  63. +   If the second number is smaller, use the first in place of it.
  64. +
  65. +   Args A and B are internal line numbers.
  66. +   We print the translated (real) line numbers.  */
  67. +
  68. +static void
  69. +print_unidiff_number_range (file, a, b)
  70. +     struct file_data *file;
  71. +     int a, b;
  72. +{
  73. +  int trans_a, trans_b;
  74. +  translate_range (file, a, b, &trans_a, &trans_b);
  75. +
  76. +  /* Note: we can have B < A in the case of a range of no lines.
  77. +     In this case, we should print the line number before the range,
  78. +     which is B.  */
  79. +  if (trans_b < trans_a)
  80. +    fprintf (outfile, "%d,0", trans_b);
  81. +  else
  82. +    fprintf (outfile, "%d,%d", trans_a, trans_b - trans_a + 1);
  83. +}
  84. +
  85. +/* Print a portion of an edit script in unidiff format.
  86. +   HUNK is the beginning of the portion to be printed.
  87. +   The end is marked by a `link' that has been nulled out.
  88. +
  89. +   Prints out lines from both files, and precedes each
  90. +   line with the appropriate flag-character.  */
  91. +
  92. +static void
  93. +pr_unidiff_hunk (hunk)
  94. +     struct change *hunk;
  95. +{
  96. +  int first0, last0, first1, last1, show_from, show_to, i, j, k;
  97. +  struct change *next;
  98. +  int lastline;
  99. +  char *function;
  100. +  int function_length;
  101. +
  102. +  /* Determine range of line numbers involved in each file.  */
  103. +
  104. +  analyze_hunk (hunk, &first0, &last0, &first1, &last1, &show_from, &show_to);
  105. +
  106. +  if (!show_from && !show_to)
  107. +    return;
  108. +
  109. +  /* Include a context's width before and after.  */
  110. +
  111. +  first0 = max (first0 - context, 0);
  112. +  first1 = max (first1 - context, 0);
  113. +  last0 = min (last0 + context, files[0].buffered_lines - 1);
  114. +  last1 = min (last1 + context, files[1].buffered_lines - 1);
  115. +
  116. +  /* If desired, find the preceding function definition line in file 0.  */
  117. +  function = 0;
  118. +  if (function_regexp)
  119. +    find_function (&files[0], first0, &function, &function_length);
  120. +
  121. +  /* If we looked for and found a function this is part of,
  122. +     include its name in the header of the diff section.  */
  123. +
  124. +  fprintf (outfile, "@@ -");
  125. +  print_unidiff_number_range (&files[0], first0, last0);
  126. +  fprintf (outfile, " +");
  127. +  print_unidiff_number_range (&files[1], first1, last1);
  128. +  fprintf (outfile, " @@");
  129. +
  130. +  if (function)
  131. +    {
  132. +      putc (' ', outfile);
  133. +      fwrite (function, 1, min (function_length - 1, 40), outfile);
  134. +    }
  135. +  putc ('\n', outfile);
  136. +
  137. +  next = hunk;
  138. +  i = first0;
  139. +  j = first1;
  140. +
  141. +  while (i <= last0 || j <= last1)
  142. +    {
  143. +
  144. +      /* If the line isn't a difference, output the context from file 0. */
  145. +
  146. +      if (!next || i < next->line0)
  147. +    {
  148. +      if (output_patch_flag)
  149. +        putc ('=', outfile);
  150. +      else
  151. +        putc (' ', outfile);
  152. +      print_1_line ("", &files[0].linbuf[i++]);
  153. +      j++;
  154. +    }
  155. +      else
  156. +    {
  157. +      /* For each difference, first output the deleted part. */
  158. +
  159. +      k = next->deleted;
  160. +      while (k--)
  161. +        {
  162. +          putc ('-', outfile);
  163. +          print_1_line ("", &files[0].linbuf[i++]);
  164. +        }
  165. +
  166. +      /* Then output the inserted part. */
  167. +
  168. +      k = next->inserted;
  169. +      while (k--)
  170. +        {
  171. +          putc ('+', outfile);
  172. +          print_1_line ("", &files[1].linbuf[j++]);
  173. +        }
  174. +
  175. +      /* We're done with this hunk, so on to the next! */
  176. +
  177. +      next = next->link;
  178. +
  179. =    }
  180. =    }
  181. Index: diff.c
  182. @@ -96,4 +96,6 @@
  183. =  {"expand-tabs", 0, 0, 't'},
  184. =  {"ignore-all-space", 0, 0, 'w'},
  185. +  {"unidiff", 0, 0, 'u'},
  186. +  {"patch", 0, 0, 'P'},
  187. =  {"version", 0, 0, 'v'},
  188. =  {0, 0, 0, 0}
  189. @@ -133,4 +135,6 @@
  190. =  ifdef_string = NULL;
  191. =  heuristic = FALSE;
  192. +  unidiff_flag = 0;
  193. +  output_patch_flag = 0;
  194. =  dir_start_file = NULL;
  195. =  msg_chain = NULL;
  196. @@ -141,5 +145,5 @@
  197. =
  198. =  while ((c = getopt_long (argc, argv,
  199. -               "0123456789abBcC:dD:efF:hHiI:lnNpqrsS:tTvw",
  200. +               "0123456789abBcC:dD:efF:hHiI:lnNpPqrsS:tTuUvw",
  201. =               longopts, &longind)) != EOF)
  202. =    {
  203. @@ -284,4 +288,8 @@
  204. =      break;
  205. =
  206. +    case 'P':
  207. +      output_patch_flag = 1;
  208. +      break;
  209. +
  210. =    case 'q':
  211. =      no_details_flag = 1;
  212. @@ -322,4 +330,15 @@
  213. =      break;
  214. =
  215. +    case 'U':
  216. +      /* Choose patch-style unidiff. */
  217. +      output_patch_flag = 1;
  218. +
  219. +      /* Falls through.  */
  220. +    case 'u':
  221. +      /* Output the context diff in unidiff format.  */
  222. +      specify_style (OUTPUT_CONTEXT);
  223. +      unidiff_flag = 1;
  224. +      break;
  225. +
  226. =    case 'w':
  227. =      /* Ignore horizontal whitespace when comparing lines.  */
  228. @@ -378,5 +397,5 @@
  229. ={
  230. =  fprintf (stderr, "\
  231. -Usage: diff [-#] [-abBcdefhHilnNprstTvw] [-C lines] [-F regexp] [-I regexp]\n\
  232. +Usage: diff [-#] [-abBcdefhHilnNpPrstTuUvw] [-C lines] [-F regexp] [-I regexp]\n\
  233. =       [-S file] [-D symbol] [+ignore-blank-lines] [+context[=lines]]\n\
  234. =       [+ifdef symbol] [+show-function-line regexp] [+speed-large-files]\n");
  235. @@ -388,5 +407,5 @@
  236. =       [+rcs] [+show-c-function] [+binary] [+brief] [+recursive]\n\
  237. =       [+report-identical-files] [+expand-tabs] [+ignore-all-space]\n\
  238. -       [+version] path1 path2\n");
  239. +       [+unidiff] [+patch] [+version] path1 path2\n");
  240. =  exit (1);
  241. =} 
  242. @@ -429,5 +448,6 @@
  243. =      char *name = name0 == 0 ? name1 : name0;
  244. =      char *dir = name0 == 0 ? dir1 : dir0;
  245. -      message ("Only in %s: %s\n", dir, name);
  246. +      if (!output_patch_flag)
  247. +    message ("Only in %s: %s\n", dir, name);
  248. =      /* Return 1 so that diff_dirs will return 1 ("some files differ").  */
  249. =      return 1;
  250. @@ -535,5 +555,6 @@
  251. =      /* But don't compare dir contents one level down
  252. =         unless -r was specified.  */
  253. -      message ("Common subdirectories: %s and %s\n",
  254. +      if (!output_patch_flag)
  255. +        message ("Common subdirectories: %s and %s\n",
  256. =           inf[0].name, inf[1].name);
  257. =      val = 0;
  258. @@ -601,5 +622,6 @@
  259. =        {
  260. =          char *dir = (inf[0].desc == -1) ? dir1 : dir0;
  261. -          message ("Only in %s: %s\n", dir, name0);
  262. +          if (!output_patch_flag)
  263. +        message ("Only in %s: %s\n", dir, name0);
  264. =          val = 1;
  265. =        }
  266. Index: diff.h
  267. @@ -185,4 +185,10 @@
  268. =EXTERN int    heuristic;
  269. =
  270. +/* Do we want to output the context diff in unidiff style? */
  271. +EXTERN int    unidiff_flag;
  272. +
  273. +/* Reduce extraneous output when they're outputting a patch. */
  274. +EXTERN int    output_patch_flag;
  275. +
  276. =/* Name of program the user invoked (for error messages).  */
  277. =EXTERN char *    program;
  278. Index: util.c
  279. @@ -170,5 +170,5 @@
  280. =      /* If handling multiple files (because scanning a directory),
  281. =     print which files the following output is about.  */
  282. -      if (depth > 0)
  283. +      if (depth > 0 && !output_patch_flag)
  284. =    printf ("%s\n", name);
  285. =    }
  286.