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

apply: fix infinite loop with multiple patches with --index

When multiple patches are passed to git-apply, it will attempt to open multiple file descriptors to an index, which means multiple entries will be in the circular cache_file_list. This change makes git-apply only open the index once and write the index at exit. Signed-off-by: Eric Wong <normalperson@yhbt.net> Signed-off-by: Junio C Hamano <junkio@cox.net>

Eric Wong committed May 9, 2006 at 01:08 UTC dbd0f7d3221fbf8e9943a114c11e5b5e5fc0c201
1 file changed +9 -9
apply.c
+9 -9
@@ -19,6 +19,7 @@
19 //
20 static const char *prefix;
21 static int prefix_length = -1;
22 +static int newfd = -1;
23
24 static int p_value = 1;
25 static int allow_binary_replacement = 0;
@@ -1873,7 +1874,6 @@ static int use_patch(struct patch *p)
1874
1875 static int apply_patch(int fd, const char *filename)
1876 {
1876 - int newfd;
1877 unsigned long offset, size;
1878 char *buffer = read_patch_file(fd, &size);
1879 struct patch *list = NULL, **listp = &list;
@@ -1904,12 +1904,11 @@ static int apply_patch(int fd, const char *filename)
1904 size -= nr;
1905 }
1906
1907 - newfd = -1;
1907 if (whitespace_error && (new_whitespace == error_on_whitespace))
1908 apply = 0;
1909
1910 write_index = check_index && apply;
1912 - if (write_index)
1911 + if (write_index && newfd < 0)
1912 newfd = hold_index_file_for_update(&cache_file, get_index_file());
1913 if (check_index) {
1914 if (read_cache() < 0)
@@ -1922,12 +1921,6 @@ static int apply_patch(int fd, const char *filename)
1921 if (apply)
1922 write_out_results(list, skipped_patch);
1923
1925 - if (write_index) {
1926 - if (write_cache(newfd, active_cache, active_nr) ||
1927 - commit_index_file(&cache_file))
1928 - die("Unable to write new cachefile");
1929 - }
1930 -
1924 if (show_index_info)
1925 show_index_list(list);
1926
@@ -2085,5 +2078,12 @@ int main(int argc, char **argv)
2078 whitespace_error == 1 ? "" : "s",
2079 whitespace_error == 1 ? "s" : "");
2080 }
2081 +
2082 + if (write_index) {
2083 + if (write_cache(newfd, active_cache, active_nr) ||
2084 + commit_index_file(&cache_file))
2085 + die("Unable to write new cachefile");
2086 + }
2087 +
2088 return 0;
2089 }