File: test-import.t

package info (click to toggle)
mercurial 6.3.2-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 42,052 kB
  • sloc: python: 199,820; ansic: 46,300; tcl: 3,715; sh: 1,676; lisp: 1,483; cpp: 864; javascript: 649; makefile: 626; xml: 36; sql: 30
file content (2092 lines) | stat: -rw-r--r-- 46,738 bytes parent folder | download
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
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
  $ hg init a
  $ mkdir a/d1
  $ mkdir a/d1/d2
  $ echo line 1 > a/a
  $ echo line 1 > a/d1/d2/a
  $ hg --cwd a ci -Ama
  adding a
  adding d1/d2/a

  $ echo line 2 >> a/a
  $ hg --cwd a ci -u someone -d '1 0' -m'second change'

import with no args:

  $ hg --cwd a import
  abort: need at least one patch to import
  [10]

generate patches for the test

  $ hg --cwd a export tip > exported-tip.patch
  $ hg --cwd a diff -r0:1 > diffed-tip.patch


import exported patch
(this also tests that editor is not invoked, if the patch contains the
commit message and '--edit' is not specified)

  $ hg clone -r0 a b
  adding changesets
  adding manifests
  adding file changes
  added 1 changesets with 2 changes to 2 files
  new changesets 80971e65b431
  updating to branch default
  2 files updated, 0 files merged, 0 files removed, 0 files unresolved
  $ HGEDITOR=cat hg --cwd b import --debug ../exported-tip.patch
  applying ../exported-tip.patch
  Subject: 
  
  Content-Type: text/plain
  found patch at byte 202
  patch generated by hg export
  From: someone
  Date: 1 0
  Node ID: 1d4bd90af0e43687763d158dfa83ff2a4b6c0c32
  message:
  second change
  patching file a
  committing files:
  a
  committing manifest
  committing changelog
  created 1d4bd90af0e4
  updating the branch cache

message and committer and date should be same

  $ hg --cwd b tip
  changeset:   1:1d4bd90af0e4
  tag:         tip
  user:        someone
  date:        Thu Jan 01 00:00:01 1970 +0000
  summary:     second change
  
  $ rm -r b


import exported patch with external patcher
(this also tests that editor is invoked, if the '--edit' is specified,
regardless of the commit message in the patch)

  $ cat > dummypatch.py <<EOF
  > print('patching file a')
  > open('a', 'wb').write(b'line2\n')
  > EOF
  $ hg clone -r0 a b
  adding changesets
  adding manifests
  adding file changes
  added 1 changesets with 2 changes to 2 files
  new changesets 80971e65b431
  updating to branch default
  2 files updated, 0 files merged, 0 files removed, 0 files unresolved
  $ HGEDITOR=cat hg --config ui.patch="\"$PYTHON\" ../dummypatch.py" --cwd b import --edit ../exported-tip.patch
  applying ../exported-tip.patch
  second change
  
  
  HG: Enter commit message.  Lines beginning with 'HG:' are removed.
  HG: Leave message empty to abort commit.
  HG: --
  HG: user: someone
  HG: branch 'default'
  HG: changed a
  $ cat b/a
  line2
  $ rm -r b


import of plain diff should fail without message
(this also tests that editor is invoked, if the patch doesn't contain
the commit message, regardless of '--edit')

  $ hg clone -r0 a b
  adding changesets
  adding manifests
  adding file changes
  added 1 changesets with 2 changes to 2 files
  new changesets 80971e65b431
  updating to branch default
  2 files updated, 0 files merged, 0 files removed, 0 files unresolved
  $ cat > $TESTTMP/editor.sh <<EOF
  > env | grep HGEDITFORM
  > cat \$1
  > EOF
  $ HGEDITOR="sh $TESTTMP/editor.sh" hg --cwd b import ../diffed-tip.patch
  applying ../diffed-tip.patch
  HGEDITFORM=import.normal.normal
  
  
  HG: Enter commit message.  Lines beginning with 'HG:' are removed.
  HG: Leave message empty to abort commit.
  HG: --
  HG: user: test
  HG: branch 'default'
  HG: changed a
  abort: empty commit message
  [10]

Test avoiding editor invocation at applying the patch with --exact,
even if commit message is empty

  $ echo a >> b/a
  $ hg --cwd b commit -m ' '
  $ hg --cwd b tip -T "{node}\n"
  d8804f3f5396d800812f579c8452796a5993bdb2
  $ hg --cwd b export -o ../empty-log.diff .
  $ hg --cwd b update -q -C ".^1"
  $ hg --cwd b --config extensions.strip= strip -q tip
  $ HGEDITOR=cat hg --cwd b import --exact ../empty-log.diff
  applying ../empty-log.diff
  $ hg --cwd b tip -T "{node}\n"
  d8804f3f5396d800812f579c8452796a5993bdb2

  $ rm -r b


import of plain diff should be ok with message

  $ hg clone -r0 a b
  adding changesets
  adding manifests
  adding file changes
  added 1 changesets with 2 changes to 2 files
  new changesets 80971e65b431
  updating to branch default
  2 files updated, 0 files merged, 0 files removed, 0 files unresolved
  $ hg --cwd b import -mpatch ../diffed-tip.patch
  applying ../diffed-tip.patch
  $ rm -r b


import of plain diff with specific date and user
(this also tests that editor is not invoked, if
'--message'/'--logfile' is specified and '--edit' is not)

  $ hg clone -r0 a b
  adding changesets
  adding manifests
  adding file changes
  added 1 changesets with 2 changes to 2 files
  new changesets 80971e65b431
  updating to branch default
  2 files updated, 0 files merged, 0 files removed, 0 files unresolved
  $ hg --cwd b import -mpatch -d '1 0' -u 'user@nowhere.net' ../diffed-tip.patch
  applying ../diffed-tip.patch
  $ hg -R b tip -pv
  changeset:   1:ca68f19f3a40
  tag:         tip
  user:        user@nowhere.net
  date:        Thu Jan 01 00:00:01 1970 +0000
  files:       a
  description:
  patch
  
  
  diff -r 80971e65b431 -r ca68f19f3a40 a
  --- a/a	Thu Jan 01 00:00:00 1970 +0000
  +++ b/a	Thu Jan 01 00:00:01 1970 +0000
  @@ -1,1 +1,2 @@
   line 1
  +line 2
  
  $ rm -r b


import of plain diff should be ok with --no-commit
(this also tests that editor is not invoked, if '--no-commit' is
specified, regardless of '--edit')

  $ hg clone -r0 a b
  adding changesets
  adding manifests
  adding file changes
  added 1 changesets with 2 changes to 2 files
  new changesets 80971e65b431
  updating to branch default
  2 files updated, 0 files merged, 0 files removed, 0 files unresolved
  $ HGEDITOR=cat hg --cwd b import --no-commit --edit ../diffed-tip.patch
  applying ../diffed-tip.patch
  $ hg --cwd b diff --nodates
  diff -r 80971e65b431 a
  --- a/a
  +++ b/a
  @@ -1,1 +1,2 @@
   line 1
  +line 2
  $ rm -r b


import of malformed plain diff should fail

  $ hg clone -r0 a b
  adding changesets
  adding manifests
  adding file changes
  added 1 changesets with 2 changes to 2 files
  new changesets 80971e65b431
  updating to branch default
  2 files updated, 0 files merged, 0 files removed, 0 files unresolved
  $ sed 's/1,1/foo/' < diffed-tip.patch > broken.patch
  $ hg --cwd b import -mpatch ../broken.patch
  applying ../broken.patch
  abort: bad hunk #1
  (check that whitespace in the patch has not been mangled)
  [10]
  $ rm -r b

