Commit 8a1479a55a4f8c0acc7a26fea7865b71fc28b54e

Vicent Martí 2012-10-29T12:57:04

Merge pull request #796 from nulltoken/topic/git-stash Stash

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
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
diff --git a/include/git2.h b/include/git2.h
index d555439..3bb2fce 100644
--- a/include/git2.h
+++ b/include/git2.h
@@ -52,5 +52,6 @@
 #include "git2/reset.h"
 #include "git2/message.h"
 #include "git2/pack.h"
+#include "git2/stash.h"
 
 #endif
diff --git a/include/git2/errors.h b/include/git2/errors.h
index fb56dcc..8bb47f3 100644
--- a/include/git2/errors.h
+++ b/include/git2/errors.h
@@ -59,6 +59,7 @@ typedef enum {
 	GITERR_SSL,
 	GITERR_SUBMODULE,
 	GITERR_THREAD,
+	GITERR_STASH,
 } git_error_t;
 
 /**
diff --git a/include/git2/index.h b/include/git2/index.h
index d8282e8..e8af362 100644
--- a/include/git2/index.h
+++ b/include/git2/index.h
@@ -347,6 +347,14 @@ GIT_EXTERN(int) git_index_entry_stage(const git_index_entry *entry);
  */
 GIT_EXTERN(int) git_index_read_tree(git_index *index, git_tree *tree);
 
+/**
+ * Get the repository this index relates to
+ *
+ * @param index The index
+ * @return A pointer to the repository
+ */
+GIT_EXTERN(git_repository *) git_index_owner(const git_index *index);
+
 /** @} */
 GIT_END_DECL
 #endif
diff --git a/include/git2/reflog.h b/include/git2/reflog.h
index 447915e..d48a897 100644
--- a/include/git2/reflog.h
+++ b/include/git2/reflog.h
@@ -97,9 +97,9 @@ GIT_EXTERN(const git_reflog_entry *) git_reflog_entry_byindex(git_reflog *reflog
 /**
  * Remove an entry from the reflog by its index
  *
- * To ensure there's no gap in the log history, set the `rewrite_previosu_entry` to 1.
- * When deleting entry `n`, member old_oid of entry `n-1` (if any) will be updated with
- * the value of memeber new_oid of entry `n+1`.
+ * To ensure there's no gap in the log history, set `rewrite_previous_entry`
+ * param value to 1. When deleting entry `n`, member old_oid of entry `n-1`
+ * (if any) will be updated with the value of member new_oid of entry `n+1`.
  *
  * @param reflog a previously loaded reflog.
  *
diff --git a/include/git2/stash.h b/include/git2/stash.h
new file mode 100644
index 0000000..3ecd9e8
--- /dev/null
+++ b/include/git2/stash.h
@@ -0,0 +1,122 @@
+/*
+ * 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_git_stash_h__
+#define INCLUDE_git_stash_h__
+
+#include "common.h"
+#include "types.h"
+
+/**
+ * @file git2/stash.h
+ * @brief Git stash management routines
+ * @ingroup Git
+ * @{
+ */
+GIT_BEGIN_DECL
+
+enum {
+	GIT_STASH_DEFAULT = 0,
+
+	/* All changes already added to the index
+	 * are left intact in the working directory
+	 */
+	GIT_STASH_KEEP_INDEX = (1 << 0),
+
+	/* All untracked files are also stashed and then
+	 * cleaned up from the working directory
+	 */
+	GIT_STASH_INCLUDE_UNTRACKED = (1 << 1),
+
+	/* All ignored files are also stashed and then
+	 * cleaned up from the working directory
+	 */
+	GIT_STASH_INCLUDE_IGNORED = (1 << 2),
+};
+
+/**
+ * Save the local modifications to a new stash.
+ *
+ * @param out Object id of the commit containing the stashed state.
+ * This commit is also the target of the direct reference refs/stash.
+ *
+ * @param repo The owning repository.
+ *
+ * @param stasher The identity of the person performing the stashing.
+ *
+ * @param message Optional description along with the stashed state.
+ *
+ * @param flags Flags to control the stashing process.
+ *
+ * @return 0 on success, GIT_ENOTFOUND where there's nothing to stash,
+ * or error code.
+ */
+
+GIT_EXTERN(int) git_stash_save(
+	git_oid *out,
+	git_repository *repo,
+	git_signature *stasher,
+	const char *message,
+	uint32_t flags);
+
+/**
+ * When iterating over all the stashed states, callback that will be
+ * issued per entry.
+ *
+ * @param index The position within the stash list. 0 points to the
+ * most recent stashed state.
+ *
+ * @param message The stash message.
+ *
+ * @param stash_oid The commit oid of the stashed state.
+ *
+ * @param payload Extra parameter to callback function.
+ *
+ * @return 0 on success, GIT_EUSER on non-zero callback, or error code
+ */
+typedef int (*stash_cb)(
+	size_t index,
+	const char* message,
+	const git_oid *stash_oid,
+	void *payload);
+
+/**
+ * Loop over all the stashed states and issue a callback for each one.
+ *
+ * If the callback returns a non-zero value, this will stop looping.
+ *
+ * @param repo Repository where to find the stash.
+ *
+ * @param callabck Callback to invoke per found stashed state. The most recent
+ * stash state will be enumerated first.
+ *
+ * @param payload Extra parameter to callback function.
+ *
+ * @return 0 on success, GIT_EUSER on non-zero callback, or error code
+ */
+GIT_EXTERN(int) git_stash_foreach(
+	git_repository *repo,
+	stash_cb callback,
+	void *payload);
+
+/**
+ * Remove a single stashed state from the stash list.
+ *
+ * @param repo The owning repository.
+ *
+ * @param index The position within the stash list. 0 points to the
+ * most recent stashed state.
+ *
+ * @return 0 on success, or error code
+ */
+
+GIT_EXTERN(int) git_stash_drop(
+	git_repository *repo,
+	size_t index);
+
+/** @} */
+GIT_END_DECL
+#endif
diff --git a/src/index.c b/src/index.c
index f92c48d..38e83d0 100644
--- a/src/index.c
+++ b/src/index.c
@@ -1071,3 +1071,8 @@ int git_index_read_tree(git_index *index, git_tree *tree)
 
 	return git_tree_walk(tree, read_tree_cb, GIT_TREEWALK_POST, index);
 }
+
+git_repository *git_index_owner(const git_index *index)
+{
+	return INDEX_OWNER(index);
+}
diff --git a/src/reflog.c b/src/reflog.c
index a1ea7a2..5d1465e 100644
--- a/src/reflog.c
+++ b/src/reflog.c
@@ -166,6 +166,9 @@ void git_reflog_free(git_reflog *reflog)
 	unsigned int i;
 	git_reflog_entry *entry;
 
