diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/attr.c | 4 | ||||
| -rw-r--r-- | src/checkout.c | 1458 | ||||
| -rw-r--r-- | src/checkout.h | 24 | ||||
| -rw-r--r-- | src/clone.c | 2 | ||||
| -rw-r--r-- | src/diff.c | 46 | ||||
| -rw-r--r-- | src/fileops.c | 18 | ||||
| -rw-r--r-- | src/index.c | 41 | ||||
| -rw-r--r-- | src/iterator.c | 28 | ||||
| -rw-r--r-- | src/iterator.h | 5 | ||||
| -rw-r--r-- | src/oid.c | 5 | ||||
| -rw-r--r-- | src/reset.c | 69 | ||||
| -rw-r--r-- | src/stash.c | 221 | ||||
| -rw-r--r-- | src/submodule.c | 27 |
13 files changed, 1336 insertions, 612 deletions
diff --git a/src/attr.c b/src/attr.c index 95d63bea8..1b414417e 100644 --- a/src/attr.c +++ b/src/attr.c @@ -572,8 +572,10 @@ static int collect_attr_files( error = git_futils_find_system_file(&dir, GIT_ATTR_FILE_SYSTEM); if (!error) error = push_attr_file(repo, files, NULL, dir.ptr); - else if (error == GIT_ENOTFOUND) + else if (error == GIT_ENOTFOUND) { + giterr_clear(); error = 0; + } } cleanup: diff --git a/src/checkout.c b/src/checkout.c index 66eb698ab..cf0a8b8e7 100644 --- a/src/checkout.c +++ b/src/checkout.c @@ -7,36 +7,636 @@ #include <assert.h> -#include "git2/checkout.h" +#include "checkout.h" + #include "git2/repository.h" #include "git2/refs.h" #include "git2/tree.h" #include "git2/blob.h" #include "git2/config.h" #include "git2/diff.h" +#include "git2/submodule.h" -#include "common.h" #include "refs.h" -#include "buffer.h" #include "repository.h" #include "filter.h" #include "blob.h" #include "diff.h" #include "pathspec.h" +/* See docs/checkout-internals.md for more information */ + +enum { + CHECKOUT_ACTION__NONE = 0, + CHECKOUT_ACTION__REMOVE = 1, + CHECKOUT_ACTION__UPDATE_BLOB = 2, + CHECKOUT_ACTION__UPDATE_SUBMODULE = 4, + CHECKOUT_ACTION__CONFLICT = 8, + CHECKOUT_ACTION__MAX = 8, + CHECKOUT_ACTION__DEFER_REMOVE = 16, + CHECKOUT_ACTION__REMOVE_AND_UPDATE = + (CHECKOUT_ACTION__UPDATE_BLOB | CHECKOUT_ACTION__REMOVE), +}; + typedef struct { git_repository *repo; git_diff_list *diff; - git_checkout_opts *opts; - git_buf *path; + git_checkout_opts opts; + bool opts_free_baseline; + char *pfx; + git_index *index; + git_pool pool; + git_vector removes; + git_buf path; size_t workdir_len; - bool can_symlink; - int error; + unsigned int strategy; + int can_symlink; + bool reload_submodules; size_t total_steps; size_t completed_steps; -} checkout_diff_data; +} checkout_data; + +static int checkout_notify( + checkout_data *data, + git_checkout_notify_t why, + const git_diff_delta *delta, + const git_index_entry *wditem) +{ + git_diff_file wdfile; + const git_diff_file *baseline = NULL, *target = NULL, *workdir = NULL; + + if (!data->opts.notify_cb) + return 0; + + if ((why & data->opts.notify_flags) == 0) + return 0; + + if (wditem) { + memset(&wdfile, 0, sizeof(wdfile)); + + git_oid_cpy(&wdfile.oid, &wditem->oid); + wdfile.path = wditem->path; + wdfile.size = wditem->file_size; + wdfile.flags = GIT_DIFF_FILE_VALID_OID; + wdfile.mode = wditem->mode; + + workdir = &wdfile; + } + + if (delta) { + switch (delta->status) { + case GIT_DELTA_UNMODIFIED: + case GIT_DELTA_MODIFIED: + case GIT_DELTA_TYPECHANGE: + default: + baseline = &delta->old_file; + target = &delta->new_file; + break; + case GIT_DELTA_ADDED: + case GIT_DELTA_IGNORED: + case GIT_DELTA_UNTRACKED: + target = &delta->new_file; + break; + case GIT_DELTA_DELETED: + baseline = &delta->old_file; + break; + } + } + + return data->opts.notify_cb( + why, delta ? delta->old_file.path : wditem->path, + baseline, target, workdir, data->opts.notify_payload); +} + +static bool checkout_is_workdir_modified( + checkout_data *data, + const git_diff_file *baseitem, + const git_index_entry *wditem) +{ + git_oid oid; + + /* handle "modified" submodule */ + if (wditem->mode == GIT_FILEMODE_COMMIT) { + git_submodule *sm; + unsigned int sm_status = 0; + const git_oid *sm_oid = NULL; + + 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)) + return true; + + sm_oid = git_submodule_wd_id(sm); + if (!sm_oid) + return false; + + return (git_oid_cmp(&baseitem->oid, sm_oid) != 0); + } + + /* depending on where base is coming from, we may or may not know + * the actual size of the data, so we can't rely on this shortcut. + */ + 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) + return false; + + return (git_oid_cmp(&baseitem->oid, &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( + 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); + + if ((action & CHECKOUT_ACTION__UPDATE_BLOB) != 0) { + if (S_ISGITLINK(delta->new_file.mode)) + action = (action & ~CHECKOUT_ACTION__UPDATE_BLOB) | + CHECKOUT_ACTION__UPDATE_SUBMODULE; + + notify = GIT_CHECKOUT_NOTIFY_UPDATED; + } + + 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; +} + +static int checkout_action_no_wd( + checkout_data *data, + const git_diff_delta *delta) +{ + int 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); + break; + case GIT_DELTA_ADDED: /* case 2 or 28 (and 5 but not really) */ + case GIT_DELTA_MODIFIED: /* case 13 (and 35 but not really) */ + action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE); + 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); + break; + case GIT_DELTA_DELETED: /* case 8 or 25 */ + default: /* impossible */ + break; + } + + return checkout_action_common(data, action, delta, NULL); +} + +static int checkout_action_wd_only( + checkout_data *data, + git_iterator *workdir, + const git_index_entry *wd, + git_vector *pathspec) +{ + bool remove = false; + git_checkout_notify_t notify = GIT_CHECKOUT_NOTIFY_NONE; + + if (!git_pathspec_match_path( + pathspec, wd->path, false, workdir->ignore_case)) + return 0; + + /* check if item is tracked in the index but not in the checkout diff */ + if (data->index != NULL) { + if (wd->mode != GIT_FILEMODE_TREE) { + if (git_index_get_bypath(data->index, wd->path, 0) != NULL) { + notify = GIT_CHECKOUT_NOTIFY_DIRTY; + remove = ((data->strategy & GIT_CHECKOUT_FORCE) != 0); + } + } 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) { + notify = GIT_CHECKOUT_NOTIFY_DIRTY; + remove = ((data->strategy & GIT_CHECKOUT_FORCE) != 0); + } + } + } + + 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 (checkout_notify(data, notify, NULL, wd)) + return GIT_EUSER; + + if (remove) { + char *path = git_pool_strdup(&data->pool, wd->path); + GITERR_CHECK_ALLOC(path); + + if (git_vector_insert(&data->removes, path) < 0) + return -1; + } + + return 0; +} + +static bool submodule_is_config_only( + checkout_data *data, + const char *path) +{ + git_submodule *sm = NULL; + unsigned int sm_loc = 0; + + if (git_submodule_lookup(&sm, data->repo, path) < 0 || + git_submodule_location(&sm_loc, sm) < 0 || + sm_loc == GIT_SUBMODULE_STATUS_IN_CONFIG) + return true; + + return false; +} + +static int checkout_action_with_wd( + checkout_data *data, + const git_diff_delta *delta, + const git_index_entry *wd) +{ + int 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); + } + break; + case GIT_DELTA_ADDED: /* case 3, 4 or 6 */ + 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); + else + 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); + else + 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) { + if (wd->mode == GIT_FILEMODE_TREE) + /* 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); + 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); + else + action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT); + } else + 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); + else + 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); + break; + default: /* impossible */ + break; + } + + return checkout_action_common(data, action, delta, wd); +} + +static int checkout_action_with_wd_blocker( + checkout_data *data, + const git_diff_delta *delta, + const git_index_entry *wd) +{ + int 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); + break; + case GIT_DELTA_ADDED: + case GIT_DELTA_MODIFIED: + action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT); + break; + case GIT_DELTA_DELETED: + 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); + break; + default: /* impossible */ + break; + } + + return checkout_action_common(data, action, delta, wd); +} + +static int checkout_action_with_wd_dir( + checkout_data *data, + const git_diff_delta *delta, + const git_index_entry *wd) +{ + int 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; + 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); + 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; + break; + case GIT_DELTA_TYPECHANGE: /* case 24 or 31 */ + if (delta->old_file.mode == GIT_FILEMODE_TREE) { + /* For typechange from dir, remove dir and add blob, but it is + * not safe to remove dir if it contains modified files. + * However, safely removing child files will remove the parent + * 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; + } + 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); + break; + default: /* impossible */ + break; + } + + return checkout_action_common(data, action, delta, wd); +} + +static int checkout_action( + checkout_data *data, + git_diff_delta *delta, + git_iterator *workdir, + const git_index_entry **wditem_ptr, + git_vector *pathspec) +{ + const git_index_entry *wd = *wditem_ptr; + int cmp = -1, act; + int (*strcomp)(const char *, const char *) = data->diff->strcomp; + int (*pfxcomp)(const char *str, const char *pfx) = data->diff->pfxcomp; + + /* move workdir iterator to follow along with deltas */ + + while (1) { + if (!wd) + return checkout_action_no_wd(data, delta); + + cmp = strcomp(wd->path, delta->old_file.path); + + /* 1. wd before delta ("a/a" before "a/b") + * 2. wd prefixes delta & should expand ("a/" before "a/b") + * 3. wd prefixes delta & cannot expand ("a/b" before "a/b/c") + * 4. wd equals delta ("a/b" and "a/b") + * 5. wd after delta & delta prefixes wd ("a/b/c" after "a/b/" or "a/b") + * 6. wd after delta ("a/c" after "a/b") + */ + + if (cmp < 0) { + cmp = pfxcomp(delta->old_file.path, wd->path); + + if (cmp == 0) { + if (wd->mode == GIT_FILEMODE_TREE) { + /* case 2 - entry prefixed by workdir tree */ + if (git_iterator_advance_into_directory(workdir, &wd) < 0) + goto fail; + 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(workdir, &wd) ? NULL : wd; + return act; + } + } + + /* case 1 - handle wd item (if it matches pathspec) */ + if (checkout_action_wd_only(data, workdir, wd, pathspec) < 0 || + git_iterator_advance(workdir, &wd) < 0) + goto fail; + + *wditem_ptr = wd; + continue; + } + + if (cmp == 0) { + /* case 4 */ + act = checkout_action_with_wd(data, delta, wd); + *wditem_ptr = git_iterator_advance(workdir, &wd) ? NULL : wd; + return act; + } + + 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); + + if (delta->status == GIT_DELTA_TYPECHANGE) { + if (delta->old_file.mode == GIT_FILEMODE_TREE) { + act = checkout_action_with_wd(data, delta, wd); + if (git_iterator_advance_into_directory(workdir, &wd) < 0) + wd = NULL; + *wditem_ptr = wd; + return act; + } + + 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 (git_iterator_advance(workdir, &wd) < 0) + wd = NULL; + *wditem_ptr = wd; + return act; + } + } + + return checkout_action_with_wd_dir(data, delta, wd); + } + + /* case 6 - wd is after delta */ + return checkout_action_no_wd(data, delta); + } + +fail: + *wditem_ptr = NULL; + return -1; +} + +static int checkout_remaining_wd_items( + checkout_data *data, + git_iterator *workdir, + const git_index_entry *wd, + git_vector *spec) +{ + int error = 0; + + while (wd && !error) { + if (!(error = checkout_action_wd_only(data, workdir, wd, spec))) + error = git_iterator_advance(workdir, &wd); + } + + return error; +} + +static int checkout_get_actions( + uint32_t **actions_ptr, + size_t **counts_ptr, + checkout_data *data, + git_iterator *workdir) +{ + int error = 0; + const git_index_entry *wditem; + git_vector pathspec = GIT_VECTOR_INIT, *deltas; + git_pool pathpool = GIT_POOL_INIT_STRINGPOOL; + git_diff_delta *delta; + size_t i, *counts = NULL; + uint32_t *actions = NULL; + + if (data->opts.paths.count > 0 && + git_pathspec_init(&pathspec, &data->opts.paths, &pathpool) < 0) + return -1; + + if ((error = git_iterator_current(workdir, &wditem)) < 0) + goto fail; + + deltas = &data->diff->deltas; + + *counts_ptr = counts = git__calloc(CHECKOUT_ACTION__MAX+1, sizeof(size_t)); + *actions_ptr = actions = git__calloc( + deltas->length ? deltas->length : 1, sizeof(uint32_t)); + if (!counts || !actions) { + error = -1; + goto fail; + } + + git_vector_foreach(deltas, i, delta) { + int act = checkout_action(data, delta, workdir, &wditem, &pathspec); + + if (act < 0) { + error = act; + goto fail; + } + + actions[i] = act; + + if (act & CHECKOUT_ACTION__REMOVE) + counts[CHECKOUT_ACTION__REMOVE]++; + if (act & CHECKOUT_ACTION__UPDATE_BLOB) + counts[CHECKOUT_ACTION__UPDATE_BLOB]++; + if (act & CHECKOUT_ACTION__UPDATE_SUBMODULE) + counts[CHECKOUT_ACTION__UPDATE_SUBMODULE]++; + if (act & CHECKOUT_ACTION__CONFLICT) + counts[CHECKOUT_ACTION__CONFLICT]++; + } + + error = checkout_remaining_wd_items(data, workdir, wditem, &pathspec); + if (error < 0) + goto fail; + + counts[CHECKOUT_ACTION__REMOVE] += data->removes.length; + + 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]); + error = -1; + goto fail; + } + + git_pathspec_free(&pathspec); + git_pool_clear(&pathpool); + + return 0; + +fail: + *counts_ptr = NULL; + git__free(counts); + *actions_ptr = NULL; + git__free(actions); + + git_pathspec_free(&pathspec); + git_pool_clear(&pathpool); + + return error; +} static int buffer_to_file( + struct stat *st, git_buf *buffer, const char *path, mode_t dir_mode, @@ -57,6 +657,9 @@ static int buffer_to_file( giterr_set(GITERR_OS, "Could not write to '%s'", path); (void)p_close(fd); } else { + if ((error = p_fstat(fd, st)) < 0) + giterr_set(GITERR_OS, "Error while statting '%s'", path); + if ((error = p_close(fd)) < 0) giterr_set(GITERR_OS, "Error while closing '%s'", path); } @@ -70,6 +673,7 @@ static int buffer_to_file( } static int blob_content_to_file( + struct stat *st, git_blob *blob, const char *path, mode_t entry_filemode, @@ -112,7 +716,12 @@ static int blob_content_to_file( file_mode = entry_filemode; error = buffer_to_file( - &filtered, path, opts->dir_mode, opts->file_open_flags, file_mode); + st, &filtered, path, opts->dir_mode, opts->file_open_flags, file_mode); + + if (!error) { + st->st_size = blob->odb_object->raw.len; + st->st_mode = entry_filemode; + } cleanup: git_filters_free(&filters); @@ -124,7 +733,7 @@ cleanup: } static int blob_content_to_link( - git_blob *blob, const char *path, bool can_symlink) + struct stat *st, git_blob *blob, const char *path, int can_symlink) { git_buf linktarget = GIT_BUF_INIT; int error; @@ -132,28 +741,61 @@ static int blob_content_to_link( if ((error = git_blob__getbuf(&linktarget, blob)) < 0) return error; - if (can_symlink) - error = p_symlink(git_buf_cstr(&linktarget), path); - else + if (can_symlink) { + if ((error = p_symlink(git_buf_cstr(&linktarget), path)) < 0) + giterr_set(GITERR_CHECKOUT, "Could not create symlink %s\n", path); + } else { error = git_futils_fake_symlink(git_buf_cstr(&linktarget), path); + } + + if (!error) { + if ((error = p_lstat(path, st)) < 0) + giterr_set(GITERR_CHECKOUT, "Could not stat symlink %s", path); + + st->st_mode = GIT_FILEMODE_LINK; + } git_buf_free(&linktarget); return error; } +static int checkout_update_index( + checkout_data *data, + const git_diff_file *file, + struct stat *st) +{ + git_index_entry entry; + + if (!data->index) + return 0; + + memset(&entry, 0, sizeof(entry)); + entry.path = (char *)file->path; /* cast to prevent warning */ + git_index_entry__init_from_stat(&entry, st); + git_oid_cpy(&entry.oid, &file->oid); + + return git_index_add(data->index, &entry); +} + static int checkout_submodule( - checkout_diff_data *data, + checkout_data *data, const git_diff_file *file) { + int error = 0; + git_submodule *sm; + /* Until submodules are supported, UPDATE_ONLY means do nothing here */ - if ((data->opts->checkout_strategy & GIT_CHECKOUT_UPDATE_ONLY) != 0) + if ((data->strategy & GIT_CHECKOUT_UPDATE_ONLY) != 0) return 0; - if (git_futils_mkdir( + if ((error = git_futils_mkdir( file->path, git_repository_workdir(data->repo), - data->opts->dir_mode, GIT_MKDIR_PATH) < 0) - return -1; + data->opts.dir_mode, GIT_MKDIR_PATH)) < 0) + return error; + + if ((error = git_submodule_lookup(&sm, data->repo, file->path)) < 0) + return error; /* TODO: Support checkout_strategy options. Two circumstances: * 1 - submodule already checked out, but we need to move the HEAD @@ -164,520 +806,458 @@ static int checkout_submodule( * command should probably be able to. Do we need a submodule callback? */ - return 0; + /* update the index unless prevented */ + if ((data->strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) == 0) { + struct stat st; + + git_buf_truncate(&data->path, data->workdir_len); + if (git_buf_puts(&data->path, file->path) < 0) + return -1; + + if ((error = p_stat(git_buf_cstr(&data->path), &st)) < 0) { + giterr_set( + GITERR_CHECKOUT, "Could not stat submodule %s\n", file->path); + return error; + } + + st.st_mode = GIT_FILEMODE_COMMIT; + + error = checkout_update_index(data, file, &st); + } + + return error; } static void report_progress( - checkout_diff_data *data, + checkout_data *data, const char *path) { - if (data->opts->progress_cb) - data->opts->progress_cb( + if (data->opts.progress_cb) + data->opts.progress_cb( path, data->completed_steps, data->total_steps, - data->opts->progress_payload); + data->opts.progress_payload); +} + +static int checkout_safe_for_update_only(const char *path, mode_t expected_mode) +{ + struct stat st; + + if (p_lstat(path, &st) < 0) { + /* if doesn't exist, then no error and no update */ + if (errno == ENOENT || errno == ENOTDIR) + return 0; + + /* otherwise, stat error and no update */ + giterr_set(GITERR_OS, "Failed to stat file '%s'", path); + return -1; + } + + /* only safe for update if this is the same type of file */ + if ((st.st_mode & ~0777) == (expected_mode & ~0777)) + return 1; + + return 0; } static int checkout_blob( - checkout_diff_data *data, + checkout_data *data, const git_diff_file *file) { int error = 0; git_blob *blob; + struct stat st; - git_buf_truncate(data->path, data->workdir_len); - if (git_buf_puts(data->path, file->path) < 0) + git_buf_truncate(&data->path, data->workdir_len); + if (git_buf_puts(&data->path, file->path) < 0) return -1; + if ((data->strategy & GIT_CHECKOUT_UPDATE_ONLY) != 0) { + int rval = checkout_safe_for_update_only( + git_buf_cstr(&data->path), file->mode); + if (rval <= 0) + return rval; + } + if ((error = git_blob_lookup(&blob, data->repo, &file->oid)) < 0) return error; if (S_ISLNK(file->mode)) error = blob_content_to_link( - blob, git_buf_cstr(data->path), data->can_symlink); + &st, blob, git_buf_cstr(&data->path), data->can_symlink); else error = blob_content_to_file( - blob, git_buf_cstr(data->path), file->mode, data->opts); + &st, blob, git_buf_cstr(&data->path), file->mode, &data->opts); git_blob_free(blob); - return error; -} - -static int retrieve_symlink_caps(git_repository *repo, bool *out) -{ - git_config *cfg; - int can_symlink = 0; - int error; - - if (git_repository_config__weakptr(&cfg, repo) < 0) - return -1; - - error = git_config_get_bool(&can_symlink, cfg, "core.symlinks"); - - /* If "core.symlinks" is not found anywhere, default to true. */ - if (error == GIT_ENOTFOUND) { - can_symlink = true; + /* if we try to create the blob and an existing directory blocks it from + * being written, then there must have been a typechange conflict in a + * parent directory - suppress the error and try to continue. + */ + if ((data->strategy & GIT_CHECKOUT_ALLOW_CONFLICTS) != 0 && + (error == GIT_ENOTFOUND || error == GIT_EEXISTS)) + { + giterr_clear(); error = 0; } - - if (error >= 0) - *out = can_symlink; + + /* update the index unless prevented */ + if (!error && (data->strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) == 0) + error = checkout_update_index(data, file, &st); + + /* update the submodule data if this was a new .gitmodules file */ + if (!error && strcmp(file->path, ".gitmodules") == 0) + data->reload_submodules = true; return error; } -static void normalize_options( - git_checkout_opts *normalized, git_checkout_opts *proposed) +static int checkout_remove_the_old( + unsigned int *actions, + checkout_data *data) { - assert(normalized); + int error = 0; + git_diff_delta *delta; + const char *str; + size_t i; + const char *workdir = git_buf_cstr(&data->path); + uint32_t flg = GIT_RMDIR_EMPTY_PARENTS | + GIT_RMDIR_REMOVE_FILES | GIT_RMDIR_REMOVE_BLOCKERS; - if (!proposed) - GIT_INIT_STRUCTURE(normalized, GIT_CHECKOUT_OPTS_VERSION); - else - memmove(normalized, proposed, sizeof(git_checkout_opts)); + git_buf_truncate(&data->path, data->workdir_len); - /* implied checkout strategies */ - if ((normalized->checkout_strategy & GIT_CHECKOUT_UPDATE_MODIFIED) != 0 || - (normalized->checkout_strategy & GIT_CHECKOUT_UPDATE_UNTRACKED) != 0) - normalized->checkout_strategy |= GIT_CHECKOUT_UPDATE_UNMODIFIED; + git_vector_foreach(&data->diff->deltas, i, delta) { + if (actions[i] & CHECKOUT_ACTION__REMOVE) { + error = git_futils_rmdir_r(delta->old_file.path, workdir, flg); + if (error < 0) + return error; - if ((normalized->checkout_strategy & GIT_CHECKOUT_UPDATE_UNTRACKED) != 0) - normalized->checkout_strategy |= GIT_CHECKOUT_UPDATE_MISSING; + data->completed_steps++; + report_progress(data, delta->old_file.path); - /* opts->disable_filters is false by default */ + if ((actions[i] & CHECKOUT_ACTION__UPDATE_BLOB) == 0 && + (data->strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) == 0 && + data->index != NULL) + { + (void)git_index_remove(data->index, delta->old_file.path, 0); + } + } + } - if (!normalized->dir_mode) - normalized->dir_mode = GIT_DIR_MODE; + git_vector_foreach(&data->removes, i, str) { + error = git_futils_rmdir_r(str, workdir, flg); + if (error < 0) + return error; - if (!normalized->file_open_flags) - normalized->file_open_flags = O_CREAT | O_TRUNC | O_WRONLY; -} + data->completed_steps++; + report_progress(data, str); -enum { - CHECKOUT_ACTION__NONE = 0, - CHECKOUT_ACTION__REMOVE = 1, - CHECKOUT_ACTION__UPDATE_BLOB = 2, - CHECKOUT_ACTION__UPDATE_SUBMODULE = 4, - CHECKOUT_ACTION__CONFLICT = 8, - CHECKOUT_ACTION__MAX = 8 -}; + if ((data->strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) == 0 && + data->index != NULL) + { + if (str[strlen(str) - 1] == '/') + (void)git_index_remove_directory(data->index, str, 0); + else + (void)git_index_remove(data->index, str, 0); + } + } -static int checkout_confirm_update_blob( - checkout_diff_data *data, - const git_diff_delta *delta, - int action) -{ - int error; - unsigned int strat = data->opts->checkout_strategy; - struct stat st; - bool update_only = ((strat & GIT_CHECKOUT_UPDATE_ONLY) != 0); + return 0; +} - /* for typechange, remove the old item first */ - if (delta->status == GIT_DELTA_TYPECHANGE) { - if (update_only) - action = CHECKOUT_ACTION__NONE; - else - action |= CHECKOUT_ACTION__REMOVE; +static int checkout_deferred_remove(git_repository *repo, const char *path) +{ +#if 0 + int error = git_futils_rmdir_r( + path, git_repository_workdir(repo), GIT_RMDIR_EMPTY_PARENTS); - return action; + if (error == GIT_ENOTFOUND) { + error = 0; + giterr_clear(); } - git_buf_truncate(data->path, data->workdir_len); - if (git_buf_puts(data->path, delta->new_file.path) < 0) - return -1; + return error; +#else + GIT_UNUSED(repo); + GIT_UNUSED(path); + assert(false); + return 0; +#endif +} - if ((error = p_lstat_posixly(git_buf_cstr(data->path), &st)) < 0) { - if (errno == ENOENT) { - if (update_only) - action = CHECKOUT_ACTION__NONE; - } else if (errno == ENOTDIR) { - /* File exists where a parent dir needs to go - i.e. untracked - * typechange. Ignore if UPDATE_ONLY, remove if allowed. +static int checkout_create_the_new( + unsigned int *actions, + checkout_data *data) +{ + int error = 0; + git_diff_delta *delta; + size_t i; + + git_vector_foreach(&data->diff->deltas, i, delta) { + if (actions[i] & CHECKOUT_ACTION__DEFER_REMOVE) { + /* this had a blocker directory that should only be removed iff + * all of the contents of the directory were safely removed */ - if (update_only) - action = CHECKOUT_ACTION__NONE; - else if ((strat & GIT_CHECKOUT_UPDATE_UNTRACKED) != 0) - action |= CHECKOUT_ACTION__REMOVE; - else - action = CHECKOUT_ACTION__CONFLICT; + if ((error = checkout_deferred_remove( + data->repo, delta->old_file.path)) < 0) + return error; + } + + if (actions[i] & CHECKOUT_ACTION__UPDATE_BLOB) { + error = checkout_blob(data, &delta->new_file); + if (error < 0) + return error; + + data->completed_steps++; + report_progress(data, delta->new_file.path); } - /* otherwise let error happen when we attempt blob checkout later */ - } - else if (S_ISDIR(st.st_mode)) { - /* Directory exists where a blob needs to go - i.e. untracked - * typechange. Ignore if UPDATE_ONLY, remove if allowed. - */ - if (update_only) - action = CHECKOUT_ACTION__NONE; - else if ((strat & GIT_CHECKOUT_UPDATE_UNTRACKED) != 0) - action |= CHECKOUT_ACTION__REMOVE; - else - action = CHECKOUT_ACTION__CONFLICT; } - return action; + return 0; } -static int checkout_action_for_delta( - checkout_diff_data *data, - const git_diff_delta *delta, - const git_index_entry *head_entry) +static int checkout_create_submodules( + unsigned int *actions, + checkout_data *data) { - int action = CHECKOUT_ACTION__NONE; - unsigned int strat = data->opts->checkout_strategy; - - switch (delta->status) { - case GIT_DELTA_UNMODIFIED: - if (!head_entry) { - /* file independently created in wd, even though not in HEAD */ - if ((strat & GIT_CHECKOUT_UPDATE_MISSING) == 0) - action = CHECKOUT_ACTION__CONFLICT; - } - else if (!git_oid_equal(&head_entry->oid, &delta->old_file.oid)) { - /* working directory was independently updated to match index */ - if ((strat & GIT_CHECKOUT_UPDATE_MODIFIED) == 0) - action = CHECKOUT_ACTION__CONFLICT; - } - break; - - case GIT_DELTA_ADDED: - /* Impossible. New files should be UNTRACKED or TYPECHANGE */ - action = CHECKOUT_ACTION__CONFLICT; - break; + int error = 0; + git_diff_delta *delta; + size_t i; - case GIT_DELTA_DELETED: - if (head_entry && /* working dir missing, but exists in HEAD */ - (strat & GIT_CHECKOUT_UPDATE_MISSING) == 0) - action = CHECKOUT_ACTION__CONFLICT; - else - action = CHECKOUT_ACTION__UPDATE_BLOB; - break; + /* initial reload of submodules if .gitmodules was changed */ + if (data->reload_submodules && + (error = git_submodule_reload_all(data->repo)) < 0) + return error; - case GIT_DELTA_MODIFIED: - case GIT_DELTA_TYPECHANGE: - if (!head_entry) { - /* working dir was independently updated & does not match index */ - if ((strat & GIT_CHECKOUT_UPDATE_UNTRACKED) == 0) - action = CHECKOUT_ACTION__CONFLICT; - else - action = CHECKOUT_ACTION__UPDATE_BLOB; + git_vector_foreach(&data->diff->deltas, i, delta) { + if (actions[i] & CHECKOUT_ACTION__DEFER_REMOVE) { + /* this has a blocker directory that should only be removed iff + * all of the contents of the directory were safely removed + */ + if ((error = checkout_deferred_remove( + data->repo, delta->old_file.path)) < 0) + return error; } - else if (git_oid_equal(&head_entry->oid, &delta->new_file.oid)) - action = CHECKOUT_ACTION__UPDATE_BLOB; - else if ((strat & GIT_CHECKOUT_UPDATE_MODIFIED) == 0) - action = CHECKOUT_ACTION__CONFLICT; - else - action = CHECKOUT_ACTION__UPDATE_BLOB; - break; - case GIT_DELTA_UNTRACKED: - if (!head_entry) { - if ((strat & GIT_CHECKOUT_REMOVE_UNTRACKED) != 0) - action = CHECKOUT_ACTION__REMOVE; - } - else if ((strat & GIT_CHECKOUT_UPDATE_MODIFIED) != 0) { - action = CHECKOUT_ACTION__REMOVE; - } else if ((strat & GIT_CHECKOUT_UPDATE_UNMODIFIED) != 0) { - git_oid wd_oid; - - /* if HEAD matches workdir, then remove, else conflict */ - - if (git_oid_iszero(&delta->new_file.oid) && - git_diff__oid_for_file( - data->repo, delta->new_file.path, delta->new_file.mode, - delta->new_file.size, &wd_oid) < 0) - action = -1; - else if (git_oid_equal(&head_entry->oid, &wd_oid)) - action = CHECKOUT_ACTION__REMOVE; - else - action = CHECKOUT_ACTION__CONFLICT; - } else { - /* present in HEAD and workdir, but absent in index */ - action = CHECKOUT_ACTION__CONFLICT; - } - break; + if (actions[i] & CHECKOUT_ACTION__UPDATE_SUBMODULE) { + int error = checkout_submodule(data, &delta->new_file); + if (error < 0) + return error; - case GIT_DELTA_IGNORED: - default: - /* just skip these files */ - break; + data->completed_steps++; + report_progress(data, delta->new_file.path); + } } - if (action > 0 && (action & CHECKOUT_ACTION__UPDATE_BLOB) != 0) { - if (S_ISGITLINK(delta->old_file.mode)) - action = (action & ~CHECKOUT_ACTION__UPDATE_BLOB) | - CHECKOUT_ACTION__UPDATE_SUBMODULE; + /* final reload once submodules have been updated */ + return git_submodule_reload_all(data->repo); +} - action = checkout_confirm_update_blob(data, delta, action); - } +static int checkout_lookup_head_tree(git_tree **out, git_repository *repo) +{ + int error = 0; + git_reference *ref = NULL; + git_object *head; - if (action == CHECKOUT_ACTION__CONFLICT && - data->opts->conflict_cb != NULL && - data->opts->conflict_cb( - delta->old_file.path, &delta->old_file.oid, - delta->old_file.mode, delta->new_file.mode, - data->opts->conflict_payload) != 0) - { - giterr_clear(); - action = GIT_EUSER; - } + if (!(error = git_repository_head(&ref, repo)) && + !(error = git_reference_peel(&head, ref, GIT_OBJ_TREE))) + *out = (git_tree *)head; - if (action > 0 && (strat & GIT_CHECKOUT_UPDATE_ONLY) != 0) - action = (action & ~CHECKOUT_ACTION__REMOVE); + git_reference_free(ref); - return action; + return error; } -static int checkout_get_actions( - uint32_t **actions_ptr, - size_t **counts_ptr, - checkout_diff_data *data) +static void checkout_data_clear(checkout_data *data) { - int error; - git_diff_list *diff = data->diff; - git_diff_delta *delta; - size_t i, *counts = NULL; - uint32_t *actions = NULL; - git_tree *head = NULL; - git_iterator *hiter = NULL; - char *pfx = git_pathspec_prefix(&data->opts->paths); - const git_index_entry *he; + if (data->opts_free_baseline) { + git_tree_free(data->opts.baseline); + data->opts.baseline = NULL; + } - /* if there is no HEAD, that's okay - we'll make an empty iterator */ - if (((error = git_repository_head_tree(&head, data->repo)) < 0) && - !(error == GIT_ENOTFOUND || error == GIT_EORPHANEDHEAD)) - return -1; + git_vector_free(&data->removes); + git_pool_clear(&data->pool); - if ((error = git_iterator_for_tree_range(&hiter, head, pfx, pfx)) < 0) - goto fail; + git__free(data->pfx); + data->pfx = NULL; - if ((diff->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) != 0 && - (error = git_iterator_spoolandsort_push(hiter, true)) < 0) - goto fail; + git_buf_free(&data->path); - if ((error = git_iterator_current(hiter, &he)) < 0) - goto fail; + git_index_free(data->index); + data->index = NULL; +} - git__free(pfx); - pfx = NULL; +static int checkout_data_init( + checkout_data *data, + git_iterator *target, + git_checkout_opts *proposed) +{ + int error = 0; + git_config *cfg; + git_repository *repo = git_iterator_owner(target); - *counts_ptr = counts = git__calloc(CHECKOUT_ACTION__MAX+1, sizeof(size_t)); - *actions_ptr = actions = git__calloc(diff->deltas.length, sizeof(uint32_t)); - if (!counts || !actions) { - error = -1; - goto fail; - } + memset(data, 0, sizeof(*data)); - git_vector_foreach(&diff->deltas, i, delta) { - int cmp = -1, act; + if (!repo) { + giterr_set(GITERR_CHECKOUT, "Cannot checkout nothing"); + return -1; + } - /* try to track HEAD entries parallel to deltas */ - while (he) { - cmp = S_ISDIR(delta->new_file.mode) ? - diff->pfxcomp(he->path, delta->new_file.path) : - diff->strcomp(he->path, delta->old_file.path); - if (cmp >= 0) - break; - if (git_iterator_advance(hiter, &he) < 0) - he = NULL; - } + if ((error = git_repository__ensure_not_bare(repo, "checkout")) < 0) + return error; - act = checkout_action_for_delta(data, delta, !cmp ? he : NULL); + if ((error = git_repository_config__weakptr(&cfg, repo)) < 0) + return error; - if (act < 0) { - error = act; - goto fail; - } + data->repo = repo; - if (!cmp && git_iterator_advance(hiter, &he) < 0) - he = NULL; + GITERR_CHECK_VERSION( + proposed, GIT_CHECKOUT_OPTS_VERSION, "git_checkout_opts"); - actions[i] = act; + if (!proposed) + GIT_INIT_STRUCTURE(&data->opts, GIT_CHECKOUT_OPTS_VERSION); + else + memmove(&data->opts, proposed, sizeof(git_checkout_opts)); - if (act & CHECKOUT_ACTION__REMOVE) - counts[CHECKOUT_ACTION__REMOVE]++; - if (act & CHECKOUT_ACTION__UPDATE_BLOB) - counts[CHECKOUT_ACTION__UPDATE_BLOB]++; - if (act & CHECKOUT_ACTION__UPDATE_SUBMODULE) - counts[CHECKOUT_ACTION__UPDATE_SUBMODULE]++; - if (act & CHECKOUT_ACTION__CONFLICT) - counts[CHECKOUT_ACTION__CONFLICT]++; - } + /* refresh config and index content unless NO_REFRESH is given */ + if ((data->opts.checkout_strategy & GIT_CHECKOUT_NO_REFRESH) == 0) { + if ((error = git_config_refresh(cfg)) < 0) + goto cleanup; - if (counts[CHECKOUT_ACTION__CONFLICT] > 0 && - (data->opts->checkout_strategy & GIT_CHECKOUT_ALLOW_CONFLICTS) == 0) - { - giterr_set(GITERR_CHECKOUT, "%d conflicts prevent checkout", - (int)counts[CHECKOUT_ACTION__CONFLICT]); - goto fail; + if (git_iterator_inner_type(target) == GIT_ITERATOR_INDEX) { + /* if we are iterating over the index, don't reload */ + data->index = git_iterator_index_get_index(target); + GIT_REFCOUNT_INC(data->index); + } else { + /* otherwise, grab and reload the index */ + if ((error = git_repository_index(&data->index, data->repo)) < 0 || + (error = git_index_read(data->index)) < 0) + goto cleanup; + } } - git_iterator_free(hiter); - git_tree_free(head); - - return 0; + /* if you are forcing, definitely allow safe updates */ + if ((data->opts.checkout_strategy & GIT_CHECKOUT_FORCE) != 0) + data->opts.checkout_strategy |= GIT_CHECKOUT_SAFE_CREATE; + if ((data->opts.checkout_strategy & GIT_CHECKOUT_SAFE_CREATE) != 0) + data->opts.checkout_strategy |= GIT_CHECKOUT_SAFE; -fail: - *counts_ptr = NULL; - git__free(counts); - *actions_ptr = NULL; - git__free(actions); + data->strategy = data->opts.checkout_strategy; - git_iterator_free(hiter); - git_tree_free(head); - git__free(pfx); + /* opts->disable_filters is false by default */ - return -1; -} + if (!data->opts.dir_mode) + data->opts.dir_mode = GIT_DIR_MODE; -static int checkout_remove_the_old( - git_diff_list *diff, - unsigned int *actions, - checkout_diff_data *data) -{ - git_diff_delta *delta; - size_t i; + if (!data->opts.file_open_flags) + data->opts.file_open_flags = O_CREAT | O_TRUNC | O_WRONLY; - git_buf_truncate(data->path, data->workdir_len); + data->pfx = git_pathspec_prefix(&data->opts.paths); - git_vector_foreach(&diff->deltas, i, delta) { - if (actions[i] & CHECKOUT_ACTION__REMOVE) { - int error = git_futils_rmdir_r( - delta->new_file.path, - git_buf_cstr(data->path), /* here set to work dir root */ - GIT_RMDIR_REMOVE_FILES | GIT_RMDIR_EMPTY_PARENTS | - GIT_RMDIR_REMOVE_BLOCKERS); - if (error < 0) - return error; + error = git_config_get_bool(&data->can_symlink, cfg, "core.symlinks"); + if (error < 0) { + if (error != GIT_ENOTFOUND) + goto cleanup; - data->completed_steps++; - report_progress(data, delta->new_file.path); - } + /* If "core.symlinks" is not found anywhere, default to true. */ + data->can_symlink = true; + giterr_clear(); + error = 0; } - return 0; -} - -static int checkout_create_the_new( - git_diff_list *diff, - unsigned int *actions, - checkout_diff_data *data) -{ - git_diff_delta *delta; - size_t i; - - git_vector_foreach(&diff->deltas, i, delta) { - if (actions[i] & CHECKOUT_ACTION__UPDATE_BLOB) { - int error = checkout_blob(data, &delta->old_file); - if (error < 0) - return error; - - data->completed_steps++; - report_progress(data, delta->old_file.path); - } + if (!data->opts.baseline) { + data->opts_free_baseline = true; + if ((error = checkout_lookup_head_tree(&data->opts.baseline, repo)) < 0) + goto cleanup; } - return 0; -} - -static int checkout_create_submodules( - git_diff_list *diff, - unsigned int *actions, - checkout_diff_data *data) -{ - git_diff_delta *delta; - size_t i; + if ((error = git_vector_init(&data->removes, 0, git__strcmp_cb)) < 0 || + (error = git_pool_init(&data->pool, 1, 0)) < 0 || + (error = git_buf_puts(&data->path, git_repository_workdir(repo))) < 0) + goto cleanup; - git_vector_foreach(&diff->deltas, i, delta) { - if (actions[i] & CHECKOUT_ACTION__UPDATE_SUBMODULE) { - int error = checkout_submodule(data, &delta->old_file); - if (error < 0) - return error; + data->workdir_len = git_buf_len(&data->path); - data->completed_steps++; - report_progress(data, delta->old_file.path); - } - } +cleanup: + if (error < 0) + checkout_data_clear(data); - return 0; + return error; } -int git_checkout_index( - git_repository *repo, - git_index *index, +int git_checkout_iterator( + git_iterator *target, git_checkout_opts *opts) { - git_diff_list *diff = NULL; + int error = 0; + git_iterator *baseline = NULL, *workdir = NULL; + checkout_data data = {0}; git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT; - git_checkout_opts checkout_opts; - checkout_diff_data data; - git_buf workdir = GIT_BUF_INIT; uint32_t *actions = NULL; size_t *counts = NULL; - int error; - - assert(repo); - - GITERR_CHECK_VERSION(opts, GIT_CHECKOUT_OPTS_VERSION, "git_checkout_opts"); - if ((error = git_repository__ensure_not_bare(repo, "checkout")) < 0) + /* initialize structures and options */ + error = checkout_data_init(&data, target, opts); + if (error < 0) return error; diff_opts.flags = - GIT_DIFF_INCLUDE_UNMODIFIED | GIT_DIFF_INCLUDE_UNTRACKED | - GIT_DIFF_INCLUDE_TYPECHANGE | GIT_DIFF_SKIP_BINARY_CHECK; - - if (opts && opts->paths.count > 0) - diff_opts.pathspec = opts->paths; - - if ((error = git_diff_index_to_workdir(&diff, repo, index, &diff_opts)) < 0) - goto cleanup; - - if ((error = git_buf_puts(&workdir, git_repository_workdir(repo))) < 0) + GIT_DIFF_INCLUDE_UNMODIFIED | + GIT_DIFF_INCLUDE_UNTRACKED | + GIT_DIFF_RECURSE_UNTRACKED_DIRS | /* needed to match baseline */ + GIT_DIFF_INCLUDE_IGNORED | + GIT_DIFF_INCLUDE_TYPECHANGE | + GIT_DIFF_INCLUDE_TYPECHANGE_TREES | + GIT_DIFF_SKIP_BINARY_CHECK; + if (data.opts.paths.count > 0) + diff_opts.pathspec = data.opts.paths; + + /* set up iterators */ + if ((error = git_iterator_reset(target, data.pfx, data.pfx)) < 0 || + (error = git_iterator_for_workdir_range( + &workdir, data.repo, data.pfx, data.pfx)) < 0 || + (error = git_iterator_for_tree_range( + &baseline, data.opts.baseline, data.pfx, data.pfx)) < 0) goto cleanup; - normalize_options(&checkout_opts, opts); + /* Handle case insensitivity for baseline if necessary */ + if (workdir->ignore_case && !baseline->ignore_case) { + if ((error = git_iterator_spoolandsort_push(baseline, true)) < 0) + goto cleanup; + } - /* Checkout is best performed with up to four passes through the diff. - * - * 0. Figure out what actions should be taken and record for later. - * 1. Next do removes, because we iterate in alphabetical order, thus - * a new untracked directory will end up sorted *after* a blob that - * should be checked out with the same name. - * 2. Then checkout all blobs. - * 3. Then checkout all submodules in case a new .gitmodules blob was - * checked out during pass #2. + /* Generate baseline-to-target diff which will include an entry for + * every possible update that might need to be made. */ + if ((error = git_diff__from_iterators( + &data.diff, data.repo, baseline, target, &diff_opts)) < 0) + goto cleanup; - memset(&data, 0, sizeof(data)); - data.path = &workdir; - data.workdir_len = git_buf_len(&workdir); - data.repo = repo; - data.diff = diff; - data.opts = &checkout_opts; - - if ((error = checkout_get_actions(&actions, &counts, &data)) < 0) + /* Loop through diff (and working directory iterator) building a list of + * actions to be taken, plus look for conflicts and send notifications. + */ + if ((error = checkout_get_actions(&actions, &counts, &data, workdir)) < 0) goto cleanup; data.total_steps = counts[CHECKOUT_ACTION__REMOVE] + counts[CHECKOUT_ACTION__UPDATE_BLOB] + counts[CHECKOUT_ACTION__UPDATE_SUBMODULE]; - if ((error = retrieve_symlink_caps(repo, &data.can_symlink)) < 0) - goto cleanup; - report_progress(&data, NULL); /* establish 0 baseline */ + /* To deal with some order dependencies, perform remaining checkout + * in three passes: removes, then update blobs, then update submodules. + */ if (counts[CHECKOUT_ACTION__REMOVE] > 0 && - (error = checkout_remove_the_old(diff, actions, &data)) < 0) + (error = checkout_remove_the_old(actions, &data)) < 0) goto cleanup; if (counts[CHECKOUT_ACTION__UPDATE_BLOB] > 0 && - (error = checkout_create_the_new(diff, actions, &data)) < 0) + (error = checkout_create_the_new(actions, &data)) < 0) goto cleanup; if (counts[CHECKOUT_ACTION__UPDATE_SUBMODULE] > 0 && - (error = checkout_create_submodules(diff, actions, &data)) < 0) + (error = checkout_create_submodules(actions, &data)) < 0) goto cleanup; assert(data.completed_steps == data.total_steps); @@ -686,10 +1266,40 @@ 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); + + git_diff_list_free(data.diff); + git_iterator_free(workdir); + git_iterator_free(baseline); git__free(actions); git__free(counts); - git_diff_list_free(diff); - git_buf_free(&workdir); + checkout_data_clear(&data); + + return error; +} + +int git_checkout_index( + git_repository *repo, + git_index *index, + git_checkout_opts *opts) +{ + int error; + git_iterator *index_i; + + if ((error = git_repository__ensure_not_bare(repo, "checkout index")) < 0) + return error; + + if (!index && (error = git_repository_index__weakptr(&index, repo)) < 0) + return error; + GIT_REFCOUNT_INC(index); + + if (!(error = git_iterator_for_index(&index_i, index))) + error = git_checkout_iterator(index_i, opts); + + git_iterator_free(index_i); + git_index_free(index); return error; } @@ -699,11 +1309,12 @@ int git_checkout_tree( const git_object *treeish, git_checkout_opts *opts) { - int error = 0; - git_index *index = NULL; + int error; git_tree *tree = NULL; + git_iterator *tree_i = NULL; - assert(repo && treeish); + if ((error = git_repository__ensure_not_bare(repo, "checkout tree")) < 0) + return error; if (git_object_peel((git_object **)&tree, treeish, GIT_OBJ_TREE) < 0) { giterr_set( @@ -711,16 +1322,10 @@ int git_checkout_tree( return -1; } - /* TODO: create a temp index, load tree there and check it out */ - - /* load paths in tree that match pathspec into index */ - if (!(error = git_repository_index(&index, repo)) && - !(error = git_index_read_tree_match( - index, tree, opts ? &opts->paths : NULL)) && - !(error = git_index_write(index))) - error = git_checkout_index(repo, NULL, opts); + if (!(error = git_iterator_for_tree(&tree_i, tree))) + error = git_checkout_iterator(tree_i, opts); - git_index_free(index); + git_iterator_free(tree_i); git_tree_free(tree); return error; @@ -731,17 +1336,18 @@ int git_checkout_head( git_checkout_opts *opts) { int error; - git_reference *head = NULL; - git_object *tree = NULL; + git_tree *head = NULL; + git_iterator *head_i = NULL; - assert(repo); + if ((error = git_repository__ensure_not_bare(repo, "checkout head")) < 0) + return error; - if (!(error = git_repository_head(&head, repo)) && - !(error = git_reference_peel(&tree, head, GIT_OBJ_TREE))) - error = git_checkout_tree(repo, tree, opts); + if (!(error = checkout_lookup_head_tree(&head, repo)) && + !(error = git_iterator_for_tree(&head_i, head))) + error = git_checkout_iterator(head_i, opts); - git_reference_free(head); - git_object_free(tree); + git_iterator_free(head_i); + git_tree_free(head); return error; } diff --git a/src/checkout.h b/src/checkout.h new file mode 100644 index 000000000..815abdfed --- /dev/null +++ b/src/checkout.h @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2009-2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_checkout_h__ +#define INCLUDE_checkout_h__ + +#include "git2/checkout.h" +#include "iterator.h" + +#define GIT_CHECKOUT__NOTIFY_CONFLICT_TREE (1u << 12) + +/** + * Update the working directory to match the target iterator. The + * expected baseline value can be passed in via the checkout options + * or else will default to the HEAD commit. + */ +extern int git_checkout_iterator( + git_iterator *target, + git_checkout_opts *opts); + +#endif diff --git a/src/clone.c b/src/clone.c index 39c0ba26c..ff586a68a 100644 --- a/src/clone.c +++ b/src/clone.c @@ -362,7 +362,7 @@ static bool should_checkout( if (!opts) return false; - if (opts->checkout_strategy == GIT_CHECKOUT_DEFAULT) + if (opts->checkout_strategy == GIT_CHECKOUT_NONE) return false; return !git_repository_head_orphan(repo); diff --git a/src/diff.c b/src/diff.c index 83e73cd03..82a816465 100644 --- a/src/diff.c +++ b/src/diff.c @@ -164,6 +164,11 @@ static git_diff_delta *diff_delta__last_for_item( if (git_oid_cmp(&delta->new_file.oid, &item->oid) == 0) return delta; break; + case GIT_DELTA_UNTRACKED: + if (diff->strcomp(delta->new_file.path, item->path) == 0 && + git_oid_cmp(&delta->new_file.oid, &item->oid) == 0) + return delta; + break; case GIT_DELTA_MODIFIED: if (git_oid_cmp(&delta->old_file.oid, &item->oid) == 0 || git_oid_cmp(&delta->new_file.oid, &item->oid) == 0) @@ -531,14 +536,14 @@ static bool entry_is_prefixed( { size_t pathlen; - if (!prefix_item || diff->pfxcomp(prefix_item->path, item->path)) + if (!item || diff->pfxcomp(item->path, prefix_item->path) != 0) return false; - pathlen = strlen(item->path); + pathlen = strlen(prefix_item->path); - return (item->path[pathlen - 1] == '/' || - prefix_item->path[pathlen] == '\0' || - prefix_item->path[pathlen] == '/'); + return (prefix_item->path[pathlen - 1] == '/' || + item->path[pathlen] == '\0' || + item->path[pathlen] == '/'); } static int diff_list_init_from_iterators( @@ -584,8 +589,7 @@ int git_diff__from_iterators( *diff_ptr = NULL; - if (!diff || - diff_list_init_from_iterators(diff, old_iter, new_iter) < 0) + if (!diff || diff_list_init_from_iterators(diff, old_iter, new_iter) < 0) goto fail; if (diff->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) { @@ -616,7 +620,7 @@ int git_diff__from_iterators( * instead of just generating a DELETE record */ if ((diff->opts.flags & GIT_DIFF_INCLUDE_TYPECHANGE_TREES) != 0 && - entry_is_prefixed(diff, oitem, nitem)) + entry_is_prefixed(diff, nitem, oitem)) { /* this entry has become a tree! convert to TYPECHANGE */ git_diff_delta *last = diff_delta__last_for_item(diff, oitem); @@ -624,6 +628,17 @@ int git_diff__from_iterators( last->status = GIT_DELTA_TYPECHANGE; last->new_file.mode = GIT_FILEMODE_TREE; } + + /* If new_iter is a workdir iterator, then this situation + * will certainly be followed by a series of untracked items. + * Unless RECURSE_UNTRACKED_DIRS is set, skip over them... + */ + if (S_ISDIR(nitem->mode) && + !(diff->opts.flags & GIT_DIFF_RECURSE_UNTRACKED_DIRS)) + { + if (git_iterator_advance(new_iter, &nitem) < 0) + goto fail; + } } if (git_iterator_advance(old_iter, &oitem) < 0) @@ -635,6 +650,7 @@ int git_diff__from_iterators( */ else if (cmp > 0) { git_delta_t delta_type = GIT_DELTA_UNTRACKED; + bool contains_oitem = entry_is_prefixed(diff, oitem, nitem); /* check if contained in ignored parent directory */ if (git_buf_len(&ignore_prefix) && @@ -646,14 +662,12 @@ int git_diff__from_iterators( * it or if the user requested the contents of untracked * directories and it is not under an ignored directory. */ - bool contains_tracked = - entry_is_prefixed(diff, nitem, oitem); bool recurse_untracked = (delta_type == GIT_DELTA_UNTRACKED && (diff->opts.flags & GIT_DIFF_RECURSE_UNTRACKED_DIRS) != 0); /* do not advance into directories that contain a .git file */ - if (!contains_tracked && recurse_untracked) { + if (!contains_oitem && recurse_untracked) { git_buf *full = NULL; if (git_iterator_current_workdir_path(new_iter, &full) < 0) goto fail; @@ -661,7 +675,7 @@ int git_diff__from_iterators( recurse_untracked = false; } - if (contains_tracked || recurse_untracked) { + if (contains_oitem || recurse_untracked) { /* if this directory is ignored, remember it as the * "ignore_prefix" for processing contained items */ @@ -707,14 +721,14 @@ int git_diff__from_iterators( goto fail; /* if we are generating TYPECHANGE records then check for that - * instead of just generating an ADD/UNTRACKED record + * instead of just generating an ADDED/UNTRACKED record */ if (delta_type != GIT_DELTA_IGNORED && (diff->opts.flags & GIT_DIFF_INCLUDE_TYPECHANGE_TREES) != 0 && - entry_is_prefixed(diff, nitem, oitem)) + contains_oitem) { - /* this entry was a tree! convert to TYPECHANGE */ - git_diff_delta *last = diff_delta__last_for_item(diff, oitem); + /* this entry was prefixed with a tree - make TYPECHANGE */ + git_diff_delta *last = diff_delta__last_for_item(diff, nitem); if (last) { last->status = GIT_DELTA_TYPECHANGE; last->old_file.mode = GIT_FILEMODE_TREE; diff --git a/src/fileops.c b/src/fileops.c index 7f023bf69..47b47d6c8 100644 --- a/src/fileops.c +++ b/src/fileops.c @@ -352,6 +352,7 @@ int git_futils_mkdir_r(const char *path, const char *base, const mode_t mode) typedef struct { const char *base; + size_t baselen; uint32_t flags; int error; } futils__rmdir_data; @@ -443,9 +444,13 @@ static int futils__rmdir_recurs_foreach(void *opaque, git_buf *path) static int futils__rmdir_empty_parent(void *opaque, git_buf *path) { - int error = p_rmdir(path->ptr); + futils__rmdir_data *data = opaque; + int error; + + if (git_buf_len(path) <= data->baselen) + return GIT_ITEROVER; - GIT_UNUSED(opaque); + error = p_rmdir(git_buf_cstr(path)); if (error) { int en = errno; @@ -457,7 +462,7 @@ static int futils__rmdir_empty_parent(void *opaque, git_buf *path) giterr_clear(); error = GIT_ITEROVER; } else { - futils__error_cannot_rmdir(path->ptr, NULL); + futils__error_cannot_rmdir(git_buf_cstr(path), NULL); } } @@ -475,9 +480,10 @@ int git_futils_rmdir_r( if (git_path_join_unrooted(&fullpath, path, base, NULL) < 0) return -1; - data.base = base ? base : ""; - data.flags = flags; - data.error = 0; + data.base = base ? base : ""; + data.baselen = base ? strlen(base) : 0; + data.flags = flags; + data.error = 0; error = futils__rmdir_recurs_foreach(&data, &fullpath); diff --git a/src/index.c b/src/index.c index c04796875..ce902c5ef 100644 --- a/src/index.c +++ b/src/index.c @@ -794,6 +794,44 @@ int git_index_remove(git_index *index, const char *path, int stage) return error; } +int git_index_remove_directory(git_index *index, const char *dir, int stage) +{ + git_buf pfx = GIT_BUF_INIT; + int error = 0; + size_t pos; + git_index_entry *entry; + + if (git_buf_sets(&pfx, dir) < 0 || git_path_to_dir(&pfx) < 0) + return -1; + + git_vector_sort(&index->entries); + + pos = git_index__prefix_position(index, pfx.ptr); + + while (1) { + entry = git_vector_get(&index->entries, pos); + if (!entry || git__prefixcmp(entry->path, pfx.ptr) != 0) + break; + + if (index_entry_stage(entry) != stage) { + ++pos; + continue; + } + + git_tree_cache_invalidate_path(index->tree, entry->path); + + if ((error = git_vector_remove(&index->entries, pos)) < 0) + break; + index_entry_free(entry); + + /* removed entry at 'pos' so we don't need to increment it */ + } + + git_buf_free(&pfx); + + return error; +} + static int index_find(git_index *index, const char *path, int stage) { struct entry_srch_key srch_key; @@ -814,7 +852,10 @@ int git_index_find(git_index *index, const char *path) if ((pos = git_vector_bsearch2( &index->entries, index->entries_search_path, path)) < 0) + { + giterr_set(GITERR_INDEX, "Index does not contain %s", path); return pos; + } /* Since our binary search only looked at path, we may be in the * middle of a list of stages. diff --git a/src/iterator.c b/src/iterator.c index 28fccce0e..cf88efffd 100644 --- a/src/iterator.c +++ b/src/iterator.c @@ -30,8 +30,8 @@ (P)->base.start = start ? git__strdup(start) : NULL; \ (P)->base.end = end ? git__strdup(end) : NULL; \ (P)->base.ignore_case = false; \ - if ((start && !(P)->base.start) || (end && !(P)->base.end)) \ - return -1; \ + if ((start && !(P)->base.start) || (end && !(P)->base.end)) { \ + git__free(P); return -1; } \ } while (0) static int iterator__reset_range( @@ -988,6 +988,26 @@ fail: return -1; } +git_index *git_iterator_index_get_index(git_iterator *iter) +{ + if (iter->type == GIT_ITERATOR_INDEX) + return ((index_iterator *)iter)->index; + + if (iter->type == GIT_ITERATOR_SPOOLANDSORT && + ((spoolandsort_callbacks *)iter->cb)->orig_type == GIT_ITERATOR_INDEX) + return ((index_iterator *)iter)->index; + + return NULL; +} + +git_iterator_type_t git_iterator_inner_type(git_iterator *iter) +{ + if (iter->type == GIT_ITERATOR_SPOOLANDSORT) + return ((spoolandsort_callbacks *)iter->cb)->orig_type; + + return iter->type; +} + int git_iterator_current_tree_entry( git_iterator *iter, const git_tree_entry **tree_entry) { @@ -1058,8 +1078,8 @@ int git_iterator_advance_into_directory( if (iter->type == GIT_ITERATOR_WORKDIR && wi->entry.path && - S_ISDIR(wi->entry.mode) && - !S_ISGITLINK(wi->entry.mode)) + (wi->entry.mode == GIT_FILEMODE_TREE || + wi->entry.mode == GIT_FILEMODE_COMMIT)) { if (workdir_iterator__expand_dir(wi) < 0) /* if error loading or if empty, skip the directory. */ diff --git a/src/iterator.h b/src/iterator.h index 8bcb6fb0c..c0e35605c 100644 --- a/src/iterator.h +++ b/src/iterator.h @@ -193,4 +193,9 @@ extern int git_iterator_cmp( extern int git_iterator_current_workdir_path( git_iterator *iter, git_buf **path); + +extern git_index *git_iterator_index_get_index(git_iterator *iter); + +extern git_iterator_type_t git_iterator_inner_type(git_iterator *iter); + #endif @@ -95,11 +95,14 @@ char *git_oid_tostr(char *out, size_t n, const git_oid *oid) { char str[GIT_OID_HEXSZ]; - if (!out || n == 0 || !oid) + if (!out || n == 0) return ""; n--; /* allow room for terminating NUL */ + if (oid == NULL) + n = 0; + if (n > 0) { git_oid_fmt(str, oid); if (n > GIT_OID_HEXSZ) diff --git a/src/reset.c b/src/reset.c index 04b0863b9..f5daa8f55 100644 --- a/src/reset.c +++ b/src/reset.c @@ -62,13 +62,10 @@ int git_reset( git_object *commit = NULL; git_index *index = NULL; git_tree *tree = NULL; - int error; + int error = 0; git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; assert(repo && target); - assert(reset_type == GIT_RESET_SOFT - || reset_type == GIT_RESET_MIXED - || reset_type == GIT_RESET_HARD); if (git_object_owner(target) != repo) { giterr_set(GITERR_OBJECT, @@ -76,56 +73,50 @@ int git_reset( return -1; } - if (reset_type != GIT_RESET_SOFT - && (error = git_repository__ensure_not_bare( - repo, + if (reset_type != GIT_RESET_SOFT && + (error = git_repository__ensure_not_bare(repo, reset_type == GIT_RESET_MIXED ? "reset mixed" : "reset hard")) < 0) - return error; - - if ((error = git_object_peel(&commit, target, GIT_OBJ_COMMIT)) < 0) - goto cleanup; + return error; - if ((error = git_repository_index(&index, repo)) < 0) + if ((error = git_object_peel(&commit, target, GIT_OBJ_COMMIT)) < 0 || + (error = git_repository_index(&index, repo)) < 0 || + (error = git_commit_tree(&tree, (git_commit *)commit)) < 0) goto cleanup; - if (reset_type == GIT_RESET_SOFT && + if (reset_type == GIT_RESET_SOFT && (git_repository_state(repo) == GIT_REPOSITORY_STATE_MERGE || - git_index_has_conflicts(index))) { - giterr_set(GITERR_OBJECT, "%s (soft) while in the middle of a merge.", ERROR_MSG); - error = GIT_EUNMERGED; - goto cleanup; - } - - if ((error = update_head(repo, commit)) < 0) - goto cleanup; - - if (reset_type == GIT_RESET_SOFT) { - error = 0; + git_index_has_conflicts(index))) + { + giterr_set(GITERR_OBJECT, "%s (soft) in the middle of a merge.", ERROR_MSG); + error = GIT_EUNMERGED; goto cleanup; } - if ((error = git_commit_tree(&tree, (git_commit *)commit)) < 0) - goto cleanup; - - if ((error = git_index_read_tree(index, tree)) < 0) + /* move HEAD to the new target */ + if ((error = update_head(repo, commit)) < 0) goto cleanup; - if ((error = git_index_write(index)) < 0) - goto cleanup; + if (reset_type == GIT_RESET_HARD) { + /* overwrite working directory with HEAD */ + opts.checkout_strategy = GIT_CHECKOUT_FORCE; - if ((error = git_repository_merge_cleanup(repo)) < 0) { - giterr_set(GITERR_REPOSITORY, "%s - Failed to clean up merge data.", ERROR_MSG); - goto cleanup; + if ((error = git_checkout_tree(repo, (git_object *)tree, &opts)) < 0) + goto cleanup; } - if (reset_type == GIT_RESET_MIXED) { - error = 0; - goto cleanup; - } + if (reset_type > GIT_RESET_SOFT) { + /* reset index to the target content */ - opts.checkout_strategy = GIT_CHECKOUT_FORCE; + if ((error = git_repository_index(&index, repo)) < 0 || + (error = git_index_read_tree(index, tree)) < 0 || + (error = git_index_write(index)) < 0) + goto cleanup; - error = git_checkout_index(repo, NULL, &opts); + if ((error = git_repository_merge_cleanup(repo)) < 0) { + giterr_set(GITERR_INDEX, "%s - failed to clean up merge data", ERROR_MSG); + goto cleanup; + } + } cleanup: git_object_free(commit); diff --git a/src/stash.c b/src/stash.c index 705fc75ea..dbd626a60 100644 --- a/src/stash.c +++ b/src/stash.c @@ -22,15 +22,6 @@ static int create_error(int error, const char *msg) return error; } -static int ensure_non_bare_repository(git_repository *repo) -{ - if (!git_repository_is_bare(repo)) - return 0; - - return create_error(GIT_EBAREREPO, - "Stash related operations require a working directory."); -} - static int retrieve_head(git_reference **out, git_repository *repo) { int error = git_repository_head(out, repo); @@ -89,23 +80,22 @@ static int retrieve_base_commit_and_message( if ((error = retrieve_head(&head, repo)) < 0) return error; - error = -1; - if (strcmp("HEAD", git_reference_name(head)) == 0) - git_buf_puts(stash_message, "(no branch): "); + error = git_buf_puts(stash_message, "(no branch): "); else - git_buf_printf( + error = git_buf_printf( stash_message, "%s: ", git_reference_name(head) + strlen(GIT_REFS_HEADS_DIR)); - - if (git_commit_lookup(b_commit, repo, git_reference_target(head)) < 0) + if (error < 0) goto cleanup; - if (append_commit_description(stash_message, *b_commit) < 0) + if ((error = git_commit_lookup( + b_commit, repo, git_reference_target(head))) < 0) goto cleanup; - error = 0; + if ((error = append_commit_description(stash_message, *b_commit)) < 0) + goto cleanup; cleanup: git_reference_free(head); @@ -114,9 +104,10 @@ cleanup: static int build_tree_from_index(git_tree **out, git_index *index) { + int error; git_oid i_tree_oid; - if (git_index_write_tree(&i_tree_oid, index) < 0) + if ((error = git_index_write_tree(&i_tree_oid, index)) < 0) return -1; return git_tree_lookup(out, git_index_owner(index), &i_tree_oid); @@ -132,15 +123,15 @@ static int commit_index( git_tree *i_tree = NULL; git_oid i_commit_oid; git_buf msg = GIT_BUF_INIT; - int error = -1; + int error; - if (build_tree_from_index(&i_tree, index) < 0) + if ((error = build_tree_from_index(&i_tree, index)) < 0) goto cleanup; - if (git_buf_printf(&msg, "index on %s\n", message) < 0) + if ((error = git_buf_printf(&msg, "index on %s\n", message)) < 0) goto cleanup; - if (git_commit_create( + if ((error = git_commit_create( &i_commit_oid, git_index_owner(index), NULL, @@ -150,8 +141,8 @@ static int commit_index( git_buf_cstr(&msg), i_tree, 1, - &parent) < 0) - goto cleanup; + &parent)) < 0) + goto cleanup; error = git_commit_lookup(i_commit, git_index_owner(index), &i_commit_oid); @@ -164,6 +155,8 @@ cleanup: struct cb_data { git_index *index; + int error; + bool include_changed; bool include_untracked; bool include_ignored; @@ -174,52 +167,50 @@ static int update_index_cb( float progress, void *payload) { - int pos; struct cb_data *data = (struct cb_data *)payload; + const char *add_path = NULL; GIT_UNUSED(progress); switch (delta->status) { case GIT_DELTA_IGNORED: - if (!data->include_ignored) - break; - - return git_index_add_from_workdir(data->index, delta->new_file.path); + if (data->include_ignored) + add_path = delta->new_file.path; + break; case GIT_DELTA_UNTRACKED: - if (!data->include_untracked) - break; - - return git_index_add_from_workdir(data->index, delta->new_file.path); + if (data->include_untracked) + add_path = delta->new_file.path; + break; case GIT_DELTA_ADDED: - /* Fall through */ case GIT_DELTA_MODIFIED: - if (!data->include_changed) - break; - - return git_index_add_from_workdir(data->index, delta->new_file.path); + if (data->include_changed) + add_path = delta->new_file.path; + break; case GIT_DELTA_DELETED: if (!data->include_changed) break; - - if ((pos = git_index_find(data->index, delta->new_file.path)) < 0) - return -1; - - if (git_index_remove(data->index, delta->new_file.path, 0) < 0) - return -1; + if (git_index_find(data->index, delta->old_file.path) == 0) + data->error = git_index_remove( + data->index, delta->old_file.path, 0); + break; default: /* Unimplemented */ giterr_set( GITERR_INVALID, - "Cannot update index. Unimplemented status kind (%d)", + "Cannot update index. Unimplemented status (%d)", delta->status); - return -1; + data->error = -1; + break; } - return 0; + if (add_path != NULL) + data->error = git_index_add_from_workdir(data->index, add_path); + + return data->error; } static int build_untracked_tree( @@ -232,14 +223,15 @@ static int build_untracked_tree( git_diff_list *diff = NULL; git_diff_options opts = GIT_DIFF_OPTIONS_INIT; struct cb_data data = {0}; - int error = -1; + int error; git_index_clear(index); data.index = index; if (flags & GIT_STASH_INCLUDE_UNTRACKED) { - opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED | GIT_DIFF_RECURSE_UNTRACKED_DIRS; + opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED | + GIT_DIFF_RECURSE_UNTRACKED_DIRS; data.include_untracked = true; } @@ -248,19 +240,22 @@ static int build_untracked_tree( data.include_ignored = true; } - if (git_commit_tree(&i_tree, i_commit) < 0) - goto cleanup; - - if (git_diff_tree_to_workdir(&diff, git_index_owner(index), i_tree, &opts) < 0) + if ((error = git_commit_tree(&i_tree, i_commit)) < 0) goto cleanup; - if (git_diff_foreach(diff, update_index_cb, NULL, NULL, &data) < 0) + if ((error = git_diff_tree_to_workdir( + &diff, git_index_owner(index), i_tree, &opts)) < 0) goto cleanup; - if (build_tree_from_index(tree_out, index) < 0) + if ((error = git_diff_foreach( + diff, update_index_cb, NULL, NULL, &data)) < 0) + { + if (error == GIT_EUSER) + error = data.error; goto cleanup; + } - error = 0; + error = build_tree_from_index(tree_out, index); cleanup: git_diff_list_free(diff); @@ -279,15 +274,15 @@ static int commit_untracked( git_tree *u_tree = NULL; git_oid u_commit_oid; git_buf msg = GIT_BUF_INIT; - int error = -1; + int error; - if (build_untracked_tree(&u_tree, index, i_commit, flags) < 0) + if ((error = build_untracked_tree(&u_tree, index, i_commit, flags)) < 0) goto cleanup; - if (git_buf_printf(&msg, "untracked files on %s\n", message) < 0) + if ((error = git_buf_printf(&msg, "untracked files on %s\n", message)) < 0) goto cleanup; - if (git_commit_create( + if ((error = git_commit_create( &u_commit_oid, git_index_owner(index), NULL, @@ -297,8 +292,8 @@ static int commit_untracked( git_buf_cstr(&msg), u_tree, 0, - NULL) < 0) - goto cleanup; + NULL)) < 0) + goto cleanup; error = git_commit_lookup(u_commit, git_index_owner(index), &u_commit_oid); @@ -318,35 +313,40 @@ static int build_workdir_tree( git_diff_list *diff = NULL, *diff2 = NULL; git_diff_options opts = GIT_DIFF_OPTIONS_INIT; struct cb_data data = {0}; - int error = -1; + int error; - if (git_commit_tree(&b_tree, b_commit) < 0) + if ((error = git_commit_tree(&b_tree, b_commit)) < 0) goto cleanup; - if (git_diff_tree_to_index(&diff, repo, b_tree, NULL, &opts) < 0) + if ((error = git_diff_tree_to_index(&diff, repo, b_tree, NULL, &opts)) < 0) goto cleanup; - if (git_diff_index_to_workdir(&diff2, repo, NULL, &opts) < 0) + if ((error = git_diff_index_to_workdir(&diff2, repo, NULL, &opts)) < 0) goto cleanup; - if (git_diff_merge(diff, diff2) < 0) + if ((error = git_diff_merge(diff, diff2)) < 0) goto cleanup; data.index = index; data.include_changed = true; - if (git_diff_foreach(diff, update_index_cb, NULL, NULL, &data) < 0) + if ((error = git_diff_foreach( + diff, update_index_cb, NULL, NULL, &data)) < 0) + { + if (error == GIT_EUSER) + error = data.error; goto cleanup; + } - if (build_tree_from_index(tree_out, index) < 0) - goto cleanup; - error = 0; + if ((error = build_tree_from_index(tree_out, index)) < 0) + goto cleanup; cleanup: git_diff_list_free(diff); git_diff_list_free(diff2); git_tree_free(b_tree); + return error; } @@ -359,25 +359,24 @@ static int commit_worktree( git_commit *b_commit, git_commit *u_commit) { + int error = 0; git_tree *w_tree = NULL, *i_tree = NULL; - int error = -1; - const git_commit *parents[] = { NULL, NULL, NULL }; parents[0] = b_commit; parents[1] = i_commit; parents[2] = u_commit; - if (git_commit_tree(&i_tree, i_commit) < 0) - return -1; + if ((error = git_commit_tree(&i_tree, i_commit)) < 0) + goto cleanup; - if (git_index_read_tree(index, i_tree) < 0) + if ((error = git_index_read_tree(index, i_tree)) < 0) goto cleanup; - if (build_workdir_tree(&w_tree, index, b_commit) < 0) + if ((error = build_workdir_tree(&w_tree, index, b_commit)) < 0) goto cleanup; - if (git_commit_create( + error = git_commit_create( w_commit_oid, git_index_owner(index), NULL, @@ -386,10 +385,8 @@ static int commit_worktree( NULL, message, w_tree, - u_commit ? 3 : 2, parents) < 0) - goto cleanup; - - error = 0; + u_commit ? 3 : 2, + parents); cleanup: git_tree_free(i_tree); @@ -402,9 +399,11 @@ static int prepare_worktree_commit_message( const char *user_message) { git_buf buf = GIT_BUF_INIT; - int error = -1; + int error; + + if ((error = git_buf_set(&buf, git_buf_cstr(msg), git_buf_len(msg))) < 0) + return error; - git_buf_set(&buf, git_buf_cstr(msg), git_buf_len(msg)); git_buf_clear(msg); if (!user_message) @@ -420,10 +419,11 @@ static int prepare_worktree_commit_message( git_buf_printf(msg, ": %s\n", user_message); } - error = git_buf_oom(msg) || git_buf_oom(&buf) ? -1 : 0; + error = (git_buf_oom(msg) || git_buf_oom(&buf)) ? -1 : 0; cleanup: git_buf_free(&buf); + return error; } @@ -449,8 +449,6 @@ static int update_reflog( if ((error = git_reflog_write(reflog)) < 0) goto cleanup; - error = 0; - cleanup: git_reference_free(stash); git_reflog_free(reflog); @@ -500,8 +498,7 @@ static int reset_index_and_workdir( { git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; - opts.checkout_strategy = - GIT_CHECKOUT_UPDATE_MODIFIED | GIT_CHECKOUT_UPDATE_UNTRACKED; + opts.checkout_strategy = GIT_CHECKOUT_FORCE; if (remove_untracked) opts.checkout_strategy |= GIT_CHECKOUT_REMOVE_UNTRACKED; @@ -523,7 +520,7 @@ int git_stash_save( assert(out && repo && stasher); - if ((error = ensure_non_bare_repository(repo)) < 0) + if ((error = git_repository__ensure_not_bare(repo, "stash save")) < 0) return error; if ((error = retrieve_base_commit_and_message(&b_commit, &msg, repo)) < 0) @@ -531,47 +528,50 @@ int git_stash_save( if ((error = ensure_there_are_changes_to_stash( repo, - (flags & GIT_STASH_INCLUDE_UNTRACKED) == GIT_STASH_INCLUDE_UNTRACKED, - (flags & GIT_STASH_INCLUDE_IGNORED) == GIT_STASH_INCLUDE_IGNORED)) < 0) + (flags & GIT_STASH_INCLUDE_UNTRACKED) != 0, + (flags & GIT_STASH_INCLUDE_IGNORED) != 0)) < 0) goto cleanup; - error = -1; - - if (git_repository_index(&index, repo) < 0) + if ((error = git_repository_index(&index, repo)) < 0) goto cleanup; - if (commit_index(&i_commit, index, stasher, git_buf_cstr(&msg), b_commit) < 0) + if ((error = commit_index( + &i_commit, index, stasher, git_buf_cstr(&msg), b_commit)) < 0) goto cleanup; - if ((flags & GIT_STASH_INCLUDE_UNTRACKED || flags & GIT_STASH_INCLUDE_IGNORED) - && commit_untracked(&u_commit, index, stasher, git_buf_cstr(&msg), i_commit, flags) < 0) + if ((flags & (GIT_STASH_INCLUDE_UNTRACKED | GIT_STASH_INCLUDE_IGNORED)) && + (error = commit_untracked( + &u_commit, index, stasher, git_buf_cstr(&msg), + i_commit, flags)) < 0) goto cleanup; - if (prepare_worktree_commit_message(&msg, message) < 0) + if ((error = prepare_worktree_commit_message(&msg, message)) < 0) goto cleanup; - if (commit_worktree(out, index, stasher, git_buf_cstr(&msg), i_commit, b_commit, u_commit) < 0) + if ((error = commit_worktree( + out, index, stasher, git_buf_cstr(&msg), + i_commit, b_commit, u_commit)) < 0) goto cleanup; git_buf_rtrim(&msg); - if (update_reflog(out, repo, stasher, git_buf_cstr(&msg)) < 0) + + if ((error = update_reflog(out, repo, stasher, git_buf_cstr(&msg))) < 0) goto cleanup; - if (reset_index_and_workdir( + if ((error = reset_index_and_workdir( repo, - ((flags & GIT_STASH_KEEP_INDEX) == GIT_STASH_KEEP_INDEX) ? - i_commit : b_commit, - (flags & GIT_STASH_INCLUDE_UNTRACKED) == GIT_STASH_INCLUDE_UNTRACKED) < 0) + ((flags & GIT_STASH_KEEP_INDEX) != 0) ? i_commit : b_commit, + (flags & GIT_STASH_INCLUDE_UNTRACKED) != 0)) < 0) goto cleanup; - error = 0; - cleanup: + git_buf_free(&msg); git_commit_free(i_commit); git_commit_free(b_commit); git_commit_free(u_commit); git_index_free(index); + return error; } @@ -589,7 +589,6 @@ int git_stash_foreach( error = git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE); if (error == GIT_ENOTFOUND) return 0; - if (error < 0) goto cleanup; @@ -599,18 +598,16 @@ int git_stash_foreach( max = git_reflog_entrycount(reflog); for (i = 0; i < max; i++) { entry = git_reflog_entry_byindex(reflog, i); - + if (callback(i, git_reflog_entry_message(entry), git_reflog_entry_id_new(entry), payload)) { error = GIT_EUSER; - goto cleanup; + break; } } - error = 0; - cleanup: git_reference_free(stash); git_reflog_free(reflog); diff --git a/src/submodule.c b/src/submodule.c index 94b4f724e..9ed6707c7 100644 --- a/src/submodule.c +++ b/src/submodule.c @@ -66,7 +66,7 @@ __KHASH_IMPL( str, static kh_inline, const char *, void *, 1, str_hash_no_trailing_slash, str_equal_no_trailing_slash); -static int load_submodule_config(git_repository *repo, bool force); +static int load_submodule_config(git_repository *repo); static git_config_backend *open_gitmodules(git_repository *, bool, const git_oid *); static int lookup_head_remote(git_buf *url, git_repository *repo); static int submodule_get(git_submodule **, git_repository *, const char *, const char *); @@ -106,7 +106,7 @@ int git_submodule_lookup( assert(repo && name); - if ((error = load_submodule_config(repo, false)) < 0) + if ((error = load_submodule_config(repo)) < 0) return error; pos = git_strmap_lookup_index(repo->submodules, name); @@ -148,7 +148,7 @@ int git_submodule_foreach( assert(repo && callback); - if ((error = load_submodule_config(repo, false)) < 0) + if ((error = load_submodule_config(repo)) < 0) return error; git_strmap_foreach_value(repo->submodules, sm, { @@ -708,7 +708,8 @@ int git_submodule_open( int git_submodule_reload_all(git_repository *repo) { assert(repo); - return load_submodule_config(repo, true); + git_submodule_config_free(repo); + return load_submodule_config(repo); } int git_submodule_reload(git_submodule *submodule) @@ -829,6 +830,20 @@ int git_submodule_status( return error; } +int git_submodule_location( + unsigned int *location_status, + git_submodule *submodule) +{ + assert(location_status && submodule); + + *location_status = submodule->flags & + (GIT_SUBMODULE_STATUS_IN_HEAD | GIT_SUBMODULE_STATUS_IN_INDEX | + GIT_SUBMODULE_STATUS_IN_CONFIG | GIT_SUBMODULE_STATUS_IN_WD); + + return 0; +} + + /* * INTERNAL FUNCTIONS */ @@ -1225,14 +1240,14 @@ static git_config_backend *open_gitmodules( return mods; } -static int load_submodule_config(git_repository *repo, bool force) +static int load_submodule_config(git_repository *repo) { int error; git_oid gitmodules_oid; git_buf path = GIT_BUF_INIT; git_config_backend *mods = NULL; - if (repo->submodules && !force) + if (repo->submodules) return 0; memset(&gitmodules_oid, 0, sizeof(gitmodules_oid)); |
