Add support for macros and cache flush API. Add support for git attribute macro definitions. Also, add support for cache flush API to clear the attribute file content cache when needed. Additionally, improved the handling of global and system files, making common utility functions in fileops and converting config and attr to both use the common functions. Adds a bunch more tests and fixed some memory leaks. Note that adding macros required me to use refcounted attribute assignment definitions, which complicated, but probably improved memory usage.
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
diff --git a/include/git2/attr.h b/include/git2/attr.h
index d585937..f4c5975 100644
--- a/include/git2/attr.h
+++ b/include/git2/attr.h
@@ -50,6 +50,30 @@ GIT_EXTERN(int) git_attr_foreach(
int (*callback)(const char *name, const char *value, void *payload),
void *payload);
+/**
+ * Flush the gitattributes cache.
+ *
+ * Call this if you have reason to believe that the attributes files
+ * on disk no longer match the cached contents of memory.
+ */
+GIT_EXTERN(void) git_attr_cache_flush(
+ git_repository *repo);
+
+/**
+ * Add a macro definition.
+ *
+ * Macros will automatically be loaded from the top level .gitattributes
+ * file of the repository (plus the build-in "binary" macro). This
+ * function allows you to add others. For example, to add the default
+ * macro, you would call:
+ *
+ * git_attr_add_macro(repo, "binary", "-diff -crlf");
+ */
+GIT_EXTERN(int) git_attr_add_macro(
+ git_repository *repo,
+ const char *name,
+ const char *values);
+
/** @} */
GIT_END_DECL
#endif
diff --git a/src/attr.c b/src/attr.c
index d8e7095..fac2fa4 100644
--- a/src/attr.c
+++ b/src/attr.c
@@ -1,19 +1,17 @@
-#include "attr.h"
-#include "buffer.h"
+#include "repository.h"
#include "fileops.h"
#include "config.h"
#include <ctype.h>
#define GIT_ATTR_FILE_INREPO "info/attributes"
#define GIT_ATTR_FILE ".gitattributes"
-#define GIT_ATTR_FILE_SYSTEM "/etc/gitattributes"
-#if GIT_WIN32
-#define GIT_ATTR_FILE_WIN32 L"%PROGRAMFILES%\\Git\\etc\\gitattributes"
-#endif
+#define GIT_ATTR_FILE_SYSTEM "gitattributes"
static int collect_attr_files(
git_repository *repo, const char *path, git_vector *files);
+static int attr_cache_init(git_repository *repo);
+
int git_attr_get(
git_repository *repo, const char *pathname,
@@ -180,6 +178,44 @@ cleanup:
}
+int git_attr_add_macro(
+ git_repository *repo,
+ const char *name,
+ const char *values)
+{
+ int error;
+ git_attr_rule *macro = NULL;
+
+ if ((error = attr_cache_init(repo)) < GIT_SUCCESS)
+ return error;
+
+ macro = git__calloc(1, sizeof(git_attr_rule));
+ if (!macro)
+ return GIT_ENOMEM;
+
+ macro->match.pattern = git__strdup(name);
+ if (!macro->match.pattern) {
+ git__free(macro);
+ return GIT_ENOMEM;
+ }
+
+ macro->match.length = strlen(macro->match.pattern);
+ macro->match.flags = GIT_ATTR_FNMATCH_MACRO;
+
+ error = git_attr_assignment__parse(repo, ¯o->assigns, &values);
+
+ if (error == GIT_SUCCESS)
+ error = git_attr_cache__insert_macro(repo, macro);
+
+ if (error < GIT_SUCCESS) {
+ git_attr_rule__free(macro);
+ git__free(macro);
+ }
+
+ return error;
+}
+
+
/* add git_attr_file to vector of files, loading if needed */
static int push_attrs(
git_repository *repo,
@@ -193,13 +229,6 @@ static int push_attrs(
git_attr_file *file;
int add_to_cache = 0;
- if (cache->files == NULL) {
- cache->files = git_hashtable_alloc(
- 8, git_hash__strhash_cb, git_hash__strcmp_cb);
- if (!cache->files)
- return git__throw(GIT_ENOMEM, "Could not create attribute cache");
- }
-
if ((error = git_path_prettify(&path, filename, base)) < GIT_SUCCESS) {
if (error == GIT_EOSERR)
/* file was not found -- ignore error */
@@ -210,7 +239,7 @@ static int push_attrs(
/* either get attr_file from cache or read from disk */
file = git_hashtable_lookup(cache->files, path.ptr);
if (file == NULL) {
- error = git_attr_file__from_file(&file, path.ptr);
+ error = git_attr_file__from_file(repo, path.ptr, &file);
add_to_cache = (error == GIT_SUCCESS);
}
@@ -238,6 +267,9 @@ static int collect_attr_files(
git_config *cfg;
const char *workdir = git_repository_workdir(repo);
+ if ((error = attr_cache_init(repo)) < GIT_SUCCESS)
+ goto cleanup;
+
if ((error = git_vector_init(files, 4, NULL)) < GIT_SUCCESS)
goto cleanup;
@@ -288,8 +320,13 @@ static int collect_attr_files(
git_config_free(cfg);
}
- if (error == GIT_SUCCESS)
- error = push_attrs(repo, files, NULL, GIT_ATTR_FILE_SYSTEM);
+ if (error == GIT_SUCCESS) {
+ error = git_futils_find_system_file(&dir, GIT_ATTR_FILE_SYSTEM);
+ if (error == GIT_SUCCESS)
+ error = push_attrs(repo, files, NULL, dir.ptr);
+ else if (error == GIT_ENOTFOUND)
+ error = GIT_SUCCESS;
+ }
cleanup:
if (error < GIT_SUCCESS) {
@@ -302,10 +339,64 @@ static int collect_attr_files(
}
-void git_repository__attr_cache_free(git_attr_cache *attrs)
+static int attr_cache_init(git_repository *repo)
{
- if (attrs && attrs->files) {
- git_hashtable_free(attrs->files);
- attrs->files = NULL;
+ int error = GIT_SUCCESS;
+ git_attr_cache *cache = &repo->attrcache;
+
+ if (cache->initialized)
+ return GIT_SUCCESS;
+
+ if (cache->files == NULL) {
+ cache->files = git_hashtable_alloc(
+ 8, git_hash__strhash_cb, git_hash__strcmp_cb);
+ if (!cache->files)
+ return git__throw(GIT_ENOMEM, "Could not initialize attribute cache");
+ }
+
+ if (cache->macros == NULL) {
+ cache->macros = git_hashtable_alloc(
+ 8, git_hash__strhash_cb, git_hash__strcmp_cb);
+ if (!cache->macros)
+ return git__throw(GIT_ENOMEM, "Could not initialize attribute cache");
}
+
+ cache->initialized = 1;
+
+ /* insert default macros */
+ error = git_attr_add_macro(repo, "binary", "-diff -crlf");
+
+ return error;
+}
+
+
+void git_attr_cache_flush(
+ git_repository *repo)
+{
+ if (!repo)
+ return;
+
+ if (repo->attrcache.files) {
+ const void *GIT_UNUSED(name);
+ git_attr_file *file;
+
+ GIT_HASHTABLE_FOREACH(repo->attrcache.files, name, file,
+ git_attr_file__free(file));
+
+ git_hashtable_free(repo->attrcache.files);
+ repo->attrcache.files = NULL;
+ }
+
+ if (repo->attrcache.macros) {
+ const void *GIT_UNUSED(name);
+ git_attr_rule *rule;
+
+ GIT_HASHTABLE_FOREACH(repo->attrcache.macros, name, rule,
+ git_attr_rule__free(rule));
+
+ git_hashtable_free(repo->attrcache.macros);
+ repo->attrcache.macros = NULL;
+ }
+
+ repo->attrcache.initialized = 0;
}
diff --git a/src/attr.h b/src/attr.h
deleted file mode 100644
index 518fb9d..0000000
--- a/src/attr.h
+++ /dev/null
@@ -1,21 +0,0 @@
-/*
- * Copyright (C) 2009-2011 the libgit2 contributors
- *
- * This file is part of libgit2, distributed under the GNU GPL v2 with
- * a Linking Exception. For full terms see the included COPYING file.
- */
-#ifndef INCLUDE_attr_h__
-#define INCLUDE_attr_h__
-
-#include "hashtable.h"
-#include "attr_file.h"
-
-/* EXPORT */
-typedef struct {
- git_hashtable *files; /* hash path to git_attr_file */
-} git_attr_cache;
-
-extern void git_repository__attr_cache_free(git_attr_cache *attrs);
-
-#endif
-
diff --git a/src/attr_file.c b/src/attr_file.c
index 5d159db..0b1eb1f 100644
--- a/src/attr_file.c
+++ b/src/attr_file.c
@@ -1,17 +1,33 @@
#include "common.h"
-#include "attr_file.h"
+#include "repository.h"
#include "filebuf.h"
#include <ctype.h>
const char *git_attr__true = "[internal]__TRUE__";
const char *git_attr__false = "[internal]__FALSE__";
-static int parse_fnmatch(git_attr_fnmatch *spec, const char **base);
-static int parse_assigns(git_vector *assigns, const char **base);
-static int free_rule(git_attr_rule *rule);
+static int git_attr_fnmatch__parse(git_attr_fnmatch *spec, const char **base);
static int sort_by_hash_and_name(const void *a_raw, const void *b_raw);
-int git_attr_file__from_buffer(git_attr_file **out, const char *buffer)
+int git_attr_cache__insert_macro(git_repository *repo, git_attr_rule *macro)
+{
+ unsigned int i;
+ git_attr_assignment *assign;
+
+ if (macro->assigns.length == 0)
+ return git__throw(GIT_EMISSINGOBJDATA, "git attribute macro with no values");
+
+ git_vector_foreach(¯o->assigns, i, assign) {
+ GIT_REFCOUNT_OWN(assign, macro);
+ GIT_REFCOUNT_INC(assign);
+ }
+
+ return git_hashtable_insert(
+ repo->attrcache.macros, macro->match.pattern, macro);
+}
+
+int git_attr_file__from_buffer(
+ git_repository *repo, const char *buffer, git_attr_file **out)
{
int error = GIT_SUCCESS;
git_attr_file *attrs = NULL;
@@ -42,13 +58,21 @@ int git_attr_file__from_buffer(git_attr_file **out, const char *buffer)
}
/* parse the next "pattern attr attr attr" line */
- if (!(error = parse_fnmatch(&rule->match, &scan)) &&
- !(error = parse_assigns(&rule->assigns, &scan)))
- error = git_vector_insert(&attrs->rules, rule);
+ if (!(error = git_attr_fnmatch__parse(&rule->match, &scan)) &&
+ !(error = git_attr_assignment__parse(repo, &rule->assigns, &scan)))
+ {
+ if (rule->match.flags & GIT_ATTR_FNMATCH_MACRO)
+ /* should generate error/warning if this is coming from any
+ * file other than .gitattributes at repo root.
+ */
+ error = git_attr_cache__insert_macro(repo, rule);
+ else
+ error = git_vector_insert(&attrs->rules, rule);
+ }
/* if the rule wasn't a pattern, on to the next */
if (error != GIT_SUCCESS) {
- free_rule(rule); /* release anything partially allocated */
+ git_attr_rule__free(rule); /* free anything partially allocated */
if (error == GIT_ENOTFOUND)
error = GIT_SUCCESS;
} else {
@@ -58,6 +82,7 @@ int git_attr_file__from_buffer(git_attr_file **out, const char *buffer)
cleanup:
if (error != GIT_SUCCESS) {
+ git__free(rule);
git_attr_file__free(attrs);
git__free(attrs);
} else {
@@ -67,7 +92,8 @@ cleanup:
return error;
}
-int git_attr_file__from_file(git_attr_file **out, const char *path)
+int git_attr_file__from_file(
+ git_repository *repo, const char *path, git_attr_file **out)
{
int error = GIT_SUCCESS;
git_fbuffer fbuf = GIT_FBUFFER_INIT;
@@ -75,7 +101,7 @@ int git_attr_file__from_file(git_attr_file **out, const char *path)
*out = NULL;
if ((error = git_futils_readbuffer(&fbuf, path)) < GIT_SUCCESS ||
- (error = git_attr_file__from_buffer(out, fbuf.data)) < GIT_SUCCESS)
+ (error = git_attr_file__from_buffer(repo, fbuf.data, out)) < GIT_SUCCESS)
{
git__rethrow(error, "Could not open attribute file '%s'", path);
} else {
@@ -97,7 +123,7 @@ void git_attr_file__free(git_attr_file *file)
return;
git_vector_foreach(&file->rules, i, rule) {
- free_rule(rule);
+ git_attr_rule__free(rule);
}
git_vector_free(&file->rules);
@@ -153,15 +179,15 @@ int git_attr_rule__match_path(
{
int matched = FNM_NOMATCH;
- if (rule->match.directory && !path->is_dir)
+ if (rule->match.flags & GIT_ATTR_FNMATCH_DIRECTORY && !path->is_dir)
return matched;
- if (rule->match.fullpath)
+ if (rule->match.flags & GIT_ATTR_FNMATCH_FULLPATH)
matched = p_fnmatch(rule->match.pattern, path->path, FNM_PATHNAME);
else
matched = p_fnmatch(rule->match.pattern, path->basename, 0);
- if (rule->match.negative)
+ if (rule->match.flags & GIT_ATTR_FNMATCH_NEGATIVE)
matched = (matched == GIT_SUCCESS) ? FNM_NOMATCH : GIT_SUCCESS;
return matched;
@@ -232,7 +258,7 @@ int git_attr_path__init(
* GIT_ENOTFOUND if the fnmatch does not require matching, or
* another error code there was an actual problem.
*/
-static int parse_fnmatch(
+static int git_attr_fnmatch__parse(
git_attr_fnmatch *spec,
const char **base)
{
@@ -251,21 +277,31 @@ static int parse_fnmatch(
goto skip_to_eol;
}
+ spec->flags = 0;
+
+ if (*pattern == '[') {
+ if (strncmp(pattern, "[attr]", 6) == 0) {
+ spec->flags = spec->flags | GIT_ATTR_FNMATCH_MACRO;
+ pattern += 6;
+ } else {
+ /* unrecognized meta instructions - skip the line */
+ error = GIT_ENOTFOUND;
+ goto skip_to_eol;
+ }
+ }
+
if (*pattern == '!') {
- spec->negative = 1;
+ spec->flags = spec->flags | GIT_ATTR_FNMATCH_NEGATIVE;
pattern++;
- } else {
- spec->negative = 0;
}
- spec->fullpath = 0;
slash_count = 0;
for (scan = pattern; *scan != '\0'; ++scan) {
if (isspace(*scan) && *(scan - 1) != '\\')
break;
if (*scan == '/') {
- spec->fullpath = 1;
+ spec->flags = spec->flags | GIT_ATTR_FNMATCH_FULLPATH;
slash_count++;
}
}
@@ -292,11 +328,9 @@ static int parse_fnmatch(
if (pattern[spec->length - 1] == '/') {
spec->length--;
spec->pattern[spec->length] = '\0';
- spec->directory = 1;
+ spec->flags = spec->flags | GIT_ATTR_FNMATCH_DIRECTORY;
if (--slash_count <= 0)
- spec->fullpath = 0;
- } else {
- spec->directory = 0;
+ spec->flags = spec->flags & ~GIT_ATTR_FNMATCH_FULLPATH;
}
return GIT_SUCCESS;
@@ -323,7 +357,21 @@ static int sort_by_hash_and_name(const void *a_raw, const void *b_raw)
return strcmp(b->name, a->name);
}
-static int parse_assigns(
+static void free_assign(git_attr_assignment *assign)
+{
+ git__free(assign->name);
+ assign->name = NULL;
+
+ if (assign->is_allocated) {
+ git__free((void *)assign->value);
+ assign->value = NULL;
+ }
+
+ git__free(assign);
+}
+
+int git_attr_assignment__parse(
+ git_repository *repo,
git_vector *assigns,
const char **base)
{
@@ -333,7 +381,7 @@ static int parse_assigns(
assert(assigns && !assigns->length);
- while (*scan && *scan != '\n') {
+ while (*scan && *scan != '\n' && error == GIT_SUCCESS) {
const char *name_start, *value_start;
/* skip leading blanks */
@@ -369,8 +417,7 @@ static int parse_assigns(
((assign->name_hash << 5) + assign->name_hash) + *scan;
scan++;
}
- assign->name_len = scan - name_start;
- if (assign->name_len <= 0) {
+ if (scan == name_start) {
/* must have found lone prefix (" - ") or leading = ("=foo")
* or end of buffer -- advance until whitespace and continue
*/
@@ -378,6 +425,13 @@ static int parse_assigns(
continue;
}
+ /* allocate permanent storage for name */
+ assign->name = git__strndup(name_start, scan - name_start);
+ if (!assign->name) {
+ error = GIT_ENOMEM;
+ break;
+ }
+
/* if there is an equals sign, find the value */
if (*scan == '=') {
for (value_start = ++scan; *scan && !isspace(*scan); ++scan);
@@ -394,11 +448,34 @@ static int parse_assigns(
}
}
- /* allocate permanent storage for name */
- assign->name = git__strndup(name_start, assign->name_len);
- if (!assign->name) {
- error = GIT_ENOMEM;
- break;
+ /* expand macros (if given a repo) */
+ if (repo != NULL) {
+ git_attr_rule *macro =
+ git_hashtable_lookup(repo->attrcache.macros, assign->name);
+
+ if (macro != NULL) {
+ unsigned int i;
+ git_attr_assignment *massign;
+
+ /* issue warning: if assign->value != GIT_ATTR_TRUE */
+
+ git__free(assign->name);
+ assign->name = NULL;
+ if (assign->is_allocated) {
+ git__free((void *)assign->value);
+ assign->value = NULL;
+ }
+
+ git_vector_foreach(¯o->assigns, i, massign) {
+ error = git_vector_insert(assigns, massign);
+ if (error != GIT_SUCCESS)
+ break;
+ GIT_REFCOUNT_INC(&massign->rc);
+ }
+
+ /* continue to next assignment */
+ continue;
+ }
}
/* insert allocated assign into vector */
@@ -417,40 +494,34 @@ static int parse_assigns(
git_vector_sort(assigns);
}
- if (assign != NULL) {
- git__free(assign->name);
- if (assign->is_allocated)
- git__free((void *)assign->value);
- git__free(assign);
- }
+ if (assign != NULL)
+ free_assign(assign);
while (*scan && *scan != '\n') scan++;
+ if (*scan == '\n') scan++;
+
*base = scan;
return error;
}
-static int free_rule(git_attr_rule *rule)
+void git_attr_rule__free(git_attr_rule *rule)
{
unsigned int i;
git_attr_assignment *assign;
if (!rule)
- return GIT_SUCCESS;
+ return;
git__free(rule->match.pattern);
rule->match.pattern = NULL;
rule->match.length = 0;
git_vector_foreach(&rule->assigns, i, assign) {
- git__free(assign->name);
- assign->name = NULL;
-
- if (assign->is_allocated) {
- git__free((void *)assign->value);
- assign->value = NULL;
- }
+ if (GIT_REFCOUNT_OWNER(assign) == rule)
+ GIT_REFCOUNT_OWN(assign, NULL);
+ GIT_REFCOUNT_DEC(assign, free_assign);
}
- return GIT_SUCCESS;
+ git_vector_free(&rule->assigns);
}
diff --git a/src/attr_file.h b/src/attr_file.h
index 4774f14..bed440d 100644
--- a/src/attr_file.h
+++ b/src/attr_file.h
@@ -9,36 +9,41 @@
#include "git2/attr.h"
#include "vector.h"
+#include "hashtable.h"
+
+#define GIT_ATTR_FNMATCH_NEGATIVE (1U << 0)
+#define GIT_ATTR_FNMATCH_DIRECTORY (1U << 1)
+#define GIT_ATTR_FNMATCH_FULLPATH (1U << 2)
+#define GIT_ATTR_FNMATCH_MACRO (1U << 3)
typedef struct {
char *pattern;
size_t length;
- int negative;
- int directory;
- int fullpath;
+ unsigned int flags;
} git_attr_fnmatch;
typedef struct {
+ git_refcount unused;
const char *name;
- unsigned long name_hash;
+ unsigned long name_hash;
} git_attr_name;
typedef struct {
+ git_refcount rc; /* for macros */
char *name;
unsigned long name_hash;
- size_t name_len;
const char *value;
int is_allocated;
} git_attr_assignment;
typedef struct {
git_attr_fnmatch match;
- git_vector assigns; /* <git_attr_assignment*> */
+ git_vector assigns; /* vector of <git_attr_assignment*> */
} git_attr_rule;
typedef struct {
- char *path;
- git_vector rules; /* <git_attr_rule*> */
+ char *path; /* cache the path this was loaded from */
+ git_vector rules; /* vector of <git_attr_rule*> */
} git_attr_file;
typedef struct {
@@ -47,12 +52,20 @@ typedef struct {
int is_dir;
} git_attr_path;
+typedef struct {
+ int initialized;
+ git_hashtable *files; /* hash path to git_attr_file */
+ git_hashtable *macros; /* hash name to vector<git_attr_assignment> */
+} git_attr_cache;
+
/*
* git_attr_file API
*/
-extern int git_attr_file__from_buffer(git_attr_file **out, const char *buf);
-extern int git_attr_file__from_file(git_attr_file **out, const char *path);
+extern int git_attr_file__from_buffer(
+ git_repository *repo, const char *buf, git_attr_file **out);
+extern int git_attr_file__from_file(
+ git_repository *repo, const char *path, git_attr_file **out);
extern void git_attr_file__free(git_attr_file *file);
@@ -74,6 +87,8 @@ extern unsigned long git_attr_file__name_hash(const char *name);
* other utilities
*/
+extern void git_attr_rule__free(git_attr_rule *rule);
+
extern int git_attr_rule__match_path(
git_attr_rule *rule,
const git_attr_path *path);
@@ -84,4 +99,12 @@ extern git_attr_assignment *git_attr_rule__lookup_assignment(
extern int git_attr_path__init(
git_attr_path *info, const char *path);
+extern int git_attr_assignment__parse(
+ git_repository *repo, /* needed to expand macros */
+ git_vector *assigns,
+ const char **scan);
+
+extern int git_attr_cache__insert_macro(
+ git_repository *repo, git_attr_rule *macro);
+
#endif
diff --git a/src/config.c b/src/config.c
index ed7c947..29f1ee2 100644
--- a/src/config.c
+++ b/src/config.c
@@ -337,6 +337,11 @@ int git_config_get_string(git_config *cfg, const char *name, const char **out)
return git__throw(error, "Config value '%s' not found", name);
}
+int git_config_find_global_r(git_buf *path)
+{
+ return git_futils_find_global_file(path, GIT_CONFIG_FILENAME);
+}
+
int git_config_find_global(char *global_config_path)
{
git_buf path = GIT_BUF_INIT;
@@ -354,79 +359,9 @@ int git_config_find_global(char *global_config_path)
return error;
}
-int git_config_find_global_r(git_buf *path)
+int git_config_find_system_r(git_buf *path)
{
- int error;
- const char *home = getenv("HOME");
-
-#ifdef GIT_WIN32
- if (home == NULL)
- home = getenv("USERPROFILE");
-#endif
-
- if (home == NULL)
- return git__throw(GIT_EOSERR, "Failed to open global config file. Cannot locate the user's home directory");
-
- if ((error = git_buf_joinpath(path, home, GIT_CONFIG_FILENAME)) < GIT_SUCCESS)
- return error;
-
- if (git_futils_exists(path->ptr) < GIT_SUCCESS) {
- git_buf_clear(path);
- return git__throw(GIT_EOSERR, "Failed to open global config file. The file does not exist");
- }
-
- return GIT_SUCCESS;
-}
-
-
-
-#if GIT_WIN32
-static int win32_find_system(git_buf *system_config_path)
-{
- const wchar_t *query = L"%PROGRAMFILES%\\Git\\etc\\gitconfig";
- wchar_t *apphome_utf16;
- char *apphome_utf8;
- DWORD size, ret;
-
- size = ExpandEnvironmentStringsW(query, NULL, 0);
- /* The function gave us the full size of the buffer in chars, including NUL */
- apphome_utf16 = git__malloc(size * sizeof(wchar_t));
- if (apphome_utf16 == NULL)
- return GIT_ENOMEM;
-
- ret = ExpandEnvironmentStringsW(query, apphome_utf16, size);
- if (ret != size)
- return git__throw(GIT_ERROR, "Failed to expand environment strings");
-
- if (_waccess(apphome_utf16, F_OK) < 0) {
- git__free(apphome_utf16);
- return GIT_ENOTFOUND;
- }
-
- apphome_utf8 = gitwin_from_utf16(apphome_utf16);
- git__free(apphome_utf16);
-
- git_buf_attach(system_config_path, apphome_utf8, 0);
-
- return GIT_SUCCESS;
-}
-#endif
-
-int git_config_find_system_r(git_buf *system_config_path)
-{
- if (git_buf_sets(system_config_path, "/etc/gitconfig") < GIT_SUCCESS)
- return git_buf_lasterror(system_config_path);
-
- if (git_futils_exists(system_config_path->ptr) == GIT_SUCCESS)
- return GIT_SUCCESS;
-
- git_buf_clear(system_config_path);
-
-#if GIT_WIN32
- return win32_find_system(system_config_path);
-#else
- return GIT_ENOTFOUND;
-#endif
+ return git_futils_find_system_file(path, GIT_CONFIG_FILENAME_SYSTEM);
}
int git_config_find_system(char *system_config_path)
diff --git a/src/config.h b/src/config.h
index fc639c6..6345b0a 100644
--- a/src/config.h
+++ b/src/config.h
@@ -14,6 +14,7 @@
#define GIT_CONFIG_FILENAME ".gitconfig"
#define GIT_CONFIG_FILENAME_INREPO "config"
+#define GIT_CONFIG_FILENAME_SYSTEM "gitconfig"
#define GIT_CONFIG_FILE_MODE 0666
struct git_config {
diff --git a/src/fileops.c b/src/fileops.c
index fb2f954..5eb7bf6 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -403,3 +403,134 @@ int git_futils_contains_file(git_buf *base, const char *file, int append_if_exis
return _check_dir_contents(base, file, append_if_exists, &git_futils_isfile);
}
+int git_futils_find_global_file(git_buf *path, const char *filename)
+{
+ int error;
+ const char *home = getenv("HOME");
+
+#ifdef GIT_WIN32
+ if (home == NULL)
+ home = getenv("USERPROFILE");
+#endif
+
+ if (home == NULL)
+ return git__throw(GIT_EOSERR, "Failed to open global %s file. "
+ "Cannot locate the user's home directory.", filename);
+
+ if ((error = git_buf_joinpath(path, home, filename)) < GIT_SUCCESS)
+ return error;
+
+ if (git_futils_exists(path->ptr) < GIT_SUCCESS) {
+ git_buf_clear(path);
+ return GIT_ENOTFOUND;
+ }
+
+ return GIT_SUCCESS;
+}
+
+#ifdef GIT_WIN32
+typedef struct {
+ wchar_t *path;
+ DWORD len;
+} win32_path;
+
+static const win32_path *win32_system_root(void)
+{
+ static win32_path s_root = { 0, 0 };
+
+ if (s_root.path == NULL) {
+ const wchar_t *root_tmpl = L"%PROGRAMFILES%\\Git\\etc\\";
+
+ s_root.len = ExpandEnvironmentStringsW(root_tmpl, NULL, 0);
+
+ if (s_root.len <= 0) {
+ git__throw(GIT_EOSERR, "Failed to expand environment strings");
+ return NULL;
+ }
+
+ s_root.path = git__calloc(s_root.len, sizeof(wchar_t));
+ if (s_root.path == NULL)
+ return NULL;
+
+ if (ExpandEnvironmentStringsW(root_tmpl, s_root.path, s_root.len) != s_root.len) {
+ git__throw(GIT_EOSERR, "Failed to expand environment strings");
+ git__free(s_root.path);
+ s_root.path = NULL;
+ return NULL;
+ }
+ }
+
+ return &s_root;
+}
+
+static int win32_find_system_file(git_buf *path, const char *filename)
+{
+ int error = GIT_SUCCESS;
+ const win32_path *root = win32_system_root();
+ size_t len;
+ wchar_t *file_utf16 = NULL, *scan;
+ char *file_utf8 = NULL;
+
+ if (!root || !filename || (len = strlen(filename)) == 0)
+ return GIT_ENOTFOUND;
+
+ /* allocate space for wchar_t path to file */
+ file_utf16 = git__calloc(root->len + len + 2, sizeof(wchar_t));
+ if (!file_utf16)
+ return GIT_ENOMEM;
+
+ /* append root + '\\' + filename as wchar_t */
+ memcpy(file_utf16, root->path, root->len * sizeof(wchar_t));
+
+ if (*filename == '/' || *filename == '\\')
+ filename++;
+
+ if (gitwin_append_utf16(file_utf16 + root->len - 1, filename, len + 1) !=
+ (int)len) {
+ error = git__throw(GIT_EOSERR, "Failed to build file path");
+ goto cleanup;
+ }
+
+ for (scan = file_utf16; *scan; scan++)
+ if (*scan == L'/')
+ *scan = L'\\';
+
+ /* check access */
+ if (_waccess(file_utf16, F_OK) < 0) {
+ error = GIT_ENOTFOUND;
+ goto cleanup;
+ }
+
+ /* convert to utf8 */
+ if ((file_utf8 = gitwin_from_utf16(file_utf16)) == NULL)
+ error = GIT_ENOMEM;
+
+ if (file_utf8) {
+ git_path_mkposix(file_utf8);
+ git_buf_attach(path, file_utf8, 0);
+ }
+
+cleanup:
+ git__free(file_utf16);
+
+ return error;
+}
+#endif
+
+int git_futils_find_system_file(git_buf *path, const char *filename)
+{
+ if (git_buf_joinpath(path, "/etc", filename) < GIT_SUCCESS)
+ return git_buf_lasterror(path);
+
+ if (git_futils_exists(path->ptr) == GIT_SUCCESS)
+ return GIT_SUCCESS;
+
+ git_buf_clear(path);
+
+#ifdef GIT_WIN32
+ return win32_find_system_file(path, filename);
+#else
+ return GIT_ENOTFOUND;
+#endif
+}
+
diff --git a/src/fileops.h b/src/fileops.h
index df135d0..31f3e6a 100644
--- a/src/fileops.h
+++ b/src/fileops.h
@@ -165,4 +165,28 @@ extern int git_futils_direach(
extern int git_futils_cmp_path(const char *name1, int len1, int isdir1,
const char *name2, int len2, int isdir2);
+/**
+ * Find a "global" file (i.e. one in a user's home directory).
+ *
+ * @param pathbuf buffer to write the full path into
+ * @param filename name of file to find in the home directory
+ * @return
+ * - GIT_SUCCESS if found;
+ * - GIT_ENOTFOUND if not found;
+ * - GIT_EOSERR on an unspecified OS related error.
+ */
+extern int git_futils_find_global_file(git_buf *path, const char *filename);
+
+/**
+ * Find a "system" file (i.e. one shared for all users of the system).
+ *
+ * @param pathbuf buffer to write the full path into
+ * @param filename name of file to find in the home directory
+ * @return
+ * - GIT_SUCCESS if found;
+ * - GIT_ENOTFOUND if not found;
+ * - GIT_EOSERR on an unspecified OS related error.
+ */
+extern int git_futils_find_system_file(git_buf *path, const char *filename);
+
#endif /* INCLUDE_fileops_h__ */
diff --git a/src/repository.c b/src/repository.c
index e0d4c63..a94ecce 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -59,7 +59,7 @@ void git_repository_free(git_repository *repo)
git_cache_free(&repo->objects);
git_repository__refcache_free(&repo->references);
- git_repository__attr_cache_free(&repo->attrcache);
+ git_attr_cache_flush(repo);
git__free(repo->path_repository);
git__free(repo->workdir);
diff --git a/src/repository.h b/src/repository.h
index 5274fc1..8205215 100644
--- a/src/repository.h
+++ b/src/repository.h
@@ -19,7 +19,7 @@
#include "refs.h"
#include "buffer.h"
#include "odb.h"
-#include "attr.h"
+#include "attr_file.h"
#define DOT_GIT ".git"
#define GIT_DIR DOT_GIT "/"
diff --git a/src/util.h b/src/util.h
index be978a6..4b1104b 100644
--- a/src/util.h
+++ b/src/util.h
@@ -109,7 +109,6 @@ extern void **git__bsearch(const void *key, void **base, size_t nmemb,
int (*compar)(const void *, const void *));
extern int git__strcmp_cb(const void *a, const void *b);
-extern uint32_t git__strhash_cb(const void *key, int hash_id);
typedef struct {
short refcount;
diff --git a/src/win32/utf-conv.c b/src/win32/utf-conv.c
index b41c78f..b1b838e 100644
--- a/src/win32/utf-conv.c
+++ b/src/win32/utf-conv.c
@@ -57,6 +57,11 @@ wchar_t* gitwin_to_utf16(const char* str)
return ret;
}
+int gitwin_append_utf16(wchar_t *buffer, const char *str, size_t len)
+{
+ return MultiByteToWideChar(_active_codepage, 0, str, -1, buffer, len);
+}
+
char* gitwin_from_utf16(const wchar_t* str)
{
char* ret;
diff --git a/src/win32/utf-conv.h b/src/win32/utf-conv.h
index da03e33..bbb5c4f 100644
--- a/src/win32/utf-conv.h
+++ b/src/win32/utf-conv.h
@@ -11,6 +11,7 @@
#define INCLUDE_git_utfconv_h__
wchar_t* gitwin_to_utf16(const char* str);
+int gitwin_append_utf16(wchar_t *buffer, const char *str, size_t len)
char* gitwin_from_utf16(const wchar_t* str);
#endif
diff --git a/tests-clay/attr/file.c b/tests-clay/attr/file.c
index 0a5bff5..d9e2d57 100644
--- a/tests-clay/attr/file.c
+++ b/tests-clay/attr/file.c
@@ -8,7 +8,7 @@ void test_attr_file__simple_read(void)
{
git_attr_file *file = NULL;
- cl_git_pass(git_attr_file__from_file(&file, cl_fixture("attr/attr0")));
+ cl_git_pass(git_attr_file__from_file(NULL, cl_fixture("attr/attr0"), &file));
cl_assert_strequal(cl_fixture("attr/attr0"), file->path);
cl_assert(file->rules.length == 1);
@@ -16,15 +16,12 @@ void test_attr_file__simple_read(void)
cl_assert(rule != NULL);
cl_assert_strequal("*", rule->match.pattern);
cl_assert(rule->match.length == 1);
- cl_assert(!rule->match.negative);
- cl_assert(!rule->match.directory);
- cl_assert(!rule->match.fullpath);
+ cl_assert(rule->match.flags == 0);
cl_assert(rule->assigns.length == 1);
git_attr_assignment *assign = get_assign(rule, 0);
cl_assert(assign != NULL);
cl_assert_strequal("binary", assign->name);
- cl_assert(assign->name_len == 6);
cl_assert(assign->value == GIT_ATTR_TRUE);
cl_assert(!assign->is_allocated);
@@ -37,7 +34,7 @@ void test_attr_file__match_variants(void)
git_attr_rule *rule;
git_attr_assignment *assign;
- cl_git_pass(git_attr_file__from_file(&file, cl_fixture("attr/attr1")));
+ cl_git_pass(git_attr_file__from_file(NULL, cl_fixture("attr/attr1"), &file));
cl_assert_strequal(cl_fixture("attr/attr1"), file->path);
cl_assert(file->rules.length == 10);
@@ -48,38 +45,31 @@ void test_attr_file__match_variants(void)
cl_assert(rule);
cl_assert_strequal("pat0", rule->match.pattern);
cl_assert(rule->match.length == strlen("pat0"));
- cl_assert(!rule->match.negative);
- cl_assert(!rule->match.directory);
- cl_assert(!rule->match.fullpath);
+ cl_assert(rule->match.flags == 0);
cl_assert(rule->assigns.length == 1);
assign = get_assign(rule,0);
cl_assert_strequal("attr0", assign->name);
cl_assert(assign->name_hash == git_attr_file__name_hash(assign->name));
- cl_assert(assign->name_len == strlen("attr0"));
cl_assert(assign->value == GIT_ATTR_TRUE);
cl_assert(!assign->is_allocated);
rule = get_rule(1);
cl_assert_strequal("pat1", rule->match.pattern);
cl_assert(rule->match.length == strlen("pat1"));
- cl_assert(rule->match.negative);
+ cl_assert(rule->match.flags == GIT_ATTR_FNMATCH_NEGATIVE);
rule = get_rule(2);
cl_assert_strequal("pat2", rule->match.pattern);
cl_assert(rule->match.length == strlen("pat2"));
- cl_assert(rule->match.directory);
- cl_assert(!rule->match.fullpath);
+ cl_assert(rule->match.flags == GIT_ATTR_FNMATCH_DIRECTORY);
rule = get_rule(3);
cl_assert_strequal("pat3dir/pat3file", rule->match.pattern);
- cl_assert(!rule->match.directory);
- cl_assert(rule->match.fullpath);
+ cl_assert(rule->match.flags == GIT_ATTR_FNMATCH_FULLPATH);
rule = get_rule(4);
cl_assert_strequal("pat4.*", rule->match.pattern);
- cl_assert(!rule->match.negative);
- cl_assert(!rule->match.directory);
- cl_assert(!rule->match.fullpath);
+ cl_assert(rule->match.flags == 0);
rule = get_rule(5);
cl_assert_strequal("*.pat5", rule->match.pattern);
@@ -94,9 +84,7 @@ void test_attr_file__match_variants(void)
rule = get_rule(8);
cl_assert_strequal("pat8 with spaces", rule->match.pattern);
cl_assert(rule->match.length == strlen("pat8 with spaces"));
- cl_assert(!rule->match.negative);
- cl_assert(!rule->match.directory);
- cl_assert(!rule->match.fullpath);
+ cl_assert(rule->match.flags == 0);
rule = get_rule(9);
cl_assert_strequal("pat9", rule->match.pattern);
@@ -120,7 +108,6 @@ static void check_one_assign(
cl_assert(rule->assigns.length == 1);
cl_assert_strequal(name, assign->name);
cl_assert(assign->name_hash == git_attr_file__name_hash(assign->name));
- cl_assert(assign->name_len == strlen(name));
cl_assert(assign->is_allocated == is_allocated);
if (is_allocated)
cl_assert_strequal(value, assign->value);
@@ -134,7 +121,7 @@ void test_attr_file__assign_variants(void)
git_attr_rule *rule;
git_attr_assignment *assign;
- cl_git_pass(git_attr_file__from_file(&file, cl_fixture("attr/attr2")));
+ cl_git_pass(git_attr_file__from_file(NULL, cl_fixture("attr/attr2"), &file));
cl_assert_strequal(cl_fixture("attr/attr2"), file->path);
cl_assert(file->rules.length == 11);
@@ -199,7 +186,7 @@ void test_attr_file__check_attr_examples(void)
git_attr_rule *rule;
git_attr_assignment *assign;
- cl_git_pass(git_attr_file__from_file(&file, cl_fixture("attr/attr3")));
+ cl_git_pass(git_attr_file__from_file(NULL, cl_fixture("attr/attr3"), &file));
cl_assert_strequal(cl_fixture("attr/attr3"), file->path);
cl_assert(file->rules.length == 3);
diff --git a/tests-clay/attr/lookup.c b/tests-clay/attr/lookup.c
index 870dcd3..fcade52 100644
--- a/tests-clay/attr/lookup.c
+++ b/tests-clay/attr/lookup.c
@@ -1,5 +1,5 @@
#include "clay_libgit2.h"
-#include "attr.h"
+#include "attr_file.h"
void test_attr_lookup__simple(void)
{
@@ -7,7 +7,7 @@ void test_attr_lookup__simple(void)
git_attr_path path;
const char *value = NULL;
- cl_git_pass(git_attr_file__from_file(&file, cl_fixture("attr/attr0")));
+ cl_git_pass(git_attr_file__from_file(NULL, cl_fixture("attr/attr0"), &file));
cl_assert_strequal(cl_fixture("attr/attr0"), file->path);
cl_assert(file->rules.length == 1);
@@ -41,10 +41,6 @@ static void run_test_cases(git_attr_file *file, test_case *cases)
int error;
for (c = cases; c->path != NULL; c++) {
- /* Put this in because I was surprised that all the tests passed */
- /* fprintf(stderr, "checking '%s' attr %s == %s\n", */
- /* c->path, c->attr, c->expected); */
-
cl_git_pass(git_attr_path__init(&path, c->path));
if (c->force_dir)
@@ -136,7 +132,7 @@ void test_attr_lookup__match_variants(void)
{ NULL, NULL, NULL, 0, 0 }
};
- cl_git_pass(git_attr_file__from_file(&file, cl_fixture("attr/attr1")));
+ cl_git_pass(git_attr_file__from_file(NULL, cl_fixture("attr/attr1"), &file));
cl_assert_strequal(cl_fixture("attr/attr1"), file->path);
cl_assert(file->rules.length == 10);
@@ -194,7 +190,7 @@ void test_attr_lookup__assign_variants(void)
{ NULL, NULL, NULL, 0, 0 }
};
- cl_git_pass(git_attr_file__from_file(&file, cl_fixture("attr/attr2")));
+ cl_git_pass(git_attr_file__from_file(NULL, cl_fixture("attr/attr2"), &file));
cl_assert(file->rules.length == 11);
run_test_cases(file, cases);
@@ -228,7 +224,31 @@ void test_attr_lookup__check_attr_examples(void)
{ NULL, NULL, NULL, 0, 0 }
};
- cl_git_pass(git_attr_file__from_file(&file, cl_fixture("attr/attr3")));
+ cl_git_pass(git_attr_file__from_file(NULL, cl_fixture("attr/attr3"), &file));
+ cl_assert(file->rules.length == 3);
+
+ run_test_cases(file, cases);
+
+ git_attr_file__free(file);
+}
+
+void test_attr_lookup__from_buffer(void)
+{
+ git_attr_file *file = NULL;
+ test_case cases[] = {
+ { "abc", "foo", GIT_ATTR_TRUE, 0, 0 },
+ { "abc", "bar", GIT_ATTR_TRUE, 0, 0 },
+ { "abc", "baz", GIT_ATTR_TRUE, 0, 0 },
+ { "aaa", "foo", GIT_ATTR_TRUE, 0, 0 },
+ { "aaa", "bar", NULL, 0, 0 },
+ { "aaa", "baz", GIT_ATTR_TRUE, 0, 0 },
+ { "qqq", "foo", NULL, 0, 0 },
+ { "qqq", "bar", NULL, 0, 0 },
+ { "qqq", "baz", GIT_ATTR_TRUE, 0, 0 },
+ { NULL, NULL, NULL, 0, 0 }
+ };
+
+ cl_git_pass(git_attr_file__from_buffer(NULL, "a* foo\nabc bar\n* baz", &file));
cl_assert(file->rules.length == 3);
run_test_cases(file, cases);
diff --git a/tests-clay/attr/repo.c b/tests-clay/attr/repo.c
index b6815f3..e80e24d 100644
--- a/tests-clay/attr/repo.c
+++ b/tests-clay/attr/repo.c
@@ -6,11 +6,14 @@ static git_repository *g_repo = NULL;
void test_attr_repo__initialize(void)
{
- /* before each test, instantiate the attr repo from the fixtures and
+ /* Before each test, instantiate the attr repo from the fixtures and
* rename the .gitted to .git so it is a repo with a working dir.
+ * Also rename gitattributes to .gitattributes, because it contains
+ * macro definitions which are only allowed in the root.
*/
cl_fixture_sandbox("attr");
cl_git_pass(p_rename("attr/.gitted", "attr/.git"));
+ cl_git_pass(p_rename("attr/gitattributes", "attr/.gitattributes"));
cl_git_pass(git_repository_open(&g_repo, "attr/.git"));
}
@@ -138,3 +141,46 @@ void test_attr_repo__foreach(void)
&count_attrs, &count));
cl_assert(count == 5); /* repoattr, rootattr, subattr, negattr, another */
}
+
+void test_attr_repo__manpage_example(void)
+{
+ const char *value;
+
+ cl_git_pass(git_attr_get(g_repo, "subdir/abc", "foo", &value));
+ cl_assert(value == GIT_ATTR_TRUE);
+
+ cl_git_pass(git_attr_get(g_repo, "subdir/abc", "bar", &value));
+ cl_assert(value == NULL);
+
+ cl_git_pass(git_attr_get(g_repo, "subdir/abc", "baz", &value));
+ cl_assert(value == GIT_ATTR_FALSE);
+
+ cl_git_pass(git_attr_get(g_repo, "subdir/abc", "merge", &value));
+ cl_assert_strequal("filfre", value);
+
+ cl_git_pass(git_attr_get(g_repo, "subdir/abc", "frotz", &value));
+ cl_assert(value == NULL);
+}
+
+void test_attr_repo__macros(void)
+{
+ const char *names[5] = { "rootattr", "binary", "diff", "crlf", "frotz" };
+ const char *names2[5] = { "mymacro", "positive", "negative", "rootattr", "another" };
+ const char *values[5];
+
+ cl_git_pass(git_attr_get_many(g_repo, "binfile", 5, names, values));
+
+ cl_assert(values[0] == GIT_ATTR_TRUE);
+ cl_assert(values[1] == NULL);
+ cl_assert(values[2] == GIT_ATTR_FALSE);
+ cl_assert(values[3] == GIT_ATTR_FALSE);
+ cl_assert(values[4] == NULL);
+
+ cl_git_pass(git_attr_get_many(g_repo, "macro_test", 5, names2, values));
+
+ cl_assert(values[0] == NULL);
+ cl_assert(values[1] == GIT_ATTR_TRUE);
+ cl_assert(values[2] == GIT_ATTR_FALSE);
+ cl_assert(values[3] == NULL);
+ cl_assert_strequal("77", values[4]);
+}
diff --git a/tests-clay/clay.h b/tests-clay/clay.h
index 8237991..4a57926 100644
--- a/tests-clay/clay.h
+++ b/tests-clay/clay.h
@@ -65,6 +65,7 @@ extern void test_attr_file__match_variants(void);
extern void test_attr_file__simple_read(void);
extern void test_attr_lookup__assign_variants(void);
extern void test_attr_lookup__check_attr_examples(void);
+extern void test_attr_lookup__from_buffer(void);
extern void test_attr_lookup__match_variants(void);
extern void test_attr_lookup__simple(void);
extern void test_attr_repo__cleanup(void);
@@ -72,6 +73,8 @@ extern void test_attr_repo__foreach(void);
extern void test_attr_repo__get_many(void);
extern void test_attr_repo__get_one(void);
extern void test_attr_repo__initialize(void);
+extern void test_attr_repo__macros(void);
+extern void test_attr_repo__manpage_example(void);
extern void test_buf_basic__printf(void);
extern void test_buf_basic__resize(void);
extern void test_config_add__cleanup(void);
diff --git a/tests-clay/clay_main.c b/tests-clay/clay_main.c
index 49a8676..ce881e4 100644
--- a/tests-clay/clay_main.c
+++ b/tests-clay/clay_main.c
@@ -117,13 +117,16 @@ static const struct clay_func _clay_cb_attr_file[] = {
static const struct clay_func _clay_cb_attr_lookup[] = {
{"assign_variants", &test_attr_lookup__assign_variants},
{"check_attr_examples", &test_attr_lookup__check_attr_examples},
+ {"from_buffer", &test_attr_lookup__from_buffer},
{"match_variants", &test_attr_lookup__match_variants},
{"simple", &test_attr_lookup__simple}
};
static const struct clay_func _clay_cb_attr_repo[] = {
{"foreach", &test_attr_repo__foreach},
{"get_many", &test_attr_repo__get_many},
- {"get_one", &test_attr_repo__get_one}
+ {"get_one", &test_attr_repo__get_one},
+ {"macros", &test_attr_repo__macros},
+ {"manpage_example", &test_attr_repo__manpage_example}
};
static const struct clay_func _clay_cb_buf_basic[] = {
{"printf", &test_buf_basic__printf},
@@ -329,13 +332,13 @@ static const struct clay_suite _clay_suites[] = {
"attr::lookup",
{NULL, NULL},
{NULL, NULL},
- _clay_cb_attr_lookup, 4
+ _clay_cb_attr_lookup, 5
},
{
"attr::repo",
{"initialize", &test_attr_repo__initialize},
{"cleanup", &test_attr_repo__cleanup},
- _clay_cb_attr_repo, 3
+ _clay_cb_attr_repo, 5
},
{
"buf::basic",
@@ -556,7 +559,7 @@ static const struct clay_suite _clay_suites[] = {
};
static size_t _clay_suite_count = 39;
-static size_t _clay_callback_count = 131;
+static size_t _clay_callback_count = 134;
/* Core test functions */
static void
diff --git a/tests/resources/attr/.gitattributes b/tests/resources/attr/.gitattributes
deleted file mode 100644
index f2c6d71..0000000
--- a/tests/resources/attr/.gitattributes
+++ /dev/null
@@ -1,4 +0,0 @@
-* rootattr
-root_test2 -rootattr
-root_test3 !rootattr
-
diff --git a/tests/resources/attr/.gitted/index b/tests/resources/attr/.gitted/index
index 6841fb2..9c59073 100644
Binary files a/tests/resources/attr/.gitted/index and b/tests/resources/attr/.gitted/index differ
diff --git a/tests/resources/attr/.gitted/info/attributes b/tests/resources/attr/.gitted/info/attributes
index 93efc0c..2e9643a 100644
--- a/tests/resources/attr/.gitted/info/attributes
+++ b/tests/resources/attr/.gitted/info/attributes
@@ -1,2 +1,2 @@
* repoattr
-
+a* foo !bar -baz
diff --git a/tests/resources/attr/.gitted/logs/HEAD b/tests/resources/attr/.gitted/logs/HEAD
index cfd1f95..3c40451 100644
--- a/tests/resources/attr/.gitted/logs/HEAD
+++ b/tests/resources/attr/.gitted/logs/HEAD
@@ -1 +1,2 @@
0000000000000000000000000000000000000000 6bab5c79cd5140d0f800917f550eb2a3dc32b0da Russell Belfer <arrbee@arrbee.com> 1324416995 -0800 commit (initial): initial test data
+6bab5c79cd5140d0f800917f550eb2a3dc32b0da 605812ab7fe421fdd325a935d35cb06a9234a7d7 Russell Belfer <arrbee@arrbee.com> 1325143098 -0800 commit: latest test updates
diff --git a/tests/resources/attr/.gitted/logs/refs/heads/master b/tests/resources/attr/.gitted/logs/refs/heads/master
index cfd1f95..3c40451 100644
--- a/tests/resources/attr/.gitted/logs/refs/heads/master
+++ b/tests/resources/attr/.gitted/logs/refs/heads/master
@@ -1 +1,2 @@
0000000000000000000000000000000000000000 6bab5c79cd5140d0f800917f550eb2a3dc32b0da Russell Belfer <arrbee@arrbee.com> 1324416995 -0800 commit (initial): initial test data
+6bab5c79cd5140d0f800917f550eb2a3dc32b0da 605812ab7fe421fdd325a935d35cb06a9234a7d7 Russell Belfer <arrbee@arrbee.com> 1325143098 -0800 commit: latest test updates
diff --git a/tests/resources/attr/.gitted/objects/3e/42ffc54a663f9401cc25843d6c0e71a33e4249 b/tests/resources/attr/.gitted/objects/3e/42ffc54a663f9401cc25843d6c0e71a33e4249
new file mode 100644
index 0000000..091d79b
Binary files /dev/null and b/tests/resources/attr/.gitted/objects/3e/42ffc54a663f9401cc25843d6c0e71a33e4249 differ
diff --git a/tests/resources/attr/.gitted/objects/60/5812ab7fe421fdd325a935d35cb06a9234a7d7 b/tests/resources/attr/.gitted/objects/60/5812ab7fe421fdd325a935d35cb06a9234a7d7
new file mode 100644
index 0000000..b0cc51e
--- /dev/null
+++ b/tests/resources/attr/.gitted/objects/60/5812ab7fe421fdd325a935d35cb06a9234a7d7
@@ -0,0 +1,2 @@
+xN
0;S˻BU J ?lٖygcáU RbacG;l㠝Dq֠ZʫAH<Ǒ3N=J2d3[0=
+}ۤIjM"x/[TwU&[/k(tJL
\ No newline at end of file
diff --git a/tests/resources/attr/.gitted/objects/94/da4faa0a6bfb8ee6ccf7153801a69202b31857 b/tests/resources/attr/.gitted/objects/94/da4faa0a6bfb8ee6ccf7153801a69202b31857
new file mode 100644
index 0000000..a9ddf5d
Binary files /dev/null and b/tests/resources/attr/.gitted/objects/94/da4faa0a6bfb8ee6ccf7153801a69202b31857 differ
diff --git a/tests/resources/attr/.gitted/objects/99/eae476896f4907224978b88e5ecaa6c5bb67a9 b/tests/resources/attr/.gitted/objects/99/eae476896f4907224978b88e5ecaa6c5bb67a9
new file mode 100644
index 0000000..8f5acc7
Binary files /dev/null and b/tests/resources/attr/.gitted/objects/99/eae476896f4907224978b88e5ecaa6c5bb67a9 differ
diff --git a/tests/resources/attr/.gitted/objects/9f/b40b6675dde60b5697afceae91b66d908c02d9 b/tests/resources/attr/.gitted/objects/9f/b40b6675dde60b5697afceae91b66d908c02d9
new file mode 100644
index 0000000..7663ad0
Binary files /dev/null and b/tests/resources/attr/.gitted/objects/9f/b40b6675dde60b5697afceae91b66d908c02d9 differ
diff --git a/tests/resources/attr/.gitted/objects/a5/6bbcecaeac760cc26239384d2d4c614e7e4320 b/tests/resources/attr/.gitted/objects/a5/6bbcecaeac760cc26239384d2d4c614e7e4320
new file mode 100644
index 0000000..d898ae9
Binary files /dev/null and b/tests/resources/attr/.gitted/objects/a5/6bbcecaeac760cc26239384d2d4c614e7e4320 differ
diff --git a/tests/resources/attr/.gitted/objects/d8/00886d9c86731ae5c4a62b0b77c437015e00d2 b/tests/resources/attr/.gitted/objects/d8/00886d9c86731ae5c4a62b0b77c437015e00d2
new file mode 100644
index 0000000..83f3b72
Binary files /dev/null and b/tests/resources/attr/.gitted/objects/d8/00886d9c86731ae5c4a62b0b77c437015e00d2 differ
diff --git a/tests/resources/attr/.gitted/objects/ff/69f8639ce2e6010b3f33a74160aad98b48da2b b/tests/resources/attr/.gitted/objects/ff/69f8639ce2e6010b3f33a74160aad98b48da2b
new file mode 100644
index 0000000..b736c0b
Binary files /dev/null and b/tests/resources/attr/.gitted/objects/ff/69f8639ce2e6010b3f33a74160aad98b48da2b differ
diff --git a/tests/resources/attr/.gitted/refs/heads/master b/tests/resources/attr/.gitted/refs/heads/master
index 279272e..1049fe4 100644
--- a/tests/resources/attr/.gitted/refs/heads/master
+++ b/tests/resources/attr/.gitted/refs/heads/master
@@ -1 +1 @@
-6bab5c79cd5140d0f800917f550eb2a3dc32b0da
+605812ab7fe421fdd325a935d35cb06a9234a7d7
diff --git a/tests/resources/attr/binfile b/tests/resources/attr/binfile
new file mode 100644
index 0000000..d800886
--- /dev/null
+++ b/tests/resources/attr/binfile
@@ -0,0 +1 @@
+123
\ No newline at end of file
diff --git a/tests/resources/attr/gitattributes b/tests/resources/attr/gitattributes
new file mode 100644
index 0000000..94da4fa
--- /dev/null
+++ b/tests/resources/attr/gitattributes
@@ -0,0 +1,7 @@
+* rootattr
+root_test2 -rootattr
+root_test3 !rootattr
+binfile binary
+abc foo bar baz
+[attr]mymacro positive -negative !rootattr
+macro* mymacro another=77
diff --git a/tests/resources/attr/macro_test b/tests/resources/attr/macro_test
new file mode 100644
index 0000000..ff69f86
--- /dev/null
+++ b/tests/resources/attr/macro_test
@@ -0,0 +1 @@
+Yo
diff --git a/tests/resources/attr/subdir/.gitattributes b/tests/resources/attr/subdir/.gitattributes
index 210f3a8..99eae47 100644
--- a/tests/resources/attr/subdir/.gitattributes
+++ b/tests/resources/attr/subdir/.gitattributes
@@ -1,3 +1,5 @@
* subattr=yes -negattr
subdir/*.txt another=one
-
+ab* merge=filfre
+abc -foo -bar
+*.c frotz
diff --git a/tests/resources/attr/subdir/abc b/tests/resources/attr/subdir/abc
new file mode 100644
index 0000000..3e42ffc
--- /dev/null
+++ b/tests/resources/attr/subdir/abc
@@ -0,0 +1,37 @@
+# Test file from gitattributes(5) example:
+
+If you have these three gitattributes file:
+
+ (in $GIT_DIR/info/attributes)
+
+ a* foo !bar -baz
+
+ (in .gitattributes)
+ abc foo bar baz
+
+ (in t/.gitattributes)
+ ab* merge=filfre
+ abc -foo -bar
+ *.c frotz
+
+the attributes given to path t/abc are computed as follows:
+
+1. By examining t/.gitattributes (which is in the same directory as the path
+ in question), git finds that the first line matches. merge attribute is
+ set. It also finds that the second line matches, and attributes foo and
+ bar are unset.
+2. Then it examines .gitattributes (which is in the parent directory), and
+ finds that the first line matches, but t/.gitattributes file already
+ decided how merge, foo and bar attributes should be given to this path,
+ so it leaves foo and bar unset. Attribute baz is set.
+3. Finally it examines $GIT_DIR/info/attributes. This file is used to
+ override the in-tree settings. The first line is a match, and foo is set,
+ bar is reverted to unspecified state, and baz is unset.
+
+As the result, the attributes assignment to t/abc becomes:
+
+ foo set to true
+ bar unspecified
+ baz set to false
+ merge set to string value "filfre"
+ frotz unspecified