+	if (reflog == NULL)
+		return;
+
 	for (i=0; i < reflog->entries.length; i++) {
 		entry = git_vector_get(&reflog->entries, i);
 
@@ -185,7 +188,10 @@ static int retrieve_reflog_path(git_buf *path, git_reference *ref)
 
 static int create_new_reflog_file(const char *filepath)
 {
-	int fd;
+	int fd, error;
+
+	if ((error = git_futils_mkpath2file(filepath, GIT_REFLOG_DIR_MODE)) < 0)
+		return error;
 
 	if ((fd = p_open(filepath,
 			O_WRONLY | O_CREAT | O_TRUNC,
@@ -463,26 +469,26 @@ int git_reflog_drop(
 	if (!rewrite_previous_entry)
 		return 0;
 
-	/* No need to rewrite anything when removing the first entry */
-	if (idx == 0)
+	/* No need to rewrite anything when removing the most recent entry */
+	if (idx == entrycount - 1)
 		return 0;
 
 	/* There are no more entries in the log */
 	if (entrycount == 1)
 		return 0;
 
-	entry = (git_reflog_entry *)git_reflog_entry_byindex(reflog, idx - 1);
+	entry = (git_reflog_entry *)git_reflog_entry_byindex(reflog, idx);
 
-	/* If the last entry has just been removed... */
-	if (idx == entrycount - 1) {
-		/* ...clear the oid_old member of the "new" last entry */
+	/* If the oldest entry has just been removed... */
+	if (idx == 0) {
+		/* ...clear the oid_old member of the "new" oldest entry */
 		if (git_oid_fromstr(&entry->oid_old, GIT_OID_HEX_ZERO) < 0)
 			return -1;
 		
 		return 0;
 	}
 
-	previous = (git_reflog_entry *)git_reflog_entry_byindex(reflog, idx);
+	previous = (git_reflog_entry *)git_reflog_entry_byindex(reflog, idx - 1);
 	git_oid_cpy(&entry->oid_old, &previous->oid_cur);
 
 	return 0;
diff --git a/src/refs.h b/src/refs.h
index 58e2fd5..7b6f961 100644
--- a/src/refs.h
+++ b/src/refs.h
@@ -35,6 +35,9 @@
 #define GIT_CHERRY_PICK_HEAD_FILE "CHERRY_PICK_HEAD"
 #define GIT_REFS_HEADS_MASTER_FILE GIT_REFS_HEADS_DIR "master"
 
+#define GIT_STASH_FILE "stash"
+#define GIT_REFS_STASH_FILE GIT_REFS_DIR GIT_STASH_FILE
+
 #define GIT_REFNAME_MAX 1024
 
 struct git_reference {
diff --git a/src/stash.c b/src/stash.c
new file mode 100644
index 0000000..3011f00
--- /dev/null
+++ b/src/stash.c
@@ -0,0 +1,659 @@
+/*
+ * 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.
+ */
+
+#include "common.h"
+#include "repository.h"
+#include "commit.h"
+#include "tree.h"
+#include "reflog.h"
+#include "git2/diff.h"
+#include "git2/stash.h"
+#include "git2/status.h"
+#include "git2/checkout.h"
+
+static int create_error(int error, const char *msg)
+{
+	giterr_set(GITERR_STASH, "Cannot stash changes - %s", 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);
+
+	if (error == GIT_EORPHANEDHEAD)
+		return create_error(error, "You do not have the initial commit yet.");
+
+	return error;
+}
+
+static int append_abbreviated_oid(git_buf *out, const git_oid *b_commit)
+{
+	char *formatted_oid;
+
+	formatted_oid = git_oid_allocfmt(b_commit);
+	GITERR_CHECK_ALLOC(formatted_oid);
+
+	git_buf_put(out, formatted_oid, 7);
+	git__free(formatted_oid);
+
+	return git_buf_oom(out) ? -1 : 0;
+}
+
+static int append_commit_description(git_buf *out, git_commit* commit)
+{
+	const char *message;
+	int pos = 0, len;
+
+	if (append_abbreviated_oid(out, git_commit_id(commit)) < 0)
+		return -1;
+
+	message = git_commit_message(commit);
+	len = strlen(message);
+
+	/* TODO: Replace with proper commit short message
+	 * when git_commit_message_short() is implemented.
+	 */
+	while (pos < len && message[pos] != '\n')
+		pos++;
+
+	git_buf_putc(out, ' ');
+	git_buf_put(out, message, pos);
+	git_buf_putc(out, '\n');
+
+	return git_buf_oom(out) ? -1 : 0;
+}
+
+static int retrieve_base_commit_and_message(
+	git_commit **b_commit,
+	git_buf *stash_message,
+	git_repository *repo)
+{
+	git_reference *head = NULL;
+	int error;
+
+	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): ");
+	else
+		git_buf_printf(
+			stash_message,
+			"%s: ",
+			git_reference_name(head) + strlen(GIT_REFS_HEADS_DIR));
+
+	if (git_commit_lookup(b_commit, repo, git_reference_oid(head)) < 0)
+		goto cleanup;
+
+	if (append_commit_description(stash_message, *b_commit) < 0)
+		goto cleanup;
+
+	error = 0;
+
+cleanup:
+	git_reference_free(head);
+	return error;
+}
+
+static int build_tree_from_index(git_tree **out, git_index *index)
+{
+	git_oid i_tree_oid;
+
+	if (git_tree_create_fromindex(&i_tree_oid, index) < 0)
+		return -1;
+
+	return git_tree_lookup(out, git_index_owner(index), &i_tree_oid);
+}
+
+static int commit_index(
+	git_commit **i_commit,
+	git_index *index,
+	git_signature *stasher,
+	const char *message,
+	const git_commit *parent)
+{
+	git_tree *i_tree = NULL;
+	git_oid i_commit_oid;
+	git_buf msg = GIT_BUF_INIT;
+	int error = -1;
+
+	if (build_tree_from_index(&i_tree, index) < 0)
+		goto cleanup;
+
+	if (git_buf_printf(&msg, "index on %s\n", message) < 0)
+		goto cleanup;
+
+	if (git_commit_create(
+		&i_commit_oid,
+		git_index_owner(index),
+		NULL,
+		stasher,
+		stasher,
+		NULL,
+		git_buf_cstr(&msg),
+		i_tree,
+		1,
+		&parent) < 0)
+			goto cleanup;
+
+	error = git_commit_lookup(i_commit, git_index_owner(index), &i_commit_oid);
+
+cleanup:
+	git_tree_free(i_tree);
+	git_buf_free(&msg);
+	return error;
+}
+
+struct cb_data {
+	git_index *index;
+
+	bool include_changed;
+	bool include_untracked;
+	bool include_ignored;
+};
+
+static int update_index_cb(
+	void *cb_data,
+	const git_diff_delta *delta,
+	float progress)
+{
+	int pos;
+	struct cb_data *data = (struct cb_data *)cb_data;
+
+	GIT_UNUSED(progress);
+
+	switch (delta->status) {
+	case GIT_DELTA_IGNORED:
+		if (!data->include_ignored)
+			break;
+
+		return git_index_add(data->index, delta->new_file.path, 0);
+
+	case GIT_DELTA_UNTRACKED:
+		if (!data->include_untracked)
+			break;
+
+		return git_index_add(data->index, delta->new_file.path, 0);
+
+	case GIT_DELTA_ADDED:
+		/* Fall through */
+	case GIT_DELTA_MODIFIED:
+		if (!data->include_changed)
+			break;
+
+		return git_index_add(data->index, delta->new_file.path, 0);
+
+	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, pos) < 0)
+			return -1;
+
+	default:
+		/* Unimplemented */
+		giterr_set(
+			GITERR_INVALID,
+			"Cannot update index. Unimplemented status kind (%d)",
+			delta->status);
+		return -1;
+	}
+
+	return 0;
+}
+
+static int build_untracked_tree(
+	git_tree **tree_out,
+	git_index *index,
+	git_commit *i_commit,
+	uint32_t flags)
+{
+	git_tree *i_tree = NULL;
+	git_diff_list *diff = NULL;
+	git_diff_options opts = {0};
+	struct cb_data data = {0};
+	int error = -1;
+
+	git_index_clear(index);
+
+	data.index = index;
+
+	if (flags & GIT_STASH_INCLUDE_UNTRACKED) {
+		opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED | GIT_DIFF_RECURSE_UNTRACKED_DIRS;
+		data.include_untracked = true;
+	}
+
+	if (flags & GIT_STASH_INCLUDE_IGNORED) {
+		opts.flags |= GIT_DIFF_INCLUDE_IGNORED;
+		data.include_ignored = true;
+	}
+
+	if (git_commit_tree(&i_tree, i_commit) < 0)
+		goto cleanup;
+
+	if (git_diff_workdir_to_tree(git_index_owner(index), &opts, i_tree, &diff) < 0)
+		goto cleanup;
+
+	if (git_diff_foreach(diff, &data, update_index_cb, NULL, NULL) < 0)
+		goto cleanup;
+
+	if (build_tree_from_index(tree_out, index) < 0)
+		goto cleanup;
+
+	error = 0;
+
+cleanup:
+	git_diff_list_free(diff);
+	git_tree_free(i_tree);
+	return error;
+}
+
+static int commit_untracked(
+	git_commit **u_commit,
+	git_index *index,
+	git_signature *stasher,
+	const char *message,
+	git_commit *i_commit,
+	uint32_t flags)
+{
+	git_tree *u_tree = NULL;
+	git_oid u_commit_oid;
+	git_buf msg = GIT_BUF_INIT;
+	int error = -1;
+
+	if (build_untracked_tree(&u_tree, index, i_commit, flags) < 0)
+		goto cleanup;
+
+	if (git_buf_printf(&msg, "untracked files on %s\n", message) < 0)
+		goto cleanup;
+
+	if (git_commit_create(
+		&u_commit_oid,
+		git_index_owner(index),
+		NULL,
+		stasher,
+		stasher,
+		NULL,
+		git_buf_cstr(&msg),
+		u_tree,
+		0,
+		NULL) < 0)
+			goto cleanup;
+
+	error = git_commit_lookup(u_commit, git_index_owner(index), &u_commit_oid);
+
+cleanup:
+	git_tree_free(u_tree);
+	git_buf_free(&msg);
+	return error;
+}
+
+static int build_workdir_tree(
+	git_tree **tree_out,
+	git_index *index,
+	git_commit *b_commit)
+{
+	git_tree *b_tree = NULL;
+	git_diff_list *diff = NULL, *diff2 = NULL;
+	git_diff_options opts = {0};
+	struct cb_data data = {0};
+	int error = -1;
+
+	if (git_commit_tree(&b_tree, b_commit) < 0)
+		goto cleanup;
+
+	if (git_diff_index_to_tree(git_index_owner(index), &opts, b_tree, &diff) < 0)
+		goto cleanup;
+
+	if (git_diff_workdir_to_index(git_index_owner(index), &opts, &diff2) < 0)
+		goto cleanup;
+
+	if (git_diff_merge(diff, diff2) < 0)
+		goto cleanup;
+
+	data.index = index;
+	data.include_changed = true;
+
+	if (git_diff_foreach(diff, &data, update_index_cb, NULL, NULL) < 0)
+		goto cleanup;
+
+	if (build_tree_from_index(tree_out, index) < 0)
+		goto cleanup;
+
+	error = 0;
+
+cleanup:
+	git_diff_list_free(diff);
+	git_diff_list_free(diff2);
+	git_tree_free(b_tree);
+	return error;
+}
+
+static int commit_worktree(
+	git_oid *w_commit_oid,
+	git_index *index,
+	git_signature *stasher,
+	const char *message,
+	git_commit *i_commit,
+	git_commit *b_commit,
+	git_commit *u_commit)
+{
+	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 (git_index_read_tree(index, i_tree) < 0)
+		goto cleanup;
+
+	if (build_workdir_tree(&w_tree, index, b_commit) < 0)
+		goto cleanup;
+
+	if (git_commit_create(
+		w_commit_oid,
+		git_index_owner(index),
+		NULL,
+		stasher,
+		stasher,
+		NULL,
+		message,
+		w_tree,
+		u_commit ? 3 : 2, parents) < 0)
+			goto cleanup;
+
+	error = 0;
+
+cleanup:
+	git_tree_free(i_tree);
+	git_tree_free(w_tree);
+	return error;
+}
+
+static int prepare_worktree_commit_message(
+	git_buf* msg,
+	const char *user_message)
+{
+	git_buf buf = GIT_BUF_INIT;
+	int error = -1;
+
+	git_buf_set(&buf, git_buf_cstr(msg), git_buf_len(msg));
+	git_buf_clear(msg);
+
+	if (!user_message)
+		git_buf_printf(msg, "WIP on %s", git_buf_cstr(&buf));
+	else {
+		const char *colon;
+
+		if ((colon = strchr(git_buf_cstr(&buf), ':')) == NULL)
+			goto cleanup;
+
+		git_buf_puts(msg, "On ");
+		git_buf_put(msg, git_buf_cstr(&buf), colon - buf.ptr);
+		git_buf_printf(msg, ": %s\n", user_message);
+	}
+
+	error = git_buf_oom(msg) || git_buf_oom(&buf) ? -1 : 0;
+
+cleanup:
+	git_buf_free(&buf);
+	return error;
+}
+
+static int update_reflog(
+	git_oid *w_commit_oid,
+	git_repository *repo,
+	git_signature *stasher,
+	const char *message)
+{
+	git_reference *stash = NULL;
+	git_reflog *reflog = NULL;
+	int error;
+
+	if ((error = git_reference_create_oid(&stash, repo, GIT_REFS_STASH_FILE, w_commit_oid, 1)) < 0)
+		goto cleanup;
+
+	if ((error = git_reflog_read(&reflog, stash)) < 0)
+		goto cleanup;
+
+	if ((error = git_reflog_append(reflog, w_commit_oid, stasher, message)) < 0)
+		goto cleanup;
+
+	if ((error = git_reflog_write(reflog)) < 0)
+		goto cleanup;
+
+	error = 0;
+
+cleanup:
+	git_reference_free(stash);
+	git_reflog_free(reflog);
+	return error;
+}
+
+static int is_dirty_cb(const char *path, unsigned int status, void *payload)
+{
+	GIT_UNUSED(path);
+	GIT_UNUSED(status);
+	GIT_UNUSED(payload);
+
+	return 1;
+}
+
+static int ensure_there_are_changes_to_stash(
+	git_repository *repo,
+	bool include_untracked_files,
+	bool include_ignored_files)
+{
+	int error;
+	git_status_options opts;
+
+	memset(&opts, 0, sizeof(opts));
+	opts.show  = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
+	if (include_untracked_files)
+		opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
+		GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
+
+	if (include_ignored_files)
+		opts.flags = GIT_STATUS_OPT_INCLUDE_IGNORED;
+
+	error = git_status_foreach_ext(repo, &opts, is_dirty_cb, NULL);
+
+	if (error == GIT_EUSER)
+		return 0;
+
+	if (!error)
+		return create_error(GIT_ENOTFOUND, "There is nothing to stash.");
+
+	return error;
+}
+
+static int reset_index_and_workdir(
+	git_repository *repo,
+	git_commit *commit,
+	bool remove_untracked)
+{
+	git_checkout_opts opts;
+
+	memset(&opts, 0, sizeof(git_checkout_opts));
+
+	opts.checkout_strategy = 
+		GIT_CHECKOUT_CREATE_MISSING | GIT_CHECKOUT_OVERWRITE_MODIFIED;
+
+	if (remove_untracked)
+		opts.checkout_strategy |= GIT_CHECKOUT_REMOVE_UNTRACKED;
+
+	return git_checkout_tree(repo, (git_object *)commit, &opts);
+}
+
+int git_stash_save(
+	git_oid *out,
+	git_repository *repo,
+	git_signature *stasher,
+	const char *message,
+	uint32_t flags)
+{
+	git_index *index = NULL;
+	git_commit *b_commit = NULL, *i_commit = NULL, *u_commit = NULL;
+	git_buf msg = GIT_BUF_INIT;
+	int error;
+
+	assert(out && repo && stasher);
+
+	if ((error = ensure_non_bare_repository(repo)) < 0)
+		return error;
+
+	if ((error = retrieve_base_commit_and_message(&b_commit, &msg, repo)) < 0)
+		goto cleanup;
+
+	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)
+		goto cleanup;
+
+	error = -1;
+
+	if (git_repository_index(&index, repo) < 0)
+		goto cleanup;
+
+	if (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)
+		goto cleanup;
+
+	if (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)
+		goto cleanup;
+
+	git_buf_rtrim(&msg);
+	if (update_reflog(out, repo, stasher, git_buf_cstr(&msg)) < 0)
+		goto cleanup;
+
+	if (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)
+		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;
+}
+
+int git_stash_foreach(
+	git_repository *repo,
+	stash_cb callback,
+	void *payload)
+{
+	git_reference *stash;
+	git_reflog *reflog = NULL;
+	int error;
+	size_t i, max;
+	const git_reflog_entry *entry;
+
+	error = git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE);
+	if (error == GIT_ENOTFOUND)
+		return 0;
+
+	if (error < 0)
+		goto cleanup;
+
+	if ((error = git_reflog_read(&reflog, stash)) < 0)
+		goto cleanup;
+
+	max = git_reflog_entrycount(reflog);
+	for (i = 0; i < max; i++) {
+		entry = git_reflog_entry_byindex(reflog, max - i - 1);
+		
+		if (callback(i,
+			git_reflog_entry_msg(entry),
+			git_reflog_entry_oidnew(entry),
+			payload)) {
+				error = GIT_EUSER;
+				goto cleanup;
+		}
+	}
+
+	error = 0;
+
+cleanup:
+	git_reference_free(stash);
+	git_reflog_free(reflog);
+	return error;
+}
+
+int git_stash_drop(
+	git_repository *repo,
+	size_t index)
+{
+	git_reference *stash;
+	git_reflog *reflog = NULL;
+	size_t max;
+	int error;
+
+	if ((error = git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE)) < 0)
+		return error;
+
+	if ((error = git_reflog_read(&reflog, stash)) < 0)
+		goto cleanup;
+
+	max = git_reflog_entrycount(reflog);
+
+	if (index > max - 1) {
+		error = GIT_ENOTFOUND;
+		giterr_set(GITERR_STASH, "No stashed state at position %" PRIuZ, index);
+		goto cleanup;
+	}
+
+	if ((error = git_reflog_drop(reflog, max - index - 1, true)) < 0)
+		goto cleanup;
+
+	if ((error = git_reflog_write(reflog)) < 0)
+		goto cleanup;
+
+	if (max == 1) {
+		error = git_reference_delete(stash);
+		stash = NULL;
+	}
+
+cleanup:
+	git_reference_free(stash);
+	git_reflog_free(reflog);
+	return error;
+}
diff --git a/tests-clar/object/commit/commitstagedfile.c b/tests-clar/object/commit/commitstagedfile.c
index 882fb49..6b6c573 100644
--- a/tests-clar/object/commit/commitstagedfile.c
+++ b/tests-clar/object/commit/commitstagedfile.c
@@ -128,68 +128,3 @@ void test_object_commit_commitstagedfile__generate_predictable_object_ids(void)
 	git_tree_free(tree);
 	git_index_free(index);
 }
-
-void test_object_commit_commitstagedfile__message_prettify(void)
-{
-	char buffer[100];
-
-	cl_assert(git_message_prettify(buffer, sizeof(buffer), "", 0) == 1);
-	cl_assert_equal_s(buffer, "");
-	cl_assert(git_message_prettify(buffer, sizeof(buffer), "", 1) == 1);
-	cl_assert_equal_s(buffer, "");
-
-	cl_assert_equal_i(7, git_message_prettify(buffer, sizeof(buffer), "Short", 0));
-	cl_assert_equal_s("Short\n", buffer);
-	cl_assert_equal_i(7, git_message_prettify(buffer, sizeof(buffer), "Short", 1));
-	cl_assert_equal_s("Short\n", buffer);
-
-	cl_assert(git_message_prettify(buffer, sizeof(buffer), "This is longer\nAnd multiline\n# with some comments still in\n", 0) > 0);
-	cl_assert_equal_s(buffer, "This is longer\nAnd multiline\n# with some comments still in\n");
-
-	cl_assert(git_message_prettify(buffer, sizeof(buffer), "This is longer\nAnd multiline\n# with some comments still in\n", 1) > 0);
-	cl_assert_equal_s(buffer, "This is longer\nAnd multiline\n");
-
-	/* try out overflow */
-	cl_assert(git_message_prettify(buffer, sizeof(buffer),
-		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890"
-		"1234567890" "1234567890" "1234567890" "1234567890" "12345678",
-		0) > 0);
-	cl_assert_equal_s(buffer,
-		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890"
-		"1234567890" "1234567890" "1234567890" "1234567890" "12345678\n");
-
-	cl_assert(git_message_prettify(buffer, sizeof(buffer),
-		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890"
-		"1234567890" "1234567890" "1234567890" "1234567890" "12345678\n",
-		0) > 0);
-	cl_assert_equal_s(buffer,
-		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890"
-		"1234567890" "1234567890" "1234567890" "1234567890" "12345678\n");
-
-	cl_git_fail(git_message_prettify(buffer, sizeof(buffer),
-		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890"
-		"1234567890" "1234567890" "1234567890" "1234567890" "123456789",
-		0));
-	cl_git_fail(git_message_prettify(buffer, sizeof(buffer),
-		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890"
-		"1234567890" "1234567890" "1234567890" "1234567890" "123456789\n",
-		0));
-	cl_git_fail(git_message_prettify(buffer, sizeof(buffer),
-		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890"
-		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890",
-		0));
-	cl_git_fail(git_message_prettify(buffer, sizeof(buffer),
-		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890"
-		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890""x",
-		0));
-
-	cl_assert(git_message_prettify(buffer, sizeof(buffer),
-		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890\n"
-		"# 1234567890" "1234567890" "1234567890" "1234567890" "1234567890\n"
-		"1234567890",
-		1) > 0);
-
-	cl_assert(git_message_prettify(NULL, 0, "", 0) == 1);
-	cl_assert(git_message_prettify(NULL, 0, "Short test", 0) == 12);
-	cl_assert(git_message_prettify(NULL, 0, "Test\n# with\nComments", 1) == 15);
-}
diff --git a/tests-clar/object/message.c b/tests-clar/object/message.c
index 43be8b1..7ef6374 100644
--- a/tests-clar/object/message.c
+++ b/tests-clar/object/message.c
@@ -169,3 +169,68 @@ void test_object_message__keep_comments(void)
 	assert_message_prettifying("# comment\n" ttt "\n", "# comment\n" ttt "\n", 0);
 	assert_message_prettifying(ttt "\n" "# comment\n" ttt "\n", ttt "\n" "# comment\n" ttt "\n", 0);
 }