hg -R repo import
put the clone in a subdir - having a directory named "a"
used to hide a bug.

  $ mkdir dir
  $ hg clone -r0 a dir/b
  adding changesets
  adding manifests
  adding file changes
  added 1 changesets with 2 changes to 2 files
  new changesets 80971e65b431
  updating to branch default
  2 files updated, 0 files merged, 0 files removed, 0 files unresolved
  $ cd dir
  $ hg -R b import ../exported-tip.patch
  applying ../exported-tip.patch
  $ cd ..
  $ rm -r dir


import from stdin

  $ hg clone -r0 a b
  adding changesets
  adding manifests
  adding file changes
  added 1 changesets with 2 changes to 2 files
  new changesets 80971e65b431
  updating to branch default
  2 files updated, 0 files merged, 0 files removed, 0 files unresolved
  $ hg --cwd b import - < exported-tip.patch
  applying patch from stdin
  $ rm -r b


import two patches in one stream

  $ hg init b
  $ hg --cwd a export 0:tip | hg --cwd b import -
  applying patch from stdin
  $ hg --cwd a id
  1d4bd90af0e4 tip
  $ hg --cwd b id
  1d4bd90af0e4 tip
  $ rm -r b


override commit message

  $ hg clone -r0 a b
  adding changesets
  adding manifests
  adding file changes
  added 1 changesets with 2 changes to 2 files
  new changesets 80971e65b431
  updating to branch default
  2 files updated, 0 files merged, 0 files removed, 0 files unresolved
  $ hg --cwd b import -m 'override' - < exported-tip.patch
  applying patch from stdin
  $ hg --cwd b tip | grep override
  summary:     override
  $ rm -r b

  $ cat > mkmsg.py <<EOF
  > import email.message
  > import sys
  > msg = email.message.Message()
  > patch = open(sys.argv[1], 'rb').read()
  > msg.set_payload(b'email commit message\n' + patch)
  > msg['Subject'] = 'email patch'
  > msg['From'] = 'email patcher'
  > open(sys.argv[2], 'wb').write(bytes(msg))
  > EOF


plain diff in email, subject, message body

  $ hg clone -r0 a b
  adding changesets
  adding manifests
  adding file changes
  added 1 changesets with 2 changes to 2 files
  new changesets 80971e65b431
  updating to branch default
  2 files updated, 0 files merged, 0 files removed, 0 files unresolved
  $ "$PYTHON" mkmsg.py diffed-tip.patch msg.patch
  $ hg --cwd b import ../msg.patch
  applying ../msg.patch
  $ hg --cwd b tip | grep email
  user:        email patcher
  summary:     email patch
  $ rm -r b


plain diff in email, no subject, message body

  $ hg clone -r0 a b
  adding changesets
  adding manifests
  adding file changes
  added 1 changesets with 2 changes to 2 files
  new changesets 80971e65b431
  updating to branch default
  2 files updated, 0 files merged, 0 files removed, 0 files unresolved
  $ grep -v '^Subject:' msg.patch | hg --cwd b import -
  applying patch from stdin
  $ rm -r b


plain diff in email, subject, no message body

  $ hg clone -r0 a b
  adding changesets
  adding manifests
  adding file changes
  added 1 changesets with 2 changes to 2 files
  new changesets 80971e65b431
  updating to branch default
  2 files updated, 0 files merged, 0 files removed, 0 files unresolved
  $ grep -v '^email ' msg.patch | hg --cwd b import -
  applying patch from stdin
  $ rm -r b


plain diff in email, no subject, no message body, should fail

  $ hg clone -r0 a b
  adding changesets
  adding manifests
  adding file changes
  added 1 changesets with 2 changes to 2 files
  new changesets 80971e65b431
  updating to branch default
  2 files updated, 0 files merged, 0 files removed, 0 files unresolved
  $ egrep -v '^(Subject|email)' msg.patch | hg --cwd b import -
  applying patch from stdin
  abort: empty commit message
  [10]
  $ rm -r b


hg export in email, should use patch header

  $ hg clone -r0 a b
  adding changesets
  adding manifests
  adding file changes
  added 1 changesets with 2 changes to 2 files
  new changesets 80971e65b431
  updating to branch default
  2 files updated, 0 files merged, 0 files removed, 0 files unresolved
  $ "$PYTHON" mkmsg.py exported-tip.patch msg.patch
  $ cat msg.patch | hg --cwd b import -
  applying patch from stdin
  $ hg --cwd b tip | grep second
  summary:     second change
  $ rm -r b

hg email --plain, should read X-Mercurial-Node header

  $ cat >> a/.hg/hgrc << EOF
  > [extensions]
  > patchbomb =
  > [email]
  > from = foo
  > cc = foo
  > to = bar
  > EOF
  $ hg --cwd a email -m ../tip-plain.mbox --plain --date '1970-1-1 0:1' tip
  this patch series consists of 1 patches.
  
  
  sending [PATCH] second change ...

  $ hg clone -r0 a b -q
  $ hg --cwd b import --debug ../tip-plain.mbox
  applying ../tip-plain.mbox
  Node ID: 1d4bd90af0e43687763d158dfa83ff2a4b6c0c32
  Subject: second change
  From: foo
  Content-Type: text/plain
  found patch at byte 0
  message:
  second change
  patching file a
  committing files:
  a
  committing manifest
  committing changelog
  created de620f6fe949
  updating the branch cache
  $ hg --cwd b tip
  changeset:   1:de620f6fe949
  tag:         tip
  user:        foo
  date:        Thu Jan 01 00:00:00 1970 +0000
  summary:     second change
  
  $ hg --cwd b phase tip
  1: draft
  $ rm -r b


hg import --secret

  $ hg clone -r0 a b -q
  $ hg --cwd b import --no-commit --secret ../exported-tip.patch
  abort: cannot specify both --no-commit and --secret
  [10]
  $ hg --cwd b import --secret ../exported-tip.patch
  applying ../exported-tip.patch
  $ hg --cwd b diff -c . --nodates
  diff -r 80971e65b431 -r 1d4bd90af0e4 a
  --- a/a
  +++ b/a
  @@ -1,1 +1,2 @@
   line 1
  +line 2
  $ hg --cwd b phase
  1: secret
  $ hg --cwd b --config extensions.strip= strip 1 --no-backup --quiet
  $ HGEDITOR=cat hg --cwd b import --secret --edit ../exported-tip.patch
  applying ../exported-tip.patch
  second change
  
  
  HG: Enter commit message.  Lines beginning with 'HG:' are removed.
  HG: Leave message empty to abort commit.
  HG: --
  HG: user: someone
  HG: branch 'default'
  HG: changed a
  $ hg --cwd b diff -c . --nodates
  diff -r 80971e65b431 -r 1d4bd90af0e4 a
  --- a/a
  +++ b/a
  @@ -1,1 +1,2 @@
   line 1
  +line 2
  $ hg --cwd b phase
  1: secret
  $ hg --cwd b --config extensions.strip= strip 1 --no-backup --quiet
  $ hg --cwd b import --bypass --secret ../exported-tip.patch
  applying ../exported-tip.patch
  $ hg --cwd b phase -r tip
  1: secret
  $ hg --cwd b --config extensions.strip= strip 1 --no-backup --quiet
  $ rm -r b


