| 789 | } |
| 790 | |
| 791 | static void render_hunk(struct add_p_state *s, struct hunk *hunk, |
| 792 | ssize_t delta, int colored, struct strbuf *out) |
| 793 | { |
| 794 | struct hunk_header *header = &hunk->header; |
| 795 | |
| 796 | if (hunk->header.old_offset != 0 || hunk->header.new_offset != 0) { |
| 797 | /* |
| 798 | * Generate the hunk header dynamically, except for special |
| 799 | * hunks (such as the diff header). |
| 800 | */ |
| 801 | const char *p; |
| 802 | size_t len; |
| 803 | unsigned long old_offset = header->old_offset; |
| 804 | unsigned long new_offset = header->new_offset; |
| 805 | |
| 806 | if (!colored) { |
| 807 | p = s->plain.buf + header->extra_start; |
| 808 | len = header->extra_end - header->extra_start; |
| 809 | } else if (header->suppress_colored_line_range) { |
| 810 | strbuf_add(out, |
| 811 | s->colored.buf + header->colored_extra_start, |
| 812 | header->colored_extra_end - |
| 813 | header->colored_extra_start); |
| 814 | |
| 815 | strbuf_add(out, s->colored.buf + hunk->colored_start, |
| 816 | hunk->colored_end - hunk->colored_start); |
| 817 | return; |
| 818 | } else { |
| 819 | strbuf_addstr(out, s->cfg.fraginfo_color); |
| 820 | p = s->colored.buf + header->colored_extra_start; |
| 821 | len = header->colored_extra_end |
| 822 | - header->colored_extra_start; |
| 823 | } |
| 824 | |
| 825 | if (s->mode->is_reverse) |
| 826 | old_offset -= delta; |
| 827 | else |
| 828 | new_offset += delta; |
| 829 | |
| 830 | strbuf_addf(out, "@@ -%lu", old_offset); |
| 831 | if (header->old_count != 1) |
| 832 | strbuf_addf(out, ",%lu", header->old_count); |
| 833 | strbuf_addf(out, " +%lu", new_offset); |
| 834 | if (header->new_count != 1) |
| 835 | strbuf_addf(out, ",%lu", header->new_count); |
| 836 | strbuf_addstr(out, " @@"); |
| 837 | |
| 838 | if (len) |
| 839 | strbuf_add(out, p, len); |
| 840 | else if (colored) |
| 841 | strbuf_addf(out, "%s\n", s->cfg.reset_color_diff); |
| 842 | else |
| 843 | strbuf_addch(out, '\n'); |
| 844 | } |
| 845 | |
| 846 | if (colored) |
| 847 | strbuf_add(out, s->colored.buf + hunk->colored_start, |
| 848 | hunk->colored_end - hunk->colored_start); |
no test coverage detected