Commit 7e5c8a5b by Russell Belfer

More checkout improvements

This flips checkout back to be driven off the changes between
the baseline and the target trees.  This reinstates the complex
code for tracking the contents of the working directory, but
overall, I think the resulting logic is easier to follow.
parent cf208031
......@@ -9,8 +9,7 @@
#include "common.h"
#include "types.h"
#include "indexer.h"
#include "strarray.h"
#include "diff.h"
/**
* @file git2/checkout.h
......@@ -25,27 +24,28 @@ GIT_BEGIN_DECL
* Checkout behavior flags
*
* In libgit2, the function of checkout is to update the working directory
* to match a target tree given an expected baseline tree. It does not move
* the HEAD commit - you do that separately. Typically the expected tree is
* the (to-be-moved) HEAD commit.
* to match a target tree. It does not move the HEAD commit - you do that
* separately. To safely perform the update, checkout relies on a baseline
* tree (generally the current HEAD) as a reference for the unmodified
* content expected in the working directory.
*
* Checkout examines the differences between the target and expected trees
* plus the current working directory and groups files into five categories:
* Checkout examines the differences between the target tree, the baseline
* tree and the working directory, and groups files into five categories:
*
* 1. UNMODIFIED - Files that match in all places.
* 2. SAFE - Files where the working directory and the expect content match
* that can be safely updated to the target.
* 2. SAFE - Files where the working directory and the baseline content
* match that can be safely updated to the target.
* 3. DIRTY/MISSING - Files where the working directory differs from the
* expected content but there is no conflicting change with the target
* tree. An example is a file that doesn't exist in the working
* directory - no data would be lost as a result of writing this file.
* The action to take with these files depends on the options you elect.
* 4. CONFLICTS - Files where changes in the working directory conflicts
* baseline but there is no conflicting change with the target. One
* example is a file that doesn't exist in the working directory - no
* data would be lost as a result of writing this file. Which action
* will be taken with these files depends on the options you use.
* 4. CONFLICTS - Files where changes in the working directory conflict
* with changes to be applied by the target. If conflicts are found,
* they prevent any other modifications from being made (although there
* are options to override that and force the update, of course).
* 5. UNTRACKED/IGNORED - Files in the working directory that are untracked
* or ignored.
* or ignored (i.e. only in the working directory, not the other places).
*
*
* You control the actions checkout takes with one of four base strategies:
......@@ -54,11 +54,11 @@ GIT_BEGIN_DECL
* run that you can use to find conflicts, etc. if you wish.
*
* - `GIT_CHECKOUT_SAFE` is like `git checkout` and only applies changes
* between the expected and target trees to files in category 2.
* between the baseline and target trees to files in category 2.
*
* - `GIT_CHECKOUT_SAFE_CREATE` also creates files that are missing from the
* working directory (category 3), even if there is no change between the
* expected and target trees for those files. See notes below on
* baseline and target trees for those files. See notes below on
* emulating `git checkout-index` for some of the subtleties of this.
*
* - `GIT_CHECKOUT_FORCE` is like `git checkout -f` and will update the
......@@ -97,7 +97,7 @@ GIT_BEGIN_DECL
* To emulate `git checkout`, use `GIT_CHECKOUT_SAFE` with a checkout
* notification callback (see below) that displays information about dirty
* files (i.e. files that don't need an update but that no longer match the
* expected content). The default behavior will cancel on conflicts.
* baseline content). The default behavior will cancel on conflicts.
*
* To emulate `git checkout-index`, use `GIT_CHECKOUT_SAFE_CREATE` with a
* notification callback that cancels the operation if a dirty-but-existing
......@@ -140,6 +140,9 @@ typedef enum {
/** Only update existing files, don't create new ones */
GIT_CHECKOUT_UPDATE_ONLY = (1u << 7),
/** Don't refresh index/config/etc before doing checkout */
GIT_CHECKOUT_NO_REFRESH = (1u << 8),
/**
* THE FOLLOWING OPTIONS ARE NOT YET IMPLEMENTED
*/
......@@ -166,14 +169,14 @@ typedef enum {
* receive a callback depend on the `notify_flags` value which is a
* combination of these flags.
*
* - GIT_CHECKOUT_NOTIFY_CONFLICTS means that conflicting files that would
* - GIT_CHECKOUT_NOTIFY_CONFLICT means that conflicting files that would
* prevent the checkout from occurring will receive callbacks. If you
* used GIT_CHECKOUT_ALLOW_CONFLICTS, the callbacks are still done, but
* the checkout will not be blocked. The callback `status_flags` will
* have both index and work tree change bits set (see `git_status_t`).
*
* - GIT_CHECKOUT_NOTIFY_DIRTY means to notify about "dirty" files, i.e.
* those that do not need to be updated but no longer match the expected
* those that do not need to be updated but no longer match the baseline
* content. Core git displays these files when checkout runs, but does
* not stop the checkout. For these, `status_flags` will have only work
* tree bits set (i.e. GIT_STATUS_WT_MODIFIED, etc).
......@@ -202,11 +205,12 @@ typedef enum {
* Dirty files will only have work tree flags set.
*/
typedef enum {
GIT_CHECKOUT_NOTIFY_CONFLICTS = (1u << 0),
GIT_CHECKOUT_NOTIFY_DIRTY = (1u << 1),
GIT_CHECKOUT_NOTIFY_UPDATED = (1u << 2),
GIT_CHECKOUT_NOTIFY_NONE = 0,
GIT_CHECKOUT_NOTIFY_CONFLICT = (1u << 0),
GIT_CHECKOUT_NOTIFY_DIRTY = (1u << 1),
GIT_CHECKOUT_NOTIFY_UPDATED = (1u << 2),
GIT_CHECKOUT_NOTIFY_UNTRACKED = (1u << 3),
GIT_CHECKOUT_NOTIFY_IGNORED = (1u << 4),
GIT_CHECKOUT_NOTIFY_IGNORED = (1u << 4),
} git_checkout_notify_t;
/**
......@@ -231,11 +235,11 @@ typedef struct git_checkout_opts {
unsigned int notify_flags; /** see `git_checkout_notify_t` above */
int (*notify_cb)(
git_checkout_notify_t why,
const char *path,
unsigned int status_flags, /** combo of git_status_t values */
const git_oid *index_oid,
unsigned int checkout_mode,
unsigned int workdir_mode,
const git_diff_file *baseline,
const git_diff_file *target,
const git_diff_file *workdir,
void *payload);
void *notify_payload;
......
......@@ -10,22 +10,15 @@
#include "git2/checkout.h"
#include "iterator.h"
#define GIT_CHECKOUT__FREE_BASELINE (1u << 24)
#define GIT_CHECKOUT__NOTIFY_CONFLICT_TREE (1u << 12)
/**
* Given a working directory which is expected to match the contents
* of iterator "expected", this will make the directory match the
* contents of "desired" according to the rules in the checkout "opts".
*
* Because the iterators for the desired and expected values were already
* created when this is invoked, if the checkout opts `paths` is in play,
* then presumably the pathspec_pfx was already computed, so it should be
* passed in to prevent reallocation.
* 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__from_iterators(
git_iterator *desired,
git_iterator *expected,
git_checkout_opts *opts,
const char *pathspec_pfx);
extern int git_checkout_iterator(
git_iterator *target,
git_checkout_opts *opts);
#endif
......@@ -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(
......@@ -616,7 +621,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 +629,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 +651,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 +663,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 +676,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 +722,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;
......
......@@ -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);
......
......@@ -126,9 +126,10 @@ void test_checkout_tree__doesnt_write_unrequested_files_to_worktree(void)
cl_git_pass(git_commit_lookup(&p_master_commit, g_repo, &master_oid));
cl_git_pass(git_commit_lookup(&p_chomped_commit, g_repo, &chomped_oid));
/* A GIT_CHECKOUT_DEFAULT checkout is not allowed to add any file to the
* working tree from the index as it is supposed to be a dry run. */
opts.checkout_strategy = GIT_CHECKOUT_DEFAULT;
/* GIT_CHECKOUT_NONE should not add any file to the working tree from the
* index as it is supposed to be a dry run.
*/
opts.checkout_strategy = GIT_CHECKOUT_NONE;
git_checkout_tree(g_repo, (git_object*)p_chomped_commit, &opts);
cl_assert_equal_i(false, git_path_isfile("testrepo/readme.txt"));
}
......@@ -44,7 +44,8 @@ void test_checkout_typechange__checkout_typechanges(void)
for (i = 0; g_typechange_oids[i] != NULL; ++i) {
cl_git_pass(git_revparse_single(&obj, g_repo, g_typechange_oids[i]));
/* fprintf(stderr, "checking out '%s'\n", g_typechange_oids[i]); */
/* fprintf(stderr, "---- checking out '%s' ----\n", g_typechange_oids[i]); */
cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment