3636
3737static const char * prefix ;
3838static int prefix_length ;
39+ static int checkout_stage ; /* default to checkout stage0 */
3940
4041static struct checkout state = {
4142 .base_dir = "" ,
@@ -48,20 +49,36 @@ static struct checkout state = {
4849
4950static 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
6784static 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
90107static 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
93110static 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