apply: get rid of --index-info in favor of --build-fake-ancestor
git-am used "git apply -z --index-info" to find the original versions of the files touched by the diff, to be able to do an inexpensive three-way merge. This operation makes only sense in a repository, since the index information in the diff refers to blobs, which have to be present in the current repository. Therefore, teach "git apply" a mode to write out the result as an index file to begin with, obviating the need for scripts to do it themselves. The sole user for --index-info is "git am" is converted to use --build-fake-ancestor in this patch. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johannes Schindelin committed
Sep 17, 2007 at 23:34 UTC
26b28007689d27a921ea90e5a29fc8eb74b0d297
3 files changed
+31
-21
Documentation/git-apply.txt
+7
-4
@@ -10,7 +10,7 @@ SYNOPSIS
10
--------
11
[verse]
12
'git-apply' [--stat] [--numstat] [--summary] [--check] [--index]
13
- [--apply] [--no-add] [--index-info] [-R | --reverse]
13
+ [--apply] [--no-add] [--build-fake-ancestor <file>] [-R | --reverse]
14
[--allow-binary-replacement | --binary] [--reject] [-z]
15
[-pNUM] [-CNUM] [--inaccurate-eof] [--cached]
16
[--whitespace=<nowarn|warn|error|error-all|strip>]
@@ -63,12 +63,15 @@ OPTIONS
63
cached data, apply the patch, and store the result in the index,
64
without using the working tree. This implies '--index'.
65
66
---index-info::
66
+--build-fake-ancestor <file>::
67
Newer git-diff output has embedded 'index information'
68
for each blob to help identify the original version that
69
the patch applies to. When this flag is given, and if
70
- the original version of the blob is available locally,
71
- outputs information about them to the standard output.
70
+ the original versions of the blobs is available locally,
71
+ builds a temporary index containing those blobs.
72
++
73
+When a pure mode change is encountered (which has no index information),
74
+the information is read from the current index instead.
75
76
-R, --reverse::
77
Apply the patch in reverse.
builtin-apply.c
+22
-13
@@ -41,7 +41,7 @@ static int apply_in_reverse;
41
static int apply_with_reject;
42
static int apply_verbosely;
43
static int no_add;
44
-static int show_index_info;
44
+static const char *fake_ancestor;
45
static int line_termination = '\n';
46
static unsigned long p_context = ULONG_MAX;
47
static const char apply_usage[] =
@@ -2248,9 +2248,12 @@ static int get_current_sha1(const char *path, unsigned char *sha1)
2248
return 0;
2249
}
2250
2251
-static void show_index_list(struct patch *list)
2251
+/* Build an index that contains the just the files needed for a 3way merge */
2252
+static void build_fake_ancestor(struct patch *list, const char *filename)
2253
{
2254
struct patch *patch;
2255
+ struct index_state result = { 0 };
2256
+ int fd;
2257
2258
/* Once we start supporting the reverse patch, it may be
2259
* worth showing the new sha1 prefix, but until then...
@@ -2258,11 +2261,12 @@ static void show_index_list(struct patch *list)
2261
for (patch = list; patch; patch = patch->next) {
2262
const unsigned char *sha1_ptr;
2263
unsigned char sha1[20];
2264
+ struct cache_entry *ce;
2265
const char *name;
2266
2267
name = patch->old_name ? patch->old_name : patch->new_name;
2268
if (0 < patch->is_new)
2265
- sha1_ptr = null_sha1;
2269
+ continue;
2270
else if (get_sha1(patch->old_sha1_prefix, sha1))
2271
/* git diff has no index line for mode/type changes */
2272
if (!patch->lines_added && !patch->lines_deleted) {
@@ -2277,13 +2281,16 @@ static void show_index_list(struct patch *list)
2281
else
2282
sha1_ptr = sha1;
2283
2280
- printf("%06o %s ",patch->old_mode, sha1_to_hex(sha1_ptr));
2281
- if (line_termination && quote_c_style(name, NULL, NULL, 0))
2282
- quote_c_style(name, NULL, stdout, 0);
2283
- else
2284
- fputs(name, stdout);
2285
- putchar(line_termination);
2284
+ ce = make_cache_entry(patch->old_mode, sha1_ptr, name, 0, 0);
2285
+ if (add_index_entry(&result, ce, ADD_CACHE_OK_TO_ADD))
2286
+ die ("Could not add %s to temporary index", name);
2287
}
2288
+
2289
+ fd = open(filename, O_WRONLY | O_CREAT, 0666);
2290
+ if (fd < 0 || write_index(&result, fd) || close(fd))
2291
+ die ("Could not write temporary index to %s", filename);
2292
+
2293
+ discard_index(&result);
2294
}
2295
2296
static void stat_patch_list(struct patch *patch)
@@ -2805,8 +2812,8 @@ static int apply_patch(int fd, const char *filename, int inaccurate_eof)
2812
if (apply && write_out_results(list, skipped_patch))
2813
exit(1);
2814
2808
- if (show_index_info)
2809
- show_index_list(list);
2815
+ if (fake_ancestor)
2816
+ build_fake_ancestor(list, fake_ancestor);
2817
2818
if (diffstat)
2819
stat_patch_list(list);
@@ -2914,9 +2921,11 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)
2921
apply = 1;
2922
continue;
2923
}
2917
- if (!strcmp(arg, "--index-info")) {
2924
+ if (!strcmp(arg, "--build-fake-ancestor")) {
2925
apply = 0;
2919
- show_index_info = 1;
2926
+ if (++i >= argc)
2927
+ die ("need a filename");
2928
+ fake_ancestor = argv[i];
2929
continue;
2930
}
2931
if (!strcmp(arg, "-z")) {
git-am.sh
+2
-4
@@ -62,10 +62,8 @@ fall_back_3way () {
62
mkdir "$dotest/patch-merge-tmp-dir"
63
64
# First see if the patch records the index info that we can use.
65
- git apply -z --index-info "$dotest/patch" \
66
- >"$dotest/patch-merge-index-info" &&
67
- GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
68
- git update-index -z --index-info <"$dotest/patch-merge-index-info" &&
65
+ git apply --build-fake-ancestor "$dotest/patch-merge-tmp-index" \
66
+ "$dotest/patch" &&
67
GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
68
git write-tree >"$dotest/patch-merge-base+" ||
69
cannot_fallback "Repository lacks necessary blobs to fall back on 3-way merge."