+
+void test_object_message__message_prettify(void)
+{
+	char buffer[100];
+
+	cl_assert(git_message_prettify(buffer, sizeof(buffer), "", 0) == 1);
+	cl_assert_equal_s(buffer, "");
+	cl_assert(git_message_prettify(buffer, sizeof(buffer), "", 1) == 1);
+	cl_assert_equal_s(buffer, "");
+
+	cl_assert_equal_i(7, git_message_prettify(buffer, sizeof(buffer), "Short", 0));
+	cl_assert_equal_s("Short\n", buffer);
+	cl_assert_equal_i(7, git_message_prettify(buffer, sizeof(buffer), "Short", 1));
+	cl_assert_equal_s("Short\n", buffer);
+
+	cl_assert(git_message_prettify(buffer, sizeof(buffer), "This is longer\nAnd multiline\n# with some comments still in\n", 0) > 0);
+	cl_assert_equal_s(buffer, "This is longer\nAnd multiline\n# with some comments still in\n");
+
+	cl_assert(git_message_prettify(buffer, sizeof(buffer), "This is longer\nAnd multiline\n# with some comments still in\n", 1) > 0);
+	cl_assert_equal_s(buffer, "This is longer\nAnd multiline\n");
+
+	/* try out overflow */
+	cl_assert(git_message_prettify(buffer, sizeof(buffer),
+		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890"
+		"1234567890" "1234567890" "1234567890" "1234567890" "12345678",
+		0) > 0);
+	cl_assert_equal_s(buffer,
+		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890"
+		"1234567890" "1234567890" "1234567890" "1234567890" "12345678\n");
+
+	cl_assert(git_message_prettify(buffer, sizeof(buffer),
+		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890"
+		"1234567890" "1234567890" "1234567890" "1234567890" "12345678\n",
+		0) > 0);
+	cl_assert_equal_s(buffer,
+		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890"
+		"1234567890" "1234567890" "1234567890" "1234567890" "12345678\n");
+
+	cl_git_fail(git_message_prettify(buffer, sizeof(buffer),
+		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890"
+		"1234567890" "1234567890" "1234567890" "1234567890" "123456789",
+		0));
+	cl_git_fail(git_message_prettify(buffer, sizeof(buffer),
+		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890"
+		"1234567890" "1234567890" "1234567890" "1234567890" "123456789\n",
+		0));
+	cl_git_fail(git_message_prettify(buffer, sizeof(buffer),
+		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890"
+		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890",
+		0));
+	cl_git_fail(git_message_prettify(buffer, sizeof(buffer),
+		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890"
+		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890""x",
+		0));
+
+	cl_assert(git_message_prettify(buffer, sizeof(buffer),
+		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890\n"
+		"# 1234567890" "1234567890" "1234567890" "1234567890" "1234567890\n"
+		"1234567890",
+		1) > 0);
+
+	cl_assert(git_message_prettify(NULL, 0, "", 0) == 1);
+	cl_assert(git_message_prettify(NULL, 0, "Short test", 0) == 12);
+	cl_assert(git_message_prettify(NULL, 0, "Test\n# with\nComments", 1) == 15);
+}
diff --git a/tests-clar/refs/reflog/drop.c b/tests-clar/refs/reflog/drop.c
index 86781c0..4be857b 100644
--- a/tests-clar/refs/reflog/drop.c
+++ b/tests-clar/refs/reflog/drop.c
@@ -43,57 +43,48 @@ void test_refs_reflog_drop__can_drop_an_entry(void)
 
 void test_refs_reflog_drop__can_drop_an_entry_and_rewrite_the_log_history(void)
 {
-	const git_reflog_entry *before_previous, *before_next;
-	const git_reflog_entry *after_next;
-	git_oid before_next_old_oid;
+	const git_reflog_entry *before_current;
+	const git_reflog_entry *after_current;
+	git_oid before_current_old_oid, before_current_cur_oid;
 
 	cl_assert(entrycount > 4);
 
-	before_previous = git_reflog_entry_byindex(g_reflog, 3);
-	before_next = git_reflog_entry_byindex(g_reflog, 1);
-	git_oid_cpy(&before_next_old_oid, &before_next->oid_old);
+	before_current = git_reflog_entry_byindex(g_reflog, 2);
+	git_oid_cpy(&before_current_old_oid, &before_current->oid_old);
+	git_oid_cpy(&before_current_cur_oid, &before_current->oid_cur);
 
 	cl_git_pass(git_reflog_drop(g_reflog, 2, 1));
 	cl_assert_equal_i(entrycount - 1, git_reflog_entrycount(g_reflog));
 
-	after_next = git_reflog_entry_byindex(g_reflog, 1);
+	after_current = git_reflog_entry_byindex(g_reflog, 2);
 
-	cl_assert_equal_i(0, git_oid_cmp(&before_next->oid_cur, &after_next->oid_cur));
-	cl_assert(git_oid_cmp(&before_next_old_oid, &after_next->oid_old) != 0);
-	cl_assert_equal_i(0, git_oid_cmp(&before_previous->oid_cur, &after_next->oid_old));
+	cl_assert_equal_i(0, git_oid_cmp(&before_current_old_oid, &after_current->oid_old));
+	cl_assert(0 != git_oid_cmp(&before_current_cur_oid, &after_current->oid_cur));
 }
 
