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

git-apply: apply submodule changes

Apply "Subproject commit HEX" changes produced by git-diff. As usual in the current git, only the superproject itself is actually modified (possibly creating empty directories for new submodules). Any checked-out submodule is left untouched and is not required to be up-to-date. With clean-ups from Junio C Hamano. Signed-off-by: Sven Verdoolaege <skimo@kotnet.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Sven Verdoolaege committed Aug 15, 2007 at 19:22 UTC e06c5a6c7bdaa8c96b72e29f7fb49a331f1e0cc2
3 files changed +117 -25
Documentation/git-apply.txt
+14
@@ -171,6 +171,20 @@ apply.whitespace::
171 When no `--whitespace` flag is given from the command
172 line, this configuration item is used as the default.
173
174 +Submodules
175 +----------
176 +If the patch contains any changes to submodules then gitlink:git-apply[1]
177 +treats these changes as follows.
178 +
179 +If --index is specified (explicitly or implicitly), then the submodule
180 +commits must match the index exactly for the patch to apply. If any
181 +of the submodules are checked-out, then these check-outs are completely
182 +ignored, i.e., they are not required to be up-to-date or clean and they
183 +are not updated.
184 +
185 +If --index is not specified, then the submodule commits in the patch
186 +are ignored and only the absence of presence of the corresponding
187 +subdirectory is checked and (if possible) updated.
188
189 Author
190 ------
builtin-apply.c
+86 -25
@@ -1984,6 +1984,25 @@ static int apply_fragments(struct buffer_desc *desc, struct patch *patch)
1984 return 0;
1985 }
1986
1987 +static int read_file_or_gitlink(struct cache_entry *ce, char **buf_p,
1988 + unsigned long *size_p)
1989 +{
1990 + if (!ce)
1991 + return 0;
1992 +
1993 + if (S_ISGITLINK(ntohl(ce->ce_mode))) {
1994 + *buf_p = xmalloc(100);
1995 + *size_p = snprintf(*buf_p, 100,
1996 + "Subproject commit %s\n", sha1_to_hex(ce->sha1));
1997 + } else {
1998 + enum object_type type;
1999 + *buf_p = read_sha1_file(ce->sha1, &type, size_p);
2000 + if (!*buf_p)
2001 + return -1;
2002 + }
2003 + return 0;
2004 +}
2005 +
2006 static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *ce)
2007 {
2008 char *buf;
@@ -1994,22 +2013,32 @@ static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *
2013 alloc = 0;
2014 buf = NULL;
2015 if (cached) {
1997 - if (ce) {
1998 - enum object_type type;
1999 - buf = read_sha1_file(ce->sha1, &type, &size);
2000 - if (!buf)
2016 + if (read_file_or_gitlink(ce, &buf, &size))
2017 + return error("read of %s failed", patch->old_name);
2018 + alloc = size;
2019 + } else if (patch->old_name) {
2020 + if (S_ISGITLINK(patch->old_mode)) {
2021 + if (ce)
2022 + read_file_or_gitlink(ce, &buf, &size);
2023 + else {
2024 + /*
2025 + * There is no way to apply subproject
2026 + * patch without looking at the index.
2027 + */
2028 + patch->fragments = NULL;
2029 + size = 0;
2030 + }
2031 + }
2032 + else {
2033 + size = xsize_t(st->st_size);
2034 + alloc = size + 8192;
2035 + buf = xmalloc(alloc);
2036 + if (read_old_data(st, patch->old_name,
2037 + &buf, &alloc, &size))
2038 return error("read of %s failed",
2039 patch->old_name);
2003 - alloc = size;
2040 }
2041 }
2006 - else if (patch->old_name) {
2007 - size = xsize_t(st->st_size);
2008 - alloc = size + 8192;
2009 - buf = xmalloc(alloc);
2010 - if (read_old_data(st, patch->old_name, &buf, &alloc, &size))
2011 - return error("read of %s failed", patch->old_name);
2012 - }
2042
2043 desc.size = size;
2044 desc.alloc = alloc;
@@ -2055,6 +2084,16 @@ static int check_to_create_blob(const char *new_name, int ok_if_exists)
2084 return 0;
2085 }
2086
2087 +static int verify_index_match(struct cache_entry *ce, struct stat *st)
2088 +{
2089 + if (S_ISGITLINK(ntohl(ce->ce_mode))) {
2090 + if (!S_ISDIR(st->st_mode))
2091 + return -1;
2092 + return 0;
2093 + }
2094 + return ce_match_stat(ce, st, 1);
2095 +}
2096 +
2097 static int check_patch(struct patch *patch, struct patch *prev_patch)
2098 {
2099 struct stat st;
@@ -2065,8 +2104,14 @@ static int check_patch(struct patch *patch, struct patch *prev_patch)
2104 int ok_if_exists;
2105
2106 patch->rejected = 1; /* we will drop this after we succeed */
2107 +
2108 + /*
2109 + * Make sure that we do not have local modifications from the
2110 + * index when we are looking at the index. Also make sure
2111 + * we have the preimage file to be patched in the work tree,
2112 + * unless --cached, which tells git to apply only in the index.
2113 + */
2114 if (old_name) {
2069 - int changed = 0;
2115 int stat_ret = 0;
2116 unsigned st_mode = 0;
2117
@@ -2096,15 +2141,12 @@ static int check_patch(struct patch *patch, struct patch *prev_patch)
2141 lstat(old_name, &st))
2142 return -1;
2143 }
2099 - if (!cached)
2100 - changed = ce_match_stat(ce, &st, 1);
2101 - if (changed)
2144 + if (!cached && verify_index_match(ce, &st))
2145 return error("%s: does not match index",
2146 old_name);
2147 if (cached)
2148 st_mode = ntohl(ce->ce_mode);
2106 - }
2107 - else if (stat_ret < 0)
2149 + } else if (stat_ret < 0)
2150 return error("%s: %s", old_name, strerror(errno));
2151
2152 if (!cached)
@@ -2354,7 +2396,11 @@ static void remove_file(struct patch *patch, int rmdir_empty)
2396 cache_tree_invalidate_path(active_cache_tree, patch->old_name);
2397 }
2398 if (!cached) {
2357 - if (!unlink(patch->old_name) && rmdir_empty) {
2399 + if (S_ISGITLINK(patch->old_mode)) {
2400 + if (rmdir(patch->old_name))
2401 + warning("unable to remove submodule %s",
2402 + patch->old_name);
2403 + } else if (!unlink(patch->old_name) && rmdir_empty) {
2404 char *name = xstrdup(patch->old_name);
2405 char *end = strrchr(name, '/');
2406 while (end) {
@@ -2382,13 +2428,21 @@ static void add_index_file(const char *path, unsigned mode, void *buf, unsigned
2428 memcpy(ce->name, path, namelen);
2429 ce->ce_mode = create_ce_mode(mode);
2430 ce->ce_flags = htons(namelen);
2385 - if (!cached) {
2386 - if (lstat(path, &st) < 0)
2387 - die("unable to stat newly created file %s", path);
2388 - fill_stat_cache_info(ce, &st);
2431 + if (S_ISGITLINK(mode)) {
2432 + const char *s = buf;
2433 +
2434 + if (get_sha1_hex(s + strlen("Subproject commit "), ce->sha1))
2435 + die("corrupt patch for subproject %s", path);
2436 + } else {
2437 + if (!cached) {
2438 + if (lstat(path, &st) < 0)
2439 + die("unable to stat newly created file %s",
2440 + path);
2441 + fill_stat_cache_info(ce, &st);
2442 + }
2443 + if (write_sha1_file(buf, size, blob_type, ce->sha1) < 0)
2444 + die("unable to create backing store for newly created file %s", path);
2445 }
2390 - if (write_sha1_file(buf, size, blob_type, ce->sha1) < 0)
2391 - die("unable to create backing store for newly created file %s", path);
2446 if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)
2447 die("unable to add cache entry for %s", path);
2448 }
@@ -2398,6 +2452,13 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,
2452 int fd;
2453 char *nbuf;
2454
2455 + if (S_ISGITLINK(mode)) {
2456 + struct stat st;
2457 + if (!lstat(path, &st) && S_ISDIR(st.st_mode))
2458 + return 0;
2459 + return mkdir(path, 0777);
2460 + }
2461 +
2462 if (has_symlinks && S_ISLNK(mode))
2463 /* Although buf:size is counted string, it also is NUL
2464 * terminated.
t/t7400-submodule-basic.sh
+17
@@ -175,4 +175,21 @@ test_expect_success 'checkout superproject with subproject already present' '
175 git-checkout master
176 '
177
178 +test_expect_success 'apply submodule diff' '
179 + git branch second &&
180 + (
181 + cd lib &&
182 + echo s >s &&
183 + git add s &&
184 + git commit -m "change subproject"
185 + ) &&
186 + git update-index --add lib &&
187 + git-commit -m "change lib" &&
188 + git-format-patch -1 --stdout >P.diff &&
189 + git checkout second &&
190 + git apply --index P.diff &&
191 + D=$(git diff --cached master) &&
192 + test -z "$D"
193 +'
194 +
195 test_done