Skip to content

Commit 3bd348a

Browse files
author
Junio C Hamano
committed
checkout-index: allow checking out from higher stages.
The new option, --stage=<n>, lets you copy out from an unmerged, higher stage. This is to help the new merge world order during a nontrivial merge. Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent 9754563 commit 3bd348a

File tree

2 files changed

+48
-19
lines changed

2 files changed

+48
-19
lines changed

Documentation/git-checkout-index.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ git-checkout-index - Copy files from the index to the working directory
99
SYNOPSIS
1010
--------
1111
'git-checkout-index' [-u] [-q] [-a] [-f] [-n] [--prefix=<string>]
12-
[--] <file>...
12+
[--stage=<number>] [--] <file>...
1313

1414
DESCRIPTION
1515
-----------
@@ -40,6 +40,10 @@ OPTIONS
4040
When creating files, prepend <string> (usually a directory
4141
including a trailing /)
4242

43+
--stage=<number>::
44+
Instead of checking out unmerged entries, copy out the
45+
files from named stage. <number> must be between 1 and 3.
46+
4347
--::
4448
Do not interpret any more arguments as options.
4549

checkout-index.c

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
static const char *prefix;
3838
static int prefix_length;
39+
static int checkout_stage; /* default to checkout stage0 */
3940

4041
static struct checkout state = {
4142
.base_dir = "",
@@ -48,20 +49,36 @@ static struct checkout state = {
4849

4950
static int checkout_file(const char *name)
5051
{
51-
int pos = cache_name_pos(name, strlen(name));
52-
if (pos < 0) {
53-
if (!state.quiet) {
54-
pos = -pos - 1;
55-
fprintf(stderr,
56-
"git-checkout-index: %s is %s.\n",
57-
name,
58-
(pos < active_nr &&
59-
!strcmp(active_cache[pos]->name, name)) ?
60-
"unmerged" : "not in the cache");
61-
}
62-
return -1;
52+
int namelen = strlen(name);
53+
int pos = cache_name_pos(name, namelen);
54+
int has_same_name = 0;
55+
56+
if (pos < 0)
57+
pos = -pos - 1;
58+
59+
while (pos < active_nr) {
60+
struct cache_entry *ce = active_cache[pos];
61+
if (ce_namelen(ce) != namelen &&
62+
memcmp(ce->name, name, namelen))
63+
break;
64+
has_same_name = 1;
65+
if (checkout_stage == ce_stage(ce))
66+
return checkout_entry(ce, &state);
67+
pos++;
6368
}
64-
return checkout_entry(active_cache[pos], &state);
69+
70+
if (!state.quiet) {
71+
fprintf(stderr, "git-checkout-index: %s ", name);
72+
if (!has_same_name)
73+
fprintf(stderr, "is not in the cache");
74+
else if (checkout_stage)
75+
fprintf(stderr, "does not exist at stage %d",
76+
checkout_stage);
77+
else
78+
fprintf(stderr, "is unmerged");
79+
fputc('\n', stderr);
80+
}
81+
return -1;
6582
}
6683

6784
static int checkout_all(void)
@@ -70,11 +87,11 @@ static int checkout_all(void)
7087

7188
for (i = 0; i < active_nr ; i++) {
7289
struct cache_entry *ce = active_cache[i];
73-
if (ce_stage(ce))
90+
if (ce_stage(ce) != checkout_stage)
7491
continue;
7592
if (prefix && *prefix &&
76-
( ce_namelen(ce) <= prefix_length ||
77-
memcmp(prefix, ce->name, prefix_length) ))
93+
(ce_namelen(ce) <= prefix_length ||
94+
memcmp(prefix, ce->name, prefix_length)))
7895
continue;
7996
if (checkout_entry(ce, &state) < 0)
8097
errs++;
@@ -88,7 +105,7 @@ static int checkout_all(void)
88105
}
89106

90107
static const char checkout_cache_usage[] =
91-
"git-checkout-index [-u] [-q] [-a] [-f] [-n] [--prefix=<string>] [--] <file>...";
108+
"git-checkout-index [-u] [-q] [-a] [-f] [-n] [--stage=[123]] [--prefix=<string>] [--] <file>...";
92109

93110
static struct cache_file cache_file;
94111

@@ -138,11 +155,19 @@ int main(int argc, char **argv)
138155
die("cannot open index.lock file.");
139156
continue;
140157
}
141-
if (!memcmp(arg, "--prefix=", 9)) {
158+
if (!strncmp(arg, "--prefix=", 9)) {
142159
state.base_dir = arg+9;
143160
state.base_dir_len = strlen(state.base_dir);
144161
continue;
145162
}
163+
if (!strncmp(arg, "--stage=", 8)) {
164+
int ch = arg[8];
165+
if ('1' <= ch && ch <= '3')
166+
checkout_stage = arg[8] - '0';
167+
else
168+
die("stage should be between 1 and 3");
169+
continue;
170+
}
146171
if (arg[0] == '-')
147172
usage(checkout_cache_usage);
148173
break;

0 commit comments

Comments
 (0)