subject: duplicate detection, removal of [PATCH]
The '---' tests the gitsendmail handling without proper mail headers

  $ cat > mkmsg2.py <<EOF
  > import email.message
  > import sys
  > msg = email.message.Message()
  > patch = open(sys.argv[1], 'rb').read()
  > msg.set_payload(b'email patch\n\nnext line\n---\n' + patch)
  > msg['Subject'] = '[PATCH] email patch'
  > msg['From'] = 'email patcher'
  > open(sys.argv[2], 'wb').write(bytes(msg))
  > EOF


plain diff in email, [PATCH] subject, message body with subject

  $ hg clone -r0 a b
  adding changesets
  adding manifests
  adding file changes
  added 1 changesets with 2 changes to 2 files
  new changesets 80971e65b431
  updating to branch default
  2 files updated, 0 files merged, 0 files removed, 0 files unresolved
  $ "$PYTHON" mkmsg2.py diffed-tip.patch msg.patch
  $ cat msg.patch | hg --cwd b import -
  applying patch from stdin
  $ hg --cwd b tip --template '{desc}\n'
  email patch
  
  next line
  $ rm -r b


Issue963: Parent of working dir incorrect after import of multiple
patches and rollback

We weren't backing up the correct dirstate file when importing many
patches: import patch1 patch2; rollback

  $ echo line 3 >> a/a
  $ hg --cwd a ci -m'third change'
  $ hg --cwd a export -o '../patch%R' 1 2
  $ hg clone -qr0 a b
  $ hg --cwd b parents --template 'parent: {rev}\n'
  parent: 0
  $ hg --cwd b import -v ../patch1 ../patch2
  applying ../patch1
  patching file a
  committing files:
  a
  committing manifest
  committing changelog
  created 1d4bd90af0e4
  applying ../patch2
  patching file a
  committing files:
  a
  committing manifest
  committing changelog
  created 6d019af21222
  $ hg --cwd b rollback
  repository tip rolled back to revision 0 (undo import)
  working directory now based on revision 0
  $ hg --cwd b parents --template 'parent: {rev}\n'
  parent: 0

Test that "hg rollback" doesn't restore dirstate to one at the
beginning of the rolled back transaction in not-"parent-gone" case.

invoking pretxncommit hook will cause marking '.hg/dirstate' as a file
to be restored when rolling back, after DirstateTransactionPlan (see wiki
page for detail).

  $ hg --cwd b branch -q foobar
  $ hg --cwd b commit -m foobar
  $ hg --cwd b update 0 -q
  $ hg --cwd b import ../patch1 ../patch2 --config hooks.pretxncommit=true
  applying ../patch1
  applying ../patch2
  $ hg --cwd b update -q 1
  $ hg --cwd b rollback -q
  $ hg --cwd b parents --template 'parent: {rev}\n'
  parent: 1

  $ hg --cwd b update -q -C 0
  $ hg --cwd b --config extensions.strip= strip -q 1

Test visibility of in-memory dirstate changes inside transaction to
external process

  $ echo foo > a/foo
  $ hg --cwd a commit -A -m 'adding foo' foo
  $ hg --cwd a export -o '../patch%R' 3

  $ cat > $TESTTMP/checkvisibility.sh <<EOF
  > echo "===="
  > hg parents --template "VISIBLE {rev}:{node|short}\n"
  > hg status -amr
  > # test that pending changes are hidden
  > unset HG_PENDING
  > hg parents --template "ACTUAL  {rev}:{node|short}\n"
  > hg status -amr
  > echo "===="
  > EOF

== test visibility to external editor

  $ (cd b && sh "$TESTTMP/checkvisibility.sh")
  ====
  VISIBLE 0:80971e65b431
  ACTUAL  0:80971e65b431
  ====

  $ HGEDITOR="sh $TESTTMP/checkvisibility.sh" hg --cwd b import -v --edit ../patch1 ../patch2 ../patch3
  applying ../patch1
  patching file a
  ====
  VISIBLE 0:80971e65b431
  M a
  ACTUAL  0:80971e65b431
  M a
  ====
  committing files:
  a
  committing manifest
  committing changelog
  created 1d4bd90af0e4
  applying ../patch2
  patching file a
  ====
  VISIBLE 1:1d4bd90af0e4
  M a
  ACTUAL  0:80971e65b431
  M a
  ====
  committing files:
  a
  committing manifest
  committing changelog
  created 6d019af21222
  applying ../patch3
  patching file foo
  adding foo
  ====
  VISIBLE 2:6d019af21222
  A foo
  ACTUAL  0:80971e65b431
  M a
  ====
  committing files:
  foo
  committing manifest
  committing changelog
  created 55e3f75b2378

  $ hg --cwd b rollback -q

(content of file "a" is already changed and it should be recognized as
"M", even though dirstate is restored to one before "hg import")

  $ (cd b && sh "$TESTTMP/checkvisibility.sh")
  ====
  VISIBLE 0:80971e65b431
  M a
  ACTUAL  0:80971e65b431
  M a
  ====
  $ hg --cwd b revert --no-backup a
  $ rm -f b/foo

== test visibility to precommit external hook

  $ cat >> b/.hg/hgrc <<EOF
  > [hooks]
  > precommit.visibility = sh $TESTTMP/checkvisibility.sh
  > EOF

  $ (cd b && sh "$TESTTMP/checkvisibility.sh")
  ====
  VISIBLE 0:80971e65b431
  ACTUAL  0:80971e65b431
  ====

  $ hg --cwd b import ../patch1 ../patch2 ../patch3
  applying ../patch1
  ====
  VISIBLE 0:80971e65b431
  M a
  ACTUAL  0:80971e65b431
  M a
  ====
  applying ../patch2
  ====
  VISIBLE 1:1d4bd90af0e4
  M a
  ACTUAL  0:80971e65b431
  M a
  ====
  applying ../patch3
  ====
  VISIBLE 2:6d019af21222
  A foo
  ACTUAL  0:80971e65b431
  M a
  ====

  $ hg --cwd b rollback -q
  $ (cd b && sh "$TESTTMP/checkvisibility.sh")
  ====
  VISIBLE 0:80971e65b431
  M a
  ACTUAL  0:80971e65b431
  M a
  ====
  $ hg --cwd b revert --no-backup a
  $ rm -f b/foo

  $ cat >> b/.hg/hgrc <<EOF
  > [hooks]
  > precommit.visibility =
  > EOF

== test visibility to pretxncommit external hook

  $ cat >> b/.hg/hgrc <<EOF
  > [hooks]
  > pretxncommit.visibility = sh $TESTTMP/checkvisibility.sh
  > EOF

  $ (cd b && sh "$TESTTMP/checkvisibility.sh")
  ====
  VISIBLE 0:80971e65b431
  ACTUAL  0:80971e65b431
  ====

  $ hg --cwd b import ../patch1 ../patch2 ../patch3
  applying ../patch1
  ====
  VISIBLE 0:80971e65b431
  M a
  ACTUAL  0:80971e65b431
  M a
  ====
  applying ../patch2
  ====
  VISIBLE 1:1d4bd90af0e4
  M a
  ACTUAL  0:80971e65b431
  M a
  ====
  applying ../patch3
  ====
  VISIBLE 2:6d019af21222
  A foo
  ACTUAL  0:80971e65b431
  M a
  ====

  $ hg --cwd b rollback -q
  $ (cd b && sh "$TESTTMP/checkvisibility.sh")
  ====
  VISIBLE 0:80971e65b431
  M a
  ACTUAL  0:80971e65b431
  M a
  ====
  $ hg --cwd b revert --no-backup a
  $ rm -f b/foo

  $ cat >> b/.hg/hgrc <<EOF
  > [hooks]
  > pretxncommit.visibility =
  > EOF

  $ rm -r b


