#include "cache.h"
#include "builtin.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.h"
#include "argv-array.h"
static const char * const checkout_usage[] = {
"git checkout [options] <branch>",
"git checkout [options] [<branch>] -- <file>...",
NULL,
};
struct checkout_opts {
int quiet;
int merge;
int force;
int force_detach;
int writeout_stage;
int writeout_error;
int overwrite_ignore;
/* not set by parse_options */
int branch_exists;
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;
};
static int post_checkout_hook(struct commit *old, struct commit *new,
int changed)
{
return run_hook(NULL, "post-checkout",
sha1_to_hex(old ? old->object.sha1 : null_sha1),
sha1_to_hex(new ? new->object.sha1 : 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, const char *base, int baselen,
const char *pathname, unsigned mode, int stage, void *context)
{
int len;
struct cache_entry *ce;
if (S_ISDIR(mode))
return READ_TREE_RECURSIVE;
len = baselen + strlen(pathname);
ce = xcalloc(1, cache_entry_size(len));
hashcpy(ce->sha1, sha1);
memcpy(ce->name, base, baselen);
memcpy(ce->name + baselen, pathname, len - baselen);
ce->ce_flags = create_ce_flags(len, 0) | CE_UPDATE;
ce->ce_mode = create_ce_mode(mode);
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 char **pathspec)
{
struct pathspec ps;
init_pathspec(&ps, pathspec);
read_tree_recursive(tree, "", 0, 0, &ps, update_some, NULL);
free_pathspec(&ps);
/* 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(struct cache_entry *ce, int pos)
{
while (++pos < active_nr &&
!strcmp(active_cache[pos]->name, ce->name))
; /* skip */
return pos;
}
static int check_stage(int stage, struct cache_entry *ce, int pos)
{
while (pos < active_nr &&
!strcmp(active_cache[pos]->name, ce->name)) {
if (ce_stage(active_cache[pos]) == stage)
return 0;
pos++;
}
if (stage == 2)
return error(_("path '%s' does not have our version"), ce->name);
else
return error(_("path '%s' does not have their version"), ce->name);
}
static int check_stages(unsigned stages, struct cache_entry *ce, int pos)
{
unsigned seen = 0;
const char *name = ce->name;
while (pos < active_nr) {
ce = active_cache[pos];
if (strcmp(name, ce->name))
break;
seen |= (1 << ce_stage(ce));
pos++;
}
if ((stages & seen) != stages)
return error(_("path '%s' does not have all necessary versions"),
name);
return 0;
}
static int checkout_stage(int stage, struct cache_entry *ce, int pos,
struct checkout *state)
{
while (pos < active_nr &&
!strcmp(active_cache[pos]->name, ce->name)) {
if (ce_stage(active_cache[pos]) == stage)
return checkout_entry(active_cache[pos], state, NULL);
pos++;
}
if (stage == 2)
return error(_("path '%s' does not have our version"), ce->name);
else
return error(_("path '%s' does not have their version"), ce->name);
}
static int checkout_merged(int pos, struct checkout *state)
{
struct cache_entry *ce = active_cache[pos];
const char *path = ce->name;
mmfile_t ancestor, ours, theirs;
int status;
unsigned char sha1[20];
mmbuffer_t result_buf;
unsigned char threeway[3][
|