oidset.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include "cache.h"
  2. #include "oidset.h"
  3. void oidset_init(struct oidset *set, size_t initial_size)
  4. {
  5. memset(&set->set, 0, sizeof(set->set));
  6. if (initial_size)
  7. kh_resize_oid_set(&set->set, initial_size);
  8. }
  9. int oidset_contains(const struct oidset *set, const struct object_id *oid)
  10. {
  11. khiter_t pos = kh_get_oid_set(&set->set, *oid);
  12. return pos != kh_end(&set->set);
  13. }
  14. int oidset_insert(struct oidset *set, const struct object_id *oid)
  15. {
  16. int added;
  17. kh_put_oid_set(&set->set, *oid, &added);
  18. return !added;
  19. }
  20. int oidset_remove(struct oidset *set, const struct object_id *oid)
  21. {
  22. khiter_t pos = kh_get_oid_set(&set->set, *oid);
  23. if (pos == kh_end(&set->set))
  24. return 0;
  25. kh_del_oid_set(&set->set, pos);
  26. return 1;
  27. }
  28. void oidset_clear(struct oidset *set)
  29. {
  30. kh_release_oid_set(&set->set);
  31. oidset_init(set, 0);
  32. }
  33. int oidset_size(struct oidset *set)
  34. {
  35. return kh_size(&set->set);
  36. }
  37. void oidset_parse_file(struct oidset *set, const char *path)
  38. {
  39. oidset_parse_file_carefully(set, path, NULL, NULL);
  40. }
  41. void oidset_parse_file_carefully(struct oidset *set, const char *path,
  42. oidset_parse_tweak_fn fn, void *cbdata)
  43. {
  44. FILE *fp;
  45. struct strbuf sb = STRBUF_INIT;
  46. struct object_id oid;
  47. fp = fopen(path, "r");
  48. if (!fp)
  49. die("could not open object name list: %s", path);
  50. while (!strbuf_getline(&sb, fp)) {
  51. const char *p;
  52. const char *name;
  53. /*
  54. * Allow trailing comments, leading whitespace
  55. * (including before commits), and empty or whitespace
  56. * only lines.
  57. */
  58. name = strchr(sb.buf, '#');
  59. if (name)
  60. strbuf_setlen(&sb, name - sb.buf);
  61. strbuf_trim(&sb);
  62. if (!sb.len)
  63. continue;
  64. if (parse_oid_hex(sb.buf, &oid, &p) || *p != '\0' ||
  65. (fn && fn(&oid, cbdata)))
  66. die("invalid object name: %s", sb.buf);
  67. oidset_insert(set, &oid);
  68. }
  69. if (ferror(fp))
  70. die_errno("Could not read '%s'", path);
  71. fclose(fp);
  72. strbuf_release(&sb);
  73. }