-void test_refs_reflog_drop__can_drop_the_first_entry(void)
-{
-	cl_assert(entrycount > 2);
-
-	cl_git_pass(git_reflog_drop(g_reflog, 0, 0));
-	cl_assert_equal_i(entrycount - 1, git_reflog_entrycount(g_reflog));
-}
-
-void test_refs_reflog_drop__can_drop_the_last_entry(void)
+void test_refs_reflog_drop__can_drop_the_oldest_entry(void)
 {
 	const git_reflog_entry *entry;
 
 	cl_assert(entrycount > 2);
 
-	cl_git_pass(git_reflog_drop(g_reflog, entrycount - 1, 0));
+	cl_git_pass(git_reflog_drop(g_reflog, 0, 0));
 	cl_assert_equal_i(entrycount - 1, git_reflog_entrycount(g_reflog));
 
-	entry = git_reflog_entry_byindex(g_reflog, entrycount - 2);
+	entry = git_reflog_entry_byindex(g_reflog, 0);
 	cl_assert(git_oid_streq(&entry->oid_old, GIT_OID_HEX_ZERO) != 0);
 }
 
-void test_refs_reflog_drop__can_drop_the_last_entry_and_rewrite_the_log_history(void)
+void test_refs_reflog_drop__can_drop_the_oldest_entry_and_rewrite_the_log_history(void)
 {
 	const git_reflog_entry *entry;
 
 	cl_assert(entrycount > 2);
 
-	cl_git_pass(git_reflog_drop(g_reflog, entrycount - 1, 1));
+	cl_git_pass(git_reflog_drop(g_reflog, 0, 1));
 	cl_assert_equal_i(entrycount - 1, git_reflog_entrycount(g_reflog));
 
-	entry = git_reflog_entry_byindex(g_reflog, entrycount - 2);
+	entry = git_reflog_entry_byindex(g_reflog, 0);
 	cl_assert(git_oid_streq(&entry->oid_old, GIT_OID_HEX_ZERO) == 0);
 }
 