importing a patch in a subdirectory failed at the commit stage

  $ echo line 2 >> a/d1/d2/a
  $ hg --cwd a ci -u someoneelse -d '1 0' -m'subdir change'

hg import in a subdirectory

  $ hg clone -r0 a b
  adding changesets
  adding manifests
  adding file changes
  added 1 changesets with 2 changes to 2 files
  new changesets 80971e65b431
  updating to branch default
  2 files updated, 0 files merged, 0 files removed, 0 files unresolved
  $ hg --cwd a export tip > tmp
  $ sed -e 's/d1\/d2\///' < tmp > subdir-tip.patch
  $ dir=`pwd`
  $ cd b/d1/d2 2>&1 > /dev/null
  $ hg import  ../../../subdir-tip.patch
  applying ../../../subdir-tip.patch
  $ cd "$dir"

message should be 'subdir change'
committer should be 'someoneelse'

  $ hg --cwd b tip
  changeset:   1:3577f5aea227
  tag:         tip
  user:        someoneelse
  date:        Thu Jan 01 00:00:01 1970 +0000
  summary:     subdir change
  

should be empty

  $ hg --cwd b status


Test fuzziness (ambiguous patch location, fuzz=2)

  $ hg init fuzzy
  $ cd fuzzy
  $ echo line1 > a
  $ echo line0 >> a
  $ echo line3 >> a
  $ hg ci -Am adda
  adding a
  $ echo line1 > a
  $ echo line2 >> a
  $ echo line0 >> a
  $ echo line3 >> a
  $ hg ci -m change a
  $ hg export tip > fuzzy-tip.patch
  $ hg up -C 0
  1 files updated, 0 files merged, 0 files removed, 0 files unresolved
  $ echo line1 > a
  $ echo line0 >> a
  $ echo line1 >> a
  $ echo line0 >> a
  $ hg ci -m brancha
  created new head
  $ hg import --config patch.fuzz=0 -v fuzzy-tip.patch
  applying fuzzy-tip.patch
  patching file a
  Hunk #1 FAILED at 0
  1 out of 1 hunks FAILED -- saving rejects to file a.rej
  abort: patch failed to apply
  [20]
  $ hg import --no-commit -v fuzzy-tip.patch
  applying fuzzy-tip.patch
  patching file a
  Hunk #1 succeeded at 2 with fuzz 1 (offset 0 lines).
  applied to working directory
  $ hg revert -a
  reverting a

Test --exact failure

  $ sed 's/^# Parent .*/# Parent '"`hg log -r. -T '{node}'`"'/' \
  > < fuzzy-tip.patch > fuzzy-reparent.patch
  $ hg import --config patch.fuzz=0 --exact fuzzy-reparent.patch
  applying fuzzy-reparent.patch
  patching file a
  Hunk #1 FAILED at 0
  1 out of 1 hunks FAILED -- saving rejects to file a.rej
  abort: patch failed to apply
  [20]
  $ hg up -qC
  $ hg import --config patch.fuzz=2 --exact fuzzy-reparent.patch
  applying fuzzy-reparent.patch
  patching file a
  Hunk #1 succeeded at 2 with fuzz 1 (offset 0 lines).
  transaction abort!
  rollback completed
  abort: patch is damaged or loses information
  [255]
  $ hg up -qC

  $ grep '^#' fuzzy-tip.patch > empty.patch
  $ cat <<'EOF' >> empty.patch
  > change
  > 
  > diff -r bb90ef1daa38 -r 0e9b883378d4 a
  > --- a/a	Thu Jan 01 00:00:00 1970 +0000
  > --- b/a	Thu Jan 01 00:00:00 1970 +0000
  > EOF
  $ hg import --exact empty.patch
  applying empty.patch
  1 files updated, 0 files merged, 0 files removed, 0 files unresolved
  abort: patch is damaged or loses information
  [255]
  $ hg up -qC

import with --no-commit should have written .hg/last-message.txt

  $ cat .hg/last-message.txt
  change (no-eol)


test fuzziness with eol=auto

  $ hg --config patch.eol=auto import --no-commit -v fuzzy-tip.patch
  applying fuzzy-tip.patch
  patching file a
  Hunk #1 succeeded at 2 with fuzz 1 (offset 0 lines).
  applied to working directory
  $ cd ..


Test hunk touching empty files (issue906)

  $ hg init empty
  $ cd empty
  $ touch a
  $ touch b1
  $ touch c1
  $ echo d > d
  $ hg ci -Am init
  adding a
  adding b1
  adding c1
  adding d
  $ echo a > a
  $ echo b > b1
  $ hg mv b1 b2
  $ echo c > c1
  $ hg copy c1 c2
  $ rm d
  $ touch d
  $ hg diff --git
  diff --git a/a b/a
  --- a/a
  +++ b/a
  @@ -0,0 +1,1 @@
  +a
  diff --git a/b1 b/b2
  rename from b1
  rename to b2
  --- a/b1
  +++ b/b2
  @@ -0,0 +1,1 @@
  +b
  diff --git a/c1 b/c1
  --- a/c1
  +++ b/c1
  @@ -0,0 +1,1 @@
  +c
  diff --git a/c1 b/c2
  copy from c1
  copy to c2
  --- a/c1
  +++ b/c2
  @@ -0,0 +1,1 @@
  +c
  diff --git a/d b/d
  --- a/d
  +++ b/d
  @@ -1,1 +0,0 @@
  -d
  $ hg ci -m empty
  $ hg export --git tip > empty.diff
  $ hg up -C 0
  4 files updated, 0 files merged, 2 files removed, 0 files unresolved
  $ hg import empty.diff
  applying empty.diff
  $ for name in a b1 b2 c1 c2 d; do
  >   echo % $name file
  >   test -f $name && cat $name
  >   done
  % a file
  a
  % b1 file
  % b2 file
  b
  % c1 file
  c
  % c2 file
  c
  % d file
  $ cd ..


Test importing a patch ending with a binary file removal

  $ hg init binaryremoval
  $ cd binaryremoval
  $ echo a > a
  $ "$PYTHON" -c "open('b', 'wb').write(b'a\x00b')"
  $ hg ci -Am addall
  adding a
  adding b
  $ hg rm a
  $ hg rm b
  $ hg st
  R a
  R b
  $ hg ci -m remove
  $ hg export --git . > remove.diff
  $ cat remove.diff | grep git
  diff --git a/a b/a
  diff --git a/b b/b
  $ hg up -C 0
  2 files updated, 0 files merged, 0 files removed, 0 files unresolved
  $ hg import remove.diff
  applying remove.diff
  $ hg manifest
  $ cd ..


Issue927: test update+rename with common name

  $ hg init t
  $ cd t
  $ touch a
  $ hg ci -Am t
  adding a
  $ echo a > a

Here, bfile.startswith(afile)

  $ hg copy a a2
  $ hg ci -m copya
  $ hg export --git tip > copy.diff
  $ hg up -C 0
  1 files updated, 0 files merged, 1 files removed, 0 files unresolved
  $ hg import copy.diff
  applying copy.diff

