refactor duplicated fill_mm() in checkout and merge-recursive
The following function is duplicated: fill_mm Move it to xdiff-interface.c and rename it 'read_mmblob', as suggested by Junio C Hamano. Also, change parameters order for consistency with read_mmfile(). Signed-off-by: Michael Lukashov <michael.lukashov@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Michael Lukashov committed
Feb 16, 2010 at 23:42 UTC
06b65939b083ba1b71043005bf83b4883e98264e
4 files changed
+24
-41
builtin-checkout.c
+3
-21
@@ -128,24 +128,6 @@ static int checkout_stage(int stage, struct cache_entry *ce, int pos,
128
(stage == 2) ? "our" : "their");
129
}
130
131
-/* NEEDSWORK: share with merge-recursive */
132
-static void fill_mm(const unsigned char *sha1, mmfile_t *mm)
133
-{
134
- unsigned long size;
135
- enum object_type type;
136
-
137
- if (!hashcmp(sha1, null_sha1)) {
138
- mm->ptr = xstrdup("");
139
- mm->size = 0;
140
- return;
141
- }
142
-
143
- mm->ptr = read_sha1_file(sha1, &type, &size);
144
- if (!mm->ptr || type != OBJ_BLOB)
145
- die("unable to read blob object %s", sha1_to_hex(sha1));
146
- mm->size = size;
147
-}
148
-
131
static int checkout_merged(int pos, struct checkout *state)
132
{
133
struct cache_entry *ce = active_cache[pos];
@@ -163,9 +145,9 @@ static int checkout_merged(int pos, struct checkout *state)
145
ce_stage(active_cache[pos+2]) != 3)
146
return error("path '%s' does not have all 3 versions", path);
147
166
- fill_mm(active_cache[pos]->sha1, &ancestor);
167
- fill_mm(active_cache[pos+1]->sha1, &ours);
168
- fill_mm(active_cache[pos+2]->sha1, &theirs);
148
+ read_mmblob(&ancestor, active_cache[pos]->sha1);
149
+ read_mmblob(&ours, active_cache[pos+1]->sha1);
150
+ read_mmblob(&theirs, active_cache[pos+2]->sha1);
151
152
status = ll_merge(&result_buf, path, &ancestor,
153
&ours, "ours", &theirs, "theirs", 0);
merge-recursive.c
+3
-20
@@ -599,23 +599,6 @@ struct merge_file_info
599
merge:1;
600
};
601
602
-static void fill_mm(const unsigned char *sha1, mmfile_t *mm)
603
-{
604
- unsigned long size;
605
- enum object_type type;
606
-
607
- if (!hashcmp(sha1, null_sha1)) {
608
- mm->ptr = xstrdup("");
609
- mm->size = 0;
610
- return;
611
- }
612
-
613
- mm->ptr = read_sha1_file(sha1, &type, &size);
614
- if (!mm->ptr || type != OBJ_BLOB)
615
- die("unable to read blob object %s", sha1_to_hex(sha1));
616
- mm->size = size;
617
-}
618
-
602
static int merge_3way(struct merge_options *o,
603
mmbuffer_t *result_buf,
604
struct diff_filespec *one,
@@ -653,9 +636,9 @@ static int merge_3way(struct merge_options *o,
636
name2 = xstrdup(mkpath("%s", branch2));
637
}
638
656
- fill_mm(one->sha1, &orig);
657
- fill_mm(a->sha1, &src1);
658
- fill_mm(b->sha1, &src2);
639
+ read_mmblob(&orig, one->sha1);
640
+ read_mmblob(&src1, a->sha1);
641
+ read_mmblob(&src2, b->sha1);
642
643
merge_status = ll_merge(result_buf, a->path, &orig,
644
&src1, name1, &src2, name2,
xdiff-interface.c
+17
@@ -218,6 +218,23 @@ int read_mmfile(mmfile_t *ptr, const char *filename)
218
return 0;
219
}
220
221
+void read_mmblob(mmfile_t *ptr, const unsigned char *sha1)
222
+{
223
+ unsigned long size;
224
+ enum object_type type;
225
+
226
+ if (!hashcmp(sha1, null_sha1)) {
227
+ ptr->ptr = xstrdup("");
228
+ ptr->size = 0;
229
+ return;
230
+ }
231
+
232
+ ptr->ptr = read_sha1_file(sha1, &type, &size);
233
+ if (!ptr->ptr || type != OBJ_BLOB)
234
+ die("unable to read blob object %s", sha1_to_hex(sha1));
235
+ ptr->size = size;
236
+}
237
+
238
#define FIRST_FEW_BYTES 8000
239
int buffer_is_binary(const char *ptr, unsigned long size)
240
{
xdiff-interface.h
+1
@@ -18,6 +18,7 @@ int parse_hunk_header(char *line, int len,
18
int *ob, int *on,
19
int *nb, int *nn);
20
int read_mmfile(mmfile_t *ptr, const char *filename);
21
+void read_mmblob(mmfile_t *ptr, const unsigned char *sha1);
22
int buffer_is_binary(const char *ptr, unsigned long size);
23
24
extern void xdiff_set_find_func(xdemitconf_t *xecfg, const char *line, int cflags);