2 * Copyright (C) the libgit2 contributors. All rights reserved.
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
10 #include "patch_generate.h"
15 #include "git2/sys/diff.h"
18 git_diff_format_t format
;
19 git_diff_line_cb print_cb
;
25 const char *old_prefix
;
26 const char *new_prefix
;
30 int (*strcomp
)(const char *, const char *);
33 static int diff_print_info_init__common(
37 git_diff_format_t format
,
43 pi
->payload
= payload
;
48 pi
->id_strlen
= GIT_ABBREV_DEFAULT
;
49 else if (git_repository__cvar(&pi
->id_strlen
, repo
, GIT_CVAR_ABBREV
) < 0)
53 if (pi
->id_strlen
> GIT_OID_HEXSZ
)
54 pi
->id_strlen
= GIT_OID_HEXSZ
;
56 memset(&pi
->line
, 0, sizeof(pi
->line
));
57 pi
->line
.old_lineno
= -1;
58 pi
->line
.new_lineno
= -1;
59 pi
->line
.num_lines
= 1;
64 static int diff_print_info_init_fromdiff(
68 git_diff_format_t format
,
72 git_repository
*repo
= diff
? diff
->repo
: NULL
;
74 memset(pi
, 0, sizeof(diff_print_info
));
77 pi
->flags
= diff
->opts
.flags
;
78 pi
->id_strlen
= diff
->opts
.id_abbrev
;
79 pi
->old_prefix
= diff
->opts
.old_prefix
;
80 pi
->new_prefix
= diff
->opts
.new_prefix
;
82 pi
->strcomp
= diff
->strcomp
;
85 return diff_print_info_init__common(pi
, out
, repo
, format
, cb
, payload
);
88 static int diff_print_info_init_frompatch(
92 git_diff_format_t format
,
98 memset(pi
, 0, sizeof(diff_print_info
));
100 pi
->flags
= patch
->diff_opts
.flags
;
101 pi
->id_strlen
= patch
->diff_opts
.id_abbrev
;
102 pi
->old_prefix
= patch
->diff_opts
.old_prefix
;
103 pi
->new_prefix
= patch
->diff_opts
.new_prefix
;
105 return diff_print_info_init__common(pi
, out
, patch
->repo
, format
, cb
, payload
);
108 static char diff_pick_suffix(int mode
)
112 else if (GIT_PERMS_IS_EXEC(mode
)) /* -V536 */
113 /* in git, modes are very regular, so we must have 0100755 mode */
119 char git_diff_status_char(git_delta_t status
)
124 case GIT_DELTA_ADDED
: code
= 'A'; break;
125 case GIT_DELTA_DELETED
: code
= 'D'; break;
126 case GIT_DELTA_MODIFIED
: code
= 'M'; break;
127 case GIT_DELTA_RENAMED
: code
= 'R'; break;
128 case GIT_DELTA_COPIED
: code
= 'C'; break;
129 case GIT_DELTA_IGNORED
: code
= 'I'; break;
130 case GIT_DELTA_UNTRACKED
: code
= '?'; break;
131 case GIT_DELTA_UNREADABLE
: code
= 'X'; break;
132 default: code
= ' '; break;
138 static int diff_print_one_name_only(
139 const git_diff_delta
*delta
, float progress
, void *data
)
141 diff_print_info
*pi
= data
;
142 git_buf
*out
= pi
->buf
;
144 GIT_UNUSED(progress
);
146 if ((pi
->flags
& GIT_DIFF_SHOW_UNMODIFIED
) == 0 &&
147 delta
->status
== GIT_DELTA_UNMODIFIED
)
151 git_buf_puts(out
, delta
->new_file
.path
);
152 git_buf_putc(out
, '\n');
153 if (git_buf_oom(out
))
156 pi
->line
.origin
= GIT_DIFF_LINE_FILE_HDR
;
157 pi
->line
.content
= git_buf_cstr(out
);
158 pi
->line
.content_len
= git_buf_len(out
);
160 return pi
->print_cb(delta
, NULL
, &pi
->line
, pi
->payload
);
163 static int diff_print_one_name_status(
164 const git_diff_delta
*delta
, float progress
, void *data
)
166 diff_print_info
*pi
= data
;
167 git_buf
*out
= pi
->buf
;
168 char old_suffix
, new_suffix
, code
= git_diff_status_char(delta
->status
);
169 int(*strcomp
)(const char *, const char *) = pi
->strcomp
?
170 pi
->strcomp
: git__strcmp
;
172 GIT_UNUSED(progress
);
174 if ((pi
->flags
& GIT_DIFF_SHOW_UNMODIFIED
) == 0 && code
== ' ')
177 old_suffix
= diff_pick_suffix(delta
->old_file
.mode
);
178 new_suffix
= diff_pick_suffix(delta
->new_file
.mode
);
182 if (delta
->old_file
.path
!= delta
->new_file
.path
&&
183 strcomp(delta
->old_file
.path
,delta
->new_file
.path
) != 0)
184 git_buf_printf(out
, "%c\t%s%c %s%c\n", code
,
185 delta
->old_file
.path
, old_suffix
, delta
->new_file
.path
, new_suffix
);
186 else if (delta
->old_file
.mode
!= delta
->new_file
.mode
&&
187 delta
->old_file
.mode
!= 0 && delta
->new_file
.mode
!= 0)
188 git_buf_printf(out
, "%c\t%s%c %s%c\n", code
,
189 delta
->old_file
.path
, old_suffix
, delta
->new_file
.path
, new_suffix
);
190 else if (old_suffix
!= ' ')
191 git_buf_printf(out
, "%c\t%s%c\n", code
, delta
->old_file
.path
, old_suffix
);
193 git_buf_printf(out
, "%c\t%s\n", code
, delta
->old_file
.path
);
194 if (git_buf_oom(out
))
197 pi
->line
.origin
= GIT_DIFF_LINE_FILE_HDR
;
198 pi
->line
.content
= git_buf_cstr(out
);
199 pi
->line
.content_len
= git_buf_len(out
);
201 return pi
->print_cb(delta
, NULL
, &pi
->line
, pi
->payload
);
204 static int diff_print_one_raw(
205 const git_diff_delta
*delta
, float progress
, void *data
)
207 diff_print_info
*pi
= data
;
208 git_buf
*out
= pi
->buf
;
210 char code
= git_diff_status_char(delta
->status
);
211 char start_oid
[GIT_OID_HEXSZ
+1], end_oid
[GIT_OID_HEXSZ
+1];
213 GIT_UNUSED(progress
);
215 if ((pi
->flags
& GIT_DIFF_SHOW_UNMODIFIED
) == 0 && code
== ' ')
220 id_abbrev
= delta
->old_file
.mode
? delta
->old_file
.id_abbrev
:
221 delta
->new_file
.id_abbrev
;
223 if (pi
->id_strlen
> id_abbrev
) {
224 giterr_set(GITERR_PATCH
,
225 "The patch input contains %d id characters (cannot print %d)",
226 id_abbrev
, pi
->id_strlen
);
230 git_oid_tostr(start_oid
, pi
->id_strlen
+ 1, &delta
->old_file
.id
);
231 git_oid_tostr(end_oid
, pi
->id_strlen
+ 1, &delta
->new_file
.id
);
234 out
, (pi
->id_strlen
<= GIT_OID_HEXSZ
) ?
235 ":%06o %06o %s... %s... %c" : ":%06o %06o %s %s %c",
236 delta
->old_file
.mode
, delta
->new_file
.mode
, start_oid
, end_oid
, code
);
238 if (delta
->similarity
> 0)
239 git_buf_printf(out
, "%03u", delta
->similarity
);
241 if (delta
->old_file
.path
!= delta
->new_file
.path
)
243 out
, "\t%s %s\n", delta
->old_file
.path
, delta
->new_file
.path
);
246 out
, "\t%s\n", delta
->old_file
.path
?
247 delta
->old_file
.path
: delta
->new_file
.path
);
249 if (git_buf_oom(out
))
252 pi
->line
.origin
= GIT_DIFF_LINE_FILE_HDR
;
253 pi
->line
.content
= git_buf_cstr(out
);
254 pi
->line
.content_len
= git_buf_len(out
);
256 return pi
->print_cb(delta
, NULL
, &pi
->line
, pi
->payload
);
259 static int diff_print_modes(
260 git_buf
*out
, const git_diff_delta
*delta
)
262 git_buf_printf(out
, "old mode %o\n", delta
->old_file
.mode
);
263 git_buf_printf(out
, "new mode %o\n", delta
->new_file
.mode
);
265 return git_buf_oom(out
) ? -1 : 0;
268 static int diff_print_oid_range(
269 git_buf
*out
, const git_diff_delta
*delta
, int id_strlen
)
271 char start_oid
[GIT_OID_HEXSZ
+1], end_oid
[GIT_OID_HEXSZ
+1];
273 if (delta
->old_file
.mode
&&
274 id_strlen
> delta
->old_file
.id_abbrev
) {
275 giterr_set(GITERR_PATCH
,
276 "The patch input contains %d id characters (cannot print %d)",
277 delta
->old_file
.id_abbrev
, id_strlen
);
281 if ((delta
->new_file
.mode
&&
282 id_strlen
> delta
->new_file
.id_abbrev
)) {
283 giterr_set(GITERR_PATCH
,
284 "The patch input contains %d id characters (cannot print %d)",
285 delta
->new_file
.id_abbrev
, id_strlen
);
289 git_oid_tostr(start_oid
, id_strlen
+ 1, &delta
->old_file
.id
);
290 git_oid_tostr(end_oid
, id_strlen
+ 1, &delta
->new_file
.id
);
292 if (delta
->old_file
.mode
== delta
->new_file
.mode
) {
293 git_buf_printf(out
, "index %s..%s %o\n",
294 start_oid
, end_oid
, delta
->old_file
.mode
);
296 if (delta
->old_file
.mode
== 0)
297 git_buf_printf(out
, "new file mode %o\n", delta
->new_file
.mode
);
298 else if (delta
->new_file
.mode
== 0)
299 git_buf_printf(out
, "deleted file mode %o\n", delta
->old_file
.mode
);
301 diff_print_modes(out
, delta
);
303 git_buf_printf(out
, "index %s..%s\n", start_oid
, end_oid
);
306 return git_buf_oom(out
) ? -1 : 0;
309 static int diff_delta_format_path(
310 git_buf
*out
, const char *prefix
, const char *filename
)
312 if (git_buf_joinpath(out
, prefix
, filename
) < 0)
315 return git_buf_quote(out
);
318 static int diff_delta_format_with_paths(
320 const git_diff_delta
*delta
,
321 const char *template,
325 if (git_oid_iszero(&delta
->old_file
.id
))
326 oldpath
= "/dev/null";
328 if (git_oid_iszero(&delta
->new_file
.id
))
329 newpath
= "/dev/null";
331 return git_buf_printf(out
, template, oldpath
, newpath
);
334 int diff_delta_format_similarity_header(
336 const git_diff_delta
*delta
)
338 git_buf old_path
= GIT_BUF_INIT
, new_path
= GIT_BUF_INIT
;
342 if (delta
->similarity
> 100) {
343 giterr_set(GITERR_PATCH
, "invalid similarity %d", delta
->similarity
);
348 if (delta
->status
== GIT_DELTA_RENAMED
)
350 else if (delta
->status
== GIT_DELTA_COPIED
)
355 if ((error
= git_buf_puts(&old_path
, delta
->old_file
.path
)) < 0 ||
356 (error
= git_buf_puts(&new_path
, delta
->new_file
.path
)) < 0 ||
357 (error
= git_buf_quote(&old_path
)) < 0 ||
358 (error
= git_buf_quote(&new_path
)) < 0)
362 "similarity index %d%%\n"
369 if (git_buf_oom(out
))
373 git_buf_free(&old_path
);
374 git_buf_free(&new_path
);
379 static bool delta_is_unchanged(const git_diff_delta
*delta
)
381 if (git_oid_iszero(&delta
->old_file
.id
) &&
382 git_oid_iszero(&delta
->new_file
.id
))
385 if (delta
->old_file
.mode
== GIT_FILEMODE_COMMIT
||
386 delta
->new_file
.mode
== GIT_FILEMODE_COMMIT
)
389 if (git_oid_equal(&delta
->old_file
.id
, &delta
->new_file
.id
))
395 int git_diff_delta__format_file_header(
397 const git_diff_delta
*delta
,
402 git_buf old_path
= GIT_BUF_INIT
, new_path
= GIT_BUF_INIT
;
403 bool unchanged
= delta_is_unchanged(delta
);
407 oldpfx
= DIFF_OLD_PREFIX_DEFAULT
;
409 newpfx
= DIFF_NEW_PREFIX_DEFAULT
;
411 id_strlen
= GIT_ABBREV_DEFAULT
;
413 if ((error
= diff_delta_format_path(
414 &old_path
, oldpfx
, delta
->old_file
.path
)) < 0 ||
415 (error
= diff_delta_format_path(
416 &new_path
, newpfx
, delta
->new_file
.path
)) < 0)
421 git_buf_printf(out
, "diff --git %s %s\n",
422 old_path
.ptr
, new_path
.ptr
);
424 if (delta
->status
== GIT_DELTA_RENAMED
||
425 (delta
->status
== GIT_DELTA_COPIED
&& unchanged
)) {
426 if ((error
= diff_delta_format_similarity_header(out
, delta
)) < 0)
431 if ((error
= diff_print_oid_range(out
, delta
, id_strlen
)) < 0)
434 if ((delta
->flags
& GIT_DIFF_FLAG_BINARY
) == 0)
435 diff_delta_format_with_paths(out
, delta
,
436 "--- %s\n+++ %s\n", old_path
.ptr
, new_path
.ptr
);
439 if (unchanged
&& delta
->old_file
.mode
!= delta
->new_file
.mode
)
440 diff_print_modes(out
, delta
);
442 if (git_buf_oom(out
))
446 git_buf_free(&old_path
);
447 git_buf_free(&new_path
);
452 static int format_binary(
454 git_diff_binary_t type
,
459 const char *typename
= type
== GIT_DIFF_BINARY_DELTA
?
461 const char *scan
, *end
;
463 git_buf_printf(pi
->buf
, "%s %" PRIuZ
"\n", typename
, inflatedlen
);
464 pi
->line
.num_lines
++;
466 for (scan
= data
, end
= data
+ datalen
; scan
< end
; ) {
467 size_t chunk_len
= end
- scan
;
472 git_buf_putc(pi
->buf
, (char)chunk_len
+ 'A' - 1);
474 git_buf_putc(pi
->buf
, (char)chunk_len
- 26 + 'a' - 1);
476 git_buf_encode_base85(pi
->buf
, scan
, chunk_len
);
477 git_buf_putc(pi
->buf
, '\n');
479 if (git_buf_oom(pi
->buf
))
483 pi
->line
.num_lines
++;
485 git_buf_putc(pi
->buf
, '\n');
490 static int diff_print_patch_file_binary_noshow(
491 diff_print_info
*pi
, git_diff_delta
*delta
,
492 const char *old_pfx
, const char *new_pfx
)
494 git_buf old_path
= GIT_BUF_INIT
, new_path
= GIT_BUF_INIT
;
497 if ((error
= diff_delta_format_path(
498 &old_path
, old_pfx
, delta
->old_file
.path
)) < 0 ||
499 (error
= diff_delta_format_path(
500 &new_path
, new_pfx
, delta
->new_file
.path
)) < 0)
504 pi
->line
.num_lines
= 1;
505 error
= diff_delta_format_with_paths(
506 pi
->buf
, delta
, "Binary files %s and %s differ\n",
507 old_path
.ptr
, new_path
.ptr
);
510 git_buf_free(&old_path
);
511 git_buf_free(&new_path
);
516 static int diff_print_patch_file_binary(
517 diff_print_info
*pi
, git_diff_delta
*delta
,
518 const char *old_pfx
, const char *new_pfx
,
519 const git_diff_binary
*binary
)
521 size_t pre_binary_size
;
524 if ((pi
->flags
& GIT_DIFF_SHOW_BINARY
) == 0)
525 return diff_print_patch_file_binary_noshow(
526 pi
, delta
, old_pfx
, new_pfx
);
528 if (binary
->new_file
.datalen
== 0 && binary
->old_file
.datalen
== 0)
531 pre_binary_size
= pi
->buf
->size
;
532 git_buf_printf(pi
->buf
, "GIT binary patch\n");
533 pi
->line
.num_lines
++;
535 if ((error
= format_binary(pi
, binary
->new_file
.type
, binary
->new_file
.data
,
536 binary
->new_file
.datalen
, binary
->new_file
.inflatedlen
)) < 0 ||
537 (error
= format_binary(pi
, binary
->old_file
.type
, binary
->old_file
.data
,
538 binary
->old_file
.datalen
, binary
->old_file
.inflatedlen
)) < 0) {
540 if (error
== GIT_EBUFS
) {
542 git_buf_truncate(pi
->buf
, pre_binary_size
);
544 return diff_print_patch_file_binary_noshow(
545 pi
, delta
, old_pfx
, new_pfx
);
549 pi
->line
.num_lines
++;
553 static int diff_print_patch_file(
554 const git_diff_delta
*delta
, float progress
, void *data
)
557 diff_print_info
*pi
= data
;
559 pi
->old_prefix
? pi
->old_prefix
: DIFF_OLD_PREFIX_DEFAULT
;
561 pi
->new_prefix
? pi
->new_prefix
: DIFF_NEW_PREFIX_DEFAULT
;
563 bool binary
= (delta
->flags
& GIT_DIFF_FLAG_BINARY
) ||
564 (pi
->flags
& GIT_DIFF_FORCE_BINARY
);
565 bool show_binary
= !!(pi
->flags
& GIT_DIFF_SHOW_BINARY
);
566 int id_strlen
= binary
&& show_binary
?
567 GIT_OID_HEXSZ
: pi
->id_strlen
;
569 GIT_UNUSED(progress
);
571 if (S_ISDIR(delta
->new_file
.mode
) ||
572 delta
->status
== GIT_DELTA_UNMODIFIED
||
573 delta
->status
== GIT_DELTA_IGNORED
||
574 delta
->status
== GIT_DELTA_UNREADABLE
||
575 (delta
->status
== GIT_DELTA_UNTRACKED
&&
576 (pi
->flags
& GIT_DIFF_SHOW_UNTRACKED_CONTENT
) == 0))
579 if ((error
= git_diff_delta__format_file_header(
580 pi
->buf
, delta
, oldpfx
, newpfx
, id_strlen
)) < 0)
583 pi
->line
.origin
= GIT_DIFF_LINE_FILE_HDR
;
584 pi
->line
.content
= git_buf_cstr(pi
->buf
);
585 pi
->line
.content_len
= git_buf_len(pi
->buf
);
587 return pi
->print_cb(delta
, NULL
, &pi
->line
, pi
->payload
);
590 static int diff_print_patch_binary(
591 const git_diff_delta
*delta
,
592 const git_diff_binary
*binary
,
595 diff_print_info
*pi
= data
;
596 const char *old_pfx
=
597 pi
->old_prefix
? pi
->old_prefix
: DIFF_OLD_PREFIX_DEFAULT
;
598 const char *new_pfx
=
599 pi
->new_prefix
? pi
->new_prefix
: DIFF_NEW_PREFIX_DEFAULT
;
602 git_buf_clear(pi
->buf
);
604 if ((error
= diff_print_patch_file_binary(
605 pi
, (git_diff_delta
*)delta
, old_pfx
, new_pfx
, binary
)) < 0)
608 pi
->line
.origin
= GIT_DIFF_LINE_BINARY
;
609 pi
->line
.content
= git_buf_cstr(pi
->buf
);
610 pi
->line
.content_len
= git_buf_len(pi
->buf
);
612 return pi
->print_cb(delta
, NULL
, &pi
->line
, pi
->payload
);
615 static int diff_print_patch_hunk(
616 const git_diff_delta
*d
,
617 const git_diff_hunk
*h
,
620 diff_print_info
*pi
= data
;
622 if (S_ISDIR(d
->new_file
.mode
))
625 pi
->line
.origin
= GIT_DIFF_LINE_HUNK_HDR
;
626 pi
->line
.content
= h
->header
;
627 pi
->line
.content_len
= h
->header_len
;
629 return pi
->print_cb(d
, h
, &pi
->line
, pi
->payload
);
632 static int diff_print_patch_line(
633 const git_diff_delta
*delta
,
634 const git_diff_hunk
*hunk
,
635 const git_diff_line
*line
,
638 diff_print_info
*pi
= data
;
640 if (S_ISDIR(delta
->new_file
.mode
))
643 return pi
->print_cb(delta
, hunk
, line
, pi
->payload
);
646 /* print a git_diff to an output callback */
649 git_diff_format_t format
,
650 git_diff_line_cb print_cb
,
654 git_buf buf
= GIT_BUF_INIT
;
656 git_diff_file_cb print_file
= NULL
;
657 git_diff_binary_cb print_binary
= NULL
;
658 git_diff_hunk_cb print_hunk
= NULL
;
659 git_diff_line_cb print_line
= NULL
;
662 case GIT_DIFF_FORMAT_PATCH
:
663 print_file
= diff_print_patch_file
;
664 print_binary
= diff_print_patch_binary
;
665 print_hunk
= diff_print_patch_hunk
;
666 print_line
= diff_print_patch_line
;
668 case GIT_DIFF_FORMAT_PATCH_HEADER
:
669 print_file
= diff_print_patch_file
;
671 case GIT_DIFF_FORMAT_RAW
:
672 print_file
= diff_print_one_raw
;
674 case GIT_DIFF_FORMAT_NAME_ONLY
:
675 print_file
= diff_print_one_name_only
;
677 case GIT_DIFF_FORMAT_NAME_STATUS
:
678 print_file
= diff_print_one_name_status
;
681 giterr_set(GITERR_INVALID
, "Unknown diff output format (%d)", format
);
685 if (!(error
= diff_print_info_init_fromdiff(
686 &pi
, &buf
, diff
, format
, print_cb
, payload
))) {
687 error
= git_diff_foreach(
688 diff
, print_file
, print_binary
, print_hunk
, print_line
, &pi
);
690 if (error
) /* make sure error message is set */
691 giterr_set_after_callback_function(error
, "git_diff_print");
699 int git_diff_print_callback__to_buf(
700 const git_diff_delta
*delta
,
701 const git_diff_hunk
*hunk
,
702 const git_diff_line
*line
,
705 git_buf
*output
= payload
;
706 GIT_UNUSED(delta
); GIT_UNUSED(hunk
);
709 giterr_set(GITERR_INVALID
, "Buffer pointer must be provided");
713 if (line
->origin
== GIT_DIFF_LINE_ADDITION
||
714 line
->origin
== GIT_DIFF_LINE_DELETION
||
715 line
->origin
== GIT_DIFF_LINE_CONTEXT
)
716 git_buf_putc(output
, line
->origin
);
718 return git_buf_put(output
, line
->content
, line
->content_len
);
721 int git_diff_print_callback__to_file_handle(
722 const git_diff_delta
*delta
,
723 const git_diff_hunk
*hunk
,
724 const git_diff_line
*line
,
727 FILE *fp
= payload
? payload
: stdout
;
729 GIT_UNUSED(delta
); GIT_UNUSED(hunk
);
731 if (line
->origin
== GIT_DIFF_LINE_CONTEXT
||
732 line
->origin
== GIT_DIFF_LINE_ADDITION
||
733 line
->origin
== GIT_DIFF_LINE_DELETION
)
734 fputc(line
->origin
, fp
);
735 fwrite(line
->content
, 1, line
->content_len
, fp
);
739 /* print a git_diff to a git_buf */
740 int git_diff_to_buf(git_buf
*out
, git_diff
*diff
, git_diff_format_t format
)
743 git_buf_sanitize(out
);
744 return git_diff_print(
745 diff
, format
, git_diff_print_callback__to_buf
, out
);
748 /* print a git_patch to an output callback */
751 git_diff_line_cb print_cb
,
755 git_buf temp
= GIT_BUF_INIT
;
758 assert(patch
&& print_cb
);
760 if (!(error
= diff_print_info_init_frompatch(
762 GIT_DIFF_FORMAT_PATCH
, print_cb
, payload
)))
764 error
= git_patch__invoke_callbacks(
766 diff_print_patch_file
, diff_print_patch_binary
,
767 diff_print_patch_hunk
, diff_print_patch_line
,
770 if (error
) /* make sure error message is set */
771 giterr_set_after_callback_function(error
, "git_patch_print");
779 /* print a git_patch to a git_buf */
780 int git_patch_to_buf(git_buf
*out
, git_patch
*patch
)
782 assert(out
&& patch
);
783 git_buf_sanitize(out
);
784 return git_patch_print(patch
, git_diff_print_callback__to_buf
, out
);