@matheusbsbs / matheus-bagliano / commits / 90921740bd
Fork 0

gitweb: Split git_patchset_body into separate subroutines

This commit makes git_patchset_body easier to read, and reduces level of nesting and indent level. It adds more lines that it removes because of extra parameter passing in subroutines, and subroutine calls in git_patchset_body. Also because there are few added comments. Below there are descriptions of all split-off subroutines: Separate formatting "git diff" header into format_git_diff_header_line. While at it fix it so it always escapes pathname. It would be even more useful if we decide to use `--cc' for merges, and need to generate by hand empty patches for anchors. Separate formatting extended (git) diff header lines into format_extended_diff_header_line. This one is copied without changes. Separate formatting two-lines from-file/to-file diff header into format_diff_from_to_header subroutine. While at it fix it so it always escapes pathname. Beware calling convention: it takes _two_ lines. Separate generating %from and %to hashes (with info used among others to generate hyperlinks) into parse_from_to_diffinfo subroutine. This one is copied without changes. Separate checking if file was deleted (and among others therefore does not have link to the result file) into is_deleted subroutine. This would allow us to easily change the algotithm to find if file is_deleted in the result. Signed-off-by: Jakub Narebski <jnareb@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jakub Narebski committed Jun 8, 2007 at 13:27 UTC 90921740bd00029708370673fdc537522aa48e6f
1 file changed +202 -111
gitweb/gitweb.perl
+202 -111
@@ -954,7 +954,149 @@ sub format_subject_html {
954 }
955 }
956
957 -# format patch (diff) line (rather not to be used for diff headers)
957 +# format git diff header line, i.e. "diff --(git|combined|cc) ..."
958 +sub format_git_diff_header_line {
959 + my $line = shift;
960 + my $diffinfo = shift;
961 + my ($from, $to) = @_;
962 +
963 + if ($diffinfo->{'nparents'}) {
964 + # combined diff
965 + $line =~ s!^(diff (.*?) )"?.*$!$1!;
966 + if ($to->{'href'}) {
967 + $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
968 + esc_path($to->{'file'}));
969 + } else { # file was deleted (no href)
970 + $line .= esc_path($to->{'file'});
971 + }
972 + } else {
973 + # "ordinary" diff
974 + $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
975 + if ($from->{'href'}) {
976 + $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
977 + 'a/' . esc_path($from->{'file'}));
978 + } else { # file was added (no href)
979 + $line .= 'a/' . esc_path($from->{'file'});
980 + }
981 + $line .= ' ';
982 + if ($to->{'href'}) {
983 + $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
984 + 'b/' . esc_path($to->{'file'}));
985 + } else { # file was deleted
986 + $line .= 'b/' . esc_path($to->{'file'});
987 + }
988 + }
989 +
990 + return "<div class=\"diff header\">$line</div>\n";
991 +}
992 +
993 +# format extended diff header line, before patch itself
994 +sub format_extended_diff_header_line {
995 + my $line = shift;
996 + my $diffinfo = shift;
997 + my ($from, $to) = @_;
998 +
999 + # match <path>
1000 + if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
1001 + $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1002 + esc_path($from->{'file'}));
1003 + }
1004 + if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
1005 + $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1006 + esc_path($to->{'file'}));
1007 + }
1008 + # match single <mode>
1009 + if ($line =~ m/\s(\d{6})$/) {
1010 + $line .= '<span class="info"> (' .
1011 + file_type_long($1) .
1012 + ')</span>';
1013 + }
1014 + # match <hash>
1015 + if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
1016 + # can match only for combined diff
1017 + $line = 'index ';
1018 + for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1019 + if ($from->{'href'}[$i]) {
1020 + $line .= $cgi->a({-href=>$from->{'href'}[$i],
1021 + -class=>"hash"},
1022 + substr($diffinfo->{'from_id'}[$i],0,7));
1023 + } else {
1024 + $line .= '0' x 7;
1025 + }
1026 + # separator
1027 + $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
1028 + }
1029 + $line .= '..';
1030 + if ($to->{'href'}) {
1031 + $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1032 + substr($diffinfo->{'to_id'},0,7));
1033 + } else {
1034 + $line .= '0' x 7;
1035 + }
1036 +
1037 + } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
1038 + # can match only for ordinary diff
1039 + my ($from_link, $to_link);
1040 + if ($from->{'href'}) {
1041 + $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
1042 + substr($diffinfo->{'from_id'},0,7));
1043 + } else {
1044 + $from_link = '0' x 7;
1045 + }
1046 + if ($to->{'href'}) {
1047 + $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1048 + substr($diffinfo->{'to_id'},0,7));
1049 + } else {
1050 + $to_link = '0' x 7;
1051 + }
1052 + my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
1053 + $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
1054 + }
1055 +
1056 + return $line . "<br/>\n";
1057 +}
1058 +
1059 +# format from-file/to-file diff header
1060 +sub format_diff_from_to_header {
1061 + my ($from_line, $to_line, $diffinfo, $from, $to) = @_;
1062 + my $line;
1063 + my $result = '';
1064 +
1065 + $line = $from_line;
1066 + #assert($line =~ m/^---/) if DEBUG;
1067 + # no extra formatting "^--- /dev/null"
1068 + if ($line =~ m!^--- "?a/!) {
1069 + if (!$diffinfo->{'nparents'} && # multiple 'from'
1070 + $from->{'href'}) {
1071 + $line = '--- a/' .
1072 + $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1073 + esc_path($from->{'file'}));
1074 + } else {
1075 + $line = '--- a/' .
1076 + esc_path($from->{'file'});
1077 + }
1078 + }
1079 + $result .= qq!<div class="diff from_file">$line</div>\n!;
1080 +
1081 + $line = $to_line;
1082 + #assert($line =~ m/^\+\+\+/) if DEBUG;
1083 + # no extra formatting for "^+++ /dev/null"
1084 + if ($line =~ m!^\+\+\+ "?b/!) {
1085 + if ($to->{'href'}) {
1086 + $line = '+++ b/' .
1087 + $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1088 + esc_path($to->{'file'}));
1089 + } else {
1090 + $line = '+++ b/' .
1091 + esc_path($to->{'file'});
1092 + }
1093 + }
1094 + $result .= qq!<div class="diff to_file">$line</div>\n!;
1095 +
1096 + return $result;
1097 +}
1098 +
1099 +# format patch (diff) line (not to be used for diff headers)
1100 sub format_diff_line {
1101 my $line = shift;
1102 my ($from, $to) = @_;
@@ -1680,6 +1822,48 @@ sub parse_ls_tree_line ($;%) {
1822 return wantarray ? %res : \%res;
1823 }
1824
1825 +# generates _two_ hashes, references to which are passed as 2 and 3 argument
1826 +sub parse_from_to_diffinfo {
1827 + my ($diffinfo, $from, $to, @parents) = @_;
1828 +
1829 + if ($diffinfo->{'nparents'}) {
1830 + # combined diff
1831 + $from->{'file'} = [];
1832 + $from->{'href'} = [];
1833 + fill_from_file_info($diffinfo, @parents)
1834 + unless exists $diffinfo->{'from_file'};
1835 + for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1836 + $from->{'file'}[$i] = $diffinfo->{'from_file'}[$i] || $diffinfo->{'to_file'};
1837 + if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
1838 + $from->{'href'}[$i] = href(action=>"blob",
1839 + hash_base=>$parents[$i],
1840 + hash=>$diffinfo->{'from_id'}[$i],
1841 + file_name=>$from->{'file'}[$i]);
1842 + } else {
1843 + $from->{'href'}[$i] = undef;
1844 + }
1845 + }
1846 + } else {
1847 + $from->{'file'} = $diffinfo->{'from_file'} || $diffinfo->{'file'};
1848 + if ($diffinfo->{'status'} ne "A") { # not new (added) file
1849 + $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
1850 + hash=>$diffinfo->{'from_id'},
1851 + file_name=>$from->{'file'});
1852 + } else {
1853 + delete $from->{'href'};
1854 + }
1855 + }
1856 +
1857 + $to->{'file'} = $diffinfo->{'to_file'} || $diffinfo->{'file'};
1858 + if (!is_deleted($diffinfo)) { # file exists in result
1859 + $to->{'href'} = href(action=>"blob", hash_base=>$hash,
1860 + hash=>$diffinfo->{'to_id'},
1861 + file_name=>$to->{'file'});
1862 + } else {
1863 + delete $to->{'href'};
1864 + }
1865 +}
1866 +
1867 ## ......................................................................
1868 ## parse to array of hashes functions
1869
@@ -2387,6 +2571,11 @@ sub from_ids_eq {
2571 }
2572 }
2573
2574 +sub is_deleted {
2575 + my $diffinfo = shift;
2576 +
2577 + return $diffinfo->{'to_id'} eq ('0' x 40);
2578 +}
2579
2580 sub git_difftree_body {
2581 my ($difftree, $hash, @parents) = @_;
@@ -2444,7 +2633,7 @@ sub git_difftree_body {
2633 fill_from_file_info($diff, @parents)
2634 unless exists $diff->{'from_file'};
2635
2447 - if ($diff->{'to_id'} ne ('0' x 40)) {
2636 + if (!is_deleted($diff)) {
2637 # file exists in the result (child) commit
2638 print "<td>" .
2639 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
@@ -2765,6 +2954,8 @@ sub git_patchset_body {
2954 } else {
2955 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
2956 }
2957 + # modifies %from, %to hashes
2958 + parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
2959 if ($diffinfo->{'nparents'}) {
2960 # combined diff
2961 $from{'file'} = [];
@@ -2794,7 +2985,7 @@ sub git_patchset_body {
2985 }
2986
2987 $to{'file'} = $diffinfo->{'to_file'} || $diffinfo->{'file'};
2797 - if ($diffinfo->{'to_id'} ne ('0' x 40)) { # file exists in result
2988 + if (!is_deleted($diffinfo)) { # file exists in result
2989 $to{'href'} = href(action=>"blob", hash_base=>$hash,
2990 hash=>$diffinfo->{'to_id'},
2991 file_name=>$to{'file'});
@@ -2808,105 +2999,15 @@ sub git_patchset_body {
2999
3000 # print "git diff" header
3001 $patch_line = shift @diff_header;
2811 - if ($diffinfo->{'nparents'}) {
2812 -
2813 - # combined diff
2814 - $patch_line =~ s!^(diff (.*?) )"?.*$!$1!;
2815 - if ($to{'href'}) {
2816 - $patch_line .= $cgi->a({-href => $to{'href'}, -class => "path"},
2817 - esc_path($to{'file'}));
2818 - } else { # file was deleted
2819 - $patch_line .= esc_path($to{'file'});
2820 - }
2821 -
2822 - } else {
2823 -
2824 - $patch_line =~ s!^(diff (.*?) )"?a/.*$!$1!;
2825 - if ($from{'href'}) {
2826 - $patch_line .= $cgi->a({-href => $from{'href'}, -class => "path"},
2827 - 'a/' . esc_path($from{'file'}));
2828 - } else { # file was added
2829 - $patch_line .= 'a/' . esc_path($from{'file'});
2830 - }
2831 - $patch_line .= ' ';
2832 - if ($to{'href'}) {
2833 - $patch_line .= $cgi->a({-href => $to{'href'}, -class => "path"},
2834 - 'b/' . esc_path($to{'file'}));
2835 - } else { # file was deleted
2836 - $patch_line .= 'b/' . esc_path($to{'file'});
2837 - }
2838 -
2839 - }
2840 - print "<div class=\"diff header\">$patch_line</div>\n";
3002 + print format_git_diff_header_line($patch_line, $diffinfo,
3003 + \%from, \%to);
3004
3005 # print extended diff header
3006 print "<div class=\"diff extended_header\">\n" if (@diff_header > 0);
3007 EXTENDED_HEADER:
3008 foreach $patch_line (@diff_header) {
2846 - # match <path>
2847 - if ($patch_line =~ s!^((copy|rename) from ).*$!$1! && $from{'href'}) {
2848 - $patch_line .= $cgi->a({-href=>$from{'href'}, -class=>"path"},
2849 - esc_path($from{'file'}));
2850 - }
2851 - if ($patch_line =~ s!^((copy|rename) to ).*$!$1! && $to{'href'}) {
2852 - $patch_line .= $cgi->a({-href=>$to{'href'}, -class=>"path"},
2853 - esc_path($to{'file'}));
2854 - }
2855 - # match single <mode>
2856 - if ($patch_line =~ m/\s(\d{6})$/) {
2857 - $patch_line .= '<span class="info"> (' .
2858 - file_type_long($1) .
2859 - ')</span>';
2860 - }
2861 - # match <hash>
2862 - if ($patch_line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
2863 - # can match only for combined diff
2864 - $patch_line = 'index ';
2865 - for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2866 - if ($from{'href'}[$i]) {
2867 - $patch_line .= $cgi->a({-href=>$from{'href'}[$i],
2868 - -class=>"hash"},
2869 - substr($diffinfo->{'from_id'}[$i],0,7));
2870 - } else {
2871 - $patch_line .= '0' x 7;
2872 - }
2873 - # separator
2874 - $patch_line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
2875 - }
2876 - $patch_line .= '..';
2877 - if ($to{'href'}) {
2878 - $patch_line .= $cgi->a({-href=>$to{'href'}, -class=>"hash"},
2879 - substr($diffinfo->{'to_id'},0,7));
2880 - } else {
2881 - $patch_line .= '0' x 7;
2882 - }
2883 -
2884 - } elsif ($patch_line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
2885 - # can match only for ordinary diff
2886 - my ($from_link, $to_link);
2887 - if ($from{'href'}) {
2888 - $from_link = $cgi->a({-href=>$from{'href'}, -class=>"hash"},
2889 - substr($diffinfo->{'from_id'},0,7));
2890 - } else {
2891 - $from_link = '0' x 7;
2892 - }
2893 - if ($to{'href'}) {
2894 - $to_link = $cgi->a({-href=>$to{'href'}, -class=>"hash"},
2895 - substr($diffinfo->{'to_id'},0,7));
2896 - } else {
2897 - $to_link = '0' x 7;
2898 - }
2899 - #affirm {
2900 - # my ($from_hash, $to_hash) =
2901 - # ($patch_line =~ m/^index ([0-9a-fA-F]{40})..([0-9a-fA-F]{40})/);
2902 - # my ($from_id, $to_id) =
2903 - # ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2904 - # ($from_hash eq $from_id) && ($to_hash eq $to_id);
2905 - #} if DEBUG;
2906 - my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2907 - $patch_line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
2908 - }
2909 - print $patch_line . "<br/>\n";
3009 + print format_extended_diff_header_line($patch_line, $diffinfo,
3010 + \%from, \%to);
3011 }
3012 print "</div>\n" if (@diff_header > 0); # class="diff extended_header"
3013
@@ -2918,24 +3019,14 @@ sub git_patchset_body {
3019 }
3020 next PATCH if ($patch_line =~ m/^diff /);
3021 #assert($patch_line =~ m/^---/) if DEBUG;
2921 - if (!$diffinfo->{'nparents'} && # not from-file line for combined diff
2922 - $from{'href'} && $patch_line =~ m!^--- "?a/!) {
2923 - $patch_line = '--- a/' .
2924 - $cgi->a({-href=>$from{'href'}, -class=>"path"},
2925 - esc_path($from{'file'}));
2926 - }
2927 - print "<div class=\"diff from_file\">$patch_line</div>\n";
3022 + #assert($patch_line eq $last_patch_line) if DEBUG;
3023
3024 $patch_line = <$fd>;
3025 chomp $patch_line;
3026 + #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
3027
2932 - #assert($patch_line =~ m/^+++/) if DEBUG;
2933 - if ($to{'href'} && $patch_line =~ m!^\+\+\+ "?b/!) {
2934 - $patch_line = '+++ b/' .
2935 - $cgi->a({-href=>$to{'href'}, -class=>"path"},
2936 - esc_path($to{'file'}));
2937 - }
2938 - print "<div class=\"diff to_file\">$patch_line</div>\n";
3028 + print format_diff_from_to_header($last_patch_line, $patch_line,
3029 + $diffinfo, \%from, \%to);
3030
3031 # the patch itself
3032 LINE: