checkout: allow baseline to be specified as index Allow the baseline to be specified as an index, so that users need not write their index to a tree just to checkout with that as the baseline.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
diff --git a/include/git2/checkout.h b/include/git2/checkout.h
index ed39bd3..58f1907 100644
--- a/include/git2/checkout.h
+++ b/include/git2/checkout.h
@@ -273,6 +273,7 @@ typedef struct git_checkout_options {
git_strarray paths;
git_tree *baseline; /**< expected content of workdir, defaults to HEAD */
+ git_index *baseline_index; /**< expected content of workdir, expressed as an index. */
const char *target_directory; /**< alternative checkout path to workdir */
diff --git a/src/checkout.c b/src/checkout.c
index a647ce0..6a1d281 100644
--- a/src/checkout.c
+++ b/src/checkout.c
@@ -2397,7 +2397,7 @@ static int checkout_data_init(
&data->can_symlink, repo, GIT_CVAR_SYMLINKS)) < 0)
goto cleanup;
- if (!data->opts.baseline) {
+ if (!data->opts.baseline && !data->opts.baseline_index) {
data->opts_free_baseline = true;
error = checkout_lookup_head_tree(&data->opts.baseline, repo);
@@ -2501,12 +2501,21 @@ int git_checkout_iterator(
(error = git_iterator_for_workdir_ext(
&workdir, data.repo, data.opts.target_directory, index, NULL,
iterflags | GIT_ITERATOR_DONT_AUTOEXPAND,
- data.pfx, data.pfx)) < 0 ||
- (error = git_iterator_for_tree(
- &baseline, data.opts.baseline,
- iterflags, data.pfx, data.pfx)) < 0)
+ data.pfx, data.pfx)) < 0)
goto cleanup;
+ if (data.opts.baseline_index) {
+ if ((error = git_iterator_for_index(
+ &baseline, data.opts.baseline_index,
+ iterflags, data.pfx, data.pfx)) < 0)
+ goto cleanup;
+ } else {
+ if ((error = git_iterator_for_tree(
+ &baseline, data.opts.baseline,
+ iterflags, data.pfx, data.pfx)) < 0)
+ goto cleanup;
+ }
+
/* Should not have case insensitivity mismatch */
assert(git_iterator_ignore_case(workdir) == git_iterator_ignore_case(baseline));