a should contain an 'a'

  $ cat a
  a

and a2 should have duplicated it

  $ cat a2
  a
  $ cd ..


test -p0

  $ hg init p0
  $ cd p0
  $ echo a > a
  $ hg ci -Am t
  adding a
  $ hg import -p foo
  abort: invalid value 'foo' for option -p, expected int
  [10]
  $ hg import -p0 - << EOF
  > foobar
  > --- a	Sat Apr 12 22:43:58 2008 -0400
  > +++ a	Sat Apr 12 22:44:05 2008 -0400
  > @@ -1,1 +1,1 @@
  > -a
  > +bb
  > EOF
  applying patch from stdin
  $ hg status
  $ cat a
  bb

test --prefix

  $ mkdir -p dir/dir2
  $ echo b > dir/dir2/b
  $ hg ci -Am b
  adding dir/dir2/b
  $ hg import -p2 --prefix dir - << EOF
  > foobar
  > --- drop1/drop2/dir2/b
  > +++ drop1/drop2/dir2/b
  > @@ -1,1 +1,1 @@
  > -b
  > +cc
  > EOF
  applying patch from stdin
  $ hg status
  $ cat dir/dir2/b
  cc
  $ cd ..


test paths outside repo root

  $ mkdir outside
  $ touch outside/foo
  $ hg init inside
  $ cd inside
  $ hg import - <<EOF
  > diff --git a/a b/b
  > rename from ../outside/foo
  > rename to bar
  > EOF
  applying patch from stdin
  abort: path contains illegal component: ../outside/foo
  [10]
  $ cd ..


test import with similarity and git and strip (issue295 et al.)

  $ hg init sim
  $ cd sim
  $ echo 'this is a test' > a
  $ hg ci -Ama
  adding a
  $ cat > ../rename.diff <<EOF
  > diff --git a/foo/a b/foo/a
  > deleted file mode 100644
  > --- a/foo/a
  > +++ /dev/null
  > @@ -1,1 +0,0 @@
  > -this is a test
  > diff --git a/foo/b b/foo/b
  > new file mode 100644
  > --- /dev/null
  > +++ b/foo/b
  > @@ -0,0 +1,2 @@
  > +this is a test
  > +foo
  > EOF
  $ hg import --no-commit -v -s 1 ../rename.diff -p2
  applying ../rename.diff
  patching file a
  patching file b
  adding b
  recording removal of a as rename to b (88% similar)
  applied to working directory
  $ echo 'mod b' > b
  $ hg st -C
  A b
    a
  R a
  $ hg revert -a
  forgetting b
  undeleting a
  $ cat b
  mod b
  $ rm b
  $ hg import --no-commit -v -s 100 ../rename.diff -p2
  applying ../rename.diff
  patching file a
  patching file b
  adding b
  applied to working directory
  $ hg st -C
  A b
  R a
  $ cd ..


Issue1495: add empty file from the end of patch

  $ hg init addemptyend
  $ cd addemptyend
  $ touch a
  $ hg addremove
  adding a
  $ hg ci -m "commit"
  $ cat > a.patch <<EOF
  > add a, b
  > diff --git a/a b/a
  > --- a/a
  > +++ b/a
  > @@ -0,0 +1,1 @@
  > +a
  > diff --git a/b b/b
  > new file mode 100644
  > EOF
  $ hg import --no-commit a.patch
  applying a.patch

apply a good patch followed by an empty patch (mainly to ensure
that dirstate is *not* updated when import crashes)
  $ hg update -q -C .
  $ rm b
  $ touch empty.patch
  $ hg import a.patch empty.patch
  applying a.patch
  applying empty.patch
  transaction abort!
  rollback completed
  abort: empty.patch: no diffs found
  [10]
  $ hg tip --template '{rev}  {desc|firstline}\n'
  0  commit
  $ hg -q status
  M a
  $ cd ..

create file when source is not /dev/null

  $ cat > create.patch <<EOF
  > diff -Naur proj-orig/foo proj-new/foo
  > --- proj-orig/foo       1969-12-31 16:00:00.000000000 -0800
  > +++ proj-new/foo        2009-07-17 16:50:45.801368000 -0700
  > @@ -0,0 +1,1 @@
  > +a
  > EOF

some people have patches like the following too

  $ cat > create2.patch <<EOF
  > diff -Naur proj-orig/foo proj-new/foo
  > --- proj-orig/foo.orig  1969-12-31 16:00:00.000000000 -0800
  > +++ proj-new/foo        2009-07-17 16:50:45.801368000 -0700
  > @@ -0,0 +1,1 @@
  > +a
  > EOF
  $ hg init oddcreate
  $ cd oddcreate
  $ hg import --no-commit ../create.patch
  applying ../create.patch
  $ cat foo
  a
  $ rm foo
  $ hg revert foo
  $ hg import --no-commit ../create2.patch
  applying ../create2.patch
  $ cat foo
  a

  $ cd ..

Issue1859: first line mistaken for email headers

  $ hg init emailconfusion
  $ cd emailconfusion
  $ cat > a.patch <<EOF
  > module: summary
  > 
  > description
  > 
  > 
  > diff -r 000000000000 -r 9b4c1e343b55 test.txt
  > --- /dev/null
  > +++ b/a
  > @@ -0,0 +1,1 @@
  > +a
  > EOF
  $ hg import -d '0 0' a.patch
  applying a.patch
  $ hg parents -v
  changeset:   0:5a681217c0ad
  tag:         tip
  user:        test
  date:        Thu Jan 01 00:00:00 1970 +0000
  files:       a
  description:
  module: summary
  
  description
  
  
  $ cd ..


in commit message

  $ hg init commitconfusion
  $ cd commitconfusion
  $ cat > a.patch <<EOF
  > module: summary
  > 
  > --- description
  > 
  > diff --git a/a b/a
  > new file mode 100644
  > --- /dev/null
  > +++ b/a
  > @@ -0,0 +1,1 @@
  > +a
  > EOF
  > hg import -d '0 0' a.patch
  > hg parents -v
  > cd ..
  > 
  > echo '% tricky header splitting'
  > cat > trickyheaders.patch <<EOF
  > From: User A <user@a>
  > Subject: [PATCH] from: tricky!
  > 
  > # HG changeset patch
  > # User User B
  > # Date 1266264441 18000
  > # Branch stable
  > # Node ID f2be6a1170ac83bf31cb4ae0bad00d7678115bc0
  > # Parent  0000000000000000000000000000000000000000
  > from: tricky!
  > 
  > That is not a header.
  > 
  > diff -r 000000000000 -r f2be6a1170ac foo
  > --- /dev/null
  > +++ b/foo
  > @@ -0,0 +1,1 @@
  > +foo
  > EOF
  applying a.patch
  changeset:   0:f34d9187897d
  tag:         tip
  user:        test
  date:        Thu Jan 01 00:00:00 1970 +0000
  files:       a
  description:
  module: summary
  
  
  % tricky header splitting

  $ hg init trickyheaders
  $ cd trickyheaders
  $ hg import -d '0 0' ../trickyheaders.patch
  applying ../trickyheaders.patch
  $ hg export --git tip
  # HG changeset patch
  # User User B
  # Date 0 0
  #      Thu Jan 01 00:00:00 1970 +0000
  # Node ID eb56ab91903632294ac504838508cb370c0901d2
  # Parent  0000000000000000000000000000000000000000
  from: tricky!
  
  That is not a header.
  
  diff --git a/foo b/foo
  new file mode 100644
  --- /dev/null
  +++ b/foo
  @@ -0,0 +1,1 @@
  +foo
  $ cd ..


