Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/libgit2.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'src/checkout.c')
-rw-r--r--src/checkout.c595
1 files changed, 368 insertions, 227 deletions
diff --git a/src/checkout.c b/src/checkout.c
index 6d7e3cfd4..20763fd35 100644
--- a/src/checkout.c
+++ b/src/checkout.c
@@ -47,7 +47,7 @@ enum {
typedef struct {
git_repository *repo;
git_diff *diff;
- git_checkout_opts opts;
+ git_checkout_options opts;
bool opts_free_baseline;
char *pfx;
git_index *index;
@@ -56,6 +56,7 @@ typedef struct {
git_vector conflicts;
git_buf path;
size_t workdir_len;
+ git_buf tmp;
unsigned int strategy;
int can_symlink;
bool reload_submodules;
@@ -70,7 +71,9 @@ typedef struct {
int name_collision:1,
directoryfile:1,
- one_to_two:1;
+ one_to_two:1,
+ binary:1,
+ submodule:1;
} checkout_conflictdata;
static int checkout_notify(
@@ -83,19 +86,17 @@ static int checkout_notify(
const git_diff_file *baseline = NULL, *target = NULL, *workdir = NULL;
const char *path = NULL;
- if (!data->opts.notify_cb)
- return 0;
-
- if ((why & data->opts.notify_flags) == 0)
+ if (!data->opts.notify_cb ||
+ (why & data->opts.notify_flags) == 0)
return 0;
if (wditem) {
memset(&wdfile, 0, sizeof(wdfile));
- git_oid_cpy(&wdfile.oid, &wditem->oid);
+ git_oid_cpy(&wdfile.id, &wditem->id);
wdfile.path = wditem->path;
wdfile.size = wditem->file_size;
- wdfile.flags = GIT_DIFF_FLAG_VALID_OID;
+ wdfile.flags = GIT_DIFF_FLAG_VALID_ID;
wdfile.mode = wditem->mode;
workdir = &wdfile;
@@ -125,8 +126,13 @@ static int checkout_notify(
path = delta->old_file.path;
}
- return data->opts.notify_cb(
- why, path, baseline, target, workdir, data->opts.notify_payload);
+ {
+ int error = data->opts.notify_cb(
+ why, path, baseline, target, workdir, data->opts.notify_payload);
+
+ return giterr_set_after_callback_function(
+ error, "git_checkout notification");
+ }
}
static bool checkout_is_workdir_modified(
@@ -142,19 +148,23 @@ static bool checkout_is_workdir_modified(
git_submodule *sm;
unsigned int sm_status = 0;
const git_oid *sm_oid = NULL;
+ bool rval = false;
- if (git_submodule_lookup(&sm, data->repo, wditem->path) < 0 ||
- git_submodule_status(&sm_status, sm) < 0)
- return true;
-
- if (GIT_SUBMODULE_STATUS_IS_WD_DIRTY(sm_status))
+ if (git_submodule_lookup(&sm, data->repo, wditem->path) < 0) {
+ giterr_clear();
return true;
+ }
- sm_oid = git_submodule_wd_id(sm);
- if (!sm_oid)
- return false;
+ if (git_submodule_status(&sm_status, sm) < 0 ||
+ GIT_SUBMODULE_STATUS_IS_WD_DIRTY(sm_status))
+ rval = true;
+ else if ((sm_oid = git_submodule_wd_id(sm)) == NULL)
+ rval = false;
+ else
+ rval = (git_oid__cmp(&baseitem->id, sm_oid) != 0);
- return (git_oid__cmp(&baseitem->oid, sm_oid) != 0);
+ git_submodule_free(sm);
+ return rval;
}
/* Look at the cache to decide if the workdir is modified. If not,
@@ -165,7 +175,7 @@ static bool checkout_is_workdir_modified(
if (wditem->mtime.seconds == ie->mtime.seconds &&
wditem->mtime.nanoseconds == ie->mtime.nanoseconds &&
wditem->file_size == ie->file_size)
- return (git_oid__cmp(&baseitem->oid, &ie->oid) != 0);
+ return (git_oid__cmp(&baseitem->id, &ie->id) != 0);
}
/* depending on where base is coming from, we may or may not know
@@ -174,113 +184,135 @@ static bool checkout_is_workdir_modified(
if (baseitem->size && wditem->file_size != baseitem->size)
return true;
- if (git_diff__oid_for_file(
- data->repo, wditem->path, wditem->mode,
- wditem->file_size, &oid) < 0)
+ if (git_diff__oid_for_entry(&oid, data->diff, wditem, NULL) < 0)
return false;
- return (git_oid__cmp(&baseitem->oid, &oid) != 0);
+ return (git_oid__cmp(&baseitem->id, &oid) != 0);
}
#define CHECKOUT_ACTION_IF(FLAG,YES,NO) \
((data->strategy & GIT_CHECKOUT_##FLAG) ? CHECKOUT_ACTION__##YES : CHECKOUT_ACTION__##NO)
static int checkout_action_common(
+ int *action,
checkout_data *data,
- int action,
const git_diff_delta *delta,
const git_index_entry *wd)
{
git_checkout_notify_t notify = GIT_CHECKOUT_NOTIFY_NONE;
- if (action <= 0)
- return action;
-
if ((data->strategy & GIT_CHECKOUT_UPDATE_ONLY) != 0)
- action = (action & ~CHECKOUT_ACTION__REMOVE);
+ *action = (*action & ~CHECKOUT_ACTION__REMOVE);
- if ((action & CHECKOUT_ACTION__UPDATE_BLOB) != 0) {
+ if ((*action & CHECKOUT_ACTION__UPDATE_BLOB) != 0) {
if (S_ISGITLINK(delta->new_file.mode))
- action = (action & ~CHECKOUT_ACTION__UPDATE_BLOB) |
+ *action = (*action & ~CHECKOUT_ACTION__UPDATE_BLOB) |
CHECKOUT_ACTION__UPDATE_SUBMODULE;
/* to "update" a symlink, we must remove the old one first */
if (delta->new_file.mode == GIT_FILEMODE_LINK && wd != NULL)
- action |= CHECKOUT_ACTION__REMOVE;
+ *action |= CHECKOUT_ACTION__REMOVE;
notify = GIT_CHECKOUT_NOTIFY_UPDATED;
}
- if ((action & CHECKOUT_ACTION__CONFLICT) != 0)
+ if ((*action & CHECKOUT_ACTION__CONFLICT) != 0)
notify = GIT_CHECKOUT_NOTIFY_CONFLICT;
- if (notify != GIT_CHECKOUT_NOTIFY_NONE &&
- checkout_notify(data, notify, delta, wd) != 0)
- return GIT_EUSER;
-
- return action;
+ return checkout_notify(data, notify, delta, wd);
}
static int checkout_action_no_wd(
+ int *action,
checkout_data *data,
const git_diff_delta *delta)
{
- int action = CHECKOUT_ACTION__NONE;
+ int error = 0;
+
+ *action = CHECKOUT_ACTION__NONE;
switch (delta->status) {
case GIT_DELTA_UNMODIFIED: /* case 12 */
- if (checkout_notify(data, GIT_CHECKOUT_NOTIFY_DIRTY, delta, NULL))
- return GIT_EUSER;
- action = CHECKOUT_ACTION_IF(SAFE_CREATE, UPDATE_BLOB, NONE);
+ error = checkout_notify(data, GIT_CHECKOUT_NOTIFY_DIRTY, delta, NULL);
+ if (error)
+ return error;
+ *action = CHECKOUT_ACTION_IF(SAFE_CREATE, UPDATE_BLOB, NONE);
break;
case GIT_DELTA_ADDED: /* case 2 or 28 (and 5 but not really) */
- action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
+ *action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
break;
case GIT_DELTA_MODIFIED: /* case 13 (and 35 but not really) */
- action = CHECKOUT_ACTION_IF(SAFE_CREATE, UPDATE_BLOB, CONFLICT);
+ *action = CHECKOUT_ACTION_IF(SAFE_CREATE, UPDATE_BLOB, CONFLICT);
break;
case GIT_DELTA_TYPECHANGE: /* case 21 (B->T) and 28 (T->B)*/
if (delta->new_file.mode == GIT_FILEMODE_TREE)
- action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
+ *action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
break;
case GIT_DELTA_DELETED: /* case 8 or 25 */
+ *action = CHECKOUT_ACTION_IF(SAFE, REMOVE, NONE);
+ break;
default: /* impossible */
break;
}
- return checkout_action_common(data, action, delta, NULL);
+ return checkout_action_common(action, data, delta, NULL);
}
+static bool wd_item_is_removable(git_iterator *iter, const git_index_entry *wd)
+{
+ git_buf *full = NULL;
+
+ if (wd->mode != GIT_FILEMODE_TREE)
+ return true;
+ if (git_iterator_current_workdir_path(&full, iter) < 0)
+ return true;
+ return !full || !git_path_contains(full, DOT_GIT);
+}
+
+static int checkout_queue_remove(checkout_data *data, const char *path)
+{
+ char *copy = git_pool_strdup(&data->pool, path);
+ GITERR_CHECK_ALLOC(copy);
+ return git_vector_insert(&data->removes, copy);
+}
+
+/* note that this advances the iterator over the wd item */
static int checkout_action_wd_only(
checkout_data *data,
git_iterator *workdir,
- const git_index_entry *wd,
+ const git_index_entry **wditem,
git_vector *pathspec)
{
+ int error = 0;
bool remove = false;
git_checkout_notify_t notify = GIT_CHECKOUT_NOTIFY_NONE;
+ const git_index_entry *wd = *wditem;
if (!git_pathspec__match(
pathspec, wd->path,
(data->strategy & GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH) != 0,
git_iterator_ignore_case(workdir), NULL, NULL))
- return 0;
+ return git_iterator_advance(wditem, workdir);
/* check if item is tracked in the index but not in the checkout diff */
if (data->index != NULL) {
- if (wd->mode != GIT_FILEMODE_TREE) {
- int error;
+ size_t pos;
+
+ error = git_index__find_pos(
+ &pos, data->index, wd->path, 0, GIT_INDEX_STAGE_ANY);
- if ((error = git_index_find(NULL, data->index, wd->path)) == 0) {
+ if (wd->mode != GIT_FILEMODE_TREE) {
+ if (!error) { /* found by git_index__find_pos call */
notify = GIT_CHECKOUT_NOTIFY_DIRTY;
remove = ((data->strategy & GIT_CHECKOUT_FORCE) != 0);
} else if (error != GIT_ENOTFOUND)
return error;
+ else
+ error = 0; /* git_index__find_pos does not set error msg */
} else {
/* for tree entries, we have to see if there are any index
* entries that are contained inside that tree
*/
- size_t pos = git_index__prefix_position(data->index, wd->path);
const git_index_entry *e = git_index_get_byindex(data->index, pos);
if (e != NULL && data->diff->pfxcomp(e->path, wd->path) == 0) {
@@ -290,29 +322,52 @@ static int checkout_action_wd_only(
}
}
- if (notify != GIT_CHECKOUT_NOTIFY_NONE)
- /* found in index */;
- else if (git_iterator_current_is_ignored(workdir)) {
- notify = GIT_CHECKOUT_NOTIFY_IGNORED;
- remove = ((data->strategy & GIT_CHECKOUT_REMOVE_IGNORED) != 0);
- }
- else {
- notify = GIT_CHECKOUT_NOTIFY_UNTRACKED;
- remove = ((data->strategy & GIT_CHECKOUT_REMOVE_UNTRACKED) != 0);
- }
+ if (notify != GIT_CHECKOUT_NOTIFY_NONE) {
+ /* if we found something in the index, notify and advance */
+ if ((error = checkout_notify(data, notify, NULL, wd)) != 0)
+ return error;
- if (checkout_notify(data, notify, NULL, wd))
- return GIT_EUSER;
+ if (remove && wd_item_is_removable(workdir, wd))
+ error = checkout_queue_remove(data, wd->path);
- if (remove) {
- char *path = git_pool_strdup(&data->pool, wd->path);
- GITERR_CHECK_ALLOC(path);
+ if (!error)
+ error = git_iterator_advance(wditem, workdir);
+ } else {
+ /* untracked or ignored - can't know which until we advance through */
+ bool over = false, removable = wd_item_is_removable(workdir, wd);
+ git_iterator_status_t untracked_state;
+
+ /* copy the entry for issuing notification callback later */
+ git_index_entry saved_wd = *wd;
+ git_buf_sets(&data->tmp, wd->path);
+ saved_wd.path = data->tmp.ptr;
+
+ error = git_iterator_advance_over_with_status(
+ wditem, &untracked_state, workdir);
+ if (error == GIT_ITEROVER)
+ over = true;
+ else if (error < 0)
+ return error;
- if (git_vector_insert(&data->removes, path) < 0)
- return -1;
+ if (untracked_state == GIT_ITERATOR_STATUS_IGNORED) {
+ notify = GIT_CHECKOUT_NOTIFY_IGNORED;
+ remove = ((data->strategy & GIT_CHECKOUT_REMOVE_IGNORED) != 0);
+ } else {
+ notify = GIT_CHECKOUT_NOTIFY_UNTRACKED;
+ remove = ((data->strategy & GIT_CHECKOUT_REMOVE_UNTRACKED) != 0);
+ }
+
+ if ((error = checkout_notify(data, notify, NULL, &saved_wd)) != 0)
+ return error;
+
+ if (remove && removable)
+ error = checkout_queue_remove(data, saved_wd.path);
+
+ if (!error && over) /* restore ITEROVER if needed */
+ error = GIT_ITEROVER;
}
- return 0;
+ return error;
}
static bool submodule_is_config_only(
@@ -321,45 +376,54 @@ static bool submodule_is_config_only(
{
git_submodule *sm = NULL;
unsigned int sm_loc = 0;
+ bool rval = false;
- if (git_submodule_lookup(&sm, data->repo, path) < 0 ||
- git_submodule_location(&sm_loc, sm) < 0 ||
- sm_loc == GIT_SUBMODULE_STATUS_IN_CONFIG)
+ if (git_submodule_lookup(&sm, data->repo, path) < 0)
return true;
- return false;
+ if (git_submodule_location(&sm_loc, sm) < 0 ||
+ sm_loc == GIT_SUBMODULE_STATUS_IN_CONFIG)
+ rval = true;
+
+ git_submodule_free(sm);
+
+ return rval;
}
static int checkout_action_with_wd(
+ int *action,
checkout_data *data,
const git_diff_delta *delta,
+ git_iterator *workdir,
const git_index_entry *wd)
{
- int action = CHECKOUT_ACTION__NONE;
+ *action = CHECKOUT_ACTION__NONE;
switch (delta->status) {
case GIT_DELTA_UNMODIFIED: /* case 14/15 or 33 */
if (checkout_is_workdir_modified(data, &delta->old_file, wd)) {
- if (checkout_notify(
- data, GIT_CHECKOUT_NOTIFY_DIRTY, delta, wd))
- return GIT_EUSER;
- action = CHECKOUT_ACTION_IF(FORCE, UPDATE_BLOB, NONE);
+ GITERR_CHECK_ERROR(
+ checkout_notify(data, GIT_CHECKOUT_NOTIFY_DIRTY, delta, wd) );
+ *action = CHECKOUT_ACTION_IF(FORCE, UPDATE_BLOB, NONE);
}
break;
case GIT_DELTA_ADDED: /* case 3, 4 or 6 */
- action = CHECKOUT_ACTION_IF(FORCE, UPDATE_BLOB, CONFLICT);
+ if (git_iterator_current_is_ignored(workdir))
+ *action = CHECKOUT_ACTION_IF(DONT_OVERWRITE_IGNORED, CONFLICT, UPDATE_BLOB);
+ else
+ *action = CHECKOUT_ACTION_IF(FORCE, UPDATE_BLOB, CONFLICT);
break;
case GIT_DELTA_DELETED: /* case 9 or 10 (or 26 but not really) */
if (checkout_is_workdir_modified(data, &delta->old_file, wd))
- action = CHECKOUT_ACTION_IF(FORCE, REMOVE, CONFLICT);
+ *action = CHECKOUT_ACTION_IF(FORCE, REMOVE, CONFLICT);
else
- action = CHECKOUT_ACTION_IF(SAFE, REMOVE, NONE);
+ *action = CHECKOUT_ACTION_IF(SAFE, REMOVE, NONE);
break;
case GIT_DELTA_MODIFIED: /* case 16, 17, 18 (or 36 but not really) */
if (checkout_is_workdir_modified(data, &delta->old_file, wd))
- action = CHECKOUT_ACTION_IF(FORCE, UPDATE_BLOB, CONFLICT);
+ *action = CHECKOUT_ACTION_IF(FORCE, UPDATE_BLOB, CONFLICT);
else
- action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
+ *action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
break;
case GIT_DELTA_TYPECHANGE: /* case 22, 23, 29, 30 */
if (delta->old_file.mode == GIT_FILEMODE_TREE) {
@@ -367,92 +431,96 @@ static int checkout_action_with_wd(
/* either deleting items in old tree will delete the wd dir,
* or we'll get a conflict when we attempt blob update...
*/
- action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
+ *action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
else if (wd->mode == GIT_FILEMODE_COMMIT) {
/* workdir is possibly a "phantom" submodule - treat as a
* tree if the only submodule info came from the config
*/
if (submodule_is_config_only(data, wd->path))
- action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
+ *action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
else
- action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
+ *action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
} else
- action = CHECKOUT_ACTION_IF(FORCE, REMOVE, CONFLICT);
+ *action = CHECKOUT_ACTION_IF(FORCE, REMOVE, CONFLICT);
}
else if (checkout_is_workdir_modified(data, &delta->old_file, wd))
- action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
+ *action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
else
- action = CHECKOUT_ACTION_IF(SAFE, REMOVE_AND_UPDATE, NONE);
+ *action = CHECKOUT_ACTION_IF(SAFE, REMOVE_AND_UPDATE, NONE);
/* don't update if the typechange is to a tree */
if (delta->new_file.mode == GIT_FILEMODE_TREE)
- action = (action & ~CHECKOUT_ACTION__UPDATE_BLOB);
+ *action = (*action & ~CHECKOUT_ACTION__UPDATE_BLOB);
break;
default: /* impossible */
break;
}
- return checkout_action_common(data, action, delta, wd);
+ return checkout_action_common(action, data, delta, wd);
}
static int checkout_action_with_wd_blocker(
+ int *action,
checkout_data *data,
const git_diff_delta *delta,
const git_index_entry *wd)
{
- int action = CHECKOUT_ACTION__NONE;
+ *action = CHECKOUT_ACTION__NONE;
switch (delta->status) {
case GIT_DELTA_UNMODIFIED:
/* should show delta as dirty / deleted */
- if (checkout_notify(data, GIT_CHECKOUT_NOTIFY_DIRTY, delta, wd))
- return GIT_EUSER;
- action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, NONE);
+ GITERR_CHECK_ERROR(
+ checkout_notify(data, GIT_CHECKOUT_NOTIFY_DIRTY, delta, wd) );
+ *action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, NONE);
break;
case GIT_DELTA_ADDED:
case GIT_DELTA_MODIFIED:
- action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
+ *action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
break;
case GIT_DELTA_DELETED:
- action = CHECKOUT_ACTION_IF(FORCE, REMOVE, CONFLICT);
+ *action = CHECKOUT_ACTION_IF(FORCE, REMOVE, CONFLICT);
break;
case GIT_DELTA_TYPECHANGE:
/* not 100% certain about this... */
- action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
+ *action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
break;
default: /* impossible */
break;
}
- return checkout_action_common(data, action, delta, wd);
+ return checkout_action_common(action, data, delta, wd);
}
static int checkout_action_with_wd_dir(
+ int *action,
checkout_data *data,
const git_diff_delta *delta,
+ git_iterator *workdir,
const git_index_entry *wd)
{
- int action = CHECKOUT_ACTION__NONE;
+ *action = CHECKOUT_ACTION__NONE;
switch (delta->status) {
case GIT_DELTA_UNMODIFIED: /* case 19 or 24 (or 34 but not really) */
- if (checkout_notify(data, GIT_CHECKOUT_NOTIFY_DIRTY, delta, NULL) ||
- checkout_notify(
- data, GIT_CHECKOUT_NOTIFY_UNTRACKED, NULL, wd))
- return GIT_EUSER;
+ GITERR_CHECK_ERROR(
+ checkout_notify(data, GIT_CHECKOUT_NOTIFY_DIRTY, delta, NULL));
+ GITERR_CHECK_ERROR(
+ checkout_notify(data, GIT_CHECKOUT_NOTIFY_UNTRACKED, NULL, wd));
break;
case GIT_DELTA_ADDED:/* case 4 (and 7 for dir) */
case GIT_DELTA_MODIFIED: /* case 20 (or 37 but not really) */
if (delta->old_file.mode == GIT_FILEMODE_COMMIT)
/* expected submodule (and maybe found one) */;
else if (delta->new_file.mode != GIT_FILEMODE_TREE)
- action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
+ *action = git_iterator_current_is_ignored(workdir) ?
+ CHECKOUT_ACTION_IF(DONT_OVERWRITE_IGNORED, CONFLICT, REMOVE_AND_UPDATE) :
+ CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
break;
case GIT_DELTA_DELETED: /* case 11 (and 27 for dir) */
- if (delta->old_file.mode != GIT_FILEMODE_TREE &&
- checkout_notify(
- data, GIT_CHECKOUT_NOTIFY_UNTRACKED, NULL, wd))
- return GIT_EUSER;
+ if (delta->old_file.mode != GIT_FILEMODE_TREE)
+ GITERR_CHECK_ERROR(
+ checkout_notify(data, GIT_CHECKOUT_NOTIFY_UNTRACKED, NULL, wd));
break;
case GIT_DELTA_TYPECHANGE: /* case 24 or 31 */
if (delta->old_file.mode == GIT_FILEMODE_TREE) {
@@ -462,39 +530,41 @@ static int checkout_action_with_wd_dir(
* directory if is it left empty, so we can defer removing the
* dir and it will succeed if no children are left.
*/
- action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
- if (action != CHECKOUT_ACTION__NONE)
- action |= CHECKOUT_ACTION__DEFER_REMOVE;
+ *action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
+ if (*action != CHECKOUT_ACTION__NONE)
+ *action |= CHECKOUT_ACTION__DEFER_REMOVE;
}
else if (delta->new_file.mode != GIT_FILEMODE_TREE)
/* For typechange to dir, dir is already created so no action */
- action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
+ *action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
break;
default: /* impossible */
break;
}
- return checkout_action_common(data, action, delta, wd);
+ return checkout_action_common(action, data, delta, wd);
}
static int checkout_action(
+ int *action,
checkout_data *data,
git_diff_delta *delta,
git_iterator *workdir,
- const git_index_entry **wditem_ptr,
+ const git_index_entry **wditem,
git_vector *pathspec)
{
- const git_index_entry *wd = *wditem_ptr;
- int cmp = -1, act;
+ int cmp = -1, error;
int (*strcomp)(const char *, const char *) = data->diff->strcomp;
int (*pfxcomp)(const char *str, const char *pfx) = data->diff->pfxcomp;
- int error;
+ int (*advance)(const git_index_entry **, git_iterator *) = NULL;
/* move workdir iterator to follow along with deltas */
while (1) {
+ const git_index_entry *wd = *wditem;
+
if (!wd)
- return checkout_action_no_wd(data, delta);
+ return checkout_action_no_wd(action, data, delta);
cmp = strcomp(wd->path, delta->old_file.path);
@@ -512,79 +582,74 @@ static int checkout_action(
if (cmp == 0) {
if (wd->mode == GIT_FILEMODE_TREE) {
/* case 2 - entry prefixed by workdir tree */
- error = git_iterator_advance_into_or_over(&wd, workdir);
- if (error && error != GIT_ITEROVER)
- goto fail;
- *wditem_ptr = wd;
+ error = git_iterator_advance_into_or_over(wditem, workdir);
+ if (error < 0 && error != GIT_ITEROVER)
+ goto done;
continue;
}
/* case 3 maybe - wd contains non-dir where dir expected */
if (delta->old_file.path[strlen(wd->path)] == '/') {
- act = checkout_action_with_wd_blocker(data, delta, wd);
- *wditem_ptr =
- git_iterator_advance(&wd, workdir) ? NULL : wd;
- return act;
+ error = checkout_action_with_wd_blocker(
+ action, data, delta, wd);
+ advance = git_iterator_advance;
+ goto done;
}
}
/* case 1 - handle wd item (if it matches pathspec) */
- if (checkout_action_wd_only(data, workdir, wd, pathspec) < 0)
- goto fail;
- if ((error = git_iterator_advance(&wd, workdir)) < 0 &&
- error != GIT_ITEROVER)
- goto fail;
-
- *wditem_ptr = wd;
+ error = checkout_action_wd_only(data, workdir, wditem, pathspec);
+ if (error && error != GIT_ITEROVER)
+ goto done;
continue;
}
if (cmp == 0) {
/* case 4 */
- act = checkout_action_with_wd(data, delta, wd);
- *wditem_ptr = git_iterator_advance(&wd, workdir) ? NULL : wd;
- return act;
+ error = checkout_action_with_wd(action, data, delta, workdir, wd);
+ advance = git_iterator_advance;
+ goto done;
}
cmp = pfxcomp(wd->path, delta->old_file.path);
if (cmp == 0) { /* case 5 */
if (wd->path[strlen(delta->old_file.path)] != '/')
- return checkout_action_no_wd(data, delta);
+ return checkout_action_no_wd(action, data, delta);
if (delta->status == GIT_DELTA_TYPECHANGE) {
if (delta->old_file.mode == GIT_FILEMODE_TREE) {
- act = checkout_action_with_wd(data, delta, wd);
- if ((error = git_iterator_advance_into(&wd, workdir)) < 0 &&
- error != GIT_ENOTFOUND)
- goto fail;
- *wditem_ptr = wd;
- return act;
+ error = checkout_action_with_wd(action, data, delta, workdir, wd);
+ advance = git_iterator_advance_into;
+ goto done;
}
if (delta->new_file.mode == GIT_FILEMODE_TREE ||
delta->new_file.mode == GIT_FILEMODE_COMMIT ||
delta->old_file.mode == GIT_FILEMODE_COMMIT)
{
- act = checkout_action_with_wd(data, delta, wd);
- if ((error = git_iterator_advance(&wd, workdir)) < 0 &&
- error != GIT_ITEROVER)
- goto fail;
- *wditem_ptr = wd;
- return act;
+ error = checkout_action_with_wd(action, data, delta, workdir, wd);
+ advance = git_iterator_advance;
+ goto done;
}
}
- return checkout_action_with_wd_dir(data, delta, wd);
+ return checkout_action_with_wd_dir(action, data, delta, workdir, wd);
}
/* case 6 - wd is after delta */
- return checkout_action_no_wd(data, delta);
+ return checkout_action_no_wd(action, data, delta);
}
-fail:
- *wditem_ptr = NULL;
- return -1;
+done:
+ if (!error && advance != NULL &&
+ (error = advance(wditem, workdir)) < 0) {
+ *wditem = NULL;
+ if (error == GIT_ITEROVER)
+ error = 0;
+ }
+
+ return error;
}
static int checkout_remaining_wd_items(
@@ -595,10 +660,8 @@ static int checkout_remaining_wd_items(
{
int error = 0;
- while (wd && !error) {
- if (!(error = checkout_action_wd_only(data, workdir, wd, spec)))
- error = git_iterator_advance(&wd, workdir);
- }
+ while (wd && !error)
+ error = checkout_action_wd_only(data, workdir, &wd, spec);
if (error == GIT_ITEROVER)
error = 0;
@@ -633,10 +696,13 @@ static int checkout_conflictdata_cmp(const void *a, const void *b)
return diff;
}
-int checkout_conflictdata_empty(const git_vector *conflicts, size_t idx)
+int checkout_conflictdata_empty(
+ const git_vector *conflicts, size_t idx, void *payload)
{
checkout_conflictdata *conflict;
+ GIT_UNUSED(payload);
+
if ((conflict = git_vector_get(conflicts, idx)) == NULL)
return -1;
@@ -674,6 +740,51 @@ GIT_INLINE(bool) conflict_pathspec_match(
return false;
}
+GIT_INLINE(int) checkout_conflict_detect_submodule(checkout_conflictdata *conflict)
+{
+ conflict->submodule = ((conflict->ancestor && S_ISGITLINK(conflict->ancestor->mode)) ||
+ (conflict->ours && S_ISGITLINK(conflict->ours->mode)) ||
+ (conflict->theirs && S_ISGITLINK(conflict->theirs->mode)));
+ return 0;
+}
+
+GIT_INLINE(int) checkout_conflict_detect_binary(git_repository *repo, checkout_conflictdata *conflict)
+{
+ git_blob *ancestor_blob = NULL, *our_blob = NULL, *their_blob = NULL;
+ int error = 0;
+
+ if (conflict->submodule)
+ return 0;
+
+ if (conflict->ancestor) {
+ if ((error = git_blob_lookup(&ancestor_blob, repo, &conflict->ancestor->id)) < 0)
+ goto done;
+
+ conflict->binary = git_blob_is_binary(ancestor_blob);
+ }
+
+ if (!conflict->binary && conflict->ours) {
+ if ((error = git_blob_lookup(&our_blob, repo, &conflict->ours->id)) < 0)
+ goto done;
+
+ conflict->binary = git_blob_is_binary(our_blob);
+ }
+
+ if (!conflict->binary && conflict->theirs) {
+ if ((error = git_blob_lookup(&their_blob, repo, &conflict->theirs->id)) < 0)
+ goto done;
+
+ conflict->binary = git_blob_is_binary(their_blob);
+ }
+
+done:
+ git_blob_free(ancestor_blob);
+ git_blob_free(our_blob);
+ git_blob_free(their_blob);
+
+ return error;
+}
+
static int checkout_conflicts_load(checkout_data *data, git_iterator *workdir, git_vector *pathspec)
{
git_index_conflict_iterator *iterator = NULL;
@@ -698,6 +809,13 @@ static int checkout_conflicts_load(checkout_data *data, git_iterator *workdir, g
conflict->ours = ours;
conflict->theirs = theirs;
+ if ((error = checkout_conflict_detect_submodule(conflict)) < 0 ||
+ (error = checkout_conflict_detect_binary(data->repo, conflict)) < 0)
+ {
+ git__free(conflict);
+ goto done;
+ }
+
git_vector_insert(&data->conflicts, conflict);
}
@@ -846,7 +964,7 @@ static int checkout_conflicts_coalesce_renames(
/* Juggle entries based on renames */
names = git_index_name_entrycount(data->index);
-
+
for (i = 0; i < names; i++) {
name_entry = git_index_name_get_byindex(data->index, i);
@@ -882,7 +1000,8 @@ static int checkout_conflicts_coalesce_renames(
ancestor_conflict->one_to_two = 1;
}
- git_vector_remove_matching(&data->conflicts, checkout_conflictdata_empty);
+ git_vector_remove_matching(
+ &data->conflicts, checkout_conflictdata_empty, NULL);
done:
return error;
@@ -965,7 +1084,7 @@ static int checkout_get_actions(
checkout_data *data,
git_iterator *workdir)
{
- int error = 0;
+ int error = 0, act;
const git_index_entry *wditem;
git_vector pathspec = GIT_VECTOR_INIT, *deltas;
git_pool pathpool = GIT_POOL_INIT_STRINGPOOL;
@@ -992,12 +1111,9 @@ static int checkout_get_actions(
}
git_vector_foreach(deltas, i, delta) {
- int act = checkout_action(data, delta, workdir, &wditem, &pathspec);
-
- if (act < 0) {
- error = act;
+ error = checkout_action(&act, data, delta, workdir, &wditem, &pathspec);
+ if (error != 0)
goto fail;
- }
actions[i] = act;
@@ -1012,7 +1128,7 @@ static int checkout_get_actions(
}
error = checkout_remaining_wd_items(data, workdir, wditem, &pathspec);
- if (error < 0)
+ if (error)
goto fail;
counts[CHECKOUT_ACTION__REMOVE] += data->removes.length;
@@ -1020,8 +1136,10 @@ static int checkout_get_actions(
if (counts[CHECKOUT_ACTION__CONFLICT] > 0 &&
(data->strategy & GIT_CHECKOUT_ALLOW_CONFLICTS) == 0)
{
- giterr_set(GITERR_CHECKOUT, "%d conflicts prevent checkout",
- (int)counts[CHECKOUT_ACTION__CONFLICT]);
+ giterr_set(GITERR_CHECKOUT, "%d %s checkout",
+ (int)counts[CHECKOUT_ACTION__CONFLICT],
+ counts[CHECKOUT_ACTION__CONFLICT] == 1 ?
+ "conflict prevents" : "conflicts prevent");
error = GIT_EMERGECONFLICT;
goto fail;
}
@@ -1082,7 +1200,7 @@ static int blob_content_to_file(
const char *path,
const char * hint_path,
mode_t entry_filemode,
- git_checkout_opts *opts)
+ git_checkout_options *opts)
{
int error = 0;
mode_t file_mode = opts->file_mode ? opts->file_mode : entry_filemode;
@@ -1094,7 +1212,8 @@ static int blob_content_to_file(
if (!opts->disable_filters)
error = git_filter_list_load(
- &fl, git_blob_owner(blob), blob, hint_path, GIT_FILTER_TO_WORKTREE);
+ &fl, git_blob_owner(blob), blob, hint_path,
+ GIT_FILTER_TO_WORKTREE, GIT_FILTER_OPT_DEFAULT);
if (!error)
error = git_filter_list_apply_to_blob(&out, fl, blob);
@@ -1160,9 +1279,8 @@ static int checkout_update_index(
memset(&entry, 0, sizeof(entry));
entry.path = (char *)file->path; /* cast to prevent warning */
- git_index_entry__init_from_stat(
- &entry, st, !(git_index_caps(data->index) & GIT_INDEXCAP_NO_FILEMODE));
- git_oid_cpy(&entry.oid, &file->oid);
+ git_index_entry__init_from_stat(&entry, st, true);
+ git_oid_cpy(&entry.id, &file->id);
return git_index_add(data->index, &entry);
}
@@ -1197,7 +1315,6 @@ static int checkout_submodule(
const git_diff_file *file)
{
int error = 0;
- git_submodule *sm;
/* Until submodules are supported, UPDATE_ONLY means do nothing here */
if ((data->strategy & GIT_CHECKOUT_UPDATE_ONLY) != 0)
@@ -1208,7 +1325,7 @@ static int checkout_submodule(
data->opts.dir_mode, GIT_MKDIR_PATH)) < 0)
return error;
- if ((error = git_submodule_lookup(&sm, data->repo, file->path)) < 0) {
+ if ((error = git_submodule_lookup(NULL, data->repo, file->path)) < 0) {
/* I've observed repos with submodules in the tree that do not
* have a .gitmodules - core Git just makes an empty directory
*/
@@ -1319,7 +1436,7 @@ static int checkout_blob(
}
error = checkout_write_content(
- data, &file->oid, git_buf_cstr(&data->path), NULL, file->mode, &st);
+ data, &file->id, git_buf_cstr(&data->path), NULL, file->mode, &st);
/* update the index unless prevented */
if (!error && (data->strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) == 0)
@@ -1449,7 +1566,7 @@ static int checkout_create_submodules(
/* initial reload of submodules if .gitmodules was changed */
if (data->reload_submodules &&
- (error = git_submodule_reload_all(data->repo)) < 0)
+ (error = git_submodule_reload_all(data->repo, 1)) < 0)
return error;
git_vector_foreach(&data->diff->deltas, i, delta) {
@@ -1473,7 +1590,7 @@ static int checkout_create_submodules(
}
/* final reload once submodules have been updated */
- return git_submodule_reload_all(data->repo);
+ return git_submodule_reload_all(data->repo, 1);
}
static int checkout_lookup_head_tree(git_tree **out, git_repository *repo)
@@ -1572,7 +1689,7 @@ static int checkout_write_entry(
return error;
return checkout_write_content(data,
- &side->oid, git_buf_cstr(&data->path), hint_path, side->mode, &st);
+ &side->id, git_buf_cstr(&data->path), hint_path, side->mode, &st);
}
static int checkout_write_entries(
@@ -1620,25 +1737,20 @@ static int checkout_write_merge(
{
git_buf our_label = GIT_BUF_INIT, their_label = GIT_BUF_INIT,
path_suffixed = GIT_BUF_INIT, path_workdir = GIT_BUF_INIT;
- git_merge_file_input ancestor = GIT_MERGE_FILE_INPUT_INIT,
- ours = GIT_MERGE_FILE_INPUT_INIT,
- theirs = GIT_MERGE_FILE_INPUT_INIT;
- git_merge_file_result result = GIT_MERGE_FILE_RESULT_INIT;
+ git_merge_file_options opts = GIT_MERGE_FILE_OPTIONS_INIT;
+ git_merge_file_result result = {0};
git_filebuf output = GIT_FILEBUF_INIT;
int error = 0;
- if ((conflict->ancestor &&
- (error = git_merge_file_input_from_index_entry(
- &ancestor, data->repo, conflict->ancestor)) < 0) ||
- (error = git_merge_file_input_from_index_entry(
- &ours, data->repo, conflict->ours)) < 0 ||
- (error = git_merge_file_input_from_index_entry(
- &theirs, data->repo, conflict->theirs)) < 0)
- goto done;
+ if (data->opts.checkout_strategy & GIT_CHECKOUT_CONFLICT_STYLE_DIFF3)
+ opts.flags |= GIT_MERGE_FILE_STYLE_DIFF3;
- ancestor.label = NULL;
- ours.label = data->opts.our_label ? data->opts.our_label : "ours";
- theirs.label = data->opts.their_label ? data->opts.their_label : "theirs";
+ opts.ancestor_label = data->opts.ancestor_label ?
+ data->opts.ancestor_label : "ancestor";
+ opts.our_label = data->opts.our_label ?
+ data->opts.our_label : "ours";
+ opts.their_label = data->opts.their_label ?
+ data->opts.their_label : "theirs";
/* If all the paths are identical, decorate the diff3 file with the branch
* names. Otherwise, append branch_name:path.
@@ -1647,16 +1759,17 @@ static int checkout_write_merge(
strcmp(conflict->ours->path, conflict->theirs->path) != 0) {
if ((error = conflict_entry_name(
- &our_label, ours.label, conflict->ours->path)) < 0 ||
+ &our_label, opts.our_label, conflict->ours->path)) < 0 ||
(error = conflict_entry_name(
- &their_label, theirs.label, conflict->theirs->path)) < 0)
+ &their_label, opts.their_label, conflict->theirs->path)) < 0)
goto done;
- ours.label = git_buf_cstr(&our_label);
- theirs.label = git_buf_cstr(&their_label);
+ opts.our_label = git_buf_cstr(&our_label);
+ opts.their_label = git_buf_cstr(&their_label);
}
- if ((error = git_merge_files(&result, &ancestor, &ours, &theirs, 0)) < 0)
+ if ((error = git_merge_file_from_index(&result, data->repo,
+ conflict->ancestor, conflict->ours, conflict->theirs, &opts)) < 0)
goto done;
if (result.path == NULL || result.mode == 0) {
@@ -1674,7 +1787,7 @@ static int checkout_write_merge(
if ((error = git_futils_mkpath2file(path_workdir.ptr, 0755)) < 0 ||
(error = git_filebuf_open(&output, path_workdir.ptr, GIT_FILEBUF_DO_NOT_BUFFER, result.mode)) < 0 ||
- (error = git_filebuf_write(&output, result.data, result.len)) < 0 ||
+ (error = git_filebuf_write(&output, result.ptr, result.len)) < 0 ||
(error = git_filebuf_commit(&output)) < 0)
goto done;
@@ -1682,9 +1795,6 @@ done:
git_buf_free(&our_label);
git_buf_free(&their_label);
- git_merge_file_input_free(&ancestor);
- git_merge_file_input_free(&ours);
- git_merge_file_input_free(&theirs);
git_merge_file_result_free(&result);
git_buf_free(&path_workdir);
git_buf_free(&path_suffixed);
@@ -1699,6 +1809,7 @@ static int checkout_create_conflicts(checkout_data *data)
int error = 0;
git_vector_foreach(&data->conflicts, i, conflict) {
+
/* Both deleted: nothing to do */
if (conflict->ours == NULL && conflict->theirs == NULL)
error = 0;
@@ -1742,7 +1853,15 @@ static int checkout_create_conflicts(checkout_data *data)
else if (S_ISLNK(conflict->theirs->mode))
error = checkout_write_entry(data, conflict, conflict->ours);
- else
+ /* If any side is a gitlink, do nothing. */
+ else if (conflict->submodule)
+ error = 0;
+
+ /* If any side is binary, write the ours side */
+ else if (conflict->binary)
+ error = checkout_write_entry(data, conflict, conflict->ours);
+
+ else if (!error)
error = checkout_write_merge(data, conflict);
if (error)
@@ -1760,9 +1879,6 @@ static int checkout_create_conflicts(checkout_data *data)
static void checkout_data_clear(checkout_data *data)
{
- checkout_conflictdata *conflict;
- size_t i;
-
if (data->opts_free_baseline) {
git_tree_free(data->opts.baseline);
data->opts.baseline = NULL;
@@ -1771,15 +1887,13 @@ static void checkout_data_clear(checkout_data *data)
git_vector_free(&data->removes);
git_pool_clear(&data->pool);
- git_vector_foreach(&data->conflicts, i, conflict)
- git__free(conflict);
-
- git_vector_free(&data->conflicts);
+ git_vector_free_deep(&data->conflicts);
git__free(data->pfx);
data->pfx = NULL;
git_buf_free(&data->path);
+ git_buf_free(&data->tmp);
git_index_free(data->index);
data->index = NULL;
@@ -1788,7 +1902,7 @@ static void checkout_data_clear(checkout_data *data)
static int checkout_data_init(
checkout_data *data,
git_iterator *target,
- const git_checkout_opts *proposed)
+ const git_checkout_options *proposed)
{
int error = 0;
git_repository *repo = git_iterator_owner(target);
@@ -1807,12 +1921,12 @@ static int checkout_data_init(
data->repo = repo;
GITERR_CHECK_VERSION(
- proposed, GIT_CHECKOUT_OPTS_VERSION, "git_checkout_opts");
+ proposed, GIT_CHECKOUT_OPTIONS_VERSION, "git_checkout_options");
if (!proposed)
- GIT_INIT_STRUCTURE(&data->opts, GIT_CHECKOUT_OPTS_VERSION);
+ GIT_INIT_STRUCTURE(&data->opts, GIT_CHECKOUT_OPTIONS_VERSION);
else
- memmove(&data->opts, proposed, sizeof(git_checkout_opts));
+ memmove(&data->opts, proposed, sizeof(git_checkout_options));
if (!data->opts.target_directory)
data->opts.target_directory = git_repository_workdir(repo);
@@ -1891,6 +2005,29 @@ static int checkout_data_init(
goto cleanup;
}
+ if ((data->opts.checkout_strategy &
+ (GIT_CHECKOUT_CONFLICT_STYLE_MERGE | GIT_CHECKOUT_CONFLICT_STYLE_DIFF3)) == 0) {
+ const char *conflict_style;
+ git_config *cfg = NULL;
+
+ if ((error = git_repository_config__weakptr(&cfg, repo)) < 0 ||
+ (error = git_config_get_string(&conflict_style, cfg, "merge.conflictstyle")) < 0 ||
+ error == GIT_ENOTFOUND)
+ ;
+ else if (error)
+ goto cleanup;
+ else if (strcmp(conflict_style, "merge") == 0)
+ data->opts.checkout_strategy |= GIT_CHECKOUT_CONFLICT_STYLE_MERGE;
+ else if (strcmp(conflict_style, "diff3") == 0)
+ data->opts.checkout_strategy |= GIT_CHECKOUT_CONFLICT_STYLE_DIFF3;
+ else {
+ giterr_set(GITERR_CHECKOUT, "unknown style '%s' given for 'merge.conflictstyle'",
+ conflict_style);
+ error = -1;
+ goto cleanup;
+ }
+ }
+
if ((error = git_vector_init(&data->removes, 0, git__strcmp_cb)) < 0 ||
(error = git_vector_init(&data->conflicts, 0, NULL)) < 0 ||
(error = git_pool_init(&data->pool, 1, 0)) < 0 ||
@@ -1909,7 +2046,7 @@ cleanup:
int git_checkout_iterator(
git_iterator *target,
- const git_checkout_opts *opts)
+ const git_checkout_options *opts)
{
int error = 0;
git_iterator *baseline = NULL, *workdir = NULL;
@@ -1966,7 +2103,7 @@ int git_checkout_iterator(
* actions to be taken, plus look for conflicts and send notifications,
* then loop through conflicts.
*/
- if ((error = checkout_get_actions(&actions, &counts, &data, workdir)) < 0)
+ if ((error = checkout_get_actions(&actions, &counts, &data, workdir)) != 0)
goto cleanup;
data.total_steps = counts[CHECKOUT_ACTION__REMOVE] +
@@ -1998,9 +2135,6 @@ int git_checkout_iterator(
assert(data.completed_steps == data.total_steps);
cleanup:
- if (error == GIT_EUSER)
- giterr_clear();
-
if (!error && data.index != NULL &&
(data.strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) == 0)
error = git_index_write(data.index);
@@ -2018,7 +2152,7 @@ cleanup:
int git_checkout_index(
git_repository *repo,
git_index *index,
- const git_checkout_opts *opts)
+ const git_checkout_options *opts)
{
int error;
git_iterator *index_i;
@@ -2053,7 +2187,7 @@ int git_checkout_index(
int git_checkout_tree(
git_repository *repo,
const git_object *treeish,
- const git_checkout_opts *opts)
+ const git_checkout_options *opts)
{
int error;
git_tree *tree = NULL;
@@ -2101,8 +2235,15 @@ int git_checkout_tree(
int git_checkout_head(
git_repository *repo,
- const git_checkout_opts *opts)
+ const git_checkout_options *opts)
{
assert(repo);
return git_checkout_tree(repo, NULL, opts);
}
+
+int git_checkout_init_options(git_checkout_options *opts, unsigned int version)
+{
+ GIT_INIT_STRUCTURE_FROM_TEMPLATE(
+ opts, version, git_checkout_options, GIT_CHECKOUT_OPTIONS_INIT);
+ return 0;
+}