apply --cached: apply a patch without using working tree.
A new flag "--cached" takes the cached data, applies the patch and stores the result in the index, without using the working tree. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
May 15, 2006 at 15:15 UTC
04e4888e5a6db847368a95eb986202fa460b2153
1 file changed
+52
-20
apply.c
+52
-20
@@ -17,6 +17,8 @@
17
// --stat does just a diffstat, and doesn't actually apply
18
// --numstat does numeric diffstat, and doesn't actually apply
19
// --index-info shows the old and new index info for paths if available.
20
+// --index updates the cache as well.
21
+// --cached updates only the cache without ever touching the working tree.
22
//
23
static const char *prefix;
24
static int prefix_length = -1;
@@ -26,6 +28,7 @@ static int p_value = 1;
28
static int allow_binary_replacement = 0;
29
static int check_index = 0;
30
static int write_index = 0;
31
+static int cached = 0;
32
static int diffstat = 0;
33
static int numstat = 0;
34
static int summary = 0;
@@ -36,7 +39,7 @@ static int show_index_info = 0;
39
static int line_termination = '\n';
40
static unsigned long p_context = -1;
41
static const char apply_usage[] =
39
-"git-apply [--stat] [--numstat] [--summary] [--check] [--index] [--apply] [--no-add] [--index-info] [--allow-binary-replacement] [-z] [-pNUM] [-CNUM] [--whitespace=<nowarn|warn|error|error-all|strip>] <patch>...";
42
+"git-apply [--stat] [--numstat] [--summary] [--check] [--index] [--cached] [--apply] [--no-add] [--index-info] [--allow-binary-replacement] [-z] [-pNUM] [-CNUM] [--whitespace=<nowarn|warn|error|error-all|strip>] <patch>...";
43
44
static enum whitespace_eol {
45
nowarn_whitespace,
@@ -1600,7 +1603,7 @@ static int apply_fragments(struct buffer_desc *desc, struct patch *patch)
1603
return 0;
1604
}
1605
1603
-static int apply_data(struct patch *patch, struct stat *st)
1606
+static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *ce)
1607
{
1608
char *buf;
1609
unsigned long size, alloc;
@@ -1609,7 +1612,17 @@ static int apply_data(struct patch *patch, struct stat *st)
1612
size = 0;
1613
alloc = 0;
1614
buf = NULL;
1612
- if (patch->old_name) {
1615
+ if (cached) {
1616
+ if (ce) {
1617
+ char type[20];
1618
+ buf = read_sha1_file(ce->sha1, type, &size);
1619
+ if (!buf)
1620
+ return error("read of %s failed",
1621
+ patch->old_name);
1622
+ alloc = size;
1623
+ }
1624
+ }
1625
+ else if (patch->old_name) {
1626
size = st->st_size;
1627
alloc = size + 8192;
1628
buf = xmalloc(alloc);
@@ -1637,16 +1650,21 @@ static int check_patch(struct patch *patch)
1650
const char *old_name = patch->old_name;
1651
const char *new_name = patch->new_name;
1652
const char *name = old_name ? old_name : new_name;
1653
+ struct cache_entry *ce = NULL;
1654
1655
if (old_name) {
1642
- int changed;
1643
- int stat_ret = lstat(old_name, &st);
1656
+ int changed = 0;
1657
+ int stat_ret = 0;
1658
+ unsigned st_mode = 0;
1659
1660
+ if (!cached)
1661
+ stat_ret = lstat(old_name, &st);
1662
if (check_index) {
1663
int pos = cache_name_pos(old_name, strlen(old_name));
1664
if (pos < 0)
1665
return error("%s: does not exist in index",
1666
old_name);
1667
+ ce = active_cache[pos];
1668
if (stat_ret < 0) {
1669
struct checkout costate;
1670
if (errno != ENOENT)
@@ -1659,37 +1677,41 @@ static int check_patch(struct patch *patch)
1677
costate.quiet = 0;
1678
costate.not_new = 0;
1679
costate.refresh_cache = 1;
1662
- if (checkout_entry(active_cache[pos],
1680
+ if (checkout_entry(ce,
1681
&costate,
1682
NULL) ||
1683
lstat(old_name, &st))
1684
return -1;
1685
}
1668
-
1669
- changed = ce_match_stat(active_cache[pos], &st, 1);
1686
+ if (!cached)
1687
+ changed = ce_match_stat(ce, &st, 1);
1688
if (changed)
1689
return error("%s: does not match index",
1690
old_name);
1691
+ if (cached)
1692
+ st_mode = ntohl(ce->ce_mode);
1693
}
1694
else if (stat_ret < 0)
1695
return error("%s: %s", old_name, strerror(errno));
1696
1697
+ if (!cached)
1698
+ st_mode = ntohl(create_ce_mode(st.st_mode));
1699
+
1700
if (patch->is_new < 0)
1701
patch->is_new = 0;
1679
- st.st_mode = ntohl(create_ce_mode(st.st_mode));
1702
if (!patch->old_mode)
1681
- patch->old_mode = st.st_mode;
1682
- if ((st.st_mode ^ patch->old_mode) & S_IFMT)
1703
+ patch->old_mode = st_mode;
1704
+ if ((st_mode ^ patch->old_mode) & S_IFMT)
1705
return error("%s: wrong type", old_name);
1684
- if (st.st_mode != patch->old_mode)
1706
+ if (st_mode != patch->old_mode)
1707
fprintf(stderr, "warning: %s has type %o, expected %o\n",
1686
- old_name, st.st_mode, patch->old_mode);
1708
+ old_name, st_mode, patch->old_mode);
1709
}
1710
1711
if (new_name && (patch->is_new | patch->is_rename | patch->is_copy)) {
1712
if (check_index && cache_name_pos(new_name, strlen(new_name)) >= 0)
1713
return error("%s: already exists in index", new_name);
1692
- if (!lstat(new_name, &st))
1714
+ if (!cached && !lstat(new_name, &st))
1715
return error("%s: already exists in working directory", new_name);
1716
if (errno != ENOENT)
1717
return error("%s: %s", new_name, strerror(errno));
@@ -1709,9 +1731,9 @@ static int check_patch(struct patch *patch)
1731
return error("new mode (%o) of %s does not match old mode (%o)%s%s",
1732
patch->new_mode, new_name, patch->old_mode,
1733
same ? "" : " of ", same ? "" : old_name);
1712
- }
1734
+ }
1735
1714
- if (apply_data(patch, &st) < 0)
1736
+ if (apply_data(patch, &st, ce) < 0)
1737
return error("%s: patch does not apply", name);
1738
return 0;
1739
}
@@ -1894,7 +1916,8 @@ static void remove_file(struct patch *patch)
1916
if (remove_file_from_cache(patch->old_name) < 0)
1917
die("unable to remove %s from index", patch->old_name);
1918
}
1897
- unlink(patch->old_name);
1919
+ if (!cached)
1920
+ unlink(patch->old_name);
1921
}
1922
1923
static void add_index_file(const char *path, unsigned mode, void *buf, unsigned long size)
@@ -1911,9 +1934,11 @@ static void add_index_file(const char *path, unsigned mode, void *buf, unsigned
1934
memcpy(ce->name, path, namelen);
1935
ce->ce_mode = create_ce_mode(mode);
1936
ce->ce_flags = htons(namelen);
1914
- if (lstat(path, &st) < 0)
1915
- die("unable to stat newly created file %s", path);
1916
- fill_stat_cache_info(ce, &st);
1937
+ if (!cached) {
1938
+ if (lstat(path, &st) < 0)
1939
+ die("unable to stat newly created file %s", path);
1940
+ fill_stat_cache_info(ce, &st);
1941
+ }
1942
if (write_sha1_file(buf, size, blob_type, ce->sha1) < 0)
1943
die("unable to create backing store for newly created file %s", path);
1944
if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)
@@ -1950,6 +1975,8 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,
1975
*/
1976
static void create_one_file(char *path, unsigned mode, const char *buf, unsigned long size)
1977
{
1978
+ if (cached)
1979
+ return;
1980
if (!try_create_file(path, mode, buf, size))
1981
return;
1982
@@ -2182,6 +2209,11 @@ int main(int argc, char **argv)
2209
check_index = 1;
2210
continue;
2211
}
2212
+ if (!strcmp(arg, "--cached")) {
2213
+ check_index = 1;
2214
+ cached = 1;
2215
+ continue;
2216
+ }
2217
if (!strcmp(arg, "--apply")) {
2218
apply = 1;
2219
continue;