Commit 300d192f7ed45112121f2a35d5ca80a4913c7aad

Edward Thomson 2013-12-02T11:15:27

Introduce git_revert to revert a single commit

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
diff --git a/include/git2/commit.h b/include/git2/commit.h
index a08cf1c..8b03df0 100644
--- a/include/git2/commit.h
+++ b/include/git2/commit.h
@@ -117,6 +117,17 @@ GIT_EXTERN(const char *) git_commit_message(const git_commit *commit);
 GIT_EXTERN(const char *) git_commit_message_raw(const git_commit *commit);
 
 /**
+ * Get the short "summary" of the git commit message.
+ *
+ * The returned message is the summary of the commit, comprising the
+ * first paragraph of the message with whitespace trimmed and squashed.
+ *
+ * @param commit a previously loaded commit.
+ * @return the summary of a commit or NULL on error
+ */
+GIT_EXTERN(const char *) git_commit_summary(git_commit *commit);
+
+/**
  * Get the commit time (i.e. committer time) of a commit.
  *
  * @param commit a previously loaded commit.
diff --git a/include/git2/errors.h b/include/git2/errors.h
index be7a31d..f1a8ea1 100644
--- a/include/git2/errors.h
+++ b/include/git2/errors.h
@@ -71,6 +71,7 @@ typedef enum {
 	GITERR_MERGE,
 	GITERR_SSH,
 	GITERR_FILTER,
+	GITERR_REVERT,
 } git_error_t;
 
 /**
diff --git a/include/git2/revert.h b/include/git2/revert.h
new file mode 100644
index 0000000..fe84238
--- /dev/null
+++ b/include/git2/revert.h
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * 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_git_revert_h__
+#define INCLUDE_git_revert_h__
+
+#include "common.h"
+#include "types.h"
+#include "merge.h"
+
+/**
+ * @file git2/revert.h
+ * @brief Git revert routines
+ * @defgroup git_revert Git revert routines
+ * @ingroup Git
+ * @{
+ */
+GIT_BEGIN_DECL
+
+typedef struct {
+	unsigned int version;
+
+	/** For merge commits, the "mainline" is treated as the parent. */
+	unsigned int mainline;
+
+	git_merge_tree_opts merge_tree_opts;
+	git_checkout_opts checkout_opts;
+} git_revert_opts;
+
+#define GIT_REVERT_OPTS_VERSION 1
+#define GIT_REVERT_OPTS_INIT {GIT_REVERT_OPTS_VERSION, 0, GIT_MERGE_TREE_OPTS_INIT, GIT_CHECKOUT_OPTS_INIT}
+
+/**
+* Reverts the given commits, producing changes in the working directory.
+*
+* @param repo the repository to revert
+* @param commits the commits to revert
+* @param commits_len the number of commits to revert
+* @param flags merge flags
+*/
+GIT_EXTERN(int) git_revert(
+	git_repository *repo,
+	git_commit *commit,
+	const git_revert_opts *given_opts);
+
+/** @} */
+GIT_END_DECL
+#endif
+
diff --git a/src/commit.c b/src/commit.c
index 91b60bb..bbb76f3 100644
--- a/src/commit.c
+++ b/src/commit.c
@@ -31,6 +31,7 @@ void git_commit__free(void *_commit)
 	git__free(commit->raw_header);
 	git__free(commit->raw_message);
 	git__free(commit->message_encoding);
+	git__free(commit->summary);
 
 	git__free(commit);
 }
@@ -286,6 +287,34 @@ const char *git_commit_message(const git_commit *commit)
 	return message;
 }
 
