Skip to content

Commit 9e6c7e0

Browse files
author
Junio C Hamano
committed
Merge branch 'jc/diffopt'
* jc/diffopt: diff -S: release the image after looking for needle in it diff -M: release the preimage candidate blobs after rename detection. diff.c: do not use a separate "size cache". diff: release blobs after generating textual diff.
2 parents a626166 + a0cb940 commit 9e6c7e0

File tree

3 files changed

+23
-73
lines changed

3 files changed

+23
-73
lines changed

diff.c

Lines changed: 21 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
#define FAST_WORKING_DIRECTORY 1
1717
#endif
1818

19-
static int use_size_cache;
20-
2119
static int diff_detect_rename_default;
2220
static int diff_rename_limit_default = -1;
2321
static int diff_use_color_default;
@@ -1236,6 +1234,8 @@ static void builtin_diff(const char *name_a,
12361234
}
12371235

12381236
free_ab_and_return:
1237+
diff_free_filespec_data(one);
1238+
diff_free_filespec_data(two);
12391239
free(a_one);
12401240
free(b_two);
12411241
return;
@@ -1262,7 +1262,7 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
12621262
diff_populate_filespec(two, 0);
12631263
data->deleted = count_lines(one->data, one->size);
12641264
data->added = count_lines(two->data, two->size);
1265-
return;
1265+
goto free_and_return;
12661266
}
12671267
if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
12681268
die("unable to read files to diff");
@@ -1284,6 +1284,10 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
12841284
ecb.priv = diffstat;
12851285
xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
12861286
}
1287+
1288+
free_and_return:
1289+
diff_free_filespec_data(one);
1290+
diff_free_filespec_data(two);
12871291
}
12881292

12891293
static void builtin_checkdiff(const char *name_a, const char *name_b,
@@ -1306,7 +1310,7 @@ static void builtin_checkdiff(const char *name_a, const char *name_b,
13061310
die("unable to read files to diff");
13071311

13081312
if (file_is_binary(two))
1309-
return;
1313+
goto free_and_return;
13101314
else {
13111315
/* Crazy xdl interfaces.. */
13121316
xpparam_t xpp;
@@ -1320,6 +1324,9 @@ static void builtin_checkdiff(const char *name_a, const char *name_b,
13201324
ecb.priv = &data;
13211325
xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
13221326
}
1327+
free_and_return:
1328+
diff_free_filespec_data(one);
1329+
diff_free_filespec_data(two);
13231330
}
13241331

13251332
struct diff_filespec *alloc_filespec(const char *path)
@@ -1399,55 +1406,6 @@ static int reuse_worktree_file(const char *name, const unsigned char *sha1, int
13991406
return 1;
14001407
}
14011408

1402-
static struct sha1_size_cache {
1403-
unsigned char sha1[20];
1404-
unsigned long size;
1405-
} **sha1_size_cache;
1406-
static int sha1_size_cache_nr, sha1_size_cache_alloc;
1407-
1408-
static struct sha1_size_cache *locate_size_cache(unsigned char *sha1,
1409-
int find_only,
1410-
unsigned long size)
1411-
{
1412-
int first, last;
1413-
struct sha1_size_cache *e;
1414-
1415-
first = 0;
1416-
last = sha1_size_cache_nr;
1417-
while (last > first) {
1418-
int cmp, next = (last + first) >> 1;
1419-
e = sha1_size_cache[next];
1420-
cmp = hashcmp(e->sha1, sha1);
1421-
if (!cmp)
1422-
return e;
1423-
if (cmp < 0) {
1424-
last = next;
1425-
continue;
1426-
}
1427-
first = next+1;
1428-
}
1429-
/* not found */
1430-
if (find_only)
1431-
return NULL;
1432-
/* insert to make it at "first" */
1433-
if (sha1_size_cache_alloc <= sha1_size_cache_nr) {
1434-
sha1_size_cache_alloc = alloc_nr(sha1_size_cache_alloc);
1435-
sha1_size_cache = xrealloc(sha1_size_cache,
1436-
sha1_size_cache_alloc *
1437-
sizeof(*sha1_size_cache));
1438-
}
1439-
sha1_size_cache_nr++;
1440-
if (first < sha1_size_cache_nr)
1441-
memmove(sha1_size_cache + first + 1, sha1_size_cache + first,
1442-
(sha1_size_cache_nr - first - 1) *
1443-
sizeof(*sha1_size_cache));
1444-
e = xmalloc(sizeof(struct sha1_size_cache));
1445-
sha1_size_cache[first] = e;
1446-
hashcpy(e->sha1, sha1);
1447-
e->size = size;
1448-
return e;
1449-
}
1450-
14511409
static int populate_from_stdin(struct diff_filespec *s)
14521410
{
14531411
#define INCREMENT 1024
@@ -1503,11 +1461,11 @@ int diff_populate_filespec(struct diff_filespec *s, int size_only)
15031461
if (S_ISDIR(s->mode))
15041462
return -1;
15051463

1506-
if (!use_size_cache)
1507-
size_only = 0;
1508-
15091464
if (s->data)
1510-
return err;
1465+
return 0;
1466+
1467+
if (size_only && 0 < s->size)
1468+
return 0;
15111469

15121470
if (S_ISDIRLNK(s->mode))
15131471
return diff_populate_gitlink(s, size_only);
@@ -1570,19 +1528,8 @@ int diff_populate_filespec(struct diff_filespec *s, int size_only)
15701528
}
15711529
else {
15721530
enum object_type type;
1573-
struct sha1_size_cache *e;
1574-
1575-
if (size_only && use_size_cache &&
1576-
(e = locate_size_cache(s->sha1, 1, 0)) != NULL) {
1577-
s->size = e->size;
1578-
return 0;
1579-
}
1580-
1581-
if (size_only) {
1531+
if (size_only)
15821532
type = sha1_object_info(s->sha1, &s->size);
1583-
if (use_size_cache && 0 < type)
1584-
locate_size_cache(s->sha1, 0, s->size);
1585-
}
15861533
else {
15871534
s->data = read_sha1_file(s->sha1, &type, &s->size);
15881535
s->should_free = 1;
@@ -1597,8 +1544,11 @@ void diff_free_filespec_data(struct diff_filespec *s)
15971544
free(s->data);
15981545
else if (s->should_munmap)
15991546
munmap(s->data, s->size);
1600-
s->should_free = s->should_munmap = 0;
1601-
s->data = NULL;
1547+
1548+
if (s->should_free || s->should_munmap) {
1549+
s->should_free = s->should_munmap = 0;
1550+
s->data = NULL;
1551+
}
16021552
free(s->cnt_data);
16031553
s->cnt_data = NULL;
16041554
}
@@ -2090,8 +2040,6 @@ int diff_setup_done(struct diff_options *options)
20902040
*/
20912041
read_cache();
20922042
}
2093-
if (options->setup & DIFF_SETUP_USE_SIZE_CACHE)
2094-
use_size_cache = 1;
20952043
if (options->abbrev <= 0 || 40 < options->abbrev)
20962044
options->abbrev = 40; /* full */
20972045

diffcore-pickaxe.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ static unsigned int contains(struct diff_filespec *one,
4444
}
4545
}
4646
}
47+
diff_free_filespec_data(one);
4748
return cnt;
4849
}
4950

diffcore-rename.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ void diffcore_rename(struct diff_options *options)
329329
m->dst = i;
330330
m->score = estimate_similarity(one, two,
331331
minimum_score);
332+
diff_free_filespec_data(one);
332333
}
333334
/* We do not need the text anymore */
334335
diff_free_filespec_data(two);

0 commit comments

Comments
 (0)