git-apply: allow operating in sparsely populated working tree.
This patch teaches 'git-apply --index' to automatically check out a file being patched. This happens only when the working tree does not have it checked out. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Oct 3, 2005 at 13:16 UTC
56d33b1105e63590577672de9afb920f6e7fd22c
1 file changed
+30
-5
apply.c
+30
-5
@@ -676,7 +676,10 @@ static int parse_fragment(char *line, unsigned long size, struct patch *patch, s
676
/* We allow "\ No newline at end of file". Depending
677
* on locale settings when the patch was produced we
678
* don't know what this line looks like. The only
679
- * thing we do know is that it begins with "\ ". */
679
+ * thing we do know is that it begins with "\ ".
680
+ * Checking for 12 is just for sanity check -- any
681
+ * l10n of "\ No newline..." is at least that long.
682
+ */
683
case '\\':
684
if (len < 12 || memcmp(line, "\\ ", 2))
685
return -1;
@@ -1030,17 +1033,39 @@ static int check_patch(struct patch *patch)
1033
1034
if (old_name) {
1035
int changed;
1036
+ int stat_ret = lstat(old_name, &st);
1037
1034
- if (lstat(old_name, &st) < 0)
1035
- return error("%s: %s", old_name, strerror(errno));
1038
if (check_index) {
1039
int pos = cache_name_pos(old_name, strlen(old_name));
1040
if (pos < 0)
1039
- return error("%s: does not exist in index", old_name);
1041
+ return error("%s: does not exist in index",
1042
+ old_name);
1043
+ if (stat_ret < 0) {
1044
+ struct checkout costate;
1045
+ if (errno != ENOENT)
1046
+ return error("%s: %s", old_name,
1047
+ strerror(errno));
1048
+ /* checkout */
1049
+ costate.base_dir = "";
1050
+ costate.base_dir_len = 0;
1051
+ costate.force = 0;
1052
+ costate.quiet = 0;
1053
+ costate.not_new = 0;
1054
+ costate.refresh_cache = 1;
1055
+ if (checkout_entry(active_cache[pos],
1056
+ &costate) ||
1057
+ lstat(old_name, &st))
1058
+ return -1;
1059
+ }
1060
+
1061
changed = ce_match_stat(active_cache[pos], &st);
1062
if (changed)
1042
- return error("%s: does not match index", old_name);
1063
+ return error("%s: does not match index",
1064
+ old_name);
1065
}
1066
+ else if (stat_ret < 0)
1067
+ return error("%s: %s", old_name, strerror(errno));
1068
+
1069
if (patch->is_new < 0)
1070
patch->is_new = 0;
1071
st.st_mode = ntohl(create_ce_mode(st.st_mode));