#include "builtin.h"
#include "lockfile.h"
#include "parse-options.h"
#include "refs.h"
#include "commit.h"
#include "tree.h"
#include "tree-walk.h"
#include "cache-tree.h"
#include "unpack-trees.h"
#include "dir.h"
#include "run-command.h"
#include "merge-recursive.h"
#include "branch.h"
#include "diff.h"
#include "revision.h"
#include "remote.h"
#include "blob.h"
#include "xdiff-interface.h"
#include "ll-merge.h"
#include "resolve-undo.h"
#include "submodule-config.h"
#include "submodule.h"
static const char * const checkout_usage[] = {
N_("git checkout [<options>] <branch>"),
N_("git checkout [<options>] [<branch>] -- <file>..."),
NULL,
};
struct checkout_opts {
int patch_mode;
int quiet;
int merge;
int force;
int force_detach;
int writeout_stage;
int overwrite_ignore;
int ignore_skipworktree;
int ignore_other_worktrees;
int show_progress;
const char *new_branch;
const char *new_branch_force;
const char *new_orphan_branch;
int new_branch_log;
enum branch_track track;
struct diff_options diff_options;
int branch_exists;
const char *prefix;
struct pathspec pathspec;
struct tree *source_tree;
};
static int post_checkout_hook(struct commit *old, struct commit *new,
int changed)
{
return run_hook_le(NULL, "post-checkout",
sha1_to_hex(old ? old->object.oid.hash : null_sha1),
sha1_to_hex(new ? new->object.oid.hash : null_sha1),
changed ? "1" : "0", NULL);
/* "new" can be NULL when checking out from the index before
a commit exists. */
}
static int update_some(const unsigned char *sha1, struct strbuf *base,
const char *pathname, unsigned mode, int stage, void *context)
{
int len;
struct cache_entry *ce;
int pos;
if (S_ISDIR(mode))
return READ_TREE_RECURSIVE;
len = base->len + strlen(pathname);
ce = xcalloc(1, cache_entry_size(len));
hashcpy(ce->sha1, sha1);
memcpy(ce->name, base->buf, base->len);
memcpy(ce->name + base->len, pathname, len - base->len);
ce->ce_flags = create_ce_flags(0) | CE_UPDATE;
ce->ce_namelen = len;
ce->ce_mode = create_ce_mode(mode);
/*
* If the entry is the same as the current index, we can leave the old
* entry in place. Whether it is UPTODATE or not, checkout_entry will
* do the right thing.
*/
pos = cache_name_pos(ce->name, ce->ce_namelen);
if (pos >= 0) {
struct cache_entry *old = active_cache[pos];
if (ce->ce_mode == old->ce_mode &&
!hashcmp(ce->sha1, old->sha1)) {
old->ce_flags |= CE_UPDATE;
free(ce);
return 0;
}
}
add_cache_entry(ce, ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
return 0;
}
static int read_tree_some(struct tree *tree, const struct pathspec *pathspec)
{
read_tree_recursive(tree, "", 0, 0, pathspec, update_some, NULL);
/* update the index with the given tree's info
* for all args, expanding wildcards, and exit
* with any non-zero return code.
*/
return 0;
}
static int skip_same_name(const struct cache_entry *ce, int pos)
{
while (+
|