diff --git a/tests-clar/stash/drop.c b/tests-clar/stash/drop.c
new file mode 100644
index 0000000..136a942
--- /dev/null
+++ b/tests-clar/stash/drop.c
@@ -0,0 +1,126 @@
+#include "clar_libgit2.h"
+#include "fileops.h"
+#include "stash_helpers.h"
+
+static git_repository *repo;
+static git_signature *signature;
+
+void test_stash_drop__initialize(void)
+{
+	cl_git_pass(git_repository_init(&repo, "stash", 0));
+	cl_git_pass(git_signature_new(&signature, "nulltoken", "emeric.fermas@gmail.com", 1323847743, 60)); /* Wed Dec 14 08:29:03 2011 +0100 */
+}
+
+void test_stash_drop__cleanup(void)
+{
+	git_signature_free(signature);
+	git_repository_free(repo);
+	cl_git_pass(git_futils_rmdir_r("stash", NULL, GIT_DIRREMOVAL_FILES_AND_DIRS));
+}
+
+void test_stash_drop__cannot_drop_from_an_empty_stash(void)
+{
+	cl_assert_equal_i(GIT_ENOTFOUND, git_stash_drop(repo, 0));
+}
+
+static void push_three_states(void)
+{
+	git_oid oid;
+	git_index *index;
+
+	cl_git_mkfile("stash/zero.txt", "content\n");
+	cl_git_pass(git_repository_index(&index, repo));
+	cl_git_pass(git_index_add(index, "zero.txt", 0));
+	commit_staged_files(&oid, index, signature);
+
+	cl_git_mkfile("stash/one.txt", "content\n");
+	cl_git_pass(git_stash_save(&oid, repo, signature, "First", GIT_STASH_INCLUDE_UNTRACKED));
+
+	cl_git_mkfile("stash/two.txt", "content\n");
+	cl_git_pass(git_stash_save(&oid, repo, signature, "Second", GIT_STASH_INCLUDE_UNTRACKED));
+
+	cl_git_mkfile("stash/three.txt", "content\n");
+	cl_git_pass(git_stash_save(&oid, repo, signature, "Third", GIT_STASH_INCLUDE_UNTRACKED));
+
+	git_index_free(index);
+}
+
+void test_stash_drop__cannot_drop_a_non_existing_stashed_state(void)
+{
+	push_three_states();
+
+	cl_assert_equal_i(GIT_ENOTFOUND, git_stash_drop(repo, 666));
+	cl_assert_equal_i(GIT_ENOTFOUND, git_stash_drop(repo, 42));
+	cl_assert_equal_i(GIT_ENOTFOUND, git_stash_drop(repo, 3));
+}
+
+void test_stash_drop__can_purge_the_stash_from_the_top(void)
+{
+	push_three_states();
+
+	cl_git_pass(git_stash_drop(repo, 0));
+	cl_git_pass(git_stash_drop(repo, 0));
+	cl_git_pass(git_stash_drop(repo, 0));
+
+	cl_assert_equal_i(GIT_ENOTFOUND, git_stash_drop(repo, 0));
+}
+
+void test_stash_drop__can_purge_the_stash_from_the_bottom(void)
+{
+	push_three_states();
+
+	cl_git_pass(git_stash_drop(repo, 2));
+	cl_git_pass(git_stash_drop(repo, 1));
+	cl_git_pass(git_stash_drop(repo, 0));
+
+	cl_assert_equal_i(GIT_ENOTFOUND, git_stash_drop(repo, 0));
+}
+
+void test_stash_drop__dropping_an_entry_rewrites_reflog_history(void)
+{
+	git_reference *stash;
+	git_reflog *reflog;
+	const git_reflog_entry *entry;
+	git_oid oid;
+	size_t count;
+
+	push_three_states();
+
+	cl_git_pass(git_reference_lookup(&stash, repo, "refs/stash"));
+	
+	cl_git_pass(git_reflog_read(&reflog, stash));
+	entry = git_reflog_entry_byindex(reflog, 1);
+
+	git_oid_cpy(&oid, git_reflog_entry_oidold(entry));
+	count = git_reflog_entrycount(reflog);
+	
+	git_reflog_free(reflog);
+
+	cl_git_pass(git_stash_drop(repo, 1));
+
+	cl_git_pass(git_reflog_read(&reflog, stash));
+	entry = git_reflog_entry_byindex(reflog, 1);
+
+	cl_assert_equal_i(0, git_oid_cmp(&oid, git_reflog_entry_oidold(entry)));
+	cl_assert_equal_i(count - 1, git_reflog_entrycount(reflog));
+
+	git_reflog_free(reflog);
+
+	git_reference_free(stash);
+}
+
+void test_stash_drop__dropping_the_last_entry_removes_the_stash(void)
+{
+	git_reference *stash;
+
+	push_three_states();
+
+	cl_git_pass(git_reference_lookup(&stash, repo, "refs/stash"));
+	git_reference_free(stash);
+
+	cl_git_pass(git_stash_drop(repo, 0));
+	cl_git_pass(git_stash_drop(repo, 0));
+	cl_git_pass(git_stash_drop(repo, 0));
+
+	cl_assert_equal_i(GIT_ENOTFOUND, git_reference_lookup(&stash, repo, "refs/stash"));
+}
diff --git a/tests-clar/stash/foreach.c b/tests-clar/stash/foreach.c
new file mode 100644
index 0000000..8086507
--- /dev/null
+++ b/tests-clar/stash/foreach.c
@@ -0,0 +1,119 @@
+#include "clar_libgit2.h"
+#include "fileops.h"
+#include "stash_helpers.h"
+
+struct callback_data
+{
+	char **oids;
+	int invokes;
+};
+
+static git_repository *repo;
+static git_signature *signature;
+static git_oid stash_tip_oid;
+struct callback_data data;
+
+#define REPO_NAME "stash"
+
+void test_stash_foreach__initialize(void)
+{
+	cl_git_pass(git_signature_new(
+		&signature,
+		"nulltoken",
+		"emeric.fermas@gmail.com",
+		1323847743, 60)); /* Wed Dec 14 08:29:03 2011 +0100 */
+
+	memset(&data, 0, sizeof(struct callback_data));
+}
+
+void test_stash_foreach__cleanup(void)
+{
+	git_signature_free(signature);
+	git_repository_free(repo);
+	cl_git_pass(git_futils_rmdir_r(REPO_NAME, NULL, GIT_DIRREMOVAL_FILES_AND_DIRS));
+}
+
+static int callback_cb(
+		size_t index,
+		const char* message,
+		const git_oid *stash_oid,
+		void *payload)
+{
+	int i = 0;
+	bool found = false;
+	struct callback_data *data = (struct callback_data *)payload;
+
+	cl_assert_equal_i(0, git_oid_streq(stash_oid, data->oids[data->invokes++]));
+	
+	return 0;
+}
+
+void test_stash_foreach__enumerating_a_empty_repository_doesnt_fail(void)
+{
+	char *oids[] = { NULL };
+
+	data.oids = oids;
+
+	cl_git_pass(git_repository_init(&repo, REPO_NAME, 0));
+
+	cl_git_pass(git_stash_foreach(repo, callback_cb, &data));
+
+	cl_assert_equal_i(0, data.invokes);
+}
+
+void test_stash_foreach__can_enumerate_a_repository(void)
+{
+	char *oids_default[] = {
+		"1d91c842a7cdfc25872b3a763e5c31add8816c25", NULL };
+
+	char *oids_untracked[] = {
+		"7f89a8b15c878809c5c54d1ff8f8c9674154017b",
+		"1d91c842a7cdfc25872b3a763e5c31add8816c25", NULL };
+
+	char *oids_ignored[] = {
+		"c95599a8fef20a7e57582c6727b1a0d02e0a5828",
+		"7f89a8b15c878809c5c54d1ff8f8c9674154017b",
+		"1d91c842a7cdfc25872b3a763e5c31add8816c25", NULL };
+
+	cl_git_pass(git_repository_init(&repo, REPO_NAME, 0));
+
+	setup_stash(repo, signature);
+
+	cl_git_pass(git_stash_save(
+		&stash_tip_oid,
+		repo,
+		signature,
+		NULL,
+		GIT_STASH_DEFAULT));
+
+	data.oids = oids_default;
+
+	cl_git_pass(git_stash_foreach(repo, callback_cb, &data));
+	cl_assert_equal_i(1, data.invokes);
+
+	data.oids = oids_untracked;
+	data.invokes = 0;
+
+	cl_git_pass(git_stash_save(
+		&stash_tip_oid,
+		repo,
+		signature,
+		NULL,
+		GIT_STASH_INCLUDE_UNTRACKED));
+
+	cl_git_pass(git_stash_foreach(repo, callback_cb, &data));
+	cl_assert_equal_i(2, data.invokes);
+
+	data.oids = oids_ignored;
+	data.invokes = 0;
+
+	cl_git_pass(git_stash_save(
+		&stash_tip_oid,
+		repo,
+		signature,
+		NULL,
+		GIT_STASH_INCLUDE_IGNORED));
+
+	cl_git_pass(git_stash_foreach(repo, callback_cb, &data));
+	cl_assert_equal_i(3, data.invokes);
+}
diff --git a/tests-clar/stash/save.c b/tests-clar/stash/save.c
new file mode 100644
index 0000000..71b0b64
--- /dev/null
+++ b/tests-clar/stash/save.c
@@ -0,0 +1,370 @@
+#include "clar_libgit2.h"
+#include "fileops.h"
+#include "stash_helpers.h"
+
+static git_repository *repo;
+static git_signature *signature;
+static git_oid stash_tip_oid;
+
+/*
+ * Friendly reminder, in order to ease the reading of the following tests:
+ *
+ * "stash"		points to the worktree commit 
+ * "stash^1"	points to the base commit (HEAD when the stash was created)
+ * "stash^2"	points to the index commit
+ * "stash^3"	points to the untracked commit
+ */
+
+void test_stash_save__initialize(void)
+{
+	cl_git_pass(git_repository_init(&repo, "stash", 0));
+	cl_git_pass(git_signature_new(&signature, "nulltoken", "emeric.fermas@gmail.com", 1323847743, 60)); /* Wed Dec 14 08:29:03 2011 +0100 */
+
+	setup_stash(repo, signature);
+}
+
+void test_stash_save__cleanup(void)
+{
+	git_signature_free(signature);
+	git_repository_free(repo);
+	cl_git_pass(git_futils_rmdir_r("stash", NULL, GIT_DIRREMOVAL_FILES_AND_DIRS));
+}
+
+static void assert_object_oid(const char* revision, const char* expected_oid, git_otype type)
+{
+	git_object *object;
+	int result;
+
+	result = git_revparse_single(&object, repo, revision);
+
+	if (!expected_oid) {
+		cl_assert_equal_i(GIT_ENOTFOUND, result);
+		return;
+	} else
+		cl_assert_equal_i(0, result);
+
+	cl_assert_equal_i(type, git_object_type(object));
+	cl_git_pass(git_oid_streq(git_object_id(object), expected_oid));
+
+	git_object_free(object);
+}
+
+static void assert_blob_oid(const char* revision, const char* expected_oid)
+{
+	assert_object_oid(revision, expected_oid, GIT_OBJ_BLOB);
+}
+
+void test_stash_save__does_not_keep_index_by_default(void)
+{
+/*
+$ git stash
+
+$ git show refs/stash:what
+see you later
+
+$ git show refs/stash:how
+not so small and
+
+$ git show refs/stash:who
+funky world
+
+$ git show refs/stash:when
+fatal: Path 'when' exists on disk, but not in 'stash'.
+
+$ git show refs/stash^2:what
+goodbye
+
+$ git show refs/stash^2:how
+not so small and
+
+$ git show refs/stash^2:who
+world
+
+$ git show refs/stash^2:when
+fatal: Path 'when' exists on disk, but not in 'stash^2'.
+
+$ git status --short
+?? when
+
+*/
+	unsigned int status;
+
+	cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_DEFAULT));
+	cl_git_pass(git_status_file(&status, repo, "when"));
+
+	assert_blob_oid("refs/stash:what", "bc99dc98b3eba0e9157e94769cd4d49cb49de449");	/* see you later */
+	assert_blob_oid("refs/stash:how", "e6d64adb2c7f3eb8feb493b556cc8070dca379a3");	/* not so small and */
+	assert_blob_oid("refs/stash:who", "a0400d4954659306a976567af43125a0b1aa8595");	/* funky world */
+	assert_blob_oid("refs/stash:when", NULL);
+	assert_blob_oid("refs/stash:just.ignore", NULL);
+
+	assert_blob_oid("refs/stash^2:what", "dd7e1c6f0fefe118f0b63d9f10908c460aa317a6");	/* goodbye */
+	assert_blob_oid("refs/stash^2:how", "e6d64adb2c7f3eb8feb493b556cc8070dca379a3");	/* not so small and */
+	assert_blob_oid("refs/stash^2:who", "cc628ccd10742baea8241c5924df992b5c019f71");	/* world */
+	assert_blob_oid("refs/stash^2:when", NULL);
+	assert_blob_oid("refs/stash^2:just.ignore", NULL);
+
+	assert_blob_oid("refs/stash^3", NULL);
+
+	cl_assert_equal_i(GIT_STATUS_WT_NEW, status);
+}
+
+static void assert_status(
+	const char *path,
+	int status_flags)
+{
+	unsigned int status;
+	int error;
+
+	error = git_status_file(&status, repo, path);
+
+	if (status_flags < 0) {
+		cl_assert_equal_i(status_flags, error);
+		return;
+	}
+
+	cl_assert_equal_i(0, error);
+	cl_assert_equal_i((unsigned int)status_flags, status);
+}
+
+void test_stash_save__can_keep_index(void)
+{
+	cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_KEEP_INDEX));
+
+	assert_status("what", GIT_STATUS_INDEX_MODIFIED);
+	assert_status("how", GIT_STATUS_INDEX_MODIFIED);
+	assert_status("who", GIT_STATUS_CURRENT);
+	assert_status("when", GIT_STATUS_WT_NEW);
+	assert_status("just.ignore", GIT_STATUS_IGNORED);
+}
+
+static void assert_commit_message_contains(const char *revision, const char *fragment)
+{
+	git_commit *commit;
+
+	cl_git_pass(git_revparse_single(((git_object **)&commit), repo, revision));
+
+	cl_assert(strstr(git_commit_message(commit), fragment) != NULL);
+
+	git_commit_free(commit);
+}
+
+void test_stash_save__can_include_untracked_files(void)
+{
+	cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_INCLUDE_UNTRACKED));
+
+	assert_commit_message_contains("refs/stash^3", "untracked files on master: ");
+
+	assert_blob_oid("refs/stash^3:what", NULL);
+	assert_blob_oid("refs/stash^3:how", NULL);
+	assert_blob_oid("refs/stash^3:who", NULL);
+	assert_blob_oid("refs/stash^3:when", "b6ed15e81e2593d7bb6265eb4a991d29dc3e628b");
+	assert_blob_oid("refs/stash^3:just.ignore", NULL);
+}
+
+void test_stash_save__can_include_untracked_and_ignored_files(void)
+{
+	cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_INCLUDE_UNTRACKED | GIT_STASH_INCLUDE_IGNORED));
+
+	assert_commit_message_contains("refs/stash^3", "untracked files on master: ");
+
+	assert_blob_oid("refs/stash^3:what", NULL);
+	assert_blob_oid("refs/stash^3:how", NULL);
+	assert_blob_oid("refs/stash^3:who", NULL);
+	assert_blob_oid("refs/stash^3:when", "b6ed15e81e2593d7bb6265eb4a991d29dc3e628b");
+	assert_blob_oid("refs/stash^3:just.ignore", "78925fb1236b98b37a35e9723033e627f97aa88b");
+}
+
+#define MESSAGE "Look Ma! I'm on TV!"
+void test_stash_save__can_accept_a_message(void)
+{
+	cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, MESSAGE, GIT_STASH_DEFAULT));
+
+	assert_commit_message_contains("refs/stash^2", "index on master: ");
+	assert_commit_message_contains("refs/stash", "On master: " MESSAGE);
+}
+
+void test_stash_save__cannot_stash_against_an_unborn_branch(void)
+{
+	git_reference *head;
+
+	cl_git_pass(git_reference_lookup(&head, repo, "HEAD"));
+	cl_git_pass(git_reference_set_target(head, "refs/heads/unborn"));
+
+	cl_assert_equal_i(GIT_EORPHANEDHEAD,
+		git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_DEFAULT));
+
+	git_reference_free(head);
+}
+
+void test_stash_save__cannot_stash_against_a_bare_repository(void)
+{
+	git_repository *local;
+
+	cl_git_pass(git_repository_init(&local, "sorry-it-is-a-non-bare-only-party", 1));
+
+	cl_assert_equal_i(GIT_EBAREREPO,
+		git_stash_save(&stash_tip_oid, local, signature, NULL, GIT_STASH_DEFAULT));
+
+	git_repository_free(local);
+}
+
+void test_stash_save__can_stash_against_a_detached_head(void)
+{
+	git_repository_detach_head(repo);
+
+	cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_DEFAULT));
+
+	assert_commit_message_contains("refs/stash^2", "index on (no branch): ");
+	assert_commit_message_contains("refs/stash", "WIP on (no branch): ");
+}
+
+void test_stash_save__stashing_updates_the_reflog(void)
+{
+	char *sha;
+
+	assert_object_oid("refs/stash@{0}", NULL, GIT_OBJ_COMMIT);
+
+	cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_DEFAULT));
+
+	sha = git_oid_allocfmt(&stash_tip_oid);
+
+	assert_object_oid("refs/stash@{0}", sha, GIT_OBJ_COMMIT);
+	assert_object_oid("refs/stash@{1}", NULL, GIT_OBJ_COMMIT);
+
+	git__free(sha);
+}
+
+void test_stash_save__cannot_stash_when_there_are_no_local_change(void)
+{
+	git_index *index;
+	git_oid commit_oid, stash_tip_oid;
+
+	cl_git_pass(git_repository_index(&index, repo));
+
+	/*
+	 * 'what' and 'who' are being committed.
+	 * 'when' remain untracked.
+	 */
+	git_index_add(index, "what", 0);
+	git_index_add(index, "who", 0);
+	cl_git_pass(git_index_write(index));
+	commit_staged_files(&commit_oid, index, signature);
+	git_index_free(index);
+
+	cl_assert_equal_i(GIT_ENOTFOUND,
+		git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_DEFAULT));
+
+	p_unlink("stash/when");
+	cl_assert_equal_i(GIT_ENOTFOUND,
+		git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_INCLUDE_UNTRACKED));
+}
+
+void test_stash_save__can_stage_normal_then_stage_untracked(void)
+{
+	/*
+	 * $ git ls-tree stash@{1}^0
+	 * 100644 blob ac4d88de61733173d9959e4b77c69b9f17a00980    .gitignore
+	 * 100644 blob e6d64adb2c7f3eb8feb493b556cc8070dca379a3    how
+	 * 100644 blob bc99dc98b3eba0e9157e94769cd4d49cb49de449    what
+	 * 100644 blob a0400d4954659306a976567af43125a0b1aa8595    who
+	 *
+	 * $ git ls-tree stash@{1}^1
+	 * 100644 blob ac4d88de61733173d9959e4b77c69b9f17a00980    .gitignore
+	 * 100644 blob ac790413e2d7a26c3767e78c57bb28716686eebc    how
+	 * 100644 blob ce013625030ba8dba906f756967f9e9ca394464a    what
+	 * 100644 blob cc628ccd10742baea8241c5924df992b5c019f71    who
+	 *
+	 * $ git ls-tree stash@{1}^2
+	 * 100644 blob ac4d88de61733173d9959e4b77c69b9f17a00980    .gitignore
+	 * 100644 blob e6d64adb2c7f3eb8feb493b556cc8070dca379a3    how
+	 * 100644 blob dd7e1c6f0fefe118f0b63d9f10908c460aa317a6    what
+	 * 100644 blob cc628ccd10742baea8241c5924df992b5c019f71    who
+	 *
+	 * $ git ls-tree stash@{1}^3
+	 * fatal: Not a valid object name stash@{1}^3
+	 *
+	 * $ git ls-tree stash@{0}^0
+	 * 100644 blob ac4d88de61733173d9959e4b77c69b9f17a00980    .gitignore
+	 * 100644 blob ac790413e2d7a26c3767e78c57bb28716686eebc    how
+	 * 100644 blob ce013625030ba8dba906f756967f9e9ca394464a    what
+	 * 100644 blob cc628ccd10742baea8241c5924df992b5c019f71    who
+	 *
+	 * $ git ls-tree stash@{0}^1
+	 * 100644 blob ac4d88de61733173d9959e4b77c69b9f17a00980    .gitignore
+	 * 100644 blob ac790413e2d7a26c3767e78c57bb28716686eebc    how
+	 * 100644 blob ce013625030ba8dba906f756967f9e9ca394464a    what
+	 * 100644 blob cc628ccd10742baea8241c5924df992b5c019f71    who
+	 *
+	 * $ git ls-tree stash@{0}^2
+	 * 100644 blob ac4d88de61733173d9959e4b77c69b9f17a00980    .gitignore
+	 * 100644 blob ac790413e2d7a26c3767e78c57bb28716686eebc    how
+	 * 100644 blob ce013625030ba8dba906f756967f9e9ca394464a    what
+	 * 100644 blob cc628ccd10742baea8241c5924df992b5c019f71    who
+	 *
+	 * $ git ls-tree stash@{0}^3
+	 * 100644 blob b6ed15e81e2593d7bb6265eb4a991d29dc3e628b    when
+	*/
+
+	assert_status("what", GIT_STATUS_WT_MODIFIED | GIT_STATUS_INDEX_MODIFIED);
+	assert_status("how", GIT_STATUS_INDEX_MODIFIED);
+	assert_status("who", GIT_STATUS_WT_MODIFIED);
+	assert_status("when", GIT_STATUS_WT_NEW);
+	assert_status("just.ignore", GIT_STATUS_IGNORED);
+
+	cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_DEFAULT));
+	assert_status("what", GIT_STATUS_CURRENT);
+	assert_status("how", GIT_STATUS_CURRENT);
+	assert_status("who", GIT_STATUS_CURRENT);
+	assert_status("when", GIT_STATUS_WT_NEW);
+	assert_status("just.ignore", GIT_STATUS_IGNORED);
+
+	cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_INCLUDE_UNTRACKED));
+	assert_status("what", GIT_STATUS_CURRENT);
+	assert_status("how", GIT_STATUS_CURRENT);
+	assert_status("who", GIT_STATUS_CURRENT);
+	assert_status("when", GIT_ENOTFOUND);
+	assert_status("just.ignore", GIT_STATUS_IGNORED);
+
+
+	assert_blob_oid("stash@{1}^0:what", "bc99dc98b3eba0e9157e94769cd4d49cb49de449");	/* see you later */
+	assert_blob_oid("stash@{1}^0:how", "e6d64adb2c7f3eb8feb493b556cc8070dca379a3");		/* not so small and */
+	assert_blob_oid("stash@{1}^0:who", "a0400d4954659306a976567af43125a0b1aa8595");		/* funky world */
+	assert_blob_oid("stash@{1}^0:when", NULL);
+
+	assert_blob_oid("stash@{1}^2:what", "dd7e1c6f0fefe118f0b63d9f10908c460aa317a6");	/* goodbye */
+	assert_blob_oid("stash@{1}^2:how", "e6d64adb2c7f3eb8feb493b556cc8070dca379a3");		/* not so small and */
+	assert_blob_oid("stash@{1}^2:who", "cc628ccd10742baea8241c5924df992b5c019f71");		/* world */
+	assert_blob_oid("stash@{1}^2:when", NULL);
+
+	assert_object_oid("stash@{1}^3", NULL, GIT_OBJ_COMMIT);
+
+	assert_blob_oid("stash@{0}^0:what", "ce013625030ba8dba906f756967f9e9ca394464a");	/* hello */
+	assert_blob_oid("stash@{0}^0:how", "ac790413e2d7a26c3767e78c57bb28716686eebc");		/* small */
+	assert_blob_oid("stash@{0}^0:who", "cc628ccd10742baea8241c5924df992b5c019f71");		/* world */
+	assert_blob_oid("stash@{0}^0:when", NULL);
+
+	assert_blob_oid("stash@{0}^2:what", "ce013625030ba8dba906f756967f9e9ca394464a");	/* hello */
+	assert_blob_oid("stash@{0}^2:how", "ac790413e2d7a26c3767e78c57bb28716686eebc");		/* small */
+	assert_blob_oid("stash@{0}^2:who", "cc628ccd10742baea8241c5924df992b5c019f71");		/* world */
+	assert_blob_oid("stash@{0}^2:when", NULL);
+
+	assert_blob_oid("stash@{0}^3:when", "b6ed15e81e2593d7bb6265eb4a991d29dc3e628b");	/* now */
+}
+
+#define EMPTY_TREE "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
+
+void test_stash_save__including_untracked_without_any_untracked_file_creates_an_empty_tree(void)
+{
+	p_unlink("stash/when");
+
+	assert_status("what", GIT_STATUS_WT_MODIFIED | GIT_STATUS_INDEX_MODIFIED);
+	assert_status("how", GIT_STATUS_INDEX_MODIFIED);
+	assert_status("who", GIT_STATUS_WT_MODIFIED);
+	assert_status("when", GIT_ENOTFOUND);
+	assert_status("just.ignore", GIT_STATUS_IGNORED);
+
+	cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_INCLUDE_UNTRACKED));
+
+	assert_object_oid("stash^3^{tree}", EMPTY_TREE, GIT_OBJ_TREE);
+}
diff --git a/tests-clar/stash/stash_helpers.c b/tests-clar/stash/stash_helpers.c
new file mode 100644
index 0000000..f646ef2
--- /dev/null
+++ b/tests-clar/stash/stash_helpers.c
@@ -0,0 +1,66 @@
+#include "clar_libgit2.h"
+#include "fileops.h"
+
+void commit_staged_files(
+	git_oid *commit_oid,
+	git_index *index,
+	git_signature *signature)
+{
+	git_tree *tree;
+	git_oid tree_oid;
+	git_repository *repo;
+
+	repo = git_index_owner(index);
+
+	cl_git_pass(git_tree_create_fromindex(&tree_oid, index));
+
+	cl_git_pass(git_tree_lookup(&tree, repo, &tree_oid));
+	cl_git_pass(git_commit_create_v(
+		commit_oid,
+		repo,
+		"HEAD",
+		signature,
+		signature,
+		NULL,
+		"Initial commit",
+		tree,
+		0));
+
+	git_tree_free(tree);
+}
+
+void setup_stash(git_repository *repo, git_signature *signature)
+{
+	git_oid commit_oid;
+	git_index *index;
+
+	cl_git_pass(git_repository_index(&index, repo));
+
+	cl_git_mkfile("stash/what", "hello\n");		/* ce013625030ba8dba906f756967f9e9ca394464a */
+	cl_git_mkfile("stash/how", "small\n");		/* ac790413e2d7a26c3767e78c57bb28716686eebc */
+	cl_git_mkfile("stash/who", "world\n");		/* cc628ccd10742baea8241c5924df992b5c019f71 */
+	cl_git_mkfile("stash/when", "now\n");		/* b6ed15e81e2593d7bb6265eb4a991d29dc3e628b */
+	cl_git_mkfile("stash/just.ignore", "me\n");	/* 78925fb1236b98b37a35e9723033e627f97aa88b */
+
+	cl_git_mkfile("stash/.gitignore", "*.ignore\n");
+
+	cl_git_pass(git_index_add(index, "what", 0));
+	cl_git_pass(git_index_add(index, "how", 0));
+	cl_git_pass(git_index_add(index, "who", 0));
+	cl_git_pass(git_index_add(index, ".gitignore", 0));
+	cl_git_pass(git_index_write(index));
+
+	commit_staged_files(&commit_oid, index, signature);
+
+	cl_git_rewritefile("stash/what", "goodbye\n");			/* dd7e1c6f0fefe118f0b63d9f10908c460aa317a6 */
+	cl_git_rewritefile("stash/how", "not so small and\n");	/* e6d64adb2c7f3eb8feb493b556cc8070dca379a3 */
+	cl_git_rewritefile("stash/who", "funky world\n");		/* a0400d4954659306a976567af43125a0b1aa8595 */
+
+	cl_git_pass(git_index_add(index, "what", 0));
+	cl_git_pass(git_index_add(index, "how", 0));
+	cl_git_pass(git_index_write(index));
+
+	cl_git_rewritefile("stash/what", "see you later\n");	/* bc99dc98b3eba0e9157e94769cd4d49cb49de449 */
+
+	git_index_free(index);
+}
diff --git a/tests-clar/stash/stash_helpers.h b/tests-clar/stash/stash_helpers.h
new file mode 100644
index 0000000..bb7fec4
--- /dev/null
+++ b/tests-clar/stash/stash_helpers.h
@@ -0,0 +1,8 @@
+void setup_stash(
+	git_repository *repo,
+	git_signature *signature);
+
+void commit_staged_files(
+	git_oid *commit_oid,
+	git_index *index,
+	git_signature *signature);
\ No newline at end of file