Skip to content

Commit d6c6285

Browse files
authored
Merge pull request libgit2#5559 from pks-t/pks/diff-print-fixups
Random fixes for diff-printing
2 parents d43d490 + 6256d02 commit d6c6285

File tree

1 file changed

+55
-52
lines changed

1 file changed

+55
-52
lines changed

src/diff_print.c

Lines changed: 55 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -351,12 +351,11 @@ static int diff_delta_format_similarity_header(
351351
goto done;
352352
}
353353

354+
GIT_ASSERT(delta->status == GIT_DELTA_RENAMED || delta->status == GIT_DELTA_COPIED);
354355
if (delta->status == GIT_DELTA_RENAMED)
355356
type = "rename";
356-
else if (delta->status == GIT_DELTA_COPIED)
357-
type = "copy";
358357
else
359-
abort();
358+
type = "copy";
360359

361360
if ((error = git_buf_puts(&old_path, delta->old_file.path)) < 0 ||
362361
(error = git_buf_puts(&new_path, delta->new_file.path)) < 0 ||
@@ -492,6 +491,9 @@ static int format_binary(
492491
}
493492
git_buf_putc(pi->buf, '\n');
494493

494+
if (git_buf_oom(pi->buf))
495+
return -1;
496+
495497
return 0;
496498
}
497499

@@ -502,21 +504,17 @@ static int diff_print_patch_file_binary_noshow(
502504
git_buf old_path = GIT_BUF_INIT, new_path = GIT_BUF_INIT;
503505
int error;
504506

505-
if ((error = diff_delta_format_path(
506-
&old_path, old_pfx, delta->old_file.path)) < 0 ||
507-
(error = diff_delta_format_path(
508-
&new_path, new_pfx, delta->new_file.path)) < 0)
507+
if ((error = diff_delta_format_path(&old_path, old_pfx, delta->old_file.path)) < 0 ||
508+
(error = diff_delta_format_path(&new_path, new_pfx, delta->new_file.path)) < 0 ||
509+
(error = diff_delta_format_with_paths(pi->buf, delta, "Binary files %s and %s differ\n",
510+
old_path.ptr, new_path.ptr)) < 0)
509511
goto done;
510512

511513
pi->line.num_lines = 1;
512-
error = diff_delta_format_with_paths(
513-
pi->buf, delta, "Binary files %s and %s differ\n",
514-
old_path.ptr, new_path.ptr);
515514

516515
done:
517516
git_buf_dispose(&old_path);
518517
git_buf_dispose(&new_path);
519-
520518
return error;
521519
}
522520