Issue2102: hg export and hg import speak different languages

  $ hg init issue2102
  $ cd issue2102
  $ mkdir -p src/cmd/gc
  $ touch src/cmd/gc/mksys.bash
  $ hg ci -Am init
  adding src/cmd/gc/mksys.bash
  $ hg import - <<EOF
  > # HG changeset patch
  > # User Rob Pike
  > # Date 1216685449 25200
  > # Node ID 03aa2b206f499ad6eb50e6e207b9e710d6409c98
  > # Parent  93d10138ad8df586827ca90b4ddb5033e21a3a84
  > help management of empty pkg and lib directories in perforce
  > 
  > R=gri
  > DELTA=4  (4 added, 0 deleted, 0 changed)
  > OCL=13328
  > CL=13328
  > 
  > diff --git a/lib/place-holder b/lib/place-holder
  > new file mode 100644
  > --- /dev/null
  > +++ b/lib/place-holder
  > @@ -0,0 +1,2 @@
  > +perforce does not maintain empty directories.
  > +this file helps.
  > diff --git a/pkg/place-holder b/pkg/place-holder
  > new file mode 100644
  > --- /dev/null
  > +++ b/pkg/place-holder
  > @@ -0,0 +1,2 @@
  > +perforce does not maintain empty directories.
  > +this file helps.
  > diff --git a/src/cmd/gc/mksys.bash b/src/cmd/gc/mksys.bash
  > old mode 100644
  > new mode 100755
  > EOF
  applying patch from stdin

#if execbit

  $ hg sum
  parent: 1:d59915696727 tip
   help management of empty pkg and lib directories in perforce
  branch: default
  commit: (clean)
  update: (current)
  phases: 2 draft

  $ hg diff --git -c tip
  diff --git a/lib/place-holder b/lib/place-holder
  new file mode 100644
  --- /dev/null
  +++ b/lib/place-holder
  @@ -0,0 +1,2 @@
  +perforce does not maintain empty directories.
  +this file helps.
  diff --git a/pkg/place-holder b/pkg/place-holder
  new file mode 100644
  --- /dev/null
  +++ b/pkg/place-holder
  @@ -0,0 +1,2 @@
  +perforce does not maintain empty directories.
  +this file helps.
  diff --git a/src/cmd/gc/mksys.bash b/src/cmd/gc/mksys.bash
  old mode 100644
  new mode 100755

#else

  $ hg sum
  parent: 1:28f089cc9ccc tip
   help management of empty pkg and lib directories in perforce
  branch: default
  commit: (clean)
  update: (current)
  phases: 2 draft

  $ hg diff --git -c tip
  diff --git a/lib/place-holder b/lib/place-holder
  new file mode 100644
  --- /dev/null
  +++ b/lib/place-holder
  @@ -0,0 +1,2 @@
  +perforce does not maintain empty directories.
  +this file helps.
  diff --git a/pkg/place-holder b/pkg/place-holder
  new file mode 100644
  --- /dev/null
  +++ b/pkg/place-holder
  @@ -0,0 +1,2 @@
  +perforce does not maintain empty directories.
  +this file helps.

/* The mode change for mksys.bash is missing here, because on platforms  */
/* that don't support execbits, mode changes in patches are ignored when */
/* they are imported. This is obviously also the reason for why the hash */
/* in the created changeset is different to the one you see above the    */
/* #else clause */

#endif
  $ cd ..


diff lines looking like headers

  $ hg init difflineslikeheaders
  $ cd difflineslikeheaders
  $ echo a >a
  $ echo b >b
  $ echo c >c
  $ hg ci -Am1
  adding a
  adding b
  adding c

  $ echo "key: value" >>a
  $ echo "key: value" >>b
  $ echo "foo" >>c
  $ hg ci -m2

  $ hg up -C 0
  3 files updated, 0 files merged, 0 files removed, 0 files unresolved
  $ hg diff --git -c1 >want
  $ hg diff -c1 | hg import --no-commit -
  applying patch from stdin
  $ hg diff --git >have
  $ diff want have
  $ cd ..

import a unified diff with no lines of context (diff -U0)

  $ hg init diffzero
  $ cd diffzero
  $ cat > f << EOF
  > c2
  > c4
  > c5
  > EOF
  $ hg commit -Am0
  adding f

  $ hg import --no-commit - << EOF
  > # HG changeset patch
  > # User test
  > # Date 0 0
  > # Node ID f4974ab632f3dee767567b0576c0ec9a4508575c
  > # Parent  8679a12a975b819fae5f7ad3853a2886d143d794
  > 1
  > diff -r 8679a12a975b -r f4974ab632f3 f
  > --- a/f	Thu Jan 01 00:00:00 1970 +0000
  > +++ b/f	Thu Jan 01 00:00:00 1970 +0000
  > @@ -0,0 +1,1 @@
  > +c1
  > @@ -1,0 +3,1 @@
  > +c3
  > @@ -3,1 +4,0 @@
  > -c5
  > EOF
  applying patch from stdin

  $ cat f
  c1
  c2
  c3
  c4

  $ cd ..

commit message that looks like a diff header (issue1879)

  $ hg init headerlikemsg
  $ cd headerlikemsg
  $ touch empty
  $ echo nonempty >> nonempty
  $ hg ci -qAl - <<EOF
  > blah blah
  > diff blah
  > blah blah
  > EOF
  $ hg --config diff.git=1 log -pv
  changeset:   0:c6ef204ef767
  tag:         tip
  user:        test
  date:        Thu Jan 01 00:00:00 1970 +0000
  files:       empty nonempty
  description:
  blah blah
  diff blah
  blah blah
  
  
  diff --git a/empty b/empty
  new file mode 100644
  diff --git a/nonempty b/nonempty
  new file mode 100644
  --- /dev/null
  +++ b/nonempty
  @@ -0,0 +1,1 @@
  +nonempty
  

 (without --git, empty file is lost, but commit message should be preserved)

  $ hg init plain
  $ hg export 0 | hg -R plain import -
  applying patch from stdin
  $ hg --config diff.git=1 -R plain log -pv
  changeset:   0:60a2d231e71f
  tag:         tip
  user:        test
  date:        Thu Jan 01 00:00:00 1970 +0000
  files:       nonempty
  description:
  blah blah
  diff blah
  blah blah
  
  
  diff --git a/nonempty b/nonempty
  new file mode 100644
  --- /dev/null
  +++ b/nonempty
  @@ -0,0 +1,1 @@
  +nonempty
  

 (with --git, patch contents should be fully preserved)

  $ hg init git
  $ hg --config diff.git=1 export 0 | hg -R git import -
  applying patch from stdin
  $ hg --config diff.git=1 -R git log -pv
  changeset:   0:c6ef204ef767
  tag:         tip
  user:        test
  date:        Thu Jan 01 00:00:00 1970 +0000
  files:       empty nonempty
  description:
  blah blah
  diff blah
  blah blah
  
  
  diff --git a/empty b/empty
  new file mode 100644
  diff --git a/nonempty b/nonempty
  new file mode 100644
  --- /dev/null
  +++ b/nonempty
  @@ -0,0 +1,1 @@
  +nonempty
  

  $ cd ..

