Commit 73dce1f688ca6ec8f5c56697dc57af36d15a1fe5

Edward Thomson 2015-03-16T18:57:57

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.

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));