@matheusbsbs / matheus-bagliano / commits / fcb3d0adc1
Fork 0

add diff_flush_patch_id() to calculate the patch id

Call it like this: unsigned char id[20]; if (diff_flush_patch_id(diff_options, id)) printf("And the patch id is: %s\n", sha1_to_hex(id)); Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>

Johannes Schindelin committed Jun 25, 2006 at 03:51 UTC fcb3d0adc13b398c930814a5e0c886717acc1b70
2 files changed +141
diff.c
+139
@@ -2104,6 +2104,145 @@ static void diff_summary(struct diff_filepair *p)
2104 }
2105 }
2106
2107 +struct patch_id_t {
2108 + struct xdiff_emit_state xm;
2109 + SHA_CTX *ctx;
2110 + int patchlen;
2111 +};
2112 +
2113 +static int remove_space(char *line, int len)
2114 +{
2115 + int i;
2116 + char *dst = line;
2117 + unsigned char c;
2118 +
2119 + for (i = 0; i < len; i++)
2120 + if (!isspace((c = line[i])))
2121 + *dst++ = c;
2122 +
2123 + return dst - line;
2124 +}
2125 +
2126 +static void patch_id_consume(void *priv, char *line, unsigned long len)
2127 +{
2128 + struct patch_id_t *data = priv;
2129 + int new_len;
2130 +
2131 + /* Ignore line numbers when computing the SHA1 of the patch */
2132 + if (!strncmp(line, "@@ -", 4))
2133 + return;
2134 +
2135 + new_len = remove_space(line, len);
2136 +
2137 + SHA1_Update(data->ctx, line, new_len);
2138 + data->patchlen += new_len;
2139 +}
2140 +
2141 +/* returns 0 upon success, and writes result into sha1 */
2142 +static int diff_get_patch_id(struct diff_options *options, unsigned char *sha1)
2143 +{
2144 + struct diff_queue_struct *q = &diff_queued_diff;
2145 + int i;
2146 + SHA_CTX ctx;
2147 + struct patch_id_t data;
2148 + char buffer[PATH_MAX * 4 + 20];
2149 +
2150 + SHA1_Init(&ctx);
2151 + memset(&data, 0, sizeof(struct patch_id_t));
2152 + data.ctx = &ctx;
2153 + data.xm.consume = patch_id_consume;
2154 +
2155 + for (i = 0; i < q->nr; i++) {
2156 + xpparam_t xpp;
2157 + xdemitconf_t xecfg;
2158 + xdemitcb_t ecb;
2159 + mmfile_t mf1, mf2;
2160 + struct diff_filepair *p = q->queue[i];
2161 + int len1, len2;
2162 +
2163 + if (p->status == 0)
2164 + return error("internal diff status error");
2165 + if (p->status == DIFF_STATUS_UNKNOWN)
2166 + continue;
2167 + if (diff_unmodified_pair(p))
2168 + continue;
2169 + if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2170 + (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2171 + continue;
2172 + if (DIFF_PAIR_UNMERGED(p))
2173 + continue;
2174 +
2175 + diff_fill_sha1_info(p->one);
2176 + diff_fill_sha1_info(p->two);
2177 + if (fill_mmfile(&mf1, p->one) < 0 ||
2178 + fill_mmfile(&mf2, p->two) < 0)
2179 + return error("unable to read files to diff");
2180 +
2181 + /* Maybe hash p->two? into the patch id? */
2182 + if (mmfile_is_binary(&mf2))
2183 + continue;
2184 +
2185 + len1 = remove_space(p->one->path, strlen(p->one->path));
2186 + len2 = remove_space(p->two->path, strlen(p->two->path));
2187 + if (p->one->mode == 0)
2188 + len1 = snprintf(buffer, sizeof(buffer),
2189 + "diff--gita/%.*sb/%.*s"
2190 + "newfilemode%06o"
2191 + "---/dev/null"
2192 + "+++b/%.*s",
2193 + len1, p->one->path,
2194 + len2, p->two->path,
2195 + p->two->mode,
2196 + len2, p->two->path);
2197 + else if (p->two->mode == 0)
2198 + len1 = snprintf(buffer, sizeof(buffer),
2199 + "diff--gita/%.*sb/%.*s"
2200 + "deletedfilemode%06o"
2201 + "---a/%.*s"
2202 + "+++/dev/null",
2203 + len1, p->one->path,
2204 + len2, p->two->path,
2205 + p->one->mode,
2206 + len1, p->one->path);
2207 + else
2208 + len1 = snprintf(buffer, sizeof(buffer),
2209 + "diff--gita/%.*sb/%.*s"
2210 + "---a/%.*s"
2211 + "+++b/%.*s",
2212 + len1, p->one->path,
2213 + len2, p->two->path,
2214 + len1, p->one->path,
2215 + len2, p->two->path);
2216 + SHA1_Update(&ctx, buffer, len1);
2217 +
2218 + xpp.flags = XDF_NEED_MINIMAL;
2219 + xecfg.ctxlen = 3;
2220 + xecfg.flags = 3;
2221 + ecb.outf = xdiff_outf;
2222 + ecb.priv = &data;
2223 + xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
2224 + }
2225 +
2226 + SHA1_Final(sha1, &ctx);
2227 + return 0;
2228 +}
2229 +
2230 +int diff_flush_patch_id(struct diff_options *options, unsigned char *sha1)
2231 +{
2232 + struct diff_queue_struct *q = &diff_queued_diff;
2233 + int i;
2234 + int result = diff_get_patch_id(options, sha1);
2235 +
2236 + for (i = 0; i < q->nr; i++)
2237 + diff_free_filepair(q->queue[i]);
2238 +
2239 + free(q->queue);
2240 + q->queue = NULL;
2241 + q->nr = q->alloc = 0;
2242 +
2243 + return result;
2244 +}
2245 +
2246 void diff_flush(struct diff_options *options)
2247 {
2248 struct diff_queue_struct *q = &diff_queued_diff;
diff.h
+2
@@ -184,4 +184,6 @@ extern int run_diff_files(struct rev_info *revs, int silent_on_removed);
184
185 extern int run_diff_index(struct rev_info *revs, int cached);
186
187 +extern int diff_flush_patch_id(struct diff_options *, unsigned char *);
188 +
189 #endif /* DIFF_H */