no segfault while importing a unified diff which start line is zero but chunk
size is non-zero

  $ hg init startlinezero
  $ cd startlinezero
  $ echo foo > foo
  $ hg commit -Amfoo
  adding foo

  $ hg import --no-commit - << EOF
  > diff a/foo b/foo
  > --- a/foo
  > +++ b/foo
  > @@ -0,1 +0,1 @@
  >  foo
  > EOF
  applying patch from stdin

  $ cd ..

Test corner case involving fuzz and skew

  $ hg init morecornercases
  $ cd morecornercases

  $ cat > 01-no-context-beginning-of-file.diff <<EOF
  > diff --git a/a b/a
  > --- a/a
  > +++ b/a
  > @@ -1,0 +1,1 @@
  > +line
  > EOF

  $ cat > 02-no-context-middle-of-file.diff <<EOF
  > diff --git a/a b/a
  > --- a/a
  > +++ b/a
  > @@ -1,1 +1,1 @@
  > -2
  > +add some skew
  > @@ -2,0 +2,1 @@
  > +line
  > EOF

  $ cat > 03-no-context-end-of-file.diff <<EOF
  > diff --git a/a b/a
  > --- a/a
  > +++ b/a
  > @@ -10,0 +10,1 @@
  > +line
  > EOF

  $ cat > 04-middle-of-file-completely-fuzzed.diff <<EOF
  > diff --git a/a b/a
  > --- a/a
  > +++ b/a
  > @@ -1,1 +1,1 @@
  > -2
  > +add some skew
  > @@ -2,2 +2,3 @@
  >  not matching, should fuzz
  >  ... a bit
  > +line
  > EOF

  $ cat > a <<EOF
  > 1
  > 2
  > 3
  > 4
  > EOF
  $ hg ci -Am adda a
  $ for p in *.diff; do
  >   hg import -v --no-commit $p
  >   cat a
  >   hg revert -aqC a
  >   # patch -p1 < $p
  >   # cat a
  >   # hg revert -aC a
  > done
  applying 01-no-context-beginning-of-file.diff
  patching file a
  applied to working directory
  1
  line
  2
  3
  4
  applying 02-no-context-middle-of-file.diff
  patching file a
  Hunk #1 succeeded at 2 (offset 1 lines).
  Hunk #2 succeeded at 4 (offset 1 lines).
  applied to working directory
  1
  add some skew
  3
  line
  4
  applying 03-no-context-end-of-file.diff
  patching file a
  Hunk #1 succeeded at 5 (offset -6 lines).
  applied to working directory
  1
  2
  3
  4
  line
  applying 04-middle-of-file-completely-fuzzed.diff
  patching file a
  Hunk #1 succeeded at 2 (offset 1 lines).
  Hunk #2 succeeded at 5 with fuzz 2 (offset 1 lines).
  applied to working directory
  1
  add some skew
  3
  4
  line
  $ cd ..

Test partial application
------------------------

prepare a stack of patches depending on each other

  $ hg init partial
  $ cd partial
  $ cat << EOF > a
  > one
  > two
  > three
  > four
  > five
  > six
  > seven
  > EOF
  $ hg add a
  $ echo 'b' > b
  $ hg add b
  $ hg commit -m 'initial' -u Babar
  $ cat << EOF > a
  > one
  > two
  > 3
  > four
  > five
  > six
  > seven
  > EOF
  $ hg commit -m 'three' -u Celeste
  $ cat << EOF > a
  > one
  > two
  > 3
  > 4
  > five
  > six
  > seven
  > EOF
  $ hg commit -m 'four' -u Rataxes
  $ cat << EOF > a
  > one
  > two
  > 3
  > 4
  > 5
  > six
  > seven
  > EOF
  $ echo bb >> b
  $ hg commit -m 'five' -u Arthur
  $ echo 'Babar' > jungle
  $ hg add jungle
  $ hg ci -m 'jungle' -u Zephir
  $ echo 'Celeste' >> jungle
  $ hg ci -m 'extended jungle' -u Cornelius
  $ hg log -G --template '{desc|firstline} [{author}] {diffstat}\n'
  @  extended jungle [Cornelius] 1: +1/-0
  |
  o  jungle [Zephir] 1: +1/-0
  |
  o  five [Arthur] 2: +2/-1
  |
  o  four [Rataxes] 1: +1/-1
  |
  o  three [Celeste] 1: +1/-1
  |
  o  initial [Babar] 2: +8/-0
  
Adding those config options should not change the output of diffstat. Bugfix #4755.

  $ hg log -r . --template '{diffstat}\n'
  1: +1/-0
  $ hg log -r . --template '{diffstat}\n' --config diff.git=1 \
  >   --config diff.noprefix=1
  1: +1/-0

Importing with some success and some errors:

  $ hg update --rev 'desc(initial)'
  2 files updated, 0 files merged, 1 files removed, 0 files unresolved
  $ hg export --rev 'desc(five)' | hg import --partial -
  applying patch from stdin
  patching file a
  Hunk #1 FAILED at 1
  1 out of 1 hunks FAILED -- saving rejects to file a.rej
  patch applied partially
  (fix the .rej files and run `hg commit --amend`)
  [1]

  $ hg log -G --template '{desc|firstline} [{author}] {diffstat}\n'
  @  five [Arthur] 1: +1/-0
  |
  | o  extended jungle [Cornelius] 1: +1/-0
  | |
  | o  jungle [Zephir] 1: +1/-0
  | |
  | o  five [Arthur] 2: +2/-1
  | |
  | o  four [Rataxes] 1: +1/-1
  | |
  | o  three [Celeste] 1: +1/-1
  |/
  o  initial [Babar] 2: +8/-0
  
  $ hg export
  # HG changeset patch
  # User Arthur
  # Date 0 0
  #      Thu Jan 01 00:00:00 1970 +0000
  # Node ID 26e6446bb2526e2be1037935f5fca2b2706f1509
  # Parent  8e4f0351909eae6b9cf68c2c076cb54c42b54b2e
  five
  
  diff -r 8e4f0351909e -r 26e6446bb252 b
  --- a/b	Thu Jan 01 00:00:00 1970 +0000
  +++ b/b	Thu Jan 01 00:00:00 1970 +0000
  @@ -1,1 +1,2 @@
   b
  +bb
  $ hg status -c .
  C a
  C b
  $ ls -A
  .hg
  a
  a.rej
  b

Importing with zero success:

  $ hg update --rev 'desc(initial)'
  1 files updated, 0 files merged, 0 files removed, 0 files unresolved
  $ hg export --rev 'desc(four)' | hg import --partial -
  applying patch from stdin
  patching file a
  Hunk #1 FAILED at 0
  1 out of 1 hunks FAILED -- saving rejects to file a.rej
  patch applied partially
  (fix the .rej files and run `hg commit --amend`)
  [1]

  $ hg log -G --template '{desc|firstline} [{author}] {diffstat}\n'
  @  four [Rataxes] 0: +0/-0
  |
  | o  five [Arthur] 1: +1/-0
  |/
  | o  extended jungle [Cornelius] 1: +1/-0
  | |
  | o  jungle [Zephir] 1: +1/-0
  | |
  | o  five [Arthur] 2: +2/-1
  | |
  | o  four [Rataxes] 1: +1/-1
  | |
  | o  three [Celeste] 1: +1/-1
  |/
  o  initial [Babar] 2: +8/-0
  
  $ hg export
  # HG changeset patch
  # User Rataxes
  # Date 0 0
  #      Thu Jan 01 00:00:00 1970 +0000
  # Node ID cb9b1847a74d9ad52e93becaf14b98dbcc274e1e
  # Parent  8e4f0351909eae6b9cf68c2c076cb54c42b54b2e
  four
  
  $ hg status -c .
  C a
  C b
  $ ls -A
  .hg
  a
  a.rej
  b

