Skip to content

Commit b7033e7

Browse files
tgummerergitster
authored andcommitted
checkout: factor out mark_cache_entry_for_checkout function
Factor out the code that marks a cache entry as matched for checkout into a separate function. We are going to introduce a new mode in 'git checkout' in a subsequent commit, that is going to have a slightly different logic. This would make this code unnecessarily complex. Moving that complexity into separate functions will make the code in the subsequent step easier to follow. Signed-off-by: Thomas Gummerer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5160fa0 commit b7033e7

File tree

1 file changed

+36
-31
lines changed

1 file changed

+36
-31
lines changed

builtin/checkout.c

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,40 @@ static int checkout_merged(int pos, const struct checkout *state)
247247
return status;
248248
}
249249

250+
static void mark_ce_for_checkout(struct cache_entry *ce,
251+
char *ps_matched,
252+
const struct checkout_opts *opts)
253+
{
254+
ce->ce_flags &= ~CE_MATCHED;
255+
if (!opts->ignore_skipworktree && ce_skip_worktree(ce))
256+
return;
257+
if (opts->source_tree && !(ce->ce_flags & CE_UPDATE))
258+
/*
259+
* "git checkout tree-ish -- path", but this entry
260+
* is in the original index but is not in tree-ish
261+
* or does not match the pathspec; it will not be
262+
* checked out to the working tree. We will not do
263+
* anything to this entry at all.
264+
*/
265+
return;
266+
/*
267+
* Either this entry came from the tree-ish we are
268+
* checking the paths out of, or we are checking out
269+
* of the index.
270+
*
271+
* If it comes from the tree-ish, we already know it
272+
* matches the pathspec and could just stamp
273+
* CE_MATCHED to it from update_some(). But we still
274+
* need ps_matched and read_tree_recursive (and
275+
* eventually tree_entry_interesting) cannot fill
276+
* ps_matched yet. Once it can, we can avoid calling
277+
* match_pathspec() for _all_ entries when
278+
* opts->source_tree != NULL.
279+
*/
280+
if (ce_path_match(&the_index, ce, &opts->pathspec, ps_matched))
281+
ce->ce_flags |= CE_MATCHED;
282+
}
283+
250284
static int checkout_paths(const struct checkout_opts *opts,
251285
const char *revision)
252286
{
@@ -297,37 +331,8 @@ static int checkout_paths(const struct checkout_opts *opts,
297331
* Make sure all pathspecs participated in locating the paths
298332
* to be checked out.
299333
*/
300-
for (pos = 0; pos < active_nr; pos++) {
301-
struct cache_entry *ce = active_cache[pos];
302-
ce->ce_flags &= ~CE_MATCHED;
303-
if (!opts->ignore_skipworktree && ce_skip_worktree(ce))
304-
continue;
305-
if (opts->source_tree && !(ce->ce_flags & CE_UPDATE))
306-
/*
307-
* "git checkout tree-ish -- path" and this entry
308-
* is in the original index, but is not in tree-ish
309-
* or does not match the pathspec; it will not be
310-
* checked out to the working tree. We will not do
311-
* anything to this entry at all.
312-
*/
313-
continue;
314-
/*
315-
* Either this entry came from the tree-ish we are
316-
* checking the paths out of, or we are checking out
317-
* of the index.
318-
*
319-
* If it comes from the tree-ish, we already know it
320-
* matches the pathspec and could just stamp
321-
* CE_MATCHED to it from update_some(). But we still
322-
* need ps_matched and read_tree_recursive (and
323-
* eventually tree_entry_interesting) cannot fill
324-
* ps_matched yet. Once it can, we can avoid calling
325-
* match_pathspec() for _all_ entries when
326-
* opts->source_tree != NULL.
327-
*/
328-
if (ce_path_match(&the_index, ce, &opts->pathspec, ps_matched))
329-
ce->ce_flags |= CE_MATCHED;
330-
}
334+
for (pos = 0; pos < active_nr; pos++)
335+
mark_ce_for_checkout(active_cache[pos], ps_matched, opts);
331336

332337
if (report_path_error(ps_matched, &opts->pathspec, opts->prefix)) {
333338
free(ps_matched);

0 commit comments

Comments
 (0)