@@ -540,10 +538,9 @@ static int diff_print_patch_file_binary(
540538
pi->line.num_lines++;
541539

542540
if ((error = format_binary(pi, binary->new_file.type, binary->new_file.data,
543-
binary->new_file.datalen, binary->new_file.inflatedlen)) < 0 ||
544-
(error = format_binary(pi, binary->old_file.type, binary->old_file.data,
545-
binary->old_file.datalen, binary->old_file.inflatedlen)) < 0) {
546-
541+
binary->new_file.datalen, binary->new_file.inflatedlen)) < 0 ||
542+
(error = format_binary(pi, binary->old_file.type, binary->old_file.data,
543+
binary->old_file.datalen, binary->old_file.inflatedlen)) < 0) {
547544
if (error == GIT_EBUFS) {
548545
git_error_clear();
549546
git_buf_truncate(pi->buf, pre_binary_size);
@@ -580,16 +577,15 @@ static int diff_print_patch_file(
580577
GIT_UNUSED(progress);
581578

582579
if (S_ISDIR(delta->new_file.mode) ||
583-
delta->status == GIT_DELTA_UNMODIFIED ||
584-
delta->status == GIT_DELTA_IGNORED ||
585-
delta->status == GIT_DELTA_UNREADABLE ||
586-
(delta->status == GIT_DELTA_UNTRACKED &&
580+
delta->status == GIT_DELTA_UNMODIFIED ||
581+
delta->status == GIT_DELTA_IGNORED ||
582+
delta->status == GIT_DELTA_UNREADABLE ||
583+
(delta->status == GIT_DELTA_UNTRACKED &&
587584
(pi->flags & GIT_DIFF_SHOW_UNTRACKED_CONTENT) == 0))
588585
return 0;
589586

590-
if ((error = git_diff_delta__format_file_header(
591-
pi->buf, delta, oldpfx, newpfx,
592-
id_strlen, print_index)) < 0)
587+
if ((error = git_diff_delta__format_file_header(pi->buf, delta, oldpfx, newpfx,
588+
id_strlen, print_index)) < 0)
593589
return error;
594590

595591
pi->line.origin = GIT_DIFF_LINE_FILE_HDR;
@@ -699,17 +695,16 @@ int git_diff_print(
699695
return -1;
700696
}
701697

702-
if (!(error = diff_print_info_init_fromdiff(
703-
&pi, &buf, diff, format, print_cb, payload))) {
704-
error = git_diff_foreach(
705-
diff, print_file, print_binary, print_hunk, print_line, &pi);
698+
if ((error = diff_print_info_init_fromdiff(&pi, &buf, diff, format, print_cb, payload)) < 0)
699+
goto out;
706700

707-
if (error) /* make sure error message is set */
708-
git_error_set_after_callback_function(error, "git_diff_print");
701+
if ((error = git_diff_foreach(diff, print_file, print_binary, print_hunk, print_line, &pi)) != 0) {
702+
git_error_set_after_callback_function(error, "git_diff_print");
703+
goto out;
709704
}
710705

706+
out:
711707
git_buf_dispose(&buf);
712-
713708
return error;
714709
}
715710

@@ -728,8 +723,8 @@ int git_diff_print_callback__to_buf(
728723
}
729724

730725
if (line->origin == GIT_DIFF_LINE_ADDITION ||
731-
line->origin == GIT_DIFF_LINE_DELETION ||
732-
line->origin == GIT_DIFF_LINE_CONTEXT)
726+
line->origin == GIT_DIFF_LINE_DELETION ||
727+
line->origin == GIT_DIFF_LINE_CONTEXT)
733728
git_buf_putc(output, line->origin);
734729

735730
return git_buf_put(output, line->content, line->content_len);
@@ -742,14 +737,27 @@ int git_diff_print_callback__to_file_handle(
742737
void *payload)
743738
{
744739
FILE *fp = payload ? payload : stdout;
740+
int error;
745741

746-
GIT_UNUSED(delta); GIT_UNUSED(hunk);
742+
GIT_UNUSED(delta);
743+
GIT_UNUSED(hunk);
747744

748745
if (line->origin == GIT_DIFF_LINE_CONTEXT ||
749-
line->origin == GIT_DIFF_LINE_ADDITION ||
750-
line->origin == GIT_DIFF_LINE_DELETION)
751-
fputc(line->origin, fp);
752-
fwrite(line->content, 1, line->content_len, fp);
746+
line->origin == GIT_DIFF_LINE_ADDITION ||
747+
line->origin == GIT_DIFF_LINE_DELETION) {
748+
while ((error = fputc(line->origin, fp)) == EINTR)
749+
continue;
750+
if (error) {
751+
git_error_set(GIT_ERROR_OS, "could not write status");
752+
return -1;
753+
}
754+
}
755+
756+
if (fwrite(line->content, line->content_len, 1, fp) != 1) {
757+
git_error_set(GIT_ERROR_OS, "could not write line");
758+
return -1;
759+
}
760+
753761
return 0;
754762
}
755763

@@ -758,8 +766,7 @@ int git_diff_to_buf(git_buf *out, git_diff *diff, git_diff_format_t format)
758766
{
759767
assert(out && diff);
760768
git_buf_sanitize(out);
761-
return git_diff_print(
762-
diff, format, git_diff_print_callback__to_buf, out);
769+
return git_diff_print(diff, format, git_diff_print_callback__to_buf, out);
763770
}
764771

765772
/* print a git_patch to an output callback */
@@ -768,28 +775,24 @@ int git_patch_print(
768775
git_diff_line_cb print_cb,
769776
void *payload)
770777
{
771-
int error;
772778
git_buf temp = GIT_BUF_INIT;
773779
diff_print_info pi;
780+
int error;
774781

775782
assert(patch && print_cb);
776783

777-
if (!(error = diff_print_info_init_frompatch(
778-
&pi, &temp, patch,
779-
GIT_DIFF_FORMAT_PATCH, print_cb, payload)))
780-
{
781-
error = git_patch__invoke_callbacks(
782-
patch,
783-
diff_print_patch_file, diff_print_patch_binary,
784-
diff_print_patch_hunk, diff_print_patch_line,
785-
&pi);
786-
787-
if (error) /* make sure error message is set */
788-
git_error_set_after_callback_function(error, "git_patch_print");
784+
if ((error = diff_print_info_init_frompatch(&pi, &temp, patch,
785+
GIT_DIFF_FORMAT_PATCH, print_cb, payload)) < 0)
786+
goto out;
787+
788+
if ((error = git_patch__invoke_callbacks(patch, diff_print_patch_file, diff_print_patch_binary,
789+
diff_print_patch_hunk, diff_print_patch_line, &pi)) < 0) {
790+
git_error_set_after_callback_function(error, "git_patch_print");
791+
goto out;
789792
}
790793

794+
out:
791795
git_buf_dispose(&temp);
792-
793796
return error;
794797
}
795798

0 commit comments

Comments
 (0)