Importing with unknown file:

  $ hg update --rev 'desc(initial)'
  0 files updated, 0 files merged, 0 files removed, 0 files unresolved
  $ hg export --rev 'desc("extended jungle")' | hg import --partial -
  applying patch from stdin
  unable to find 'jungle' for patching
  (use '--prefix' to apply patch relative to the current directory)
  1 out of 1 hunks FAILED -- saving rejects to file jungle.rej
  patch applied partially
  (fix the .rej files and run `hg commit --amend`)
  [1]

  $ hg log -G --template '{desc|firstline} [{author}] {diffstat}\n'
  @  extended jungle [Cornelius] 0: +0/-0
  |
  | o  four [Rataxes] 0: +0/-0
  |/
  | o  five [Arthur] 1: +1/-0
  |/
  | o  extended jungle [Cornelius] 1: +1/-0
  | |
  | o  jungle [Zephir] 1: +1/-0
  | |
  | o  five [Arthur] 2: +2/-1
  | |
  | o  four [Rataxes] 1: +1/-1
  | |
  | o  three [Celeste] 1: +1/-1
  |/
  o  initial [Babar] 2: +8/-0
  
  $ hg export
  # HG changeset patch
  # User Cornelius
  # Date 0 0
  #      Thu Jan 01 00:00:00 1970 +0000
  # Node ID 1fb1f86bef43c5a75918178f8d23c29fb0a7398d
  # Parent  8e4f0351909eae6b9cf68c2c076cb54c42b54b2e
  extended jungle
  
  $ hg status -c .
  C a
  C b
  $ ls -A
  .hg
  a
  a.rej
  b
  jungle.rej

Importing multiple failing patches:

  $ hg update --rev 'desc(initial)'
  0 files updated, 0 files merged, 0 files removed, 0 files unresolved
  $ echo 'B' > b # just to make another commit
  $ hg commit -m "a new base"
  created new head
  $ hg export --rev 'desc("four") + desc("extended jungle")' | hg import --partial -
  applying patch from stdin
  patching file a
  Hunk #1 FAILED at 0
  1 out of 1 hunks FAILED -- saving rejects to file a.rej
  patch applied partially
  (fix the .rej files and run `hg commit --amend`)
  [1]
  $ hg log -G --template '{desc|firstline} [{author}] {diffstat}\n'
  @  four [Rataxes] 0: +0/-0
  |
  o  a new base [test] 1: +1/-1
  |
  | o  extended jungle [Cornelius] 0: +0/-0
  |/
  | o  four [Rataxes] 0: +0/-0
  |/
  | o  five [Arthur] 1: +1/-0
  |/
  | o  extended jungle [Cornelius] 1: +1/-0
  | |
  | o  jungle [Zephir] 1: +1/-0
  | |
  | o  five [Arthur] 2: +2/-1
  | |
  | o  four [Rataxes] 1: +1/-1
  | |
  | o  three [Celeste] 1: +1/-1
  |/
  o  initial [Babar] 2: +8/-0
  
  $ hg export
  # HG changeset patch
  # User Rataxes
  # Date 0 0
  #      Thu Jan 01 00:00:00 1970 +0000
  # Node ID a9d7b6d0ffbb4eb12b7d5939250fcd42e8930a1d
  # Parent  f59f8d2e95a8ca5b1b4ca64320140da85f3b44fd
  four
  
  $ hg status -c .
  C a
  C b

Importing some extra header
===========================

  $ cat > $TESTTMP/parseextra.py <<EOF
  > import mercurial.cmdutil
  > import mercurial.patch
  > 
  > def processfoo(repo, data, extra, opts):
  >     if b'foo' in data:
  >         extra[b'foo'] = data[b'foo']
  > def postimport(ctx):
  >     if b'foo' in ctx.extra():
  >         ctx.repo().ui.write(b'imported-foo: %s\n' % ctx.extra()[b'foo'])
  > 
  > mercurial.patch.patchheadermap.append((b'Foo', b'foo'))
  > mercurial.cmdutil.extrapreimport.append(b'foo')
  > mercurial.cmdutil.extrapreimportmap[b'foo'] = processfoo
  > mercurial.cmdutil.extrapostimport.append(b'foo')
  > mercurial.cmdutil.extrapostimportmap[b'foo'] = postimport
  > EOF
  $ cat >> $HGRCPATH <<EOF
  > [extensions]
  > parseextra=$TESTTMP/parseextra.py
  > EOF
  $ hg up -C tip
  0 files updated, 0 files merged, 0 files removed, 0 files unresolved
  $ cat > $TESTTMP/foo.patch <<EOF
  > # HG changeset patch
  > # User Rataxes
  > # Date 0 0
  > #      Thu Jan 01 00:00:00 1970 +0000
  > # Foo bar
  > height
  > 
  > --- a/a	Thu Jan 01 00:00:00 1970 +0000
  > +++ b/a	Wed Oct 07 09:17:44 2015 +0000
  > @@ -5,3 +5,4 @@
  >  five
  >  six
  >  seven
  > +heigt
  > EOF
  $ hg import $TESTTMP/foo.patch
  applying $TESTTMP/foo.patch
  imported-foo: bar
  $ hg log --debug -r . | grep extra
  extra:       branch=default
  extra:       foo=bar

Warn the user that paths are relative to the root of
repository when file not found for patching

  $ mkdir filedir
  $ echo "file1" >> filedir/file1
  $ hg add filedir/file1
  $ hg commit -m "file1"
  $ cd filedir
  $ hg import -p 2 - <<EOF
  > # HG changeset patch
  > # User test
  > # Date 0 0
  > file2
  > 
  > diff --git a/filedir/file1 b/filedir/file1
  > --- a/filedir/file1
  > +++ b/filedir/file1
  > @@ -1,1 +1,2 @@
  >  file1
  > +file2
  > EOF
  applying patch from stdin
  unable to find 'file1' for patching
  (use '--prefix' to apply patch relative to the current directory)
  1 out of 1 hunks FAILED -- saving rejects to file file1.rej
  abort: patch failed to apply
  [20]

test import crash (issue5375)
  $ cd ..
  $ hg init repo
  $ cd repo
  $ printf "diff --git a/a b/b\nrename from a\nrename to b" | hg import -
  applying patch from stdin
  a not tracked!
  abort: source file 'a' does not exist
  [20]

test immature end of hunk

  $ hg import - <<'EOF'
  > diff --git a/foo b/foo
  > --- a/foo
  > --- b/foo
  > @@ -0,0 +1,1 @@
  > EOF
  applying patch from stdin
  abort: bad hunk #1: incomplete hunk
  (check that whitespace in the patch has not been mangled)
  [10]

  $ hg import - <<'EOF'
  > diff --git a/foo b/foo
  > --- a/foo
  > --- b/foo
  > @@ -0,0 +1,1 @@
  > \ No newline at end of file
  > EOF
  applying patch from stdin
  abort: bad hunk #1: incomplete hunk
  (check that whitespace in the patch has not been mangled)
  [10]