This file is indexed.

/usr/share/doc/monotone/html/Default-hooks.html is in monotone-doc 1.0-12.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
<head>
<title>monotone documentation: Default hooks</title>

<meta name="description" content="monotone documentation: Default hooks">
<meta name="keywords" content="monotone documentation: Default hooks">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="General-Index.html#General-Index" rel="index" title="General Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="index.html#Top" rel="up" title="Top">
<link href="General-Index.html#General-Index" rel="next" title="General Index">
<link href="Regexp-Details.html#Regexp-Details" rel="prev" title="Regexp Details">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
div.smalllisp {margin-left: 3.2em}
kbd {font-style:oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
-->
</style>
<link rel="stylesheet" type="text/css" href="texinfo.css">


</head>

<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Default-hooks"></a>
<div class="header">
<p>
Next: <a href="General-Index.html#General-Index" accesskey="n" rel="next">General Index</a>, Previous: <a href="Special-Topics.html#Special-Topics" accesskey="p" rel="prev">Special Topics</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="General-Index.html#General-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="Default-hooks-1"></a>
<h2 class="appendix">Appendix A Default hooks</h2>

<p>This section contains the entire source code of the standard hook file,
that is built in to the monotone executable, and read before any user
hooks files (unless <samp>--no-builtin-rcfiles</samp> is passed).  It contains the
default values for all hooks. See <a href="Lua-Reference.html#rcfiles">rcfiles</a>.
</p>
<pre class="verbatim">-- Copyright (C) 2003 Graydon Hoare &lt;graydon@pobox.com&gt;
--
-- This program is made available under the GNU GPL version 2.0 or
-- greater. See the accompanying file COPYING for details.
--
-- This program is distributed WITHOUT ANY WARRANTY; without even the
-- implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
-- PURPOSE.

-- this is the standard set of lua hooks for monotone;
-- user-provided files can override it or add to it.

function temp_file(namehint)
   local tdir
   tdir = os.getenv(&quot;TMPDIR&quot;)
   if tdir == nil then tdir = os.getenv(&quot;TMP&quot;) end
   if tdir == nil then tdir = os.getenv(&quot;TEMP&quot;) end
   if tdir == nil then tdir = &quot;/tmp&quot; end
   local filename
   if namehint == nil then
      filename = string.format(&quot;%s/mtn.XXXXXX&quot;, tdir)
   else
      filename = string.format(&quot;%s/mtn.%s.XXXXXX&quot;, tdir, namehint)
   end
   local name = mkstemp(filename)
   local file = io.open(name, &quot;r+&quot;)
   return file, name
end

function execute(path, ...)
   local pid
   local ret = -1
   pid = spawn(path, ...)
   if (pid ~= -1) then ret, pid = wait(pid) end
   return ret
end

function execute_redirected(stdin, stdout, stderr, path, ...)
   local pid
   local ret = -1
   io.flush();
   pid = spawn_redirected(stdin, stdout, stderr, path, ...)
   if (pid ~= -1) then ret, pid = wait(pid) end
   return ret
end

-- Wrapper around execute to let user confirm in the case where a subprocess
-- returns immediately
-- This is needed to work around some brokenness with some merge tools
-- (e.g. on OS X)
function execute_confirm(path, ...)
   ret = execute(path, ...)

   if (ret ~= 0)
   then
      print(gettext(&quot;Press enter&quot;))
   else
      print(gettext(&quot;Press enter when the subprocess has completed&quot;))
   end
   io.read()
   return ret
end

-- attributes are persistent metadata about files (such as execute
-- bit, ACLs, various special flags) which we want to have set and
-- re-set any time the files are modified. the attributes themselves
-- are stored in the roster associated with the revision. each (f,k,v)
-- attribute triple turns into a call to attr_functions[k](f,v) in lua.

if (attr_init_functions == nil) then
   attr_init_functions = {}
end

attr_init_functions[&quot;mtn:execute&quot;] =
   function(filename)
      if (is_executable(filename)) then
        return &quot;true&quot;
      else
        return nil
      end
   end

attr_init_functions[&quot;mtn:manual_merge&quot;] =
   function(filename)
      if (binary_file(filename)) then
        return &quot;true&quot; -- binary files must be merged manually
      else
        return nil
      end
   end

if (attr_functions == nil) then
   attr_functions = {}
end

attr_functions[&quot;mtn:execute&quot;] =
   function(filename, value)
      if (value == &quot;true&quot;) then
         set_executable(filename)
      else
         clear_executable(filename)
      end
   end

function dir_matches(name, dir)
   -- helper for ignore_file, matching files within dir, or dir itself.
   -- eg for dir of 'CVS', matches CVS/, CVS/*, */CVS/ and */CVS/*
   if (string.find(name, &quot;^&quot; .. dir .. &quot;/&quot;)) then return true end
   if (string.find(name, &quot;^&quot; .. dir .. &quot;$&quot;)) then return true end
   if (string.find(name, &quot;/&quot; .. dir .. &quot;/&quot;)) then return true end
   if (string.find(name, &quot;/&quot; .. dir .. &quot;$&quot;)) then return true end
   return false
end

function portable_readline(f)
    line = f:read()
    if line ~= nil then
        line = string.gsub(line, &quot;\r$&quot;,&quot;&quot;) -- strip possible \r left from windows editing
    end
    return line
end

function ignore_file(name)
   -- project specific
   if (ignored_files == nil) then
      ignored_files = {}
      local ignfile = io.open(&quot;.mtn-ignore&quot;, &quot;r&quot;)
      if (ignfile ~= nil) then
         local line = portable_readline(ignfile)
         while (line ~= nil) do
            if line ~= &quot;&quot; then
                table.insert(ignored_files, line)
            end
            line = portable_readline(ignfile)
         end
         io.close(ignfile)
      end
   end

   local warn_reported_file = false
   for i, line in pairs(ignored_files)
   do
      if (line ~= nil) then
         local pcallstatus, result = pcall(function()
        return regex.search(line, name)
     end)
         if pcallstatus == true then
            -- no error from the regex.search call
            if result == true then return true end
         else
            -- regex.search had a problem, warn the user their
            -- .mtn-ignore file syntax is wrong
        if not warn_reported_file then
           io.stderr:write(&quot;mtn: warning: while matching file '&quot;
                       .. name .. &quot;':\n&quot;)
           warn_reported_file = true
        end
        local prefix = &quot;.mtn-ignore:&quot; .. i .. &quot;: warning: &quot;
            io.stderr:write(prefix
                            .. string.gsub(result, &quot;\n&quot;, &quot;\n&quot; .. prefix)
                               .. &quot;\n\t- skipping this regex for &quot;
                               .. &quot;all remaining files.\n&quot;)
            ignored_files[i] = nil
         end
      end
   end

   local file_pats = {
      -- c/c++
      &quot;%.a$&quot;, &quot;%.so$&quot;, &quot;%.o$&quot;, &quot;%.la$&quot;, &quot;%.lo$&quot;, &quot;^core$&quot;,
      &quot;/core$&quot;, &quot;/core%.%d+$&quot;,
      -- java
      &quot;%.class$&quot;,
      -- python
      &quot;%.pyc$&quot;, &quot;%.pyo$&quot;,
      -- gettext
      &quot;%.g?mo$&quot;,
      -- intltool
      &quot;%.intltool%-merge%-cache$&quot;,
      -- TeX
      &quot;%.aux$&quot;,
      -- backup files
      &quot;%.bak$&quot;, &quot;%.orig$&quot;, &quot;%.rej$&quot;, &quot;%~$&quot;,
      -- vim creates .foo.swp files
      &quot;%.[^/]*%.swp$&quot;,
      -- emacs creates #foo# files
      &quot;%#[^/]*%#$&quot;,
      -- other VCSes (where metadata is stored in named files):
      &quot;%.scc$&quot;,
      -- desktop/directory configuration metadata
      &quot;^%.DS_Store$&quot;, &quot;/%.DS_Store$&quot;, &quot;^desktop%.ini$&quot;, &quot;/desktop%.ini$&quot;
   }

   local dir_pats = {
      -- autotools detritus:
      &quot;autom4te%.cache&quot;, &quot;%.deps&quot;, &quot;%.libs&quot;,
      -- Cons/SCons detritus:
      &quot;%.consign&quot;, &quot;%.sconsign&quot;,
      -- other VCSes (where metadata is stored in named dirs):
      &quot;CVS&quot;, &quot;%.svn&quot;, &quot;SCCS&quot;, &quot;_darcs&quot;, &quot;%.cdv&quot;, &quot;%.git&quot;, &quot;%.bzr&quot;, &quot;%.hg&quot;
   }

   for _, pat in ipairs(file_pats) do
      if string.find(name, pat) then return true end
   end
   for _, pat in ipairs(dir_pats) do
      if dir_matches(name, pat) then return true end
   end

   return false;
end

-- return true means &quot;binary&quot;, false means &quot;text&quot;,
-- nil means &quot;unknown, try to guess&quot;
function binary_file(name)
   -- some known binaries, return true
   local bin_pats = {
      &quot;%.gif$&quot;, &quot;%.jpe?g$&quot;, &quot;%.png$&quot;, &quot;%.bz2$&quot;, &quot;%.gz$&quot;, &quot;%.zip$&quot;,
      &quot;%.class$&quot;, &quot;%.jar$&quot;, &quot;%.war$&quot;, &quot;%.ear$&quot;
   }

   -- some known text, return false
   local txt_pats = {
      &quot;%.cc?$&quot;, &quot;%.cxx$&quot;, &quot;%.hh?$&quot;, &quot;%.hxx$&quot;, &quot;%.cpp$&quot;, &quot;%.hpp$&quot;,
      &quot;%.lua$&quot;, &quot;%.texi$&quot;, &quot;%.sql$&quot;, &quot;%.java$&quot;
   }

   local lowname=string.lower(name)
   for _, pat in ipairs(bin_pats) do
      if string.find(lowname, pat) then return true end
   end
   for _, pat in ipairs(txt_pats) do
      if string.find(lowname, pat) then return false end
   end

   -- unknown - read file and use the guess-binary
   -- monotone built-in function
   return guess_binary_file_contents(name)
end

-- given a file name, return a regular expression which will match
-- lines that name top-level constructs in that file, or &quot;&quot;, to disable
-- matching.
function get_encloser_pattern(name)
   -- texinfo has special sectioning commands
   if (string.find(name, &quot;%.texi$&quot;)) then
      -- sectioning commands in texinfo: @node, @chapter, @top,
      -- @((sub)?sub)?section, @unnumbered(((sub)?sub)?sec)?,
      -- @appendix(((sub)?sub)?sec)?, @(|major|chap|sub(sub)?)heading
      return (&quot;^@(&quot;
              .. &quot;node|chapter|top&quot;
              .. &quot;|((sub)?sub)?section&quot;
              .. &quot;|(unnumbered|appendix)(((sub)?sub)?sec)?&quot;
              .. &quot;|(major|chap|sub(sub)?)?heading&quot;
              .. &quot;)&quot;)
   end
   -- LaTeX has special sectioning commands.  This rule is applied to ordinary
   -- .tex files too, since there's no reliable way to distinguish those from
   -- latex files anyway, and there's no good pattern we could use for
   -- arbitrary plain TeX anyway.
   if (string.find(name, &quot;%.tex$&quot;)
       or string.find(name, &quot;%.ltx$&quot;)
       or string.find(name, &quot;%.latex$&quot;)) then
      return (&quot;\\\\(&quot;
              .. &quot;part|chapter|paragraph|subparagraph&quot;
              .. &quot;|((sub)?sub)?section&quot;
              .. &quot;)&quot;)
   end
   -- There's no good way to find section headings in raw text, and trying
   -- just gives distracting output, so don't even try.
   if (string.find(name, &quot;%.txt$&quot;)
       or string.upper(name) == &quot;README&quot;) then
      return &quot;&quot;
   end
   -- This default is correct surprisingly often -- in pretty much any text
   -- written with code-like indentation.
   return &quot;^[[:alnum:]$_]&quot;
end

function edit_comment(user_log_message)
   local exe = nil

   -- top priority is VISUAL, then EDITOR, then a series of hardcoded
   -- defaults, if available.

   local visual = os.getenv(&quot;VISUAL&quot;)
   local editor = os.getenv(&quot;EDITOR&quot;)
   if (visual ~= nil) then exe = visual
   elseif (editor ~= nil) then exe = editor
   elseif (program_exists_in_path(&quot;editor&quot;)) then exe = &quot;editor&quot;
   elseif (program_exists_in_path(&quot;vi&quot;)) then exe = &quot;vi&quot;
   elseif (string.sub(get_ostype(), 1, 6) ~= &quot;CYGWIN&quot; and
       program_exists_in_path(&quot;notepad.exe&quot;)) then exe = &quot;notepad&quot;
   else
      io.write(gettext(&quot;Could not find editor to enter commit message\n&quot;
               .. &quot;Try setting the environment variable EDITOR\n&quot;))
      return nil
   end

   local tmp, tname = temp_file()
   if (tmp == nil) then return nil end
   tmp:write(user_log_message)
   if user_log_message == &quot;&quot; or string.sub(user_log_message, -1) ~= &quot;\n&quot; then
      tmp:write(&quot;\n&quot;)
   end
   io.close(tmp)

   -- By historical convention, VISUAL and EDITOR can contain arguments
   -- (and, in fact, arbitrarily complicated shell constructs).  Since Lua
   -- has no word-splitting functionality, we invoke the shell to deal with
   -- anything more complicated than a single word with no metacharacters.
   -- This, unfortunately, means we have to quote the file argument.

   if (not string.find(exe, &quot;[^%w_.+-]&quot;)) then
      -- safe to call spawn directly
      if (execute(exe, tname) ~= 0) then
         io.write(string.format(gettext(&quot;Error running editor '%s' &quot;..
                                        &quot;to enter log message\n&quot;),
                                exe))
         os.remove(tname)
         return nil
      end
   else
      -- must use shell
      local shell = os.getenv(&quot;SHELL&quot;)
      if (shell == nil) then shell = &quot;sh&quot; end
      if (not program_exists_in_path(shell)) then
         io.write(string.format(gettext(&quot;Editor command '%s' needs a shell, &quot;..
                                        &quot;but '%s' is not to be found&quot;),
                                exe, shell))
         os.remove(tname)
         return nil
      end

      -- Single-quoted strings in both Bourne shell and csh can contain
      -- anything but a single quote.
      local safe_tname = &quot; '&quot; .. string.gsub(tname, &quot;'&quot;, &quot;'\\''&quot;) .. &quot;'&quot;

      if (execute(shell, &quot;-c&quot;, editor .. safe_tname) ~= 0) then
         io.write(string.format(gettext(&quot;Error running editor '%s' &quot;..
                                        &quot;to enter log message\n&quot;),
                                exe))
         os.remove(tname)
         return nil
      end
   end

   tmp = io.open(tname, &quot;r&quot;)
   if (tmp == nil) then os.remove(tname); return nil end
   local res = tmp:read(&quot;*a&quot;)
   io.close(tmp)
   os.remove(tname)
   return res
end


function get_local_key_name(key_identity)
   return key_identity.given_name
end


function persist_phrase_ok()
   return true
end


function use_inodeprints()
   return false
end

function get_date_format_spec(wanted)
   -- Return the strftime(3) specification to be used to print dates
   -- in human-readable format after conversion to the local timezone.
   -- The default uses the preferred date and time representation for
   -- the current locale, e.g. the output looks like this: &quot;09/08/2009
   -- 06:49:26 PM&quot; for en_US and &quot;date_time_long&quot;, or &quot;08.09.2009&quot;
   -- for de_DE and &quot;date_short&quot;
   --
   -- A sampling of other possible formats you might want:
   --   default for your locale: &quot;%c&quot; (may include a confusing timezone label)
   --   12 hour format: &quot;%d %b %Y, %I:%M:%S %p&quot;
   --   like ctime(3):  &quot;%a %b %d %H:%M:%S %Y&quot;
   --   email style:    &quot;%a, %d %b %Y %H:%M:%S&quot;
   --   ISO 8601:       &quot;%Y-%m-%d %H:%M:%S&quot; or &quot;%Y-%m-%dT%H:%M:%S&quot;
   --
   --   ISO 8601, no timezone conversion: &quot;&quot;
   --.
   if (wanted == &quot;date_long&quot; or wanted == &quot;date_short&quot;) then
       return &quot;%x&quot;
   end
   if (wanted == &quot;time_long&quot; or wanted == &quot;time_short&quot;) then
       return &quot;%X&quot;
   end
   return &quot;%x %X&quot;
end

-- trust evaluation hooks

function intersection(a,b)
   local s={}
   local t={}
   for k,v in pairs(a) do s[v.name] = 1 end
   for k,v in pairs(b) do if s[v] ~= nil then table.insert(t,v) end end
   return t
end

function get_revision_cert_trust(signers, id, name, val)
   return true
end

function get_manifest_cert_trust(signers, id, name, val)
   return true
end

function get_file_cert_trust(signers, id, name, val)
   return true
end

function accept_testresult_change(old_results, new_results)
   local reqfile = io.open(&quot;_MTN/wanted-testresults&quot;, &quot;r&quot;)
   if (reqfile == nil) then return true end
   local line = reqfile:read()
   local required = {}
   while (line ~= nil)
   do
      required[line] = true
      line = reqfile:read()
   end
   io.close(reqfile)
   for test, res in pairs(required)
   do
      if old_results[test] == true and new_results[test] ~= true
      then
         return false
      end
   end
   return true
end

-- merger support

-- Fields in the mergers structure:
-- cmd       : a function that performs the merge operation using the chosen
--             program, best try.
-- available : a function that checks that the needed program is installed and
--             in $PATH
-- wanted    : a function that checks if the user doesn't want to use this
--             method, and returns false if so.  This should normally return
--             true, but in some cases, especially when the merger is really
--             an editor, the user might have a preference in EDITOR and we
--             need to respect that.
--             NOTE: wanted is only used when the user has NOT defined the
--             `merger' variable or the MTN_MERGE environment variable.
mergers = {}

-- This merger is designed to fail if there are any conflicts without trying to resolve them
mergers.fail = {
   cmd = function (tbl) return false end,
   available = function () return true end,
   wanted = function () return true end
}

mergers.meld = {
   cmd = function (tbl)
      io.write(string.format(
        &quot;\nWARNING: 'meld' was chosen to perform an external 3-way merge.\n&quot;..
        &quot;You must merge all changes to the *CENTER* file.\n\n&quot;
      ))
      local path = &quot;meld&quot;
      local ret = execute(path, tbl.lfile, tbl.afile, tbl.rfile)
      if (ret ~= 0) then
         io.write(string.format(gettext(&quot;Error running merger '%s'\n&quot;), path))
         return false
      end
      return tbl.afile
   end ,
   available = function () return program_exists_in_path(&quot;meld&quot;) end,
   wanted = function () return true end
}

mergers.diffuse = {
   cmd = function (tbl)
      io.write(string.format(
        &quot;\nWARNING: 'diffuse' was chosen to perform an external 3-way merge.\n&quot;..
        &quot;You must merge all changes to the *CENTER* file.\n\n&quot;
      ))
      local path = &quot;diffuse&quot;
      local ret = execute(path, tbl.lfile, tbl.afile, tbl.rfile)
      if (ret ~= 0) then
         io.write(string.format(gettext(&quot;Error running merger '%s'\n&quot;), path))
         return false
      end
      return tbl.afile
   end ,
   available = function () return program_exists_in_path(&quot;diffuse&quot;) end,
   wanted = function () return true end
}

mergers.tortoise = {
   cmd = function (tbl)
      local path = &quot;tortoisemerge&quot;
      local ret = execute(path,
                          string.format(&quot;/base:%s&quot;, tbl.afile),
                          string.format(&quot;/theirs:%s&quot;, tbl.lfile),
                          string.format(&quot;/mine:%s&quot;, tbl.rfile),
                          string.format(&quot;/merged:%s&quot;, tbl.outfile))
      if (ret ~= 0) then
         io.write(string.format(gettext(&quot;Error running merger '%s'\n&quot;), path))
         return false
      end
      return tbl.outfile
   end ,
   available = function() return program_exists_in_path (&quot;tortoisemerge&quot;) end,
   wanted = function () return true end
}

mergers.vim = {
   cmd = function (tbl)
      function execute_diff3(mine, yours, out)
     local diff3_args = {
        &quot;diff3&quot;,
        &quot;--merge&quot;,
        &quot;--easy-only&quot;,
     }
     table.insert(diff3_args, string.gsub(mine, &quot;\\&quot;, &quot;/&quot;) .. &quot;&quot;)
     table.insert(diff3_args, string.gsub(tbl.afile, &quot;\\&quot;, &quot;/&quot;) .. &quot;&quot;)
     table.insert(diff3_args, string.gsub(yours, &quot;\\&quot;, &quot;/&quot;) .. &quot;&quot;)

     return execute_redirected(&quot;&quot;, string.gsub(out, &quot;\\&quot;, &quot;/&quot;), &quot;&quot;, unpack(diff3_args))
      end

      io.write (string.format(&quot;\nWARNING: 'vim' was chosen to perform &quot;..
                  &quot;an external 3-way merge.\n&quot;..
                  &quot;You must merge all changes to the &quot;..
                  &quot;*LEFT* file.\n&quot;))

      local vim
      if os.getenv (&quot;DISPLAY&quot;) ~= nil and program_exists_in_path (&quot;gvim&quot;) then
     vim = &quot;gvim&quot;
      else
     vim = &quot;vim&quot;
      end

      local lfile_merged = tbl.lfile .. &quot;.merged&quot;
      local rfile_merged = tbl.rfile .. &quot;.merged&quot;

      -- first merge lfile using diff3
      local ret = execute_diff3(tbl.lfile, tbl.rfile, lfile_merged)
      if ret == 2 then
         io.write(string.format(gettext(&quot;Error running diff3 for merger '%s'\n&quot;), vim))
         os.remove(lfile_merged)
     return false
      end

      -- now merge rfile using diff3
      ret = execute_diff3(tbl.rfile, tbl.lfile, rfile_merged)
      if ret == 2 then
         io.write(string.format(gettext(&quot;Error running diff3 for merger '%s'\n&quot;), vim))
         os.remove(lfile_merged)
         os.remove(rfile_merged)
     return false
      end

      os.rename(lfile_merged, tbl.lfile)
      os.rename(rfile_merged, tbl.rfile)

      local ret = execute(vim, &quot;-f&quot;, &quot;-d&quot;, &quot;-c&quot;, string.format(&quot;silent file %s&quot;, tbl.outfile),
                          tbl.lfile, tbl.rfile)
      if (ret ~= 0) then
         io.write(string.format(gettext(&quot;Error running merger '%s'\n&quot;), vim))
         return false
      end
      return tbl.outfile
   end ,
   available =
      function ()
     return program_exists_in_path(&quot;diff3&quot;) and
            (program_exists_in_path(&quot;vim&quot;) or
        program_exists_in_path(&quot;gvim&quot;))
      end ,
   wanted =
      function ()
     local editor = os.getenv(&quot;EDITOR&quot;)
     if editor and
        not (string.find(editor, &quot;vim&quot;) or
         string.find(editor, &quot;gvim&quot;)) then
        return false
     end
     return true
      end
}

mergers.rcsmerge = {
   cmd = function (tbl)
      -- XXX: This is tough - should we check if conflict markers stay or not?
      -- If so, we should certainly give the user some way to still force
      -- the merge to proceed since they can appear in the files (and I saw
      -- that). --pasky
      local merge = os.getenv(&quot;MTN_RCSMERGE&quot;)
      if execute(merge, tbl.lfile, tbl.afile, tbl.rfile) == 0 then
         copy_text_file(tbl.lfile, tbl.outfile);
         return tbl.outfile
      end
      local ret = execute(&quot;vim&quot;, &quot;-f&quot;, &quot;-c&quot;, string.format(&quot;file %s&quot;, tbl.outfile
),
                          tbl.lfile)
      if (ret ~= 0) then
         io.write(string.format(gettext(&quot;Error running merger '%s'\n&quot;), &quot;vim&quot;))
         return false
      end
      return tbl.outfile
   end,
   available =
      function ()
     local merge = os.getenv(&quot;MTN_RCSMERGE&quot;)
     return merge and
        program_exists_in_path(merge) and program_exists_in_path(&quot;vim&quot;)
      end ,
   wanted = function () return os.getenv(&quot;MTN_RCSMERGE&quot;) ~= nil end
}

--  GNU diffutils based merging
mergers.diffutils = {
    --  merge procedure execution
    cmd = function (tbl)
        --  parse options
        local option = {}
        option.partial = false
        option.diff3opts = &quot;&quot;
        option.sdiffopts = &quot;&quot;
        local options = os.getenv(&quot;MTN_MERGE_DIFFUTILS&quot;)
        if options ~= nil then
            for spec in string.gmatch(options, &quot;%s*(%w[^,]*)%s*,?&quot;) do
                local name, value = string.match(spec, &quot;^(%w+)=([^,]*)&quot;)
                if name == nil then
                    name = spec
                    value = true
                end
                if type(option[name]) == &quot;nil&quot; then
                    io.write(&quot;mtn: &quot; .. string.format(gettext(&quot;invalid \&quot;diffutils\&quot; merger option \&quot;%s\&quot;&quot;), name) .. &quot;\n&quot;)
                    return false
                end
                option[name] = value
            end
        end

        --  determine the diff3(1) command
        local diff3 = {
            &quot;diff3&quot;,
            &quot;--merge&quot;,
            &quot;--label&quot;, string.format(&quot;%s [left]&quot;,     tbl.left_path ),
            &quot;--label&quot;, string.format(&quot;%s [ancestor]&quot;, tbl.anc_path  ),
            &quot;--label&quot;, string.format(&quot;%s [right]&quot;,    tbl.right_path),
        }
        if option.diff3opts ~= &quot;&quot; then
            for opt in string.gmatch(option.diff3opts, &quot;%s*([^%s]+)%s*&quot;) do
                table.insert(diff3, opt)
            end
        end
        table.insert(diff3, string.gsub(tbl.lfile, &quot;\\&quot;, &quot;/&quot;) .. &quot;&quot;)
        table.insert(diff3, string.gsub(tbl.afile, &quot;\\&quot;, &quot;/&quot;) .. &quot;&quot;)
        table.insert(diff3, string.gsub(tbl.rfile, &quot;\\&quot;, &quot;/&quot;) .. &quot;&quot;)

        --  dispatch according to major operation mode
        if option.partial then
            --  partial batch/non-modal 3-way merge &quot;resolution&quot;:
            --  simply merge content with help of conflict markers
            io.write(&quot;mtn: &quot; .. gettext(&quot;3-way merge via GNU diffutils, resolving conflicts via conflict markers&quot;) .. &quot;\n&quot;)
            local ret = execute_redirected(&quot;&quot;, string.gsub(tbl.outfile, &quot;\\&quot;, &quot;/&quot;), &quot;&quot;, unpack(diff3))
            if ret == 2 then
                io.write(&quot;mtn: &quot; .. gettext(&quot;error running GNU diffutils 3-way difference/merge tool \&quot;diff3\&quot;&quot;) .. &quot;\n&quot;)
                return false
            end
            return tbl.outfile
        else
            --  real interactive/modal 3/2-way merge resolution:
            --  display 3-way merge conflict and perform 2-way merge resolution
            io.write(&quot;mtn: &quot; .. gettext(&quot;3-way merge via GNU diffutils, resolving conflicts via interactive prompt&quot;) .. &quot;\n&quot;)

            --  display 3-way merge conflict (batch)
            io.write(&quot;\n&quot;)
            io.write(&quot;mtn: &quot; .. gettext(&quot;---- CONFLICT SUMMARY ------------------------------------------------&quot;) .. &quot;\n&quot;)
            local ret = execute(unpack(diff3))
            if ret == 2 then
                io.write(&quot;mtn: &quot; .. gettext(&quot;error running GNU diffutils 3-way difference/merge tool \&quot;diff3\&quot;&quot;) .. &quot;\n&quot;)
                return false
            end

            --  perform 2-way merge resolution (interactive)
            io.write(&quot;\n&quot;)
            io.write(&quot;mtn: &quot; .. gettext(&quot;---- CONFLICT RESOLUTION ---------------------------------------------&quot;) .. &quot;\n&quot;)
            local sdiff = {
                &quot;sdiff&quot;,
                &quot;--diff-program=diff&quot;,
                &quot;--suppress-common-lines&quot;,
                &quot;--minimal&quot;,
                &quot;--output=&quot; .. string.gsub(tbl.outfile, &quot;\\&quot;, &quot;/&quot;)
            }
            if option.sdiffopts ~= &quot;&quot; then
                for opt in string.gmatch(option.sdiffopts, &quot;%s*([^%s]+)%s*&quot;) do
                    table.insert(sdiff, opt)
                end
            end
            table.insert(sdiff, string.gsub(tbl.lfile, &quot;\\&quot;, &quot;/&quot;) .. &quot;&quot;)
            table.insert(sdiff, string.gsub(tbl.rfile, &quot;\\&quot;, &quot;/&quot;) .. &quot;&quot;)
            local ret = execute(unpack(sdiff))
            if ret == 2 then
                io.write(&quot;mtn: &quot; .. gettext(&quot;error running GNU diffutils 2-way merging tool \&quot;sdiff\&quot;&quot;) .. &quot;\n&quot;)
                return false
            end
            return tbl.outfile
        end
    end,

    --  merge procedure availability check
    available = function ()
        --  make sure the GNU diffutils tools are available
        return program_exists_in_path(&quot;diff3&quot;) and
               program_exists_in_path(&quot;sdiff&quot;) and
               program_exists_in_path(&quot;diff&quot;);
    end,

    --  merge procedure request check
    wanted = function ()
        --  assume it is requested (if it is available at all)
        return true
    end
}

mergers.emacs = {
   cmd = function (tbl)
      local emacs
      if program_exists_in_path(&quot;xemacs&quot;) then
         emacs = &quot;xemacs&quot;
      else
         emacs = &quot;emacs&quot;
      end
      local elisp = &quot;(ediff-merge-files-with-ancestor \&quot;%s\&quot; \&quot;%s\&quot; \&quot;%s\&quot; nil \&quot;%s\&quot;)&quot;
      -- Converting backslashes is necessary on Win32 MinGW; emacs
      -- lisp string syntax says '\' is an escape.
      local ret = execute(emacs, &quot;--eval&quot;,
                          string.format(elisp,
                          string.gsub (tbl.lfile, &quot;\\&quot;, &quot;/&quot;),
                          string.gsub (tbl.rfile, &quot;\\&quot;, &quot;/&quot;),
                          string.gsub (tbl.afile, &quot;\\&quot;, &quot;/&quot;),
                          string.gsub (tbl.outfile, &quot;\\&quot;, &quot;/&quot;)))
      if (ret ~= 0) then
         io.write(string.format(gettext(&quot;Error running merger '%s'\n&quot;), emacs))
         return false
      end
      return tbl.outfile
   end,
   available =
      function ()
     return program_exists_in_path(&quot;xemacs&quot;) or
        program_exists_in_path(&quot;emacs&quot;)
      end ,
   wanted =
      function ()
     local editor = os.getenv(&quot;EDITOR&quot;)
     if editor and
        not (string.find(editor, &quot;emacs&quot;) or
         string.find(editor, &quot;gnu&quot;)) then
        return false
     end
     return true
      end
}

mergers.xxdiff = {
   cmd = function (tbl)
      local path = &quot;xxdiff&quot;
      local ret = execute(path,
                        &quot;--title1&quot;, tbl.left_path,
                        &quot;--title2&quot;, tbl.right_path,
                        &quot;--title3&quot;, tbl.merged_path,
                        tbl.lfile, tbl.afile, tbl.rfile,
                        &quot;--merge&quot;,
                        &quot;--merged-filename&quot;, tbl.outfile,
                        &quot;--exit-with-merge-status&quot;)
      if (ret ~= 0) then
         io.write(string.format(gettext(&quot;Error running merger '%s'\n&quot;), path))
         return false
      end
      return tbl.outfile
   end,
   available = function () return program_exists_in_path(&quot;xxdiff&quot;) end,
   wanted = function () return true end
}

mergers.kdiff3 = {
   cmd = function (tbl)
      local path = &quot;kdiff3&quot;
      local ret = execute(path,
                          &quot;--L1&quot;, tbl.anc_path,
                          &quot;--L2&quot;, tbl.left_path,
                          &quot;--L3&quot;, tbl.right_path,
                          tbl.afile, tbl.lfile, tbl.rfile,
                          &quot;--merge&quot;,
                          &quot;--o&quot;, tbl.outfile)
      if (ret ~= 0) then
         io.write(string.format(gettext(&quot;Error running merger '%s'\n&quot;), path))
         return false
      end
      return tbl.outfile
   end,
   available = function () return program_exists_in_path(&quot;kdiff3&quot;) end,
   wanted = function () return true end
}

mergers.opendiff = {
   cmd = function (tbl)
      local path = &quot;opendiff&quot;
      -- As opendiff immediately returns, let user confirm manually
      local ret = execute_confirm(path,
                                  tbl.lfile,tbl.rfile,
                                  &quot;-ancestor&quot;,tbl.afile,
                                  &quot;-merge&quot;,tbl.outfile)
      if (ret ~= 0) then
         io.write(string.format(gettext(&quot;Error running merger '%s'\n&quot;), path))
         return false
      end
      return tbl.outfile
   end,
   available = function () return program_exists_in_path(&quot;opendiff&quot;) end,
   wanted = function () return true end
}

function write_to_temporary_file(data, namehint)
   tmp, filename = temp_file(namehint)
   if (tmp == nil) then
      return nil
   end;
   tmp:write(data)
   io.close(tmp)
   return filename
end

function copy_text_file(srcname, destname)
   src = io.open(srcname, &quot;r&quot;)
   if (src == nil) then return nil end
   dest = io.open(destname, &quot;w&quot;)
   if (dest == nil) then return nil end

   while true do
      local line = src:read()
      if line == nil then break end
      dest:write(line, &quot;\n&quot;)
   end

   io.close(dest)
   io.close(src)
end

function read_contents_of_file(filename, mode)
   tmp = io.open(filename, mode)
   if (tmp == nil) then
      return nil
   end
   local data = tmp:read(&quot;*a&quot;)
   io.close(tmp)
   return data
end

function program_exists_in_path(program)
   return existsonpath(program) == 0
end

function get_preferred_merge3_command (tbl)
   local default_order = {&quot;diffuse&quot;, &quot;kdiff3&quot;, &quot;xxdiff&quot;, &quot;opendiff&quot;,
                          &quot;tortoise&quot;, &quot;emacs&quot;, &quot;vim&quot;, &quot;meld&quot;, &quot;diffutils&quot;}
   local function existmerger(name)
      local m = mergers[name]
      if type(m) == &quot;table&quot; and m.available(tbl) then
         return m.cmd
      end
      return nil
   end
   local function trymerger(name)
      local m = mergers[name]
      if type(m) == &quot;table&quot; and m.available(tbl) and m.wanted(tbl) then
         return m.cmd
      end
      return nil
   end
   -- Check if there's a merger given by the user.
   local mkey = os.getenv(&quot;MTN_MERGE&quot;)
   if not mkey then mkey = merger end
   if not mkey and os.getenv(&quot;MTN_RCSMERGE&quot;) then mkey = &quot;rcsmerge&quot; end
   -- If there was a user-given merger, see if it exists.  If it does, return
   -- the cmd function.  If not, return nil.
   local c
   if mkey then c = existmerger(mkey) end
   if c then return c,mkey end
   if mkey then return nil,mkey end
   -- If there wasn't any user-given merger, take the first that's available
   -- and wanted.
   for _,mkey in ipairs(default_order) do
      c = trymerger(mkey) ; if c then return c,mkey end
   end
end

function merge3 (anc_path, left_path, right_path, merged_path, ancestor, left, right)
   local ret = nil
   local tbl = {}

   tbl.anc_path = anc_path
   tbl.left_path = left_path
   tbl.right_path = right_path

   tbl.merged_path = merged_path
   tbl.afile = nil
   tbl.lfile = nil
   tbl.rfile = nil
   tbl.outfile = nil
   tbl.meld_exists = false
   tbl.lfile = write_to_temporary_file (left, &quot;left&quot;)
   tbl.afile = write_to_temporary_file (ancestor, &quot;ancestor&quot;)
   tbl.rfile = write_to_temporary_file (right, &quot;right&quot;)
   tbl.outfile = write_to_temporary_file (&quot;&quot;, &quot;merged&quot;)

   if tbl.lfile ~= nil and tbl.rfile ~= nil and tbl.afile ~= nil and tbl.outfile ~= nil
   then
      local cmd,mkey = get_preferred_merge3_command (tbl)
      if cmd ~=nil
      then
         io.write (&quot;mtn: &quot; .. string.format(gettext(&quot;executing external 3-way merge via \&quot;%s\&quot; merger\n&quot;), mkey))
         ret = cmd (tbl)
         if not ret then
            ret = nil
         else
            ret = read_contents_of_file (ret, &quot;r&quot;)
            if string.len (ret) == 0
            then
               ret = nil
            end
         end
      else
     if mkey then
        io.write (string.format(&quot;The possible commands for the &quot;..mkey..&quot; merger aren't available.\n&quot;..
                &quot;You may want to check that $MTN_MERGE or the lua variable `merger' is set\n&quot;..
                &quot;to something available.  If you want to use vim or emacs, you can also\n&quot;..
        &quot;set $EDITOR to something appropriate.\n&quot;))
     else
        io.write (string.format(&quot;No external 3-way merge command found.\n&quot;..
                &quot;You may want to check that $EDITOR is set to an editor that supports 3-way\n&quot;..
                &quot;merge, set this explicitly in your get_preferred_merge3_command hook,\n&quot;..
                &quot;or add a 3-way merge program to your path.\n&quot;))
     end
      end
   end

   os.remove (tbl.lfile)
   os.remove (tbl.rfile)
   os.remove (tbl.afile)
   os.remove (tbl.outfile)

   return ret
end

-- expansion of values used in selector completion

function expand_selector(str)

   -- something which looks like a generic cert pattern
   if string.find(str, &quot;^[^=]*=.*$&quot;)
   then
      return (&quot;c:&quot; .. str)
   end

   -- something which looks like an email address
   if string.find(str, &quot;[%w%-_]+@[%w%-_]+&quot;)
   then
      return (&quot;a:&quot; .. str)
   end

   -- something which looks like a branch name
   if string.find(str, &quot;[%w%-]+%.[%w%-]+&quot;)
   then
      return (&quot;b:&quot; .. str)
   end

   -- a sequence of nothing but hex digits
   if string.find(str, &quot;^%x+$&quot;)
   then
      return (&quot;i:&quot; .. str)
   end

   -- tries to expand as a date
   local dtstr = expand_date(str)
   if  dtstr ~= nil
   then
      return (&quot;d:&quot; .. dtstr)
   end

   return nil
end

-- expansion of a date expression
function expand_date(str)
   -- simple date patterns
   if string.find(str, &quot;^19%d%d%-%d%d&quot;)
      or string.find(str, &quot;^20%d%d%-%d%d&quot;)
   then
      return (str)
   end

   -- &quot;now&quot;
   if str == &quot;now&quot;
   then
      local t = os.time(os.date('!*t'))
      return os.date(&quot;%Y-%m-%dT%H:%M:%S&quot;, t)
   end

   -- today don't uses the time         # for xgettext's sake, an extra quote
   if str == &quot;today&quot;
   then
      local t = os.time(os.date('!*t'))
      return os.date(&quot;%Y-%m-%d&quot;, t)
   end

   -- &quot;yesterday&quot;, the source of all hangovers
   if str == &quot;yesterday&quot;
   then
      local t = os.time(os.date('!*t'))
      return os.date(&quot;%Y-%m-%d&quot;, t - 86400)
   end

   -- &quot;CVS style&quot; relative dates such as &quot;3 weeks ago&quot;
   local trans = {
      minute = 60;
      hour = 3600;
      day = 86400;
      week = 604800;
      month = 2678400;
      year = 31536000
   }
   local pos, len, n, type = string.find(str, &quot;(%d+) ([minutehordaywk]+)s? ago&quot;)
   if trans[type] ~= nil
   then
      local t = os.time(os.date('!*t'))
      if trans[type] &lt;= 3600
      then
        return os.date(&quot;%Y-%m-%dT%H:%M:%S&quot;, t - (n * trans[type]))
      else
        return os.date(&quot;%Y-%m-%d&quot;, t - (n * trans[type]))
      end
   end

   return nil
end


external_diff_default_args = &quot;-u&quot;

-- default external diff, works for gnu diff
function external_diff(file_path, data_old, data_new, is_binary, diff_args, rev_old, rev_new)
   local old_file = write_to_temporary_file(data_old);
   local new_file = write_to_temporary_file(data_new);

   if diff_args == nil then diff_args = external_diff_default_args end
   execute(&quot;diff&quot;, diff_args, &quot;--label&quot;, file_path .. &quot;\told&quot;, old_file, &quot;--label&quot;, file_path .. &quot;\tnew&quot;, new_file);

   os.remove (old_file);
   os.remove (new_file);
end

-- netsync permissions hooks (and helper)

function globish_match(glob, str)
      local pcallstatus, result = pcall(function() if (globish.match(glob, str)) then return true else return false end end)
      if pcallstatus == true then
          -- no error
          return result
      else
          -- globish.match had a problem
          return nil
      end
end

function _get_netsync_read_permitted(branch, ident, permfilename, state)
   if not exists(permfilename) or isdir(permfilename) then
      return false
   end
   local permfile = io.open(permfilename, &quot;r&quot;)
   if (permfile == nil) then return false end
   local dat = permfile:read(&quot;*a&quot;)
   io.close(permfile)
   local res = parse_basic_io(dat)
   if res == nil then
      io.stderr:write(&quot;file &quot;..permfilename..&quot; cannot be parsed\n&quot;)
      return false,&quot;continue&quot;
   end
   state[&quot;matches&quot;] = state[&quot;matches&quot;] or false
   state[&quot;cont&quot;] = state[&quot;cont&quot;] or false
   for i, item in pairs(res)
   do
      -- legal names: pattern, allow, deny, continue
      if item.name == &quot;pattern&quot; then
         if state[&quot;matches&quot;] and not state[&quot;cont&quot;] then return false end
         state[&quot;matches&quot;] = false
         state[&quot;cont&quot;] = false
         for j, val in pairs(item.values) do
            if globish_match(val, branch) then state[&quot;matches&quot;] = true end
         end
      elseif item.name == &quot;allow&quot; then if state[&quot;matches&quot;] then
         for j, val in pairs(item.values) do
            if val == &quot;*&quot; then return true end
            if val == &quot;&quot; and ident == nil then return true end
            if ident ~= nil and val == ident.id then return true end
            if ident ~= nil and globish_match(val, ident.name) then return true end
         end
      end elseif item.name == &quot;deny&quot; then if state[&quot;matches&quot;] then
         for j, val in pairs(item.values) do
            if val == &quot;*&quot; then return false end
            if val == &quot;&quot; and ident == nil then return false end
            if ident ~= nil and val == ident.id then return false end
            if ident ~= nil and globish_match(val, ident.name) then return false end
         end
      end elseif item.name == &quot;continue&quot; then if state[&quot;matches&quot;] then
         state[&quot;cont&quot;] = true
         for j, val in pairs(item.values) do
            if val == &quot;false&quot; or val == &quot;no&quot; then
              state[&quot;cont&quot;] = false
            end
         end
      end elseif item.name ~= &quot;comment&quot; then
         io.stderr:write(&quot;unknown symbol in read-permissions: &quot; .. item.name .. &quot;\n&quot;)
         return false
      end
   end
   return false
end

function get_netsync_read_permitted(branch, ident)
   local permfilename = get_confdir() .. &quot;/read-permissions&quot;
   local permdirname = permfilename .. &quot;.d&quot;
   local state = {}
   if _get_netsync_read_permitted(branch, ident, permfilename, state) then
      return true
   end
   if isdir(permdirname) then
      local files = read_directory(permdirname)
      table.sort(files)
      for _,f in ipairs(files) do
        pf = permdirname..&quot;/&quot;..f
        if _get_netsync_read_permitted(branch, ident, pf, state) then
          return true
        end
      end
   end
   return false
end

function _get_netsync_write_permitted(ident, permfilename)
   if not exists(permfilename) or isdir(permfilename) then return false end
   local permfile = io.open(permfilename, &quot;r&quot;)
   if (permfile == nil) then
      return false
   end
   local matches = false
   local line = permfile:read()
   while (not matches and line ~= nil) do
      local _, _, ln = string.find(line, &quot;%s*([^%s]*)%s*&quot;)
      if ln == &quot;*&quot; then matches = true end
      if ln == ident.id then matches = true end
      if globish_match(ln, ident.name) then matches = true end
      line = permfile:read()
   end
   io.close(permfile)
   return matches
end

function get_netsync_write_permitted(ident)
   local permfilename = get_confdir() .. &quot;/write-permissions&quot;
   local permdirname = permfilename .. &quot;.d&quot;
   if _get_netsync_write_permitted(ident, permfilename) then return true end
   if isdir(permdirname) then
      local files = read_directory(permdirname)
      table.sort(files)
      for _,f in ipairs(files) do
        pf = permdirname..&quot;/&quot;..f
        if _get_netsync_write_permitted(ident, pf) then return true end
      end
   end
   return false
end

-- This is a simple function which assumes you're going to be spawning
-- a copy of mtn, so reuses a common bit at the end for converting
-- local args into remote args. You might need to massage the logic a
-- bit if this doesn't fit your assumptions.

function get_netsync_connect_command(uri, args)

        local argv = nil

        if uri[&quot;scheme&quot;] == &quot;ssh&quot;
                and uri[&quot;host&quot;]
                and uri[&quot;path&quot;] then

                argv = { &quot;ssh&quot; }
                if uri[&quot;user&quot;] then
                        table.insert(argv, &quot;-l&quot;)
                        table.insert(argv, uri[&quot;user&quot;])
                end
                if uri[&quot;port&quot;] then
                        table.insert(argv, &quot;-p&quot;)
                        table.insert(argv, uri[&quot;port&quot;])
                end

                -- ssh://host/~/dir/file.mtn or
                -- ssh://host/~user/dir/file.mtn should be home-relative
                if string.find(uri[&quot;path&quot;], &quot;^/~&quot;) then
                        uri[&quot;path&quot;] = string.sub(uri[&quot;path&quot;], 2)
                end

                table.insert(argv, uri[&quot;host&quot;])
        end

        if uri[&quot;scheme&quot;] == &quot;file&quot; and uri[&quot;path&quot;] then
                argv = { }
        end

        if uri[&quot;scheme&quot;] == &quot;ssh+ux&quot;
                and uri[&quot;host&quot;]
                and uri[&quot;path&quot;] then

                argv = { &quot;ssh&quot; }
                if uri[&quot;user&quot;] then
                        table.insert(argv, &quot;-l&quot;)
                        table.insert(argv, uri[&quot;user&quot;])
                end
                if uri[&quot;port&quot;] then
                        table.insert(argv, &quot;-p&quot;)
                        table.insert(argv, uri[&quot;port&quot;])
                end

                -- ssh://host/~/dir/file.mtn or
                -- ssh://host/~user/dir/file.mtn should be home-relative
                if string.find(uri[&quot;path&quot;], &quot;^/~&quot;) then
                        uri[&quot;path&quot;] = string.sub(uri[&quot;path&quot;], 2)
                end

                table.insert(argv, uri[&quot;host&quot;])
                table.insert(argv, get_remote_unix_socket_command(uri[&quot;host&quot;]))
                table.insert(argv, &quot;-&quot;)
                table.insert(argv, &quot;UNIX-CONNECT:&quot; .. uri[&quot;path&quot;])
        else
            if argv then
                    -- start remote monotone process

                    table.insert(argv, get_mtn_command(uri[&quot;host&quot;]))

                    if args[&quot;debug&quot;] then
                            table.insert(argv, &quot;--verbose&quot;)
                    else
                            table.insert(argv, &quot;--quiet&quot;)
                    end

                    table.insert(argv, &quot;--db&quot;)
                    table.insert(argv, uri[&quot;path&quot;])
                    table.insert(argv, &quot;serve&quot;)
                    table.insert(argv, &quot;--stdio&quot;)
                    table.insert(argv, &quot;--no-transport-auth&quot;)

            -- else scheme does not require starting a new remote
            -- process (ie mtn:)
            end
        end
        return argv
end

function use_transport_auth(uri)
        if uri[&quot;scheme&quot;] == &quot;ssh&quot;
        or uri[&quot;scheme&quot;] == &quot;ssh+ux&quot;
        or uri[&quot;scheme&quot;] == &quot;file&quot; then
                return false
        else
                return true
        end
end

function get_mtn_command(host)
        return &quot;mtn&quot;
end

function get_remote_unix_socket_command(host)
    return &quot;socat&quot;
end

function get_default_command_options(command)
    local default_args = {}
    return default_args
end

function get_default_database_alias()
    return &quot;:default.mtn&quot;
end

function get_default_database_locations()
    local paths = {}
    table.insert(paths, get_confdir() .. &quot;/databases&quot;)
    return paths
end

function get_default_database_glob()
    return &quot;*.{mtn,db}&quot;
end

hook_wrapper_dump                = {}
hook_wrapper_dump.depth          = 0
hook_wrapper_dump._string        = function(s) return string.format(&quot;%q&quot;, s) end
hook_wrapper_dump._number        = function(n) return tostring(n) end
hook_wrapper_dump._boolean       = function(b) if (b) then return &quot;true&quot; end return &quot;false&quot; end
hook_wrapper_dump._userdata      = function(u) return &quot;nil --[[userdata]]&quot; end
-- if we really need to return / serialize functions we could do it
-- like cbreak@irc.freenode.net did here: http://lua-users.org/wiki/TablePersistence
hook_wrapper_dump._function      = function(f) return &quot;nil --[[function]]&quot; end
hook_wrapper_dump._nil           = function(n) return &quot;nil&quot; end
hook_wrapper_dump._thread        = function(t) return &quot;nil --[[thread]]&quot; end
hook_wrapper_dump._lightuserdata = function(l) return &quot;nil --[[lightuserdata]]&quot; end

hook_wrapper_dump._table = function(t)
    local buf = ''
    if (hook_wrapper_dump.depth &gt; 0) then
        buf = buf .. '{\n'
    end
    hook_wrapper_dump.depth = hook_wrapper_dump.depth + 1;
    for k,v in pairs(t) do
        buf = buf..string.format('%s[%s] = %s;\n',
              string.rep(&quot;\t&quot;, hook_wrapper_dump.depth - 1),
              hook_wrapper_dump[&quot;_&quot; .. type(k)](k),
              hook_wrapper_dump[&quot;_&quot; .. type(v)](v))
    end
    hook_wrapper_dump.depth = hook_wrapper_dump.depth - 1;
    if (hook_wrapper_dump.depth &gt; 0) then
        buf = buf .. string.rep(&quot;\t&quot;, hook_wrapper_dump.depth - 1) .. '}'
    end
    return buf
end

function hook_wrapper(func_name, ...)
    -- we have to ensure that nil arguments are restored properly for the
    -- function call, see http://lua-users.org/wiki/StoringNilsInTables
    local args = { n=select('#', ...), ... }
    for i=1,args.n do
        local val = assert(loadstring(&quot;return &quot; .. args[i]),
                         &quot;argument &quot;..args[i]..&quot; could not be evaluated&quot;)()
        assert(val ~= nil or args[i] == &quot;nil&quot;,
               &quot;argument &quot;..args[i]..&quot; was evaluated to nil&quot;)
        args[i] = val
    end
    local res = { _G[func_name](unpack(args, 1, args.n)) }
    return hook_wrapper_dump._table(res)
end

do
   -- Hook functions are tables containing any of the following 6 items
   -- with associated functions:
   --
   --   startup			Corresponds to note_mtn_startup()
   --   start			Corresponds to note_netsync_start()
   --   revision_received	Corresponds to note_netsync_revision_received()
   --   revision_sent		Corresponds to note_netsync_revision_sent()
   --   cert_received		Corresponds to note_netsync_cert_received()
   --   cert_sent		Corresponds to note_netsync_cert_sent()
   --   pubkey_received		Corresponds to note_netsync_pubkey_received()
   --   pubkey_sent		Corresponds to note_netsync_pubkey_sent()
   --   end			Corresponds to note_netsync_end()
   --
   -- Those functions take exactly the same arguments as the corresponding
   -- global functions, but return a different kind of value, a tuple
   -- composed of a return code and a value to be returned back to monotone.
   -- The codes are strings:
   -- &quot;continue&quot; and &quot;stop&quot;
   -- When the code &quot;continue&quot; is returned and there's another notifier, the
   -- second value is ignored and the next notifier is called.  Otherwise,
   -- the second value is returned immediately.
   local hook_functions = {}
   local supported_items = {
      &quot;startup&quot;,
      &quot;start&quot;, &quot;revision_received&quot;, &quot;revision_sent&quot;, &quot;cert_received&quot;, &quot;cert_sent&quot;,
      &quot;pubkey_received&quot;, &quot;pubkey_sent&quot;, &quot;end&quot;
   }

   function _hook_functions_helper(f,...)
      local s = &quot;continue&quot;
      local v = nil
      for _,n in pairs(hook_functions) do
         if n[f] then
            s,v = n[f](...)
         end
         if s ~= &quot;continue&quot; then
            break
         end
      end
      return v
   end
   function note_mtn_startup(...)
      return _hook_functions_helper(&quot;startup&quot;,...)
   end
   function note_netsync_start(...)
      return _hook_functions_helper(&quot;start&quot;,...)
   end
   function note_netsync_revision_received(...)
      return _hook_functions_helper(&quot;revision_received&quot;,...)
   end
   function note_netsync_revision_sent(...)
      return _hook_functions_helper(&quot;revision_sent&quot;,...)
   end
   function note_netsync_cert_received(...)
      return _hook_functions_helper(&quot;cert_received&quot;,...)
   end
   function note_netsync_cert_sent(...)
      return _hook_functions_helper(&quot;cert_sent&quot;,...)
   end
   function note_netsync_pubkey_received(...)
      return _hook_functions_helper(&quot;pubkey_received&quot;,...)
   end
   function note_netsync_pubkey_sent(...)
      return _hook_functions_helper(&quot;pubkey_sent&quot;,...)
   end
   function note_netsync_end(...)
      return _hook_functions_helper(&quot;end&quot;,...)
   end

   function add_hook_functions(functions, precedence)
      if type(functions) ~= &quot;table&quot; or type(precedence) ~= &quot;number&quot; then
         return false, &quot;Invalid type&quot;
      end
      if hook_functions[precedence] then
         return false, &quot;Precedence already taken&quot;
      end

      local unknown_items = &quot;&quot;
      local warning = nil
      local is_member =
         function (s,t)
            for k,v in pairs(t) do if s == v then return true end end
            return false
         end

      for n,f in pairs(functions) do
         if type(n) == &quot;string&quot; then
            if not is_member(n, supported_items) then
               if unknown_items ~= &quot;&quot; then
                  unknown_items = unknown_items .. &quot;,&quot;
               end
               unknown_items = unknown_items .. n
            end
            if type(f) ~= &quot;function&quot; then
               return false, &quot;Value for functions item &quot;..n..&quot; isn't a function&quot;
            end
         else
            warning = &quot;Non-string item keys found in functions table&quot;
         end
      end

      if warning == nil and unknown_items ~= &quot;&quot; then
         warning = &quot;Unknown item(s) &quot; .. unknown_items .. &quot; in functions table&quot;
      end

      hook_functions[precedence] = functions
      return true, warning
   end
   function push_hook_functions(functions)
      local n = table.maxn(hook_functions) + 1
      return add_hook_functions(functions, n)
   end

   -- Kept for backward compatibility
   function add_netsync_notifier(notifier, precedence)
      return add_hook_functions(notifier, precedence)
   end
   function push_netsync_notifier(notifier)
      return push_hook_functions(notifier)
   end
end

-- to ensure only mapped authors are allowed through
-- return &quot;&quot; from unmapped_git_author
-- and validate_git_author will fail

function unmapped_git_author(author)
   -- replace &quot;foo@bar&quot; with &quot;foo &lt;foo@bar&gt;&quot;
   name = author:match(&quot;^([^&lt;&gt;]+)@[^&lt;&gt;]+$&quot;)
   if name then
      return name .. &quot; &lt;&quot; .. author .. &quot;&gt;&quot;
   end

   -- replace &quot;&lt;foo@bar&gt;&quot; with &quot;foo &lt;foo@bar&gt;&quot;
   name = author:match(&quot;^&lt;([^&lt;&gt;]+)@[^&lt;&gt;]+&gt;$&quot;)
   if name then
      return name .. &quot; &quot; .. author
   end

   -- replace &quot;foo&quot; with &quot;foo &lt;foo&gt;&quot;
   name = author:match(&quot;^[^&lt;&gt;@]+$&quot;)
   if name then
      return name .. &quot; &lt;&quot; .. name .. &quot;&gt;&quot;
   end

   return author -- unchanged
end

function validate_git_author(author)
   -- ensure author matches the &quot;Name &lt;email&gt;&quot; format git expects
   if author:match(&quot;^[^&lt;]+ &lt;[^&gt;]*&gt;$&quot;) then
      return true
   end

   return false
end

function get_man_page_formatter_command()
   local term_width = guess_terminal_width() - 2
   -- The string returned is run in a process created with 'popen'
   -- (see cmd.cc manpage).
   --
   -- On Unix (and POSIX compliant systems), 'popen' runs 'sh' with
   -- the inherited path.
   --
   -- On MinGW, 'popen' runs 'cmd.exe' with the inherited path. MinGW
   -- does not (currently) provide nroff or equivalent. So we assume
   -- sh, nroff, locale and less are also installed, from Cygwin or
   -- some other toolset.
   --
   -- GROFF_ENCODING is an environment variable that, when set, tells
   -- groff (called by nroff where applicable) to use preconv to convert
   -- the input from the given encoding to something groff understands.
   -- For example, groff doesn NOT understand raw UTF-8 as input, but
   -- it does understand unicode, which preconv will happily provide.
   -- This doesn't help people that don't use groff, unfortunately.
   -- Patches are welcome!
   if string.sub(get_ostype(), 1, 7) == &quot;Windows&quot; then
      return string.format(&quot;sh -c 'GROFF_ENCODING=`locale charmap` nroff -man -rLL=%dn' | less -R&quot;, term_width)
   else
      return string.format(&quot;GROFF_ENCODING=`locale charmap` nroff -man -rLL=%dn | less -R&quot;, term_width)
   end
end

</pre>
<hr>
<div class="header">
<p>
Next: <a href="General-Index.html#General-Index" accesskey="n" rel="next">General Index</a>, Previous: <a href="Special-Topics.html#Special-Topics" accesskey="p" rel="prev">Special Topics</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="General-Index.html#General-Index" title="Index" rel="index">Index</a>]</p>
</div>



</body>
</html>