+const char *git_commit_summary(git_commit *commit)
+{
+	git_buf summary = GIT_BUF_INIT;
+	const char *msg, *space;
+
+	assert(commit);
+
+	if (!commit->summary) {
+		for (msg = git_commit_message(commit), space = NULL; *msg; ++msg) {
+			if (msg[0] == '\n' && (!msg[1] || msg[1] == '\n'))
+				break;
+			else if (msg[0] == '\n')
+				git_buf_putc(&summary, ' ');
+			else if (git__isspace(msg[0]))
+				space = space ? space : msg;
+			else if (space) {
+				git_buf_put(&summary, space, (msg - space) + 1);
+				space = NULL;
+			} else
+				git_buf_putc(&summary, *msg);
+		}
+
+		commit->summary = git_buf_detach(&summary);
+	}
+
+	return commit->summary;
+}
+
 int git_commit_tree(git_tree **tree_out, const git_commit *commit)
 {
 	assert(commit);
diff --git a/src/commit.h b/src/commit.h
index d452e29..efb080b 100644
--- a/src/commit.h
+++ b/src/commit.h
@@ -26,6 +26,8 @@ struct git_commit {
 	char *message_encoding;
 	char *raw_message;
 	char *raw_header;
+
+	char *summary;
 };
 
 void git_commit__free(void *commit);
diff --git a/src/merge.c b/src/merge.c
index 1158679..c31a935 100644
--- a/src/merge.c
+++ b/src/merge.c
@@ -1206,7 +1206,7 @@ static git_merge_diff *merge_diff_from_index_entries(
 
 /* Merge trees */
 
-static int merge_index_insert_conflict(
+static int merge_diff_list_insert_conflict(
 	git_merge_diff_list *diff_list,
 	struct merge_diff_df_data *merge_df_data,
 	const git_index_entry *tree_items[3])
@@ -1222,7 +1222,7 @@ static int merge_index_insert_conflict(
 	return 0;
 }
 
-static int merge_index_insert_unmodified(
+static int merge_diff_list_insert_unmodified(
 	git_merge_diff_list *diff_list,
 	const git_index_entry *tree_items[3])
 {
@@ -1252,7 +1252,7 @@ int git_merge_diff_list__find_differences(
 	size_t i, j;
 	int error = 0;
 
-	assert(diff_list && our_tree && their_tree);
+	assert(diff_list && (our_tree || their_tree));
 
 	if ((error = git_iterator_for_tree(&iterators[TREE_IDX_ANCESTOR], (git_tree *)ancestor_tree, GIT_ITERATOR_DONT_IGNORE_CASE, NULL, NULL)) < 0 ||
 		(error = git_iterator_for_tree(&iterators[TREE_IDX_OURS], (git_tree *)our_tree, GIT_ITERATOR_DONT_IGNORE_CASE, NULL, NULL)) < 0 ||
@@ -1262,6 +1262,7 @@ int git_merge_diff_list__find_differences(
 	/* Set up the iterators */
 	for (i = 0; i < 3; i++) {
 		error = git_iterator_current(&items[i], iterators[i]);
+
 		if (error < 0 && error != GIT_ITEROVER)
 			goto done;
 	}
@@ -1313,9 +1314,9 @@ int git_merge_diff_list__find_differences(
 			break;
 
 		if (cur_item_modified)
-			error = merge_index_insert_conflict(diff_list, &df_data, cur_items);
+			error = merge_diff_list_insert_conflict(diff_list, &df_data, cur_items);
 		else
-			error = merge_index_insert_unmodified(diff_list, cur_items);
+			error = merge_diff_list_insert_unmodified(diff_list, cur_items);
 		if (error < 0)
 			goto done;
 
@@ -1325,6 +1326,7 @@ int git_merge_diff_list__find_differences(
 				continue;
 
 			error = git_iterator_advance(&items[i], iterators[i]);
+
 			if (error < 0 && error != GIT_ITEROVER)
 				goto done;
 		}
@@ -1569,7 +1571,7 @@ int git_merge_trees(
 	size_t i;
 	int error = 0;
 
-	assert(out && repo && our_tree && their_tree);
+	assert(out && repo && (our_tree || their_tree));
 
 	*out = NULL;
 
@@ -2268,7 +2270,7 @@ done:
 	return error;
 }
 
-static int merge_indexes(git_repository *repo, git_index *index_new)
+int git_merge__indexes(git_repository *repo, git_index *index_new)
 {
 	git_index *index_repo;
 	unsigned int index_repo_caps;
@@ -2440,7 +2442,7 @@ int git_merge(
 	/* TODO: recursive, octopus, etc... */
 
 	if ((error = git_merge_trees(&index_new, repo, ancestor_tree, our_tree, their_trees[0], &opts.merge_tree_opts)) < 0 ||
-		(error = merge_indexes(repo, index_new)) < 0 ||
+		(error = git_merge__indexes(repo, index_new)) < 0 ||
 		(error = git_repository_index(&index_repo, repo)) < 0 ||
 		(error = git_checkout_index(repo, index_repo, &opts.checkout_opts)) < 0)
 		goto on_error;
diff --git a/src/merge.h b/src/merge.h
index d7d1c67..b240f6c 100644
--- a/src/merge.h
+++ b/src/merge.h
@@ -158,4 +158,6 @@ int git_merge__setup(
 	size_t their_heads_len,
 	unsigned int flags);
 
+int git_merge__indexes(git_repository *repo, git_index *index_new);
+
 #endif
diff --git a/src/revert.c b/src/revert.c
new file mode 100644
index 0000000..34ba343
--- /dev/null
+++ b/src/revert.c
@@ -0,0 +1,217 @@
+/*
+* Copyright (C) the libgit2 contributors. All rights reserved.
+*
+* This file is part of libgit2, distributed under the GNU GPL v2 with
+* a Linking Exception. For full terms see the included COPYING file.
+*/
+
+#include "common.h"
+#include "repository.h"
+#include "filebuf.h"
+#include "merge.h"
+#include "revert.h"
+
+#include "git2/types.h"
+#include "git2/merge.h"
+#include "git2/revert.h"
+#include "git2/commit.h"
+#include "git2/sys/commit.h"
+
+#define GIT_REVERT_FILE_MODE		0666
+
+static int write_revert_head(
+	git_repository *repo,
+	const git_commit *commit,
+	const char *commit_oidstr)
+{
+	git_filebuf file = GIT_FILEBUF_INIT;
+	git_buf file_path = GIT_BUF_INIT;
+	int error = 0;
+
+	assert(repo && commit);
+
+	if ((error = git_buf_joinpath(&file_path, repo->path_repository, GIT_REVERT_HEAD_FILE)) >= 0 &&
+		(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE, GIT_REVERT_FILE_MODE)) >= 0 &&
+		(error = git_filebuf_printf(&file, "%s\n", commit_oidstr)) >= 0)
+		error = git_filebuf_commit(&file);
+
+	if (error < 0)
+		git_filebuf_cleanup(&file);
+
+	git_buf_free(&file_path);
+
+	return error;
+}
+
+static int write_merge_msg(
+	git_repository *repo,
+	const git_commit *commit,
+	const char *commit_oidstr,
+	const char *commit_msgline)
+{
+	git_filebuf file = GIT_FILEBUF_INIT;
+	git_buf file_path = GIT_BUF_INIT;
+	int error = 0;
+
+	assert(repo && commit);
+
+	if ((error = git_buf_joinpath(&file_path, repo->path_repository, GIT_MERGE_MSG_FILE)) < 0 ||
+		(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE, GIT_REVERT_FILE_MODE)) < 0 ||
+		(error = git_filebuf_printf(&file, "Revert \"%s\"\n\nThis reverts commit %s.\n",
+		commit_msgline, commit_oidstr)) < 0)
+		goto cleanup;
+
+	error = git_filebuf_commit(&file);
+
+cleanup:
+	if (error < 0)
+		git_filebuf_cleanup(&file);
+
+	git_buf_free(&file_path);
+
+	return error;
+}
+
+static int revert_normalize_opts(
+	git_repository *repo,
+	git_revert_opts *opts,
+	const git_revert_opts *given,
+	const char *their_label)
+{
+	int error = 0;
+	unsigned int default_checkout_strategy = GIT_CHECKOUT_SAFE_CREATE |
+		GIT_CHECKOUT_ALLOW_CONFLICTS;
+
+	GIT_UNUSED(repo);
+
+	if (given != NULL)
+		memcpy(opts, given, sizeof(git_revert_opts));
+	else {
+		git_revert_opts default_opts = GIT_REVERT_OPTS_INIT;
+		memcpy(opts, &default_opts, sizeof(git_revert_opts));
+	}
+
+	if (!opts->checkout_opts.checkout_strategy)
+		opts->checkout_opts.checkout_strategy = default_checkout_strategy;
+
+	if (!opts->checkout_opts.our_label)
+		opts->checkout_opts.our_label = "HEAD";
+
+	if (!opts->checkout_opts.their_label)
+		opts->checkout_opts.their_label = their_label;
+
+	return error;
+}
+
+int git_revert(
+	git_repository *repo,
+	git_commit *commit,
+	const git_revert_opts *given_opts)
+{
+	git_revert_opts opts;
+	git_commit *parent_commit = NULL;
+	git_tree *parent_tree = NULL, *our_tree = NULL, *revert_tree = NULL;
+	git_index *index_new = NULL, *index_repo = NULL;
+	char commit_oidstr[GIT_OID_HEXSZ + 1];
+	const char *commit_msg;
+	git_buf their_label = GIT_BUF_INIT;
+	int parent = 0;
+	int error = 0;
+
+	assert(repo && commit);
+
+	if ((error = git_repository__ensure_not_bare(repo, "revert")) < 0)
+		return error;
+
+	git_oid_fmt(commit_oidstr, git_commit_id(commit));
+	commit_oidstr[GIT_OID_HEXSZ] = '\0';
+
+	if ((commit_msg = git_commit_summary(commit)) == NULL) {
+		error = -1;
+		goto on_error;
+	}
+
+	if ((error = git_buf_printf(&their_label, "parent of %.7s... %s", commit_oidstr, commit_msg)) < 0 ||
+		(error = revert_normalize_opts(repo, &opts, given_opts, git_buf_cstr(&their_label))) < 0 ||
+		(error = write_revert_head(repo, commit, commit_oidstr)) < 0 ||
+		(error = write_merge_msg(repo, commit, commit_oidstr, commit_msg)) < 0 ||
+		(error = git_repository_head_tree(&our_tree, repo)) < 0 ||
+		(error = git_commit_tree(&revert_tree, commit)) < 0)
+		goto on_error;
+
+	if (git_commit_parentcount(commit) > 1) {
+		if (!opts.mainline) {
+			giterr_set(GITERR_REVERT,
+				"Mainline branch is not specified but %s is a merge commit",
+				commit_oidstr);
+			error = -1;
+			goto on_error;
+		}
+
+		parent = opts.mainline;
+	} else {
+		if (opts.mainline) {
+			giterr_set(GITERR_REVERT,
+				"Mainline branch was specified but %s is not a merge",
+				commit_oidstr);
+			error = -1;
+			goto on_error;
+		}
+
+		parent = git_commit_parentcount(commit);
+	}
+
+	if (parent &&
+		((error = git_commit_parent(&parent_commit, commit, (parent - 1))) < 0 ||
+		(error = git_commit_tree(&parent_tree, parent_commit)) < 0))
+		goto on_error;
+
+	if ((error = git_merge_trees(&index_new, repo, revert_tree, our_tree, parent_tree, &opts.merge_tree_opts)) < 0 ||
+		(error = git_merge__indexes(repo, index_new)) < 0 ||
+		(error = git_repository_index(&index_repo, repo)) < 0 ||
+		(error = git_checkout_index(repo, index_repo, &opts.checkout_opts)) < 0)
+		goto on_error;
+
+	goto done;
+
+on_error:
+	git_revert__cleanup(repo);
+
+done:
+	git_index_free(index_new);
+	git_index_free(index_repo);
+	git_tree_free(parent_tree);
+	git_tree_free(our_tree);
+	git_tree_free(revert_tree);
+	git_commit_free(parent_commit);
+	git_buf_free(&their_label);
+
+	return error;
+}
+
+int git_revert__cleanup(git_repository *repo)
+{
+	int error = 0;
+	git_buf revert_head_path = GIT_BUF_INIT,
+		merge_msg_path = GIT_BUF_INIT;
+
+	assert(repo);
+
+	if (git_buf_joinpath(&revert_head_path, repo->path_repository, GIT_REVERT_HEAD_FILE) < 0 ||
+		git_buf_joinpath(&merge_msg_path, repo->path_repository, GIT_MERGE_MSG_FILE) < 0)
+		return -1;
+
+	if (git_path_isfile(revert_head_path.ptr)) {
+		if ((error = p_unlink(revert_head_path.ptr)) < 0)
+			goto cleanup;
+	}
+
+	if (git_path_isfile(merge_msg_path.ptr))
+		(void)p_unlink(merge_msg_path.ptr);
+
+cleanup:
+	git_buf_free(&merge_msg_path);
+	git_buf_free(&revert_head_path);
+
+	return error;
+}
diff --git a/src/revert.h b/src/revert.h
new file mode 100644
index 0000000..ed39121
--- /dev/null
+++ b/src/revert.h
@@ -0,0 +1,14 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * 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_revert_h__
+#define INCLUDE_revert_h__
+
+#include "git2/repository.h"
+
+int git_revert__cleanup(git_repository *repo);
+
+#endif
diff --git a/tests/commit/commit.c b/tests/commit/commit.c
index 8f071ff..4af9190 100644
--- a/tests/commit/commit.c
+++ b/tests/commit/commit.c
@@ -1,4 +1,6 @@
 #include "clar_libgit2.h"
+#include "commit.h"
+#include "git2/commit.h"
 
 static git_repository *_repo;
 
@@ -44,3 +46,30 @@ void test_commit_commit__create_unexisting_update_ref(void)
 	git_signature_free(s);
 	git_reference_free(ref);
 }
+
+void assert_commit_summary(const char *expected, const char *given)
+{
+	git_commit *dummy;
+
+	cl_assert(dummy = git__calloc(1, sizeof(struct git_commit)));
+
+	dummy->raw_message = git__strdup(given);
+	cl_assert_equal_s(expected, git_commit_summary(dummy));
+
+	git_commit__free(dummy);
+}
+
+void test_commit_commit__summary(void)
+{
+	assert_commit_summary("One-liner with no trailing newline", "One-liner with no trailing newline");
+	assert_commit_summary("One-liner with trailing newline", "One-liner with trailing newline\n");
+	assert_commit_summary("Trimmed leading&trailing newlines", "\n\nTrimmed leading&trailing newlines\n\n");
+	assert_commit_summary("First paragraph only", "\nFirst paragraph only\n\n(There are more!)");
+	assert_commit_summary("First paragraph with  unwrapped trailing\tlines", "\nFirst paragraph\nwith  unwrapped\ntrailing\tlines\n\n(Yes, unwrapped!)");
+	assert_commit_summary("\tLeading \ttabs", "\tLeading\n\ttabs\n\nis preserved");
+	assert_commit_summary(" Leading  Spaces", " Leading\n Spaces\n\nare preserved");
+	assert_commit_summary("Trailing tabs\tare removed", "Trailing tabs\tare removed\t\t");
+	assert_commit_summary("Trailing spaces  are removed", "Trailing spaces  are removed  ");
+	assert_commit_summary("Trailing tabs", "Trailing tabs\t\n\nare removed");
+	assert_commit_summary("Trailing spaces", "Trailing spaces \n\nare removed");
+}
diff --git a/tests/resources/revert/.gitted/HEAD b/tests/resources/revert/.gitted/HEAD
new file mode 100644
index 0000000..cb43805
--- /dev/null
+++ b/tests/resources/revert/.gitted/HEAD
@@ -0,0 +1 @@
+ref: refs/heads/master
diff --git a/tests/resources/revert/.gitted/config b/tests/resources/revert/.gitted/config
new file mode 100644
index 0000000..454e576
--- /dev/null
+++ b/tests/resources/revert/.gitted/config
@@ -0,0 +1,8 @@
+[core]
+	repositoryformatversion = 0
+	filemode = false
+	bare = false
+	logallrefupdates = true
+	symlinks = false
+	ignorecase = true
+	hideDotFiles = dotGitOnly
diff --git a/tests/resources/revert/.gitted/index b/tests/resources/revert/.gitted/index
new file mode 100644
index 0000000..87419ff
Binary files /dev/null and b/tests/resources/revert/.gitted/index differ
diff --git a/tests/resources/revert/.gitted/info/exclude b/tests/resources/revert/.gitted/info/exclude
new file mode 100644
index 0000000..f006809
--- /dev/null
+++ b/tests/resources/revert/.gitted/info/exclude
@@ -0,0 +1,6 @@
+# git ls-files --others --exclude-from=.git/info/exclude
+# Lines that start with '#' are comments.
+# For a project mostly in C, the following would be a good set of
+# exclude patterns (uncomment them if you want to use them):
+# *.[oa]
+# *~
diff --git a/tests/resources/revert/.gitted/objects/0a/a8c7e40d342fff78d60b29a4ba8e993ed79c51 b/tests/resources/revert/.gitted/objects/0a/a8c7e40d342fff78d60b29a4ba8e993ed79c51
new file mode 100644
index 0000000..5c811ef
--- /dev/null
+++ b/tests/resources/revert/.gitted/objects/0a/a8c7e40d342fff78d60b29a4ba8e993ed79c51
@@ -0,0 +1,2 @@
+xK
+0@]s%4?q
@2Ѐ@3{{$itf@L91OIKcdrYXcsW>R]ʸ	Gଭx	U:jz/?
\ No newline at end of file
diff --git a/tests/resources/revert/.gitted/objects/0a/b09ea6d4c3634bdf6c221626d8b6f7dd890767 b/tests/resources/revert/.gitted/objects/0a/b09ea6d4c3634bdf6c221626d8b6f7dd890767
new file mode 100644
index 0000000..c050e5a
Binary files /dev/null and b/tests/resources/revert/.gitted/objects/0a/b09ea6d4c3634bdf6c221626d8b6f7dd890767 differ
diff --git a/tests/resources/revert/.gitted/objects/0c/db66192ee192f70f891f05a47636057420e871 b/tests/resources/revert/.gitted/objects/0c/db66192ee192f70f891f05a47636057420e871
new file mode 100644
index 0000000..31c107f
Binary files /dev/null and b/tests/resources/revert/.gitted/objects/0c/db66192ee192f70f891f05a47636057420e871 differ
diff --git a/tests/resources/revert/.gitted/objects/0f/5bfcf58c558d865da6be0281d7795993646cee b/tests/resources/revert/.gitted/objects/0f/5bfcf58c558d865da6be0281d7795993646cee
new file mode 100644
index 0000000..fb4fc76
Binary files /dev/null and b/tests/resources/revert/.gitted/objects/0f/5bfcf58c558d865da6be0281d7795993646cee differ
diff --git a/tests/resources/revert/.gitted/objects/13/ee9cd5d8e1023c218e0e1ea684ec0c582b5050 b/tests/resources/revert/.gitted/objects/13/ee9cd5d8e1023c218e0e1ea684ec0c582b5050
new file mode 100644
index 0000000..aed4647
Binary files /dev/null and b/tests/resources/revert/.gitted/objects/13/ee9cd5d8e1023c218e0e1ea684ec0c582b5050 differ
diff --git a/tests/resources/revert/.gitted/objects/15/6ef9bcb968dccec8472a0f2eff49f1a713bc6b b/tests/resources/revert/.gitted/objects/15/6ef9bcb968dccec8472a0f2eff49f1a713bc6b
new file mode 100644
index 0000000..3ee2a18
Binary files /dev/null and b/tests/resources/revert/.gitted/objects/15/6ef9bcb968dccec8472a0f2eff49f1a713bc6b differ
diff --git a/tests/resources/revert/.gitted/objects/18/1aab27ddb37b40d9a284fb4733497006d57091 b/tests/resources/revert/.gitted/objects/18/1aab27ddb37b40d9a284fb4733497006d57091
new file mode 100644
index 0000000..6b422b8
Binary files /dev/null and b/tests/resources/revert/.gitted/objects/18/1aab27ddb37b40d9a284fb4733497006d57091 differ
diff --git a/tests/resources/revert/.gitted/objects/1f/a4e069a641f10f5fb7588138b2d147fcd22c36 b/tests/resources/revert/.gitted/objects/1f/a4e069a641f10f5fb7588138b2d147fcd22c36
new file mode 100644
index 0000000..3df134a
Binary files /dev/null and b/tests/resources/revert/.gitted/objects/1f/a4e069a641f10f5fb7588138b2d147fcd22c36 differ
diff --git a/tests/resources/revert/.gitted/objects/29/6a6d3be1dff05c5d1f631d2459389fa7b619eb b/tests/resources/revert/.gitted/objects/29/6a6d3be1dff05c5d1f631d2459389fa7b619eb
new file mode 100644
index 0000000..5555268
Binary files /dev/null and b/tests/resources/revert/.gitted/objects/29/6a6d3be1dff05c5d1f631d2459389fa7b619eb differ
diff --git a/tests/resources/revert/.gitted/objects/2d/440f2b3147d3dc7ad1085813478d6d869d5a4d b/tests/resources/revert/.gitted/objects/2d/440f2b3147d3dc7ad1085813478d6d869d5a4d
new file mode 100644
index 0000000..c06cc94
--- /dev/null
+++ b/tests/resources/revert/.gitted/objects/2d/440f2b3147d3dc7ad1085813478d6d869d5a4d
@@ -0,0 +1,2 @@
+xK
+1D]}%I3"nt:΀I$3)W(9oqޘaaQ9Ryf"]JvYmиtN92%\F!/DHƊkmp"ךZ̃5o^S?QP8Y~J)G;<P/lP
\ No newline at end of file
diff --git a/tests/resources/revert/.gitted/objects/33/c6fd981c49a2abf2971482089350bfc5cda8ea b/tests/resources/revert/.gitted/objects/33/c6fd981c49a2abf2971482089350bfc5cda8ea
new file mode 100644
index 0000000..683f27f
Binary files /dev/null and b/tests/resources/revert/.gitted/objects/33/c6fd981c49a2abf2971482089350bfc5cda8ea differ
diff --git a/tests/resources/revert/.gitted/objects/39/467716290f6df775a91cdb9a4eb39295018145 b/tests/resources/revert/.gitted/objects/39/467716290f6df775a91cdb9a4eb39295018145
new file mode 100644
index 0000000..7c1e01d
Binary files /dev/null and b/tests/resources/revert/.gitted/objects/39/467716290f6df775a91cdb9a4eb39295018145 differ
diff --git a/tests/resources/revert/.gitted/objects/3a/3ef367eaf3fe79effbfb0a56b269c04c2b59fe b/tests/resources/revert/.gitted/objects/3a/3ef367eaf3fe79effbfb0a56b269c04c2b59fe
new file mode 100644
index 0000000..b83806e
Binary files /dev/null and b/tests/resources/revert/.gitted/objects/3a/3ef367eaf3fe79effbfb0a56b269c04c2b59fe differ
diff --git a/tests/resources/revert/.gitted/objects/4b/8fcff56437e60f58e9a6bc630dd242ebf6ea2c b/tests/resources/revert/.gitted/objects/4b/8fcff56437e60f58e9a6bc630dd242ebf6ea2c
new file mode 100644
index 0000000..c0bd3db
Binary files /dev/null and b/tests/resources/revert/.gitted/objects/4b/8fcff56437e60f58e9a6bc630dd242ebf6ea2c differ
diff --git a/tests/resources/revert/.gitted/objects/52/c95c4264245469a0617e289a7d737f156826b4 b/tests/resources/revert/.gitted/objects/52/c95c4264245469a0617e289a7d737f156826b4
new file mode 100644
index 0000000..7cc9e96
--- /dev/null
+++ b/tests/resources/revert/.gitted/objects/52/c95c4264245469a0617e289a7d737f156826b4
@@ -0,0 +1,2 @@
+x=N1s
+kmU~	!J8IP&2@jk+7fC^'FF34{dCN")JGh\:@ІQf^E`vEKmCp]jj'}{+T3(sqcpNJ1Q?Nw0Phc#Wؽeݠrnk	ZB)Cd	QM4ńWg,k鷺(ہ
\ No newline at end of file
diff --git a/tests/resources/revert/.gitted/objects/55/568c8de5322ff9a95d72747a239cdb64a19965 b/tests/resources/revert/.gitted/objects/55/568c8de5322ff9a95d72747a239cdb64a19965
new file mode 100644
index 0000000..4dae50b
--- /dev/null
+++ b/tests/resources/revert/.gitted/objects/55/568c8de5322ff9a95d72747a239cdb64a19965
@@ -0,0 +1 @@
+xK!]s
x莓x}^jru`ݍZ1.ExYQ۸]\vQOD1vX3"bYp5JS\B^+z[p)V_Gu˽CnS
#Ig?.,ʰYe/ZOH
\ No newline at end of file
diff --git a/tests/resources/revert/.gitted/objects/55/acf326a69f0aab7a974ec53ffa55a50bcac14e b/tests/resources/revert/.gitted/objects/55/acf326a69f0aab7a974ec53ffa55a50bcac14e
new file mode 100644
index 0000000..e9cdfc5
Binary files /dev/null and b/tests/resources/revert/.gitted/objects/55/acf326a69f0aab7a974ec53ffa55a50bcac14e differ
diff --git a/tests/resources/revert/.gitted/objects/5a/cdc74af27172ec491d213ee36cea7eb9ef2579 b/tests/resources/revert/.gitted/objects/5a/cdc74af27172ec491d213ee36cea7eb9ef2579
new file mode 100644
index 0000000..61055ea
--- /dev/null
+++ b/tests/resources/revert/.gitted/objects/5a/cdc74af27172ec491d213ee36cea7eb9ef2579
@@ -0,0 +1,3 @@
+x=N1s
+w[-8?B4t\ ?3E&L''=?9	if6TgDhI޳4=*<>zRAq:VYmQK
+pAП5sȅgJgU"IR'[og?oomϣ+fuťXt=6'/47:yL:c
\ No newline at end of file
diff --git a/tests/resources/revert/.gitted/objects/72/333f47d4e83616630ff3b0ffe4c0faebcc3c45 b/tests/resources/revert/.gitted/objects/72/333f47d4e83616630ff3b0ffe4c0faebcc3c45
new file mode 100644
index 0000000..1f69787
Binary files /dev/null and b/tests/resources/revert/.gitted/objects/72/333f47d4e83616630ff3b0ffe4c0faebcc3c45 differ
diff --git a/tests/resources/revert/.gitted/objects/73/ec36fa120f8066963a0bc9105bb273dbd903d7 b/tests/resources/revert/.gitted/objects/73/ec36fa120f8066963a0bc9105bb273dbd903d7
new file mode 100644
index 0000000..3c8d2c2
Binary files /dev/null and b/tests/resources/revert/.gitted/objects/73/ec36fa120f8066963a0bc9105bb273dbd903d7 differ
diff --git a/tests/resources/revert/.gitted/objects/74/7726e021bc5f44b86de60e3032fd6f9f1b8383 b/tests/resources/revert/.gitted/objects/74/7726e021bc5f44b86de60e3032fd6f9f1b8383
new file mode 100644
index 0000000..e4d1477
Binary files /dev/null and b/tests/resources/revert/.gitted/objects/74/7726e021bc5f44b86de60e3032fd6f9f1b8383 differ
diff --git a/tests/resources/revert/.gitted/objects/77/31926a337c4eaba1e2187d90ebfa0a93659382 b/tests/resources/revert/.gitted/objects/77/31926a337c4eaba1e2187d90ebfa0a93659382
new file mode 100644
index 0000000..b87fa15
Binary files /dev/null and b/tests/resources/revert/.gitted/objects/77/31926a337c4eaba1e2187d90ebfa0a93659382 differ
diff --git a/tests/resources/revert/.gitted/objects/83/f65df4606c4f8dbf8da43de25de1b7e4c03238 b/tests/resources/revert/.gitted/objects/83/f65df4606c4f8dbf8da43de25de1b7e4c03238
new file mode 100644
index 0000000..152ed5f
Binary files /dev/null and b/tests/resources/revert/.gitted/objects/83/f65df4606c4f8dbf8da43de25de1b7e4c03238 differ
diff --git a/tests/resources/revert/.gitted/objects/8f/d40e13fff575b63e86af87175e70fa7fb92f80 b/tests/resources/revert/.gitted/objects/8f/d40e13fff575b63e86af87175e70fa7fb92f80
new file mode 100644
index 0000000..9ff8617
Binary files /dev/null and b/tests/resources/revert/.gitted/objects/8f/d40e13fff575b63e86af87175e70fa7fb92f80 differ
diff --git a/tests/resources/revert/.gitted/objects/97/f3574e92f1730d365fb9e00c10e3c507c1cfe9 b/tests/resources/revert/.gitted/objects/97/f3574e92f1730d365fb9e00c10e3c507c1cfe9
new file mode 100644
index 0000000..12ebc58
Binary files /dev/null and b/tests/resources/revert/.gitted/objects/97/f3574e92f1730d365fb9e00c10e3c507c1cfe9 differ
diff --git a/tests/resources/revert/.gitted/objects/a6/9f74efcb51634b88e04ea81273158a85257f41 b/tests/resources/revert/.gitted/objects/a6/9f74efcb51634b88e04ea81273158a85257f41
new file mode 100644
index 0000000..b96831f
Binary files /dev/null and b/tests/resources/revert/.gitted/objects/a6/9f74efcb51634b88e04ea81273158a85257f41 differ
diff --git a/tests/resources/revert/.gitted/objects/b7/a55408832174c54708906a372a9be2ffe3649b b/tests/resources/revert/.gitted/objects/b7/a55408832174c54708906a372a9be2ffe3649b
new file mode 100644
index 0000000..77d4e20
Binary files /dev/null and b/tests/resources/revert/.gitted/objects/b7/a55408832174c54708906a372a9be2ffe3649b differ
diff --git a/tests/resources/revert/.gitted/objects/be/ead165e017269e8dc0dd6f01195726a2e1e01b b/tests/resources/revert/.gitted/objects/be/ead165e017269e8dc0dd6f01195726a2e1e01b
new file mode 100644
index 0000000..57de3f2
Binary files /dev/null and b/tests/resources/revert/.gitted/objects/be/ead165e017269e8dc0dd6f01195726a2e1e01b differ
diff --git a/tests/resources/revert/.gitted/objects/ce/f56612d71a6af8d8015691e4865f7fece905b5 b/tests/resources/revert/.gitted/objects/ce/f56612d71a6af8d8015691e4865f7fece905b5
new file mode 100644
index 0000000..5645885
Binary files /dev/null and b/tests/resources/revert/.gitted/objects/ce/f56612d71a6af8d8015691e4865f7fece905b5 differ
diff --git a/tests/resources/revert/.gitted/objects/d1/d403d22cbe24592d725f442835cf46fe60c8ac b/tests/resources/revert/.gitted/objects/d1/d403d22cbe24592d725f442835cf46fe60c8ac
new file mode 100644
index 0000000..2190bb4
Binary files /dev/null and b/tests/resources/revert/.gitted/objects/d1/d403d22cbe24592d725f442835cf46fe60c8ac differ
diff --git a/tests/resources/revert/.gitted/objects/dd/9a159c89509e73fd37d6af99619994cf7dfc06 b/tests/resources/revert/.gitted/objects/dd/9a159c89509e73fd37d6af99619994cf7dfc06
new file mode 100644
index 0000000..ed80d0a
Binary files /dev/null and b/tests/resources/revert/.gitted/objects/dd/9a159c89509e73fd37d6af99619994cf7dfc06 differ
diff --git a/tests/resources/revert/.gitted/objects/eb/b03002cee5d66c7732dd06241119fe72ab96a5 b/tests/resources/revert/.gitted/objects/eb/b03002cee5d66c7732dd06241119fe72ab96a5
new file mode 100644
index 0000000..802125e
--- /dev/null
+++ b/tests/resources/revert/.gitted/objects/eb/b03002cee5d66c7732dd06241119fe72ab96a5
@@ -0,0 +1,2 @@
+x	1E=i@I6&Aċ@6;dGlXwHkUa<h'lyf)̘\
+,eb&uU.\iߺZz*n<#m;J\Ue8[>`
\ No newline at end of file
diff --git a/tests/resources/revert/.gitted/objects/f4/e107c230d08a60fb419d19869f1f282b272d9c b/tests/resources/revert/.gitted/objects/f4/e107c230d08a60fb419d19869f1f282b272d9c
new file mode 100644
index 0000000..029da1b
Binary files /dev/null and b/tests/resources/revert/.gitted/objects/f4/e107c230d08a60fb419d19869f1f282b272d9c differ
diff --git a/tests/resources/revert/.gitted/refs/heads/master b/tests/resources/revert/.gitted/refs/heads/master
new file mode 100644
index 0000000..d3850da
--- /dev/null
+++ b/tests/resources/revert/.gitted/refs/heads/master
@@ -0,0 +1 @@
+2d440f2b3147d3dc7ad1085813478d6d869d5a4d
diff --git a/tests/resources/revert/.gitted/refs/heads/merges b/tests/resources/revert/.gitted/refs/heads/merges
new file mode 100644
index 0000000..6533a94
--- /dev/null
+++ b/tests/resources/revert/.gitted/refs/heads/merges
@@ -0,0 +1 @@
+5acdc74af27172ec491d213ee36cea7eb9ef2579
diff --git a/tests/resources/revert/.gitted/refs/heads/merges-branch b/tests/resources/revert/.gitted/refs/heads/merges-branch
new file mode 100644
index 0000000..febb29c
--- /dev/null
+++ b/tests/resources/revert/.gitted/refs/heads/merges-branch
@@ -0,0 +1 @@
+13ee9cd5d8e1023c218e0e1ea684ec0c582b5050
diff --git a/tests/resources/revert/.gitted/refs/heads/reverted-branch b/tests/resources/revert/.gitted/refs/heads/reverted-branch
new file mode 100644
index 0000000..16bb7a2
--- /dev/null
+++ b/tests/resources/revert/.gitted/refs/heads/reverted-branch
@@ -0,0 +1 @@
+52c95c4264245469a0617e289a7d737f156826b4
diff --git a/tests/resources/revert/file1.txt b/tests/resources/revert/file1.txt
new file mode 100644
index 0000000..84b2259
--- /dev/null
+++ b/tests/resources/revert/file1.txt
@@ -0,0 +1,14 @@
+!File one!
+!File one!
+File one!
+File one
+File one
+File one
+File one
+File one
+File one
+File one
+File one!
+!File one!
+!File one!
+!File one!
diff --git a/tests/resources/revert/file2.txt b/tests/resources/revert/file2.txt
new file mode 100644
index 0000000..acb5747
--- /dev/null
+++ b/tests/resources/revert/file2.txt
@@ -0,0 +1,16 @@
+File two
+File two
+File two
+File two
+File two
+File two
+File two
+File two
+File two
+File two
+File two
+File two
+File two
+File two
+File two
+File two
diff --git a/tests/resources/revert/file3.txt b/tests/resources/revert/file3.txt
new file mode 100644
index 0000000..b033059
--- /dev/null
+++ b/tests/resources/revert/file3.txt
@@ -0,0 +1,16 @@
+File three
+File three
+File three
+File three
+File three
+File three
+File three
+File three
+File three
+File three
+File three
+File three
+File three
+File three
+File three
+File three
diff --git a/tests/resources/revert/file6.txt b/tests/resources/revert/file6.txt
new file mode 100644
index 0000000..5c0cd5d
--- /dev/null
+++ b/tests/resources/revert/file6.txt
@@ -0,0 +1,14 @@
+File six, actually!
+File four!
+File four!
+File four!
+File four!
+File four!
+File four!
+File four!
+File four!
+File four!
+File four!
+File four!
+File four!
+File four!
diff --git a/tests/revert/revert.c b/tests/revert/revert.c
new file mode 100644
index 0000000..4a45190
--- /dev/null
+++ b/tests/revert/revert.c
@@ -0,0 +1,364 @@
+#include "clar.h"
+#include "clar_libgit2.h"
+
+#include "buffer.h"
+#include "fileops.h"
+#include "git2/revert.h"
+
+#include "../merge/merge_helpers.h"
+
+#define TEST_REPO_PATH "revert"
+
+static git_repository *repo;
+static git_index *repo_index;
+
+// Fixture setup and teardown
+void test_revert_revert__initialize(void)
+{
+	repo = cl_git_sandbox_init(TEST_REPO_PATH);
+	git_repository_index(&repo_index, repo);
+}
+
+void test_revert_revert__cleanup(void)
+{
+	git_index_free(repo_index);
+	cl_git_sandbox_cleanup();
+}
+
+/* git reset --hard 72333f47d4e83616630ff3b0ffe4c0faebcc3c45
+ * git revert --no-commit d1d403d22cbe24592d725f442835cf46fe60c8ac */
+void test_revert_revert__automerge(void)
+{
+	git_commit *head, *commit;
+	git_oid head_oid, revert_oid;
+
+	struct merge_index_entry merge_index_entries[] = {
+		{ 0100644, "caf99de3a49827117bb66721010eac461b06a80c", 0, "file1.txt" },
+		{ 0100644, "0ab09ea6d4c3634bdf6c221626d8b6f7dd890767", 0, "file2.txt" },
+		{ 0100644, "f4e107c230d08a60fb419d19869f1f282b272d9c", 0, "file3.txt" },
+		{ 0100644, "0f5bfcf58c558d865da6be0281d7795993646cee", 0, "file6.txt" },
+	};
+
+	git_oid_fromstr(&head_oid, "72333f47d4e83616630ff3b0ffe4c0faebcc3c45");
+	cl_git_pass(git_commit_lookup(&head, repo, &head_oid));
+	cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD));
+
+	git_oid_fromstr(&revert_oid, "d1d403d22cbe24592d725f442835cf46fe60c8ac");
+	cl_git_pass(git_commit_lookup(&commit, repo, &revert_oid));
+	cl_git_pass(git_revert(repo, commit, NULL));
+
+	cl_assert(merge_test_index(repo_index, merge_index_entries, 4));
+
+	git_commit_free(commit);
+	git_commit_free(head);
+}
+
+/* git revert --no-commit 72333f47d4e83616630ff3b0ffe4c0faebcc3c45 */
+void test_revert_revert__conflicts(void)
+{
+	git_reference *head_ref;
+	git_commit *head, *commit;
+	git_oid revert_oid;
+	git_buf conflicting_buf = GIT_BUF_INIT;
+
+	struct merge_index_entry merge_index_entries[] = {
+		{ 0100644, "7731926a337c4eaba1e2187d90ebfa0a93659382", 1, "file1.txt" },
+		{ 0100644, "4b8fcff56437e60f58e9a6bc630dd242ebf6ea2c", 2, "file1.txt" },
+		{ 0100644, "3a3ef367eaf3fe79effbfb0a56b269c04c2b59fe", 3, "file1.txt" },
+		{ 0100644, "0ab09ea6d4c3634bdf6c221626d8b6f7dd890767", 0, "file2.txt" },
+		{ 0100644, "f4e107c230d08a60fb419d19869f1f282b272d9c", 0, "file3.txt" },
+		{ 0100644, "0f5bfcf58c558d865da6be0281d7795993646cee", 0, "file6.txt" },
+	};
+
+	git_oid_fromstr(&revert_oid, "72333f47d4e83616630ff3b0ffe4c0faebcc3c45");
+
+	cl_git_pass(git_repository_head(&head_ref, repo));
+	cl_git_pass(git_reference_peel((git_object **)&head, head_ref, GIT_OBJ_COMMIT));
+	cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD));
+
+	cl_git_pass(git_commit_lookup(&commit, repo, &revert_oid));
+	cl_git_pass(git_revert(repo, commit, NULL));
+
+	cl_assert(merge_test_index(repo_index, merge_index_entries, 6));
+
+	cl_git_pass(git_futils_readbuffer(&conflicting_buf,
+		TEST_REPO_PATH "/file1.txt"));
+	cl_assert(strcmp(conflicting_buf.ptr, "!File one!\n" \
+		"!File one!\n" \
+		"File one!\n" \
+		"File one\n" \
+		"File one\n" \
+		"File one\n" \
+		"File one\n" \
+		"File one\n" \
+		"File one\n" \
+		"File one\n" \
+		"<<<<<<< HEAD\n" \
+		"File one!\n" \
+		"!File one!\n" \
+		"!File one!\n" \
+		"!File one!\n" \
+		"=======\n" \
+		"File one\n" \
+		"File one\n" \
+		"File one\n" \
+		"File one\n" \
+		">>>>>>> parent of 72333f4... automergeable changes\n") == 0);
+
+	git_commit_free(commit);
+	git_commit_free(head);
+	git_reference_free(head_ref);
+	git_buf_free(&conflicting_buf);
+}
+
+/* git reset --hard 39467716290f6df775a91cdb9a4eb39295018145
+ * git revert --no-commit ebb03002cee5d66c7732dd06241119fe72ab96a5
+*/
+void test_revert_revert__orphan(void)
+{
+	git_commit *head, *commit;
+	git_oid head_oid, revert_oid;
+
+	struct merge_index_entry merge_index_entries[] = {
+		{ 0100644, "296a6d3be1dff05c5d1f631d2459389fa7b619eb", 0, "file-mainline.txt" },
+	};
+
+	git_oid_fromstr(&head_oid, "39467716290f6df775a91cdb9a4eb39295018145");
+	cl_git_pass(git_commit_lookup(&head, repo, &head_oid));
+	cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD));
+
+	git_oid_fromstr(&revert_oid, "ebb03002cee5d66c7732dd06241119fe72ab96a5");
+	cl_git_pass(git_commit_lookup(&commit, repo, &revert_oid));
+	cl_git_pass(git_revert(repo, commit, NULL));
+
+	cl_assert(merge_test_index(repo_index, merge_index_entries, 1));
+
+	git_commit_free(commit);
+	git_commit_free(head);
+}
+
+/* git reset --hard 72333f47d4e83616630ff3b0ffe4c0faebcc3c45
+ * git revert --no-commit d1d403d22cbe24592d725f442835cf46fe60c8ac */
+void test_revert_revert__conflict_use_ours(void)
+{
+	git_commit *head, *commit;
+	git_oid head_oid, revert_oid;
+	git_revert_opts opts = GIT_REVERT_OPTS_INIT;
+
+	opts.merge_tree_opts.automerge_flags = GIT_MERGE_AUTOMERGE_NONE;
+	opts.checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_USE_OURS;
+
+	struct merge_index_entry merge_index_entries[] = {
+		{ 0100644, "3a3ef367eaf3fe79effbfb0a56b269c04c2b59fe", 1, "file1.txt" },
+		{ 0100644, "7731926a337c4eaba1e2187d90ebfa0a93659382", 2, "file1.txt" },
+		{ 0100644, "747726e021bc5f44b86de60e3032fd6f9f1b8383", 3, "file1.txt" },
+		{ 0100644, "0ab09ea6d4c3634bdf6c221626d8b6f7dd890767", 0, "file2.txt" },
+		{ 0100644, "f4e107c230d08a60fb419d19869f1f282b272d9c", 0, "file3.txt" },
+		{ 0100644, "0f5bfcf58c558d865da6be0281d7795993646cee", 0, "file6.txt" },
+	};
+
+	struct merge_index_entry merge_filesystem_entries[] = {
+		{ 0100644, "7731926a337c4eaba1e2187d90ebfa0a93659382", 0, "file1.txt" },
+		{ 0100644, "0ab09ea6d4c3634bdf6c221626d8b6f7dd890767", 0, "file2.txt" },
+		{ 0100644, "f4e107c230d08a60fb419d19869f1f282b272d9c", 0, "file3.txt" },
+		{ 0100644, "0f5bfcf58c558d865da6be0281d7795993646cee", 0, "file6.txt" },
+	};
+
+	git_oid_fromstr(&head_oid, "72333f47d4e83616630ff3b0ffe4c0faebcc3c45");
+	cl_git_pass(git_commit_lookup(&head, repo, &head_oid));
+	cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD));
+
+	git_oid_fromstr(&revert_oid, "d1d403d22cbe24592d725f442835cf46fe60c8ac");
+	cl_git_pass(git_commit_lookup(&commit, repo, &revert_oid));
+	cl_git_pass(git_revert(repo, commit, &opts));
+
+	cl_assert(merge_test_index(repo_index, merge_index_entries, 6));
+	cl_assert(merge_test_workdir(repo, merge_filesystem_entries, 4));
+
+	git_commit_free(commit);
+	git_commit_free(head);
+}
+
+/* git reset --hard cef56612d71a6af8d8015691e4865f7fece905b5
+ * git revert --no-commit 55568c8de5322ff9a95d72747a239cdb64a19965
+ */
+void test_revert_revert__rename_1_of_2(void)
+{
+	git_commit *head, *commit;
+	git_oid head_oid, revert_oid;
+	git_revert_opts opts = GIT_REVERT_OPTS_INIT;
+
+	opts.merge_tree_opts.flags |= GIT_MERGE_TREE_FIND_RENAMES;
+	opts.merge_tree_opts.rename_threshold = 50;
+
+	struct merge_index_entry merge_index_entries[] = {
+		{ 0100644, "747726e021bc5f44b86de60e3032fd6f9f1b8383", 0, "file1.txt" },
+		{ 0100644, "0ab09ea6d4c3634bdf6c221626d8b6f7dd890767", 0, "file2.txt" },
+		{ 0100644, "f4e107c230d08a60fb419d19869f1f282b272d9c", 0, "file3.txt" },
+		{ 0100644, "55acf326a69f0aab7a974ec53ffa55a50bcac14e", 3, "file4.txt" },
+		{ 0100644, "55acf326a69f0aab7a974ec53ffa55a50bcac14e", 1, "file5.txt" },
+		{ 0100644, "0f5bfcf58c558d865da6be0281d7795993646cee", 2, "file6.txt" },
+	};
+
+	git_oid_fromstr(&head_oid, "cef56612d71a6af8d8015691e4865f7fece905b5");
+	cl_git_pass(git_commit_lookup(&head, repo, &head_oid));
+	cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD));
+
+	git_oid_fromstr(&revert_oid, "55568c8de5322ff9a95d72747a239cdb64a19965");
+	cl_git_pass(git_commit_lookup(&commit, repo, &revert_oid));
+	cl_git_pass(git_revert(repo, commit, &opts));
+
+	cl_assert(merge_test_index(repo_index, merge_index_entries, 6));
+
+	git_commit_free(commit);
+	git_commit_free(head);
+}
+
+/* git reset --hard 55568c8de5322ff9a95d72747a239cdb64a19965
+ * git revert --no-commit HEAD~1 */
+void test_revert_revert__rename(void)
+{
+	git_commit *head, *commit;
+	git_oid head_oid, revert_oid;
+	git_revert_opts opts = GIT_REVERT_OPTS_INIT;
+
+	opts.merge_tree_opts.flags |= GIT_MERGE_TREE_FIND_RENAMES;
+	opts.merge_tree_opts.rename_threshold = 50;
+
+	struct merge_index_entry merge_index_entries[] = {
+		{ 0100644, "55acf326a69f0aab7a974ec53ffa55a50bcac14e", 1, "file4.txt" },
+		{ 0100644, "55acf326a69f0aab7a974ec53ffa55a50bcac14e", 2, "file5.txt" },
+	};
+
+	struct merge_name_entry merge_name_entries[] = {
+		{ "file4.txt", "file5.txt", "" },
+	};
+
+	git_oid_fromstr(&head_oid, "55568c8de5322ff9a95d72747a239cdb64a19965");
+	cl_git_pass(git_commit_lookup(&head, repo, &head_oid));
+	cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD));
+
+	git_oid_fromstr(&revert_oid, "0aa8c7e40d342fff78d60b29a4ba8e993ed79c51");
+	cl_git_pass(git_commit_lookup(&commit, repo, &revert_oid));
+	cl_git_pass(git_revert(repo, commit, &opts));
+
+	cl_assert(merge_test_index(repo_index, merge_index_entries, 2));
+	cl_assert(merge_test_names(repo_index, merge_name_entries, 1));
+
+	git_commit_free(commit);
+	git_commit_free(head);
+}
+
+/* git revert --no-commit HEAD */
+void test_revert_revert__head(void)
+{
+	git_reference *head;
+	git_commit *commit;
+
+	struct merge_index_entry merge_index_entries[] = {
+		{ 0100644, "7731926a337c4eaba1e2187d90ebfa0a93659382", 0, "file1.txt" },
+		{ 0100644, "0ab09ea6d4c3634bdf6c221626d8b6f7dd890767", 0, "file2.txt" },
+		{ 0100644, "f4e107c230d08a60fb419d19869f1f282b272d9c", 0, "file3.txt" },
+		{ 0100644, "0f5bfcf58c558d865da6be0281d7795993646cee", 0, "file6.txt" },
+	};
+
+	/* HEAD is 2d440f2b3147d3dc7ad1085813478d6d869d5a4d */
+	cl_git_pass(git_repository_head(&head, repo));
+	cl_git_pass(git_reference_peel((git_object **)&commit, head, GIT_OBJ_COMMIT));
+	cl_git_pass(git_reset(repo, (git_object *)commit, GIT_RESET_HARD));
+	cl_git_pass(git_revert(repo, commit, NULL));
+
+	cl_assert(merge_test_index(repo_index, merge_index_entries, 4));
+	cl_assert(merge_test_workdir(repo, merge_index_entries, 4));
+
+	git_reference_free(head);
+	git_commit_free(commit);
+}
+
+void test_revert_revert__nonmerge_fails_mainline_specified(void)
+{
+	git_reference *head;
+	git_commit *commit;
+	git_revert_opts opts = GIT_REVERT_OPTS_INIT;
+
+	cl_git_pass(git_repository_head(&head, repo));
+	cl_git_pass(git_reference_peel((git_object **)&commit, head, GIT_OBJ_COMMIT));
+
+	opts.mainline = 1;
+	cl_must_fail(git_revert(repo, commit, &opts));
+	cl_assert(!git_path_exists(TEST_REPO_PATH "/.git/MERGE_MSG"));
+	cl_assert(!git_path_exists(TEST_REPO_PATH "/.git/REVERT_HEAD"));
+
+	git_reference_free(head);
+	git_commit_free(commit);
+}
+
+/* git reset --hard 5acdc74af27172ec491d213ee36cea7eb9ef2579
+ * git revert HEAD */
+void test_revert_revert__merge_fails_without_mainline_specified(void)
+{
+	git_commit *head;
+	git_oid head_oid;
+
+	git_oid_fromstr(&head_oid, "5acdc74af27172ec491d213ee36cea7eb9ef2579");
+	cl_git_pass(git_commit_lookup(&head, repo, &head_oid));
+	cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD));
+
+	cl_must_fail(git_revert(repo, head, NULL));
+	cl_assert(!git_path_exists(TEST_REPO_PATH "/.git/MERGE_MSG"));
+	cl_assert(!git_path_exists(TEST_REPO_PATH "/.git/REVERT_HEAD"));
+
+	git_commit_free(head);
+}
+
+/* git reset --hard 5acdc74af27172ec491d213ee36cea7eb9ef2579
+ * git revert HEAD -m1 --no-commit */
+void test_revert_revert__merge_first_parent(void)
+{
+	git_commit *head;
+	git_oid head_oid;
+	git_revert_opts opts = GIT_REVERT_OPTS_INIT;
+
+	opts.mainline = 1;
+
+	struct merge_index_entry merge_index_entries[] = {
+		{ 0100644, "296a6d3be1dff05c5d1f631d2459389fa7b619eb", 0, "file-mainline.txt" },
+		{ 0100644, "0cdb66192ee192f70f891f05a47636057420e871", 0, "file1.txt" },
+		{ 0100644, "73ec36fa120f8066963a0bc9105bb273dbd903d7", 0, "file2.txt" },
+	};
+
+	git_oid_fromstr(&head_oid, "5acdc74af27172ec491d213ee36cea7eb9ef2579");
+	cl_git_pass(git_commit_lookup(&head, repo, &head_oid));
+	cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD));
+
+	cl_git_pass(git_revert(repo, head, &opts));
+
+	cl_assert(merge_test_index(repo_index, merge_index_entries, 3));
+
+	git_commit_free(head);
+}
+
+void test_revert_revert__merge_second_parent(void)
+{
+	git_commit *head;
+	git_oid head_oid;
+	git_revert_opts opts = GIT_REVERT_OPTS_INIT;
+
+	opts.mainline = 2;
+
+	struct merge_index_entry merge_index_entries[] = {
+		{ 0100644, "33c6fd981c49a2abf2971482089350bfc5cda8ea", 0, "file-branch.txt" },
+		{ 0100644, "0cdb66192ee192f70f891f05a47636057420e871", 0, "file1.txt" },
+		{ 0100644, "73ec36fa120f8066963a0bc9105bb273dbd903d7", 0, "file2.txt" },
+	};
+
+	git_oid_fromstr(&head_oid, "5acdc74af27172ec491d213ee36cea7eb9ef2579");
+	cl_git_pass(git_commit_lookup(&head, repo, &head_oid));
+	cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD));
+
+	cl_git_pass(git_revert(repo, head, &opts));
+
+	cl_assert(merge_test_index(repo_index, merge_index_entries, 3));
+
+	git_commit_free(head);
+}