@@ -1999,16 +1999,86 @@ static void run_external_diff(const char *pgm,
1999
1999
}
2000
2000
}
2001
2001
2002
+ static int similarity_index (struct diff_filepair * p )
2003
+ {
2004
+ return p -> score * 100 / MAX_SCORE ;
2005
+ }
2006
+
2007
+ static void fill_metainfo (struct strbuf * msg ,
2008
+ const char * name ,
2009
+ const char * other ,
2010
+ struct diff_filespec * one ,
2011
+ struct diff_filespec * two ,
2012
+ struct diff_options * o ,
2013
+ struct diff_filepair * p )
2014
+ {
2015
+ strbuf_init (msg , PATH_MAX * 2 + 300 );
2016
+ switch (p -> status ) {
2017
+ case DIFF_STATUS_COPIED :
2018
+ strbuf_addf (msg , "similarity index %d%%" , similarity_index (p ));
2019
+ strbuf_addstr (msg , "\ncopy from " );
2020
+ quote_c_style (name , msg , NULL , 0 );
2021
+ strbuf_addstr (msg , "\ncopy to " );
2022
+ quote_c_style (other , msg , NULL , 0 );
2023
+ strbuf_addch (msg , '\n' );
2024
+ break ;
2025
+ case DIFF_STATUS_RENAMED :
2026
+ strbuf_addf (msg , "similarity index %d%%" , similarity_index (p ));
2027
+ strbuf_addstr (msg , "\nrename from " );
2028
+ quote_c_style (name , msg , NULL , 0 );
2029
+ strbuf_addstr (msg , "\nrename to " );
2030
+ quote_c_style (other , msg , NULL , 0 );
2031
+ strbuf_addch (msg , '\n' );
2032
+ break ;
2033
+ case DIFF_STATUS_MODIFIED :
2034
+ if (p -> score ) {
2035
+ strbuf_addf (msg , "dissimilarity index %d%%\n" ,
2036
+ similarity_index (p ));
2037
+ break ;
2038
+ }
2039
+ /* fallthru */
2040
+ default :
2041
+ /* nothing */
2042
+ ;
2043
+ }
2044
+ if (one && two && hashcmp (one -> sha1 , two -> sha1 )) {
2045
+ int abbrev = DIFF_OPT_TST (o , FULL_INDEX ) ? 40 : DEFAULT_ABBREV ;
2046
+
2047
+ if (DIFF_OPT_TST (o , BINARY )) {
2048
+ mmfile_t mf ;
2049
+ if ((!fill_mmfile (& mf , one ) && diff_filespec_is_binary (one )) ||
2050
+ (!fill_mmfile (& mf , two ) && diff_filespec_is_binary (two )))
2051
+ abbrev = 40 ;
2052
+ }
2053
+ strbuf_addf (msg , "index %.*s..%.*s" ,
2054
+ abbrev , sha1_to_hex (one -> sha1 ),
2055
+ abbrev , sha1_to_hex (two -> sha1 ));
2056
+ if (one -> mode == two -> mode )
2057
+ strbuf_addf (msg , " %06o" , one -> mode );
2058
+ strbuf_addch (msg , '\n' );
2059
+ }
2060
+ if (msg -> len )
2061
+ strbuf_setlen (msg , msg -> len - 1 );
2062
+ }
2063
+
2002
2064
static void run_diff_cmd (const char * pgm ,
2003
2065
const char * name ,
2004
2066
const char * other ,
2005
2067
const char * attr_path ,
2006
2068
struct diff_filespec * one ,
2007
2069
struct diff_filespec * two ,
2008
- const char * xfrm_msg ,
2070
+ struct strbuf * msg ,
2009
2071
struct diff_options * o ,
2010
- int complete_rewrite )
2072
+ struct diff_filepair * p )
2011
2073
{
2074
+ const char * xfrm_msg = NULL ;
2075
+ int complete_rewrite = (p -> status == DIFF_STATUS_MODIFIED ) && p -> score ;
2076
+
2077
+ if (msg ) {
2078
+ fill_metainfo (msg , name , other , one , two , o , p );
2079
+ xfrm_msg = msg -> len ? msg -> buf : NULL ;
2080
+ }
2081
+
2012
2082
if (!DIFF_OPT_TST (o , ALLOW_EXTERNAL ))
2013
2083
pgm = NULL ;
2014
2084
else {
@@ -2048,11 +2118,6 @@ static void diff_fill_sha1_info(struct diff_filespec *one)
2048
2118
hashclr (one -> sha1 );
2049
2119
}
2050
2120
2051
- static int similarity_index (struct diff_filepair * p )
2052
- {
2053
- return p -> score * 100 / MAX_SCORE ;
2054
- }
2055
-
2056
2121
static void strip_prefix (int prefix_length , const char * * namep , const char * * otherp )
2057
2122
{
2058
2123
/* Strip the prefix but do not molest /dev/null and absolute paths */
@@ -2066,13 +2131,11 @@ static void run_diff(struct diff_filepair *p, struct diff_options *o)
2066
2131
{
2067
2132
const char * pgm = external_diff ();
2068
2133
struct strbuf msg ;
2069
- char * xfrm_msg ;
2070
2134
struct diff_filespec * one = p -> one ;
2071
2135
struct diff_filespec * two = p -> two ;
2072
2136
const char * name ;
2073
2137
const char * other ;
2074
2138
const char * attr_path ;
2075
- int complete_rewrite = 0 ;
2076
2139
2077
2140
name = p -> one -> path ;
2078
2141
other = (strcmp (name , p -> two -> path ) ? p -> two -> path : NULL );
@@ -2082,83 +2145,34 @@ static void run_diff(struct diff_filepair *p, struct diff_options *o)
2082
2145
2083
2146
if (DIFF_PAIR_UNMERGED (p )) {
2084
2147
run_diff_cmd (pgm , name , NULL , attr_path ,
2085
- NULL , NULL , NULL , o , 0 );
2148
+ NULL , NULL , NULL , o , p );
2086
2149
return ;
2087
2150
}
2088
2151
2089
2152
diff_fill_sha1_info (one );
2090
2153
diff_fill_sha1_info (two );
2091
2154
2092
- strbuf_init (& msg , PATH_MAX * 2 + 300 );
2093
- switch (p -> status ) {
2094
- case DIFF_STATUS_COPIED :
2095
- strbuf_addf (& msg , "similarity index %d%%" , similarity_index (p ));
2096
- strbuf_addstr (& msg , "\ncopy from " );
2097
- quote_c_style (name , & msg , NULL , 0 );
2098
- strbuf_addstr (& msg , "\ncopy to " );
2099
- quote_c_style (other , & msg , NULL , 0 );
2100
- strbuf_addch (& msg , '\n' );
2101
- break ;
2102
- case DIFF_STATUS_RENAMED :
2103
- strbuf_addf (& msg , "similarity index %d%%" , similarity_index (p ));
2104
- strbuf_addstr (& msg , "\nrename from " );
2105
- quote_c_style (name , & msg , NULL , 0 );
2106
- strbuf_addstr (& msg , "\nrename to " );
2107
- quote_c_style (other , & msg , NULL , 0 );
2108
- strbuf_addch (& msg , '\n' );
2109
- break ;
2110
- case DIFF_STATUS_MODIFIED :
2111
- if (p -> score ) {
2112
- strbuf_addf (& msg , "dissimilarity index %d%%\n" ,
2113
- similarity_index (p ));
2114
- complete_rewrite = 1 ;
2115
- break ;
2116
- }
2117
- /* fallthru */
2118
- default :
2119
- /* nothing */
2120
- ;
2121
- }
2122
-
2123
- if (hashcmp (one -> sha1 , two -> sha1 )) {
2124
- int abbrev = DIFF_OPT_TST (o , FULL_INDEX ) ? 40 : DEFAULT_ABBREV ;
2125
-
2126
- if (DIFF_OPT_TST (o , BINARY )) {
2127
- mmfile_t mf ;
2128
- if ((!fill_mmfile (& mf , one ) && diff_filespec_is_binary (one )) ||
2129
- (!fill_mmfile (& mf , two ) && diff_filespec_is_binary (two )))
2130
- abbrev = 40 ;
2131
- }
2132
- strbuf_addf (& msg , "index %.*s..%.*s" ,
2133
- abbrev , sha1_to_hex (one -> sha1 ),
2134
- abbrev , sha1_to_hex (two -> sha1 ));
2135
- if (one -> mode == two -> mode )
2136
- strbuf_addf (& msg , " %06o" , one -> mode );
2137
- strbuf_addch (& msg , '\n' );
2138
- }
2139
-
2140
- if (msg .len )
2141
- strbuf_setlen (& msg , msg .len - 1 );
2142
- xfrm_msg = msg .len ? msg .buf : NULL ;
2143
-
2144
2155
if (!pgm &&
2145
2156
DIFF_FILE_VALID (one ) && DIFF_FILE_VALID (two ) &&
2146
2157
(S_IFMT & one -> mode ) != (S_IFMT & two -> mode )) {
2147
- /* a filepair that changes between file and symlink
2158
+ /*
2159
+ * a filepair that changes between file and symlink
2148
2160
* needs to be split into deletion and creation.
2149
2161
*/
2150
2162
struct diff_filespec * null = alloc_filespec (two -> path );
2151
2163
run_diff_cmd (NULL , name , other , attr_path ,
2152
- one , null , xfrm_msg , o , 0 );
2164
+ one , null , & msg , o , p );
2153
2165
free (null );
2166
+ strbuf_release (& msg );
2167
+
2154
2168
null = alloc_filespec (one -> path );
2155
2169
run_diff_cmd (NULL , name , other , attr_path ,
2156
- null , two , xfrm_msg , o , 0 );
2170
+ null , two , & msg , o , p );
2157
2171
free (null );
2158
2172
}
2159
2173
else
2160
2174
run_diff_cmd (pgm , name , other , attr_path ,
2161
- one , two , xfrm_msg , o , complete_rewrite );
2175
+ one , two , & msg , o , p );
2162
2176
2163
2177
strbuf_release (& msg );
2164
2178
}
0 commit comments