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
|
#!/usr/bin/perl -w
# makepatch.pl -- generate a patch kit from two files or directories.
# Author : Johan Vromans
# Created On : Tue Jul 7 20:39:39 1992
# Last Modified By: Johan Vromans
# Last Modified On: Sun Oct 8 15:06:50 2006
# Update Count : 1187
# Status : Released
#
my $RCS_Id = '$Id: makepatch.pl,v 1.135 2006/10/08 13:06:55 jv Exp $ ';
use strict;
use Getopt::Long 2.00;
use IO qw(File);
use File::Basename;
use File::Spec;
use File::Path;
################ Common stuff ################
my $my_package = 'Sciurix';
my $my_name = "makepatch";
my $my_version = "2.03";
my $data_version = '1.0';
$my_version .= '*' if length('$Locker: $ ') > 12;
################ Globals ################
## Options and defaults
my $opt_diff = 'diff -c'; # default diff command
my $opt_sort; # sort entries. Default = 1
my $opt_follow = 0; # follow symbolic links
my $opt_automanifest = "MANIFEST";
my $opt_oldmanifest; # list of files of the old tree
my $opt_newmanifest; # list of files of the new tree
my $opt_nomanifest = 0; # suppress use of MANIFEST files
my $opt_patchlevel; # patchlevel.h file
my $opt_prefix = ''; # prefix to be added
my $opt_filelist = 0; # make file list
my $opt_infocmd; # info command
my $opt_exclude_standard = 1; # standard excludes
my $opt_exclude_rcs = 0; # exclude RCS files
my $opt_exclude_cvs = 0; # exclude CVS files
my $opt_exclude_sccs = 0; # exclude SCCS files
my $opt_ignore_rcs_keywords = 0; # exclude CVS/RCS keyword data
my @opt_exclude; # list of excludes (wildcards)
my @opt_exclude_regex; # list of excludes (regex)
my $opt_recurse = 1; # recurse
my @opt_descr = (); # description
my %opt_extract = (); # extraction rules
# Development options (not shown with -help).
my $opt_trace = 0; # trace messages
my $opt_verbose = 0; # verbose info
my $opt_quiet = 0; # (almost?) no info
my $opt_debug = 0; # debugging messages
my $opt_test = 0; # testing
## Misc
my $exclude_pat; # regex to exclude
my @workq = (); # pre/post work
my $TMPDIR = $ENV{TMPDIR} || $ENV{TEMP};
$TMPDIR = File::Spec->tmpdir if !$TMPDIR && File::Spec->can("tmpdir");
$TMPDIR ||= "/tmp";
my $dot_u = File::Spec::Unix->curdir; # UNIX current dir
my $dot = File::Spec->curdir; # current dir
my $dotdot = File::Spec->updir; # parent dir
my $HOME = $ENV{HOME} || ($ENV{HOMEDRIVE} && $ENV{HOMEPATH} && $ENV{HOMEDRIVE}.$ENV{HOMEPATH}) || $dot;
my $nul = ($^O =~ /^MSWin/i) ? "nul" : "/dev/null"; # NLA0:
my $timestamp = "".localtime(); # timestamp, in string format
my $nulpat = quotemeta ($nul); # pattern to match nul
my $unified = 0; # produce unified diff
my $skipped = 0; # number of files skipped.
my $excluded = 0; # number of files excluded.
## Subroutine prototypes
sub app_options ();
sub app_parse_rc ($$$);
sub app_usage ($);
sub app_usage_filelist ($);
sub catfile ($$);
sub check_extract ($);
sub cleanup ();
sub cvs_excludes($$$);
sub cvs_ignore($);
sub debug (@);
sub dodiff ($$$$);
sub makepatch ();
sub extract ($$);
sub filelist ($);
sub generate_perl ($);
sub generate_shell ($);
sub make_filelist ($;$);
sub make_filelist_from_manifest ($);
sub message (@);
sub newfile ($$);
sub quotfn ($);
sub setup_excludes ();
sub showopts ($);
sub trace (@);
sub verbose (@);
sub wrapup (;$);
sub yesno ($);
################ INI files, program parameters ################
app_options ();
################ Presets ################
if ( $opt_exclude_sccs ) {
unshift (@opt_exclude, qw(p.* s.* SCCS));
}
if ( $opt_exclude_rcs ) {
unshift (@opt_exclude, ',*', '*,v', qw(RCS RCSLOG));
}
if ( $opt_exclude_cvs ) {
# Load common .cvsignore, if present.
for ( $HOME ."/.cvsignore" ) {
unshift (@opt_exclude, cvs_ignore($_)) if -s $_;
}
unshift (@opt_exclude, '.#*', '#*',
qw(_$* *$ CVS CVS.adm cvslog.*));
}
if ( $opt_exclude_standard ) {
# Common excludes.
# Mostly copied from 'Open Source Development with CVS', p. 170.
unshift (@opt_exclude,
qw(*~ *.a *.bak *.BAK *.elc *.exe *.gz *.ln *.o *.obj
*.olb *.old *.orig *.rej *.so *.Z
.del-* .make.state .nse_depinfo core
tags TAGS));
}
setup_excludes ();
if ( $opt_ignore_rcs_keywords ) {
# Note: We ignore 'Log' since that wouldn't work anyway.
$opt_diff .= ' ' .
q{'--ignore-matching-lines=\\$\\(} .
join('\\|', qw(Author Date Header Id Locker Name RCSfile
Revision Source State)) .
q{\\)[^$]*\\$'};
}
################ The Process ################
# Handle --filelist. Special but obsolete case.
if ( $opt_filelist ) {
filelist ($ARGV[0]);
die ("Okay\n") if $opt_test;
exit (0);
}
# Check temp dir.
unless ( -d $TMPDIR && -w $TMPDIR ) {
print STDERR <<EOD;
Please use environment variable TMPDIR or TEMP to designate a writable
directory to hold temporary files.
EOD
die ("Cannot continue\n");
}
# Create temp dir and names for temp files.
my $tmpdir = File::Spec->catdir ($TMPDIR, "mp$$.d");
mkdir ($tmpdir, 0777) or die ("tmpdir: $!\n");
my $thepatch = catfile ($tmpdir, ".mp$$.p");
my $tmpfile = catfile ($tmpdir, ".mp$$.t");
my $patch = new IO::File;
# Attach cleanup handler.
$SIG{INT} = \&cleanup;
$SIG{QUIT} = \&cleanup;
# The arguments.
my ($old, $new);
if ( $] >= 5.005 && $] < 5.008 ) {
# Use pseudo-hashes if possible.
my %fields = ( tag => 1, # old/new
name => 2, # given name on command line
root => 3, # real (physical) directory
base => 4, # basename (for archives)
man => 5, # name of manifest
manfn => 6, # same, real file name
files => 7, # list of files
);
$old = [ \%fields, "old", shift(@ARGV) ];
$new = [ \%fields, "new", shift(@ARGV) ];
}
else {
$old = { tag => "old", name => shift(@ARGV) };
$new = { tag => "new", name => shift(@ARGV) };
}
# Unpack archives, if applicable.
# $old->{root} and $new->{root} are the real locations for the source trees.
check_extract ($old);
check_extract ($new);
# The process.
makepatch ();
# Wrap up.
wrapup ();
die ("Okay\n") if $opt_test;
# In case nothing went wrong...
END { cleanup (); }
################ Subroutines ################
sub message (@) { print STDERR (@_) unless $opt_quiet; }
sub verbose (@) { print STDERR (@_) if $opt_verbose; }
sub debug (@) { print STDERR (@_) if $opt_debug; }
sub trace (@) { print STDERR (@_) if $opt_trace; }
sub makepatch () {
# This will bail out if the directory could not be created.
$patch->open(">$thepatch") || die ("$thepatch: $!\n");
binmode($patch);
if ( -f $old->{root} && -f $new->{root} ) {
# Two files.
verbose ("Old file = $old->{root}.\n", "New file = $new->{root}.\n");
dodiff ($dot, $new->{root}, $dot, $old->{root}) &&
push (@workq, [ 'p', $old->{root}, -s $old->{root},
(stat($new->{root}))[9], (stat(_))[2] ]);
}
elsif ( -f $old->{root} && -d $new->{root} ) {
# File and dir -> File and dir/File.
$new->{root} = $new->{base} = catfile ($new->{root}, $old->{root});
verbose ("Old file = $old->{root}.\n", "New file = $new->{root}.\n");
if ( -f $new->{root} ) {
dodiff ($dot, $new->{root}, $dot, $old->{root}) &&
push (@workq, [ 'p', $old->{root}, -s $old->{root},
(stat($new->{root}))[9], (stat(_))[2] ]);
}
else {
unshift (@workq, [ 'r', $old->{root}, -s $old->{root}, 0 ]);
}
}
elsif ( -d $old->{root} && -f $new->{root} ) {
# Dir and file -> Dir/file and file.
$old->{root} = $old->{base} = catfile ($old->{root}, $new->{root});
verbose ("Old file = $old->{root}.\n", "New file = $new->{root}.\n");
if ( -f $old->{root} ) {
dodiff ($dot, $new->{root}, $dot, $old->{root}) &&
push (@workq, [ 'p', $old->{root}, -s $old->{root},
(stat($new->{root}))[9], (stat(_))[2] ]);
}
else {
newfile ($new->{root}, $old->{root}) &&
push (@workq, [ 'c', $old->{root}, 0,
(stat($new->{root}))[9], (stat(_))[2] ]);
}
}
elsif ( -d $old->{root} && -d $new->{root} ) {
# Two directories.
if ( $opt_nomanifest ) {
verbose ("Not using MANIFEST files.\n");
undef $opt_oldmanifest;
undef $opt_newmanifest;
}
elsif ( defined $opt_automanifest &&
!(defined $opt_oldmanifest || defined $opt_newmanifest) &&
(-s catfile($old->{root}, $opt_automanifest) &&
-s catfile($new->{root}, $opt_automanifest)) ) {
verbose ("Using standard $opt_automanifest files.\n");
$opt_oldmanifest = catfile($old->{root},$opt_automanifest);
$opt_newmanifest = catfile($new->{root},$opt_automanifest);
$new->{man} = $old->{man} = $opt_automanifest;
$old->{manfn} = $opt_oldmanifest;
$new->{manfn} = $opt_newmanifest;
}
else {
$old->{man} = $old->{manfn} = $opt_oldmanifest;
$new->{man} = $new->{manfn} = $opt_newmanifest;
}
for ( $old, $new ) {
if ( defined ($_->{manfn}) ) {
my $t = $_->{name} eq $dot ? "current directory" :
$_->{name} eq $dotdot ? "parent directory" : $_->{base};
$_->{files} = [ make_filelist_from_manifest ($_->{manfn}) ];
message ("Manifest $_->{man} for $t contains ",
scalar(@{$_->{files}}), " file",
scalar(@{$_->{files}}) == 1 ? "" : "s", ".\n");
}
else {
my $t = $_->{name} eq $dot ? "current directory" :
$_->{name} eq $dotdot ? "parent directory" :
"directory $_->{base}";
message ("Building file list for $t ...\n");
$_->{files} = [ make_filelist ($_->{root}) ];
message (ucfirst($t)." contains ",
scalar(@{$_->{files}}), " file",
scalar(@{$_->{files}}) == 1 ? "" : "s", ".\n");
}
}
# Handle patchlevel file first.
$opt_patchlevel = (grep (/patchlevel\.h/, @{$new->{files}}))[0]
unless defined $opt_patchlevel;
if ( defined $opt_patchlevel && $opt_patchlevel ne "" ) {
my $oldpl = catfile ($old->{root}, $opt_patchlevel);
my $newpl = catfile ($new->{root}, $opt_patchlevel);
if ( ! -f $newpl ) {
die ("$newpl: $!\n");
}
if ( -f $oldpl ) {
push (@workq, [ dodiff ($new->{root}, $opt_patchlevel,
$old->{root}, $opt_patchlevel) ? 'p' : 'v',
$opt_patchlevel,
-s $oldpl,
(stat($newpl))[9], (stat(_))[2] ]);
# Remove patchlevel.h from the list of old files.
$old->{files} = [ grep ($_ ne $opt_patchlevel, @{$old->{files}}) ];
}
else {
newfile ($new->{root}, $opt_patchlevel) &&
push (@workq, [ 'c', $opt_patchlevel, 0,
(stat($newpl))[9], (stat(_))[2] ]);
}
# Remove patchlevel.h from the list of new files.
$new->{files} = [ grep ($_ ne $opt_patchlevel, @{$new->{files}}) ];
}
else {
undef $opt_patchlevel;
}
my $o;
my $n;
message ("Processing the filelists ...\n");
while ( scalar(@{$old->{files}}) + scalar(@{$new->{files}}) > 0
|| defined $o || defined $n ) {
$o = shift (@{$old->{files}}) unless defined $o;
$n = shift (@{$new->{files}}) unless defined $n;
debug ("* ", $o || "(undef)", " <-> ", $n || "(undef)", " ",
"* $old->{files}->[0] <-> $new->{files}->[0]\n") if $opt_debug;
if ( defined $n && (!defined $o || $o gt $n) ) {
# New file.
debug ("*> New file: $n\n");
newfile ($new->{root}, $n) &&
push (@workq, [ 'c', $n, 0,
(stat(catfile($new->{root},$n)))[9],
(stat(_))[2] ]);
undef $n;
}
elsif ( !defined $n || $o lt $n ) {
# Obsolete (removed) file.
debug ("*> Obsolete: $o\n");
unshift (@workq, [ 'r', $o, -s catfile($old->{root},$o), 0 ]);
undef $o;
}
elsif ( $o eq $n ) {
# Same file.
debug ("*> Compare: $n\n");
dodiff ($new->{root}, $n, $old->{root}, $o) &&
push (@workq, [ 'p', $o, -s catfile($old->{root},$o),
(stat(catfile($new->{root},$n)))[9],
(stat(_))[2] ]);
undef $n;
undef $o;
}
}
}
else {
$patch->close;
app_usage (1);
}
$patch->close;
# For the sake of memory usage...
undef $old->{files};
undef $new->{files};
}
sub cleanup () {
return unless defined $tmpdir;
return unless -d $tmpdir;
verbose ("Cleaning up...\n");
rmtree ($tmpdir);
die ("Okay\n") if $opt_test;
exit (0);
}
sub shellpat($) {
my ($pat) = (@_);
my @a = split (/(\[[^\]]+\]|[*.?])/, $pat);
join ('',
(map { ($_ eq '*' ? '.*' :
($_ eq '?' ? '.' :
($_ eq '.' ? '\.' :
($_ =~ /^\[/ ? $_ : quotemeta ($_)))))
} @a));
}
sub setup_excludes () {
# Add --exclude wildcards to --exclude-regex list.
if ( @opt_exclude ) {
my $pat;
foreach $pat ( @opt_exclude ) {
push (@opt_exclude_regex, '(\A|/)'.shellpat($pat).'\Z');
}
}
# Build regex from --exclude-regex list.
if ( @opt_exclude_regex ) {
$exclude_pat = '(';
my $re;
foreach $re ( @opt_exclude_regex ) {
verbose (" Exclude regex: ", $re, "\n");
eval { '' =~ /$re/ };
if ( $@ ) {
$@ =~ s/ at .* line.*$//;
die ("Invalid regex: $re $@");
}
$exclude_pat .= "($re)|";
}
chop ($exclude_pat);
$exclude_pat .= ')';
debug ("Exclude pattern: $exclude_pat\n");
}
}
sub cvs_ignore($) {
my ($f) = @_;
my $fh = do { local *F; *F; };
unless ( open($fh, $f) ) {
warn("$f: $!\n");
return ();
}
local($/) = undef;
my $pat = <$fh>;
close($fh);
$pat =~ s/[\n\r]+/\n/g;
$pat =~ s/\s+$//;
$pat =~ s/^\s+//;
split(/\n/, $pat);
}
sub cvs_excludes($$$) {
my ($f, $dir, $disp) = @_;
my @list = cvs_ignore($f);
return "" unless @list;
for ( $dir, $disp ) {
$_ = "" unless defined $_;
$_ .= '/' if $_ && $_ !~ /\/$/;
$_ = '\A' . quotemeta($_);
}
my $ret = "";
foreach my $pat ( @list ) {
my $re = shellpat($pat);
debug ("$f: '$pat' -> '$re'\n");
eval { '' =~ /$re/ };
if ( $@ ) {
$@ =~ s/ at .* line.*$//;
warn("$f: invalid pattern '$pat'");
next;
}
push(@opt_exclude_regex, $dir.$re.'\Z');
$ret .= "($re)|";
}
if ( $ret ) {
chop($ret);
$ret = '('.$disp.'('.$ret.')\Z)';
}
debug ("Exclude pattern ($f): $ret\n");
$ret;
}
sub make_filelist ($;$) {
my ($dir, $disp) = @_;
# Return a list of files, sorted, for this directory.
# Recurses if $opt_recurse.
my $dh = new IO::File;
trace ("+ recurse $dir\n");
opendir ($dh, $dir) || die ("$dir: $!\n");
my @tmp = readdir ($dh);
closedir ($dh);
debug ("Dir $dir: ", scalar(@tmp), " entries\n");
my @ret = ();
my $file;
my $excl = $exclude_pat;
for ( catfile($dir, ".cvsignore") ) {
$excl = '('.$excl.'|'.cvs_excludes($_,$dir,$disp).')' if -s $_;
debug("Exclude pattern: $excl\n");
}
foreach $file ( @tmp ) {
# Skip unwanted files.
next if $file =~ /^\.\.?$/; # dot and dotdot
next if $file =~ /~$/; # editor backup files
my $realname = catfile ($dir, $file);
my $display_name = defined $disp ? catfile($disp,$file) : $file;
# Skip exclusions.
if ( defined $excl && $display_name =~ /$excl/mso ) {
verbose ("Excluding $display_name\n");
$excluded++;
next;
}
# Push on the list.
if ( -d $realname && ( $opt_follow || ! -l $realname ) ) {
next unless $opt_recurse;
# Recurse.
push (@ret, make_filelist ($realname, $display_name));
}
elsif ( -f _ ) {
debug("+ file $display_name\n");
push (@ret, $display_name);
}
else {
verbose ("WARNING: Not a file: $realname -- skipped\n");
$skipped++;
}
}
@ret = sort @ret if $opt_sort;
@ret;
}
sub make_filelist_from_manifest ($) {
# Return a list of files, optionally sorted, from a manifest file.
my ($man) = @_;
my $fh = new IO::File;
my @ret = ();
local ($_);
$fh->open($man) || die ("$man: $!\n");
binmode($fh);
while ( <$fh> ) {
if ( $. == 2 && /^[-=_\s]*$/ ) {
@ret = ();
next;
}
next if /^#/;
next unless /\S/;
$_ = $1 if /^(\S+)\s/;
if ( defined $exclude_pat && /$exclude_pat/mso ) {
verbose ("Excluding $_\n");
$excluded++;
next;
}
push (@ret, $_);
}
$fh->close;
@ret = sort @ret if $opt_sort;
@ret;
}
sub check_extract ($) {
my ($arg) = @_;
my @exctrl = ('.+\.(tar\.gz|tgz)' => "gzip -d | tar xpf -",
'.+\.(tar\.bz2)' => "bzip2 -d | tar xpf -",
'.+\.(tar)' => "tar xf -",
'.+\.(zip)' => "unzip -",
);
# Plug in user defined rules.
if ( %opt_extract ) {
my ($k, $v);
while ( ($k,$v) = each (%opt_extract) ) {
unshift (@exctrl, $v);
unshift (@exctrl, $k);
}
}
$arg->{root} = File::Spec->canonpath ($arg->{name});
my $base = basename ($arg->{root});
while ( @exctrl > 0 ) {
my $pat = shift (@exctrl);
my $cmd = shift (@exctrl);
if ( $base =~ /^$pat$/is ) {
extract ($arg, $cmd);
verbose ("Using $arg->{root} for $arg->{name}\n")
unless $arg->{root} eq $arg->{name};
return;
}
}
$arg->{root} = $arg->{base} = $arg->{name};
}
sub extract ($$) {
my ($arg, $cmd) = @_;
my $tmp = catfile ($tmpdir, $arg->{tag});
message ("Extracting $arg->{name} to $tmp...\n");
# Create a temp directory.
mkdir ($tmp, 0777) || die ("Cannot mkdir $tmp [$!]\n");
# Extract the kit.
$cmd = "( cd $tmp; $cmd ) < $arg->{name}";
trace ("+ $cmd\n");
my $ret = system ("$cmd 1>&2");
if ( $ret || ($? & 127) ) {
die ("Not okay 1\n") if $opt_test;
exit (1);
}
# Inspect the directory.
my $dir = new IO::File;
opendir ($dir, $tmp) || die ("Cannot read $tmp [$!]\n");
my @files = grep ($_ !~ /^\.+$/, readdir ($dir));
closedir ($dir);
# If we have only one directory, assume it is the root.
if ( @files == 1 && -d catfile($tmp,$files[0]) ) {
$arg->{base} = $files[0];
$arg->{root} = catfile($tmp,$files[0]);
return;
}
# Else, take the temp dir as root.
$arg->{root} = $tmp;
$arg->{base} = $arg->{name};
}
sub catfile ($$) {
File::Spec->canonpath(File::Spec->catfile(@_));
}
sub dot_file_u ($) {
$_[0] =~ s,\\,/,g if $^O =~ /^MSWin/i;
File::Spec::Unix->catfile($dot_u, File::Spec::Unix->canonpath(@_));
}
sub dodiff ($$$$) {
my ($newdir, $new, $olddir, $old) = @_;
my $fh = new IO::File;
my $oldfn = catfile ($olddir, $old);
my $newfn = catfile ($newdir, $new);
# Check for binary files.
if ( -s $oldfn && -B _ ) {
verbose ("WARNING: Binary file $oldfn -- skipped\n");
$skipped++;
return 0;
}
if ( -s $newfn && -B _ ) {
verbose ("WARNING: Binary file $newfn -- skipped\n");
$skipped++;
return 0;
}
# Produce a patch hunk.
my $cmd = $opt_diff . ' ' . quotfn($oldfn) . ' ' . quotfn($newfn);
trace ("+ ", $cmd, "\n");
my $result = system ("$cmd > $tmpfile");
debug (sprintf ("+> result = 0x%x\n", $result)) if $result;
if ( $result && $result < 128 ) {
wrapup (($result == 2 || $result == 3)
? "User request" : "System error");
die ("Not okay 2\n") if $opt_test;
exit (1);
}
return 0 unless $result == 0x100; # no diffs
print $patch ($cmd, "\n");
# Add output from user defined file information command.
if ( defined $opt_infocmd ) {
my $cmd = $opt_infocmd;
$cmd =~ s/\002P/$oldfn/eg;
$cmd =~ s/\003P/$newfn/eg;
print $patch (`$cmd`);
}
# By prepending $dot to the names, we can use 'patch -p0' as well
# as 'patch -p1'.
print $patch ("Index: ", dot_file_u($old), "\n");
# Try to find a prereq.
# The RCS code is based on a suggestion by jima@netcom.com, who also
# pointed out that patch requires blanks around the prereq string.
if ( $fh->open($oldfn) ) {
binmode($fh);
while ( <$fh> ) {
next unless (/(\@\(\#\)\@?|\$Header\:|\$Id\:)(.*)$/);
next unless $+ =~ /(\s\d+(\.\d+)*\s)/; # e.g. 5.4
print $patch ("Prereq: $1\n");
last;
}
$fh->close;
}
else {
warn ("$oldfn: $!\n");
}
# Copy patch.
$fh->open($tmpfile) || die ("$tmpfile: $!\n");
binmode($fh);
# Skip to beginning of patch. Adjust $unified if needed.
my $found = 0;
while ( <$fh> ) {
if ( /^\@\@/ ) {
$unified = 1;
$found = 1;
last;
}
elsif ( /^\*{15}/ ) {
$unified = 0;
$found = 1;
last;
}
}
unless ( $found ) {
die ("ALARM: No patch data found for $old\n",
"Something is wrong with your diff command \"$opt_diff\".\n",
"It should produce context or unified diff output.\n");
}
# Replace patch header.
if ( $unified ) {
print $patch ("--- ", dot_file_u($old),
"\t" . localtime((stat($oldfn))[9]), "\n",
"+++ ", dot_file_u($new),
"\t" . localtime((stat($newfn))[9]), "\n",
$_);
}
else {
print $patch ("*** ", dot_file_u($old),
"\t" . localtime((stat($oldfn))[9]), "\n",
"--- ", dot_file_u($new),
"\t" . localtime((stat($newfn))[9]), "\n",
$_);
}
# Copy rest.
print $patch ($_) while <$fh>;
$fh->close;
return 1;
}
sub newfile ($$) {
# In-line production of what diff would have produced.
my ($newdir, $new) = @_;
my $fh = new IO::File;
my $newfn = catfile ($newdir, $new);
my $lines = 0;
unless ( $fh->open($newfn) ) {
warn ("$newfn: $!\n");
$skipped++;
return 0;
}
binmode($fh);
# We cannot trust stdio here.
if ( -s $newfn && -B _ ) {
verbose ("WARNING: Binary file $new -- skipped\n");
$skipped++;
return 0;
}
my $pos = $fh->getpos;
while ( <$fh> ) {
$lines++;
}
$fh->setpos($pos);
# Avoid creating a patch if the new file is empty.
if ($lines == 0) {
return 1;
}
my $cmd = $opt_diff . " " . $nul . " " . quotfn($newfn);
trace ("+ $cmd (inlined)\n");
print $patch ($cmd, "\n");
# Add output from user defined file information command.
if ( defined $opt_infocmd ) {
my $cmd = $opt_infocmd;
$cmd =~ s/\002P/$newfn/eg;
$cmd =~ s/\003P/$newfn/eg;
print $patch (`$cmd`);
}
# Prepending $dot, so we can use 'patch -p0' as well as 'patch -p1'.
$new = dot_file_u($new);
print $patch ("Index: $new\n");
$lines = "1,$lines" unless $lines == 1;
if ( $unified ) {
print $patch ("--- ", $new, "\t" . localtime(0), "\n",
"+++ ", $new, "\t" . localtime((stat($fh))[9]), "\n",
"\@\@ -0,0 +", $lines, " \@\@\n");
while ( <$fh> ) {
print $patch ("+$_");
}
}
else {
print $patch ("*** ", $new, "\t" . localtime(0), "\n",
"--- ", $new, "\t" . localtime((stat($fh))[9]), "\n",
"***************\n",
"*** 0 ****\n",
"--- ", $lines, " ----\n");
while ( <$fh> ) {
print $patch ("+ $_");
}
}
$fh->close;
return 1;
}
sub remove_file ($$) {
# diff -c -N -r t1/f2 t2/f2
# *** t1/f2 Tue Jul 7 21:28:45 1992
# --- t2/f2 Thu Jan 1 01:00:00 1970
# ***************
# *** 1,1 ****
# - foo
# - bar
# --- 0 ----
# diff -u -N -r t1/f2 t2/f2
# --- t1/f2 Tue Jul 7 21:28:45 1992
# +++ t2/f2 Thu Jan 1 01:00:00 1970
# @@ -1,1 +0,0 @@
# -foo
# -bar
}
sub quotfn ($) {
my ($file) = @_;
# Protect file name.
$file =~ s/`/\\`/g;
($^O =~ /^MSWin/i) ? "\"$file\"" : "'$file'";
}
sub wrapup (;$) {
my ($reason) = @_;
if ( defined $reason ) {
warn ("*** Aborted: $reason ***\n");
return;
}
warn ("WARNING: $skipped file",
$skipped == 1 ? " was" : "s were", " skipped!",
$opt_verbose ? "" : " Use \"--verbose\" for more details.",
"\n") if $skipped;
# Construct a description, if possible.
if ( @opt_descr == 0 ) {
my $old = $old->{base};
my $new = $new->{base};
# We can infer a name if the file name does not contain a
# directory part, and is not equal to . or ..
if ( $old ne $dot && $old ne $dotdot && basename($old) eq $old &&
$new ne $dot && $new ne $dotdot && basename($new) eq $new
) {
@opt_descr = ("This is a patch for $old to update it to $new");
}
}
# Get a description, unless provided.
if ( @opt_descr == 0 ) {
print STDERR ("Enter patch description, ",
"terminated with a single '.':\n>> ");
while ( <STDIN> ) {
chomp;
last if $_ eq ".";
push (@opt_descr, $_);
print STDERR (">> ");
}
print STDERR ("\n") unless $_ eq ".";
}
push (@opt_descr, "");
message ("Collecting patches ...\n");
my $removed = 0; # files removed
my $created = 0; # files added
my $patched = 0; # files patched
my $dremoved = 0; # directories removed
my $dcreated = 0; # directories created
{ my @goners = ();
my %dir_gone = ();
my @newcomers = ();
my %dir_ok = ();
foreach ( @workq ) {
my ($op, $fn) = @$_;
push (@newcomers, $fn) if $op eq 'c';
push (@goners, $fn) if $op eq 'r';
$patched++ if $op eq 'p';
}
$created = @newcomers;
$removed = @goners;
foreach ( sort @goners ) {
# WARNING: This code assumes you are running some Unix.
my @p = split (/\//, $_);
pop (@p);
foreach my $i ( (1-@p)..0 ) {
my $dir = join('/',@p[0..-$i]);
unless ( defined $dir_gone{$dir} ) {
unless ( -d catfile($new->{root},$dir) ) {
$dremoved++;
$dir_gone{$dir} = 1;
}
}
}
}
foreach ( reverse sort keys %dir_gone ) {
push (@workq, [ 'R', $_ ]);
}
foreach ( sort @newcomers ) {
# Explicitly create the new files since not all patch versions
# can handle creating new files.
# Create intermediate directories first.
# WARNING: This code assumes you are running some Unix.
my @p = split (/\//, $_);
pop (@p);
foreach my $i ( 0..(@p-1) ) {
my $dir = join('/',@p[0..$i]);
unless ( defined $dir_ok{$dir} ) {
unless ( -d catfile($old->{root},$dir) ) {
push (@workq, [ 'C', $dir, 0,
(stat(catfile($new->{root},$dir)))[9],
(stat(_))[2] ]);
$dcreated++;
}
$dir_ok{$dir} = 1;
}
}
}
}
my $fh = new IO::File;
$fh->open(">$tmpfile") || die ("$tmpfile: $!\n");
binmode($fh);
foreach ( @opt_descr ) {
print $fh ("# ", $_, "\n");
}
print $fh <<EOD;
# To apply this patch:
# STEP 1: Chdir to the source directory.
# STEP 2: Run the 'applypatch' program with this patch file as input.
#
# If you do not have 'applypatch', it is part of the 'makepatch' package
# that you can fetch from the Comprehensive Perl Archive Network:
# http://www.perl.com/CPAN/authors/Johan_Vromans/makepatch-x.y.tar.gz
# In the above URL, 'x' should be 2 or higher.
#
# To apply this patch without the use of 'applypatch':
# STEP 1: Chdir to the source directory.
EOD
if ( $removed || $created ) {
my $cd = "";
my $fd = "";
$cd = "create" if $created;
if ( $removed ) {
$cd .= "/" if $cd;
$cd .= "delete";
}
$fd = "files";
if ( $dcreated || $dremoved ) {
$fd .= "/" if $fd;
$fd .= "directories";
}
print $fh <<EOD;
# If you have a decent Bourne-type shell:
# STEP 2: Run the shell with this file as input.
# If you don't have such a shell, you may need to manually $cd
# the $fd as shown below.
# STEP 3: Run the 'patch' program with this file as input.
#
# These are the commands needed to create/delete files/directories:
#
EOD
foreach ( @workq ) {
my ($op, $file, @args) = @$_;
if ( $op eq 'C' ) {
print $fh ("mkdir ", quotfn($file), "\n");
if ( defined $args[2] && ($args[2] &= 0777) ) {
printf $fh ("chmod 0%o %s\n", $args[2], quotfn($file))
}
}
}
foreach ( @workq ) {
my ($op, $file, @args) = @$_;
if ( $op eq 'r' ) {
print $fh ("rm -f ", quotfn($file), "\n");
}
elsif ( $op eq 'R' ) {
print $fh ("rmdir ", quotfn($file), "\n");
}
elsif ( $op eq 'c' ) {
print $fh ("touch ", quotfn($file), "\n");
if ( defined $args[2] && ($args[2] &= 0777) ) {
printf $fh ("chmod 0%o %s\n", $args[2], quotfn($file))
}
}
}
print $fh <<EOD;
#
# This command terminates the shell and need not be executed manually.
exit
#
EOD
}
else {
print $fh <<EOD;
# STEP 2: Run the 'patch' program with this file as input.
#
EOD
}
print $fh <<EOD;
#### End of Preamble ####
#### Patch data follows ####
EOD
# Copy patch.
$patch->open($thepatch);
binmode($patch);
while ( <$patch> ) {
print $fh $_;
}
$patch->close;
# Print a reassuring "End of Patch" note so people won't
# wonder if their mailer truncated patches.
print $fh ("#### End of Patch data ####\n\n",
"#### ApplyPatch data follows ####\n",
"# Data version : $data_version\n",
"# Date generated : $timestamp\n",
"# Generated by : $my_name $my_version\n");
print $fh ("# Recurse directories : Yes\n") if $opt_recurse;
print $fh ("# Excluded files : ",
join("\n# ", @opt_exclude_regex), "\n")
if @opt_exclude_regex;
foreach ( @workq ) {
my ($op, $file, @args) = @$_;
$file = quotfn ($file);
print $fh ("# ", $op, " ", $file);
if ( defined ($args[2]) && ($op eq 'c' || $op eq 'C' || $op eq 'p') ) {
$args[2] = sprintf ("0%o", $args[2]);
}
print $fh (" ", join(" ", @args)) if @args;
print $fh ("\n");
}
print $fh ("#### End of ApplyPatch data ####\n");
print $fh ("\n#### End of Patch kit [created: $timestamp] ####\n");
$fh->close;
# Checksum calculation.
# Two checksums are calculated: one for the whole file (for compatibilty),
# and one for just the patch data (so the preamble can be modified).
my $lines = 0;
my $bytes = 0;
my $sum = 0;
my $all_lines = 0;
my $all_bytes = 0;
my $all_sum = 0;
$fh->open ($tmpfile) || die ("$tmpfile: $!\n");
binmode($fh);
binmode(STDOUT);
while ( <$fh> ) {
$lines = $bytes = $sum = 0
if /^#### Patch data follows ####/;
chomp;
$_ .= "\n";
$lines++;
$all_lines++;
$bytes += length ($_);
$all_bytes += length ($_);
# System V 'sum' checksum
$sum = ($sum + unpack ("%16C*", $_)) % 65535;
$all_sum = ($all_sum + unpack ("%16C*", $_)) % 65535;
print STDOUT ($_);
}
$fh->close;
# Checksum info for the patch data.
$_ = "#### Patch checksum: $lines $bytes $sum ####\n";
print STDOUT ($_);
$all_lines++;
$all_bytes += length ($_);
$all_sum = ($all_sum + unpack ("%16C*", $_)) % 65535;
# Overall checksum info.
print STDOUT ("#### Checksum: $all_lines $all_bytes $all_sum ####\n");
message (" $patched file",
$patched == 1 ? "" : "s", " need to be patched.\n");
if ( $created ) {
message (" $created file", $created == 1 ? "" : "s");
message (" and $dcreated director",
$dcreated == 1 ? "y" : "ies") if $dcreated;
message (" need", ($created+$dcreated != 1) ? "" : "s",
" to be created.\n");
}
if ( $removed ) {
message (" $removed file", $removed == 1 ? "" : "s");
message (" and $dremoved director",
$dremoved == 1 ? "y" : "ies") if $dremoved;
message (" need", ($removed+$dremoved != 1) ? "" : "s",
" to be removed.\n");
}
message (" $excluded file",
$excluded == 1 ? " was" : "s were", " excluded.\n") if $excluded;
}
sub filelist ($) {
my ($man) = @_;
my @new = make_filelist_from_manifest ($man);
foreach ( @new ) {
print STDOUT ($opt_prefix, $_, "\n");
}
}
sub app_options () {
my $opt_manifest;
my $opt_help = 0;
my $opt_ident = 0;
my $opt_rcfile;
my @o = (
"automanifest=s" => \$opt_automanifest,
"debug!" => \$opt_debug,
"description=s@" => \@opt_descr,
"diff=s" => \$opt_diff,
"exclude-regex=s@" => \@opt_exclude_regex,
"exclude-standard!" => \$opt_exclude_standard,
"exclude-rcs!" => \$opt_exclude_rcs,
"exclude-sccs!" => \$opt_exclude_sccs,
"exclude-cvs!" => \$opt_exclude_cvs,
"exclude-vc!" => sub { $opt_exclude_rcs =
$opt_exclude_cvs =
$opt_exclude_sccs = $_[1] },
"exclude=s@" => \@opt_exclude,
"extract=s%" => \%opt_extract,
"filelist|list!" => \$opt_filelist,
"follow!" => \$opt_follow,
"help" => \$opt_help,
"ident!" => \$opt_ident,
"ignore-cvs-keywords|ignore-rcs-keywords!"
=> \$opt_ignore_rcs_keywords,
"infocmd=s" => \$opt_infocmd,
"manifest=s" => \$opt_manifest,
"newmanifest=s" => \$opt_newmanifest,
"nomanifest!" => \$opt_nomanifest,
"oldmanifest=s" => \$opt_oldmanifest,
"patchlevel=s" => \$opt_patchlevel,
"prefix=s" => \$opt_prefix,
"quiet!" => \$opt_quiet,
"sort!" => \$opt_sort,
"recurse!" => \$opt_recurse,
"test" => \$opt_test,
"trace!" => \$opt_trace,
"verbose!" => \$opt_verbose,
);
my $init;
# Process ENV options.
if ( defined ($init = $ENV{MAKEPATCHINIT}) ) {
require Text::ParseWords;
local (@ARGV) = Text::ParseWords::shellwords ($init);
unless ( GetOptions (@o, "rcfile=s" => \$opt_rcfile) &&
@ARGV == 0 ) {
warn ("Error in MAKEPATCHINIT\n");
app_usage (1);
}
else {
trace ("+ INIT: $init\n");
}
}
unless ( $opt_test ) {
# Process ini file options.
# First, try system wide file. Unix specific.
app_parse_rc ("/etc/makepatchrc", 1, \@o);
my $rcname = ".".$my_name."rc";
# Then, try HOME .rc.
app_parse_rc (catfile ($HOME, $rcname), 1, \@o);
# Then try --rcfile, defaulting to .rc in current dir.
if ( defined $opt_rcfile ) {
app_parse_rc ($opt_rcfile, 0, \@o);
}
else {
app_parse_rc (catfile ($dot, $rcname), 1, \@o);
}
}
# Process command line options
if ( !GetOptions (@o) || $opt_help ) {
app_usage (1);
}
# Argument check.
if ( $opt_filelist ) {
if ( defined $opt_manifest ) {
app_usage (1) if @ARGV;
@ARGV = ( $opt_manifest );
}
else {
app_usage (1) unless @ARGV == 1;
}
}
else {
app_usage (1) unless @ARGV == 2;
}
$opt_trace = 1 if $opt_debug;
print STDERR ("This is $my_name version $my_version\n")
if $opt_verbose || $opt_ident;
if ( $opt_prefix ne '' ) {
die ("$0: option \"-prefix\" requires \"-filelist\"\n")
unless $opt_filelist;
}
if ( defined $opt_sort ) {
die ("$0: option \"-[no]sort\" requires \"-filelist\"\n")
unless $opt_filelist;
}
else {
$opt_sort = 1;
}
if ( $opt_filelist ) {
die ("$0: option \"-filelist\" only uses \"-manifest\"\n")
if defined $opt_oldmanifest || defined $opt_newmanifest;
}
if ( defined $opt_manifest ) {
die ("$0: do not use \"-manifest\" with \"-oldmanifest\"".
" or \"-newmanifest\"\n")
if defined $opt_newmanifest || defined $opt_oldmanifest;
$opt_newmanifest = $opt_oldmanifest = $opt_manifest;
}
if ( defined $opt_infocmd ) {
die ("$0: \"-infocmd\" can not be used with \"-filelist\"\n")
if $opt_filelist;
# Protect %% sequences.
$opt_infocmd =~ s/\%\%/\001/g;
# Encode %o and %n sequences.
$opt_infocmd =~ s/\%o([P])/\002$1/g;
$opt_infocmd =~ s/\%n([P])/\003$1/g;
# Restore %% sequences.
$opt_infocmd =~ s/\001/%%/g;
while ( $opt_infocmd =~ /(\%[on]\S)/g ) {
warn ("Warning: $1 in info command may become ",
"special in the future\n");
}
}
$opt_verbose = 0 if $opt_quiet;
$opt_trace ||= $opt_debug;
$opt_verbose ||= $opt_trace;
}
sub app_parse_rc ($$$) {
my ($file, $opt, $optref) = @_;
my $rcfile = new IO::File;
unless ( $rcfile->open($file) ) {
die ("$file: $!\n") unless $opt;
return;
}
require Text::ParseWords;
local (@ARGV);
my $ok = 1;
# Intercept Getopt::Long warning messages.
my $warn;
$SIG{__WARN__} = sub { $warn = "@_"; };
# Process the file.
while ( <$rcfile> ) {
# Skip blank and comment lines.
next if /^\s*[;#]/;
next unless /\S/;
# Split.
my @a = Text::ParseWords::shellwords ($_);
$warn = '';
trace ("+ RC: @a\n");
# Handle.
@ARGV = @a;
unless ( GetOptions (@$optref) ) {
chomp ($warn);
print STDERR ("$warn -- at line $. in $file\n");
$ok = 0;
}
if ( @ARGV > 0 ) {
print STDERR ("Garbage \"@ARGV\"",
" -- at line $. in $file\n");
$ok = 0;
}
}
$rcfile->close;
$SIG{__WARN__} = 'DEFAULT';
unless ( $ok ) {
app_usage (1);
}
$ok;
}
sub app_usage ($) {
my ($exit) = @_;
print STDERR <<EoU;
This is $my_name version $my_version
Usage: $0 [options] old-src new-src
Makepatch options:
-description XXX descriptive message about this patch
-diff cmd diff command to use, default \"$opt_diff\"
-patchlevel file file to use as patchlevel.h
-man[ifest] file list of files for old and new dir, defaults to $opt_automanifest
-nomanifest suppress use of MANIFEST files
-automan[ifest] XXX assumend name for MANIFEST files
-newman[ifest] file list of files for new dir
-oldman[ifest] file list of files for old dir
-follow follow symbolic links
-infocmd cmd add output of cmd to each patch chunk
-exclude pat exclude files according to wildcard pattern
-exclude-regex pat exclude files and dirs matching regex pattern
-exclude-vc exclude version control files (RCS, CVS, SCCS)
-exclude-rcs exclude version control files for RCS
-exclude-cvs exclude version control files for CVS
-exclude-sccs exclude version control files for SCCS
-ignore-cvs-keywords ignore diffs in CVS keyword data (same as RCS)
-ignore-rcs-keywords ignore diffs in RCS keyword data (same as CVS)
-extract PAT=RULE define an archive extraction rule
-[no]recurse recurse through directories (default)
-generate XXX style of output, either "perl" (default) or "shell"
-verbose verbose progress information
-quiet no progress information
-help this message
EoU
die ("Not okay 99\n") if $opt_test;
exit $exit if defined $exit && $exit != 0;
}
sub app_usage_filelist ($) {
my ($exit) = @_;
print STDERR <<EoU;
This is $my_name version $my_version
Usage: $0 -filelist [ options ] [ -manifest ] file
Filelist options:
-[file]list extract filenames from manifest file
-prefix XXX add a prefix to these filenames
-nosort do not sort manifest entries
-man[ifest] file list of files
-exclude pat exclude files according to wildcard pattern
-exclude-regex pat exclude files and dirs matching regex pattern
-exclude-vc exclude version control files (RCS, CVS, SCCS)
-exclude-rcs exclude version control files for RCS
-exclude-cvs exclude version control files for CVS
-exclude-sccs exclude version control files for SCCS
-verbose verbose output (default)
-quiet no verbose output
-help this message
EoU
die ("Not okay 99\n") if $opt_test;
exit $exit if defined $exit && $exit != 0;
}
1;
__END__
################ Documentation ################
=head1 NAME
makepatch - create script to update a source tree
=head1 SYNOPSIS
B<makepatch> [ I<options> ] I<old-src> I<new-src>
=for comment
B<makepatch> B<-filelist> [ I<options> ] I<manifest>
=head1 Introduction
Traditionally, source trees are updated with the B<patch> program,
processing patch information that is generated by the B<diff> program.
Although B<diff> and B<patch> do a very good job at patching file
contents, most versions do not handle creating and deleting files and
directories, and adjusting of file modes and time stamps. Newer
versions of B<diff> and B<patch> seem to be able to create files, and
very new versions of B<patch> can remove files. But that's about it.
Another typical problem is that patch kits are typically downloaded
from the Internet, or transmitted via electronic mail. It is often
desirable to verify the correctness of a patch kit before even
attempting to apply it.
The B<makepatch> package is designed to overcome these limitations.
=head1 DESCRIPTION
The B<makepatch> package contains two Perl programs:
B<makepatch> and B<applypatch>.
B<makepatch> will generate a patch kit from two source trees. It
traverses the source directory and runs a B<diff> on each pair of
corresponding files, accumulating the output into a patch kit. It
knows about the conventions for patch kits: if a file named
C<patchlevel.h> exists, it is handled first, so B<patch> can check the
version of the source tree. Also, to deal with the non-perfect
versions of B<patch> that are in use, it supplies "C<Index:>" and
"C<Prereq:>" lines, so B<patch> can correctly locate the files to
patch, and it relocates the patch to the current directory to avoid
problems with creating new files.
The list of files can be specified in a so called B<MANIFEST> file,
but it can also be generated by recursively traversing the source
tree. Files can be excluded using shell style wildcards and Perl regex
patterns.
But that is not it! B<makepatch> also inserts some additional
information in the patch kit for use by the B<applypatch> program.
It is important to emphasize that the generated patch kit is still
valid input for B<patch>. When used with B<patch>, there are no
verifications and problems may arise when new files need to be
created. B<makepatch> prepends a small shell script in front of the
patch kit that creates the necessary files and directories for the
patch process. If you can not run B<applypatch> for some reason, you
can run the patch kit I<as a shell script> to prepare the source
directory for the patching process.
The B<applypatch> program will do the following:
=over 4
=item *
It will extensively verify that the patch kit is complete and not
corrupted during transfer.
=item *
It will apply some heuristics to verify that the directory in
which the patch will be applied does indeed contain the expected
sources.
=item *
It creates files and directories as necessary.
=item *
It applies the patch by running the B<patch> program.
=item *
Upon completion, obsolete files, directories and C<.orig> files are
removed, file modes of new files are set, and the timestamps of
all patched files are adjusted.
=back
Note that B<applypatch> only requires the B<patch> program. It does not
rely on a shell or shell tools. This makes it possible to apply
patches on non-Unix systems.
=head1 General usage
Suppose you have an archive `C<pkg-1.6.tar.gz>' containing the sources
for package `C<pkg>' version 1.6, and a directory tree `C<pkg-1.7>'
containing the sources for version 1.7. The following command will
generate a patch kit that updates the 1.6 sources into their 1.7
versions:
makepatch pkg-1.6.tar.gz pkg-1.7 > pkg-1.6-1.7.patch
To apply this script, go to the directory containing the 1.6 sources
and feed the script to B<applypatch>:
cd old/pkg-1.6
applypatch pkg-1.6-1.7.patch
B<applypatch> will verify that it is executing in the right place and
make all necessary updates.
By default, B<makepatch> will provide a few lines of progress
information, for example:
Extracting pkg-1.6.tar.gz to /tmp/mp21575.d/old...
Manifest MANIFEST for pkg-1.6 contains 1083 files.
Manifest MANIFEST for pkg-1.7 contains 1292 files.
Processing the filelists ...
Collecting patches ...
266 files need to be patched.
216 files and 8 directories need to be created.
7 files need to be removed.
B<applypatch> will provide no feedback information by default.
=head1 Makepatch arguments
B<makepatch> requires two arguments: I<old_src> and I<new_src>.
=over 4
=item I<old-src>
This is the name of either a single file or a directory that contains
copies of the older version of the target files; in other words,
copies of the files I<prior> to any modifications.
Alternatively, it may be the name of an archive that holds the files
to be processed. Allowable archive formats are gzipped tar (name ends
in "C<.tar.gz>" or "C<.tgz>"), bzipped tar (name ends in
"C<.tar.bz2>"), plain tar (name ends in "C<.tar>" and zip (name ends in
"C<.zip>").
=item I<new-src>
This is the name of either a single file or a directory that contains
copies of the newer version of the target files; in other words,
copies of the files I<after> the modifications have been made.
Alternatively, it may be the name of an archive that holds the files
to be processed.
=back
The patch script generated by B<makepatch> will take care of creating
new files and directories, update existing files, and remove files and
directories that are no longer present in the I<new-src> directory.
=head1 MANIFEST files
The purpose of a manifest file is to provide the list of files that
constitute a package. Manifest files are traditionally called
"C<MANIFEST>" and reside in the top level directory of the package.
Although there is no formal standard for the contents of manifest
files, B<makepatch> uses the following rules:
=over 4
=item *
If the second line from the manifest file looks like a separator line
(e.g. it is empty, or contains only dashes), it is discarded and so is
the first line.
=item *
Empty lines and lines that start with a C<#> are ignored.
=item *
If there are multiple space-separated "words" on a line, the first
word is considered to be the filename.
=back
=head2 Default treatment
By default, B<makepatch> looks for files named "C<MANIFEST>" in the
top level directories of the old and the new source trees. If these
files (or one of them) are found, they are used.
If no manifest file could be found, the package is assumed to consist
of all files in the directory.
The default name of the default manifest file can be modified with the
command line option "C<-automanifest>", see Section L<Command line
options>.
=head2 Explicitly naming of manifest files
Command line options "C<-oldmanifest>" and "C<-newmanifest>" can be
used to explicitly designate old and new manifest files. Option
"C<-manifest>" is a short way to set one manifest file for both the
old and new source trees.
=head2 Suppress manifest file processing
Command line option "C<-nomanifest>" can be used to suppress all
manifest file processing. The package is assumed to consist
of all files in the source directories.
=head1 Makepatch options
B<makepatch> takes several options to control its behaviour. Options
are usually specified on the command line, but B<makepatch> can take
options from three sources in the following order:
=over 4
=item *
Environment variable B<MAKEPATCHINIT>.
When this environment variable is set its contents are considered to
be command line options that are processed upon startup. All normal
options are allowed, plus one: B<-rcfile >I<filename>. Option
B<-rcfile> can be used to specify an alternate option file, see below.
=item *
Options files.
B<makepatch> first tries to process a file named B</etc/makepatchrc>.
(This is a Unix-ism.)
It is okay if this file is missing.
Next, B<makepatch> will process a file named B<.makepatchrc> in
the user's home directory, if it exists.
After processing this file, B<makepatch> will process a file named
B<.makepatchrc> in the current directory, if it exists. An alternative
name for this file can be specified with option B<-rcfile> in
environment variable B<MAKEPATCHINIT>. This is the only way to specify
an alternative options file name.
In all option files, empty lines and lines starting with C<;> or C<#>
are ignored. All other lines are considered to contain options exactly
as if they had been supplied on the command line.
=item *
The command line.
=back
=head1 Command line options
Options are matched case insensitive, and may be abbreviated to uniqueness.
=over 4
=item B<-description> I<text>
Provide a descriptive text for this patch. Multiple B<-description>
options may be supplied.
If no description is provided, the program try to guess one. This is
usually possible if both directories are simple names, e.g.
'C<pkg-1.16>'. If no description can be determined, the program will
ask for one.
=item B<-diff> I<cmd>
If specified, I<cmd> is the command to be used to
generate the differences between the two versions of the files. If
not specified, this command defaults to "C<diff -c>".
For best results, only use "C<diff -c>" or "C<diff -u>".
In any case, it B<must> produce either context or unified diff output.
=item B<-patchlevel> I<pfile>
If specified, I<pfile> indicates an alternate file that is to be
used in lieu of "B<patchlevel.h>".
=item B<-automanifest> I<mfile>
B<makepatch> will automatically use manifest files of the given name
if they appear in the directories. The default name is "B<MANIFEST>".
=item B<-nomanifest>
Suppress using manifest files.
=item B<-manifest> I<mfile>
If specified, I<mfile> indicates the name of the manifest file
which consists of a list of the files contained in both the I<old>
and the I<new> directories.
=item B<-oldmanifest> I<omfile>
If specified, I<omfile> indicates the name of the manifest file which
consists of a list of the files contained in the I<old> directory.
This option is designed to be used in conjunction with the
B<-newmanifest> option. Note that the I<old> and I<new> directories
must still be indicated.
=item B<-newmanifest> I<nmfile>
If specified, I<nmfile> indicates the name of the manifest file which
consists of a list of the files contained in the I<new> directory.
This option is designed to be used in conjunction with the
B<-oldmanifest> option. Note that the I<old> and I<new>
directories must still be indicated.
=item B<->[B<no>]B<recurse>
B<makepatch> recurses through directories by default. Option
B<-norecurse> prevents recursion beyond the initial directories.
=item B<->[B<no>]B<follow>
If set, symbolic links to directories are traversed as if they
were real directories.
=item B<-infocmd> I<command>
If specified, the output of running I<command> will be added before
each patch chunk. I<command> will undergo the following substitutions
first: C<%oP> will be replaced by the name of the old file, C<%nP>
will be replaced by the name of the new file. C<%%> will be replaced
by a single C<%>; other C<%> sequences may be added in future
versions. When a new file is being created, the name of the new file
will be supplied for both C<%oP> and C<%nP>.
Note that C<%oP> and C<%nP> are modeled after the C<%> sequences of
B<find -printf>.
=item B<-exclude> I<pattern>
If specified, files that match the shell pattern I<pattern> will be
excluded. Only wildcard characters C<*> and C<?>, and character
classes C<[...]> are handled. Multiple B<-exclude> options may be
supplied.
=item B<-exclude-regex> I<pattern>
If specified, files and directories that match the Perl regular
expression pattern I<pattern> will be excluded.
Multiple B<-exclude-regex> options may be supplied.
=item B<->[B<no>]B<exclude-standard>
Set by default. If set, a common set of files and directories are ignored.
See also section L<Standard Exclude Patterns>.
=item B<->[B<no>]B<exclude-cvs>
If set, files and directories that are usually part of version control
system CVS are excluded.
Also, C<.cvsignore> files are honoured just like CVS does it.
See also section L<Standard Exclude Patterns>.
=item B<->[B<no>]B<exclude-rcs>
If set, files and directories that are usually part of version control
system RCS are excluded.
See also section L<Standard Exclude Patterns>.
=item B<->[B<no>]B<exclude-sccs>
If set, files and directories that are usually part of version control
system SCCS are excluded.
See also section L<Standard Exclude Patterns>.
=item B<->[B<no>]B<exclude-vc>
Short for (re)setting B<-exclude-rcs>, B<-exclude-cvs>, and
B<-exclude-sccs>.
=item B<->[B<no>]B<ignore-cvs-keywords>
Differences in CVS keyword data (e.g. C<Id>, C<Header>, C<Revision>)
are ignored, provided there are no other differences in the same hunk.
This option passes a very hairy regex to the
B<--ignore-matching-lines> option of the I<diff> program, and hence
requires GNU I<diff>. This restriction may be lifted in a future version.
=item B<->[B<no>]B<ignore-rcs-keywords>
Same as B<->[B<no>]B<ignore-cvs-keywords>.
=item B<-extract> I<pattern>B<=>I<command>
Define additional extraction rules for archives. If the name of the
source or destination matches the Perl I<pattern>, the I<command> is
executed with the archive on standard input and the current directory
set to the location where the files must be extracted. Multiple
B<-extract> options may be supplied. User defined rules override
built-in rules.
Builtin rules are:
.+\.(tar\.gz|tgz) => "gzip -d | tar xpf -"
.+\.(tar\.bz2) => "bzip2 -d | tar xpf -"
.+\.tar => "tar xf -"
.+\.zip => "unzip -"
The patterns are implicitly anchored to the begin and end of the filename.
=begin comment
=back
=head1 Filelist options
=over
=item B<->[B<file>]B<list>
This option instructs B<makepatch> to read a manifest file, and output
the list of files included in this manifest. This option is useful to
turn the contents of a manifest file into a list of files suitable for
other programs.
=item B<-manifest> I<mfile>
If specified, I<mfile> indicates the name of the manifest file to
be used. Alternatively, the name of the manifest file may follow the
command line options.
=item B<-prefix> I<string>
Every entry in the manifest file is prefixed with I<string> before it
is written to standard output.
=item B<-nosort>
Retain the order of filenames from the manifest file.
=back
The exclude options B<-exclude>, B<-exclude-regex>, B<-exclude-rcs>,
B<-exclude-cvs>, B<-exclude-sccs> and B<-exclude-vc> can also be used
with B<filelist>.
=over *
=end comment
=item B<->[B<no>]B<ident>
If set, the program name and version is reported.
=item B<->[B<no>]B<verbose>
This is set by default, making B<makepatch> display information concerning
its activity to I<stderr>.
=item B<->[B<no>]B<quiet>
The opposite of B<-verbose>. If set, this instructs B<makepatch> to
suppress the display of activity information.
=item B<->[B<no>]B<help>
If set, this causes a short help message to be displayed, after which
the program immediately exits.
=back
=head1 Standard Exclude Patterns
The following file patterns are always excluded:
*~ *.a *.bak *.BAK *.elc *.exe *.gz *.ln *.o *.obj
*.olb *.old *.orig *.rej *.so *.Z
.del-* .make.state .nse_depinfo core
tags TAGS
Option B<-exclude-sccs> adds:
p.* s.* SCCS
Option B<-exclude-rcs> adds:
,* *,v RCS RCSLOG
Option B<-exclude-cvs> adds C<.cvsignore> patterns, and:
.#* #* _$* *$ CVS CVS.adm cvslog.*
Please let me know if I missed some.
=head1 Environment variables
=over
=item MAKEPATCHINIT
When this environment variable is set its contents is considered to be
command line options that are processed upon startup. All normal
options are allowed, plus one: B<-rcfile >I<filename>. If B<-rcfile>
is specified, the file is read and all lines of it are considered to
contain option settings as described in section L<Makepatch options>.
=item TMPDIR
C<TMPDIR> can be used to designate the area where temporary files are
placed. It defaults to C</tmp>.
=item TEMP
C<TEMP> can be used as an alternative to C<TMPDIR>.
=back
=head1 Examples
Suppose you have a directory tree `C<pkg-1.6>' containing the sources
for package `C<pkg>' version 1.6, and a directory tree `C<pkg-1.7>'
containing the sources for version 1.7. The following command will
generate a patch kit that updates the 1.6 sources into their 1.7
versions:
makepatch pkg-1.6 pkg-1.7 > pkg-1.6-1.7.patch
To apply this script, go to the pkg-1.6 directory and feed the
script to B<applypatch>:
cd old/pkg-1.6
applypatch pkg-1.6-1.7.patch
B<applypatch> will verify that it is executing in the right place and
make all necessary updates.
This is one way to generate and use manifest files:
(cd pkg-1.6; find . -type f -print > OLDMANIFEST)
(cd pkg-1.7; find . -type f -print > NEWMANIFEST)
makepatch \
-oldmanifest pkg-1.6/OLDMANIFEST \
-newmanifest pkg-1.7/NEWMANIFEST \
pkg-1.6 pkg-1.7 > pkg-1.6-1.7.diff
=begin comment
The following example transforms the manifest file into a list of
files suitable for GNU tar. Note the trailing C</> in the prefix
string:
makepatch -filelist -prefix pkg-1.7/ pkg-1.7/MANIFEST | \
tar -cvf - -T -Op | gzip > pkg-1.7.tar.gz
=end comment
=head1 Bugs and restrictions
Much of the job of B<makepatch> is processing file names. B<makepatch>
has been tested extensively on Unix systems, but it is not guaranteed
to work on other systems.
B<applypatch> is repeatedly reported to correctly process B<makepatch>
generated patch kits on modern 32-bit Windows systems as well.
B<makepatch> does not know about symbolic links.
These will be treated like plain files.
Wrong results can be generated if the file lists that are used or
generated use different path separators.
=head1 SEE ALSO
B<applypatch>(1),
B<diff>(1),
B<patch>(1),
B<perl>(1),
B<rm>(1).
=head1 AUTHOR AND CREDITS
Johan Vromans (jvromans@squirrel.nl) wrote the program, with a little
help and inspiration from: Jeffery Small, Ulrich Pfeifer, Nigel
Metheringham, Julian Yip, Tim Bunce, Gurusamy Sarathy, Hugo van der
Sanden, Rob Browning, Joshua Pritikin, and others.
=head1 COPYRIGHT AND DISCLAIMER
This program is Copyright 1992,2004,2006 by Squirrel Consultancy. All
rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of either: a) the GNU General Public License as
published by the Free Software Foundation; either version 1, or (at
your option) any later version, or b) the "Artistic License" which
comes with Perl.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the
GNU General Public License or the Artistic License for more details.
=cut
|