patch-ids.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include "cache.h"
  2. #include "diff.h"
  3. #include "commit.h"
  4. #include "sha1-lookup.h"
  5. #include "patch-ids.h"
  6. static int patch_id_defined(struct commit *commit)
  7. {
  8. /* must be 0 or 1 parents */
  9. return !commit->parents || !commit->parents->next;
  10. }
  11. int commit_patch_id(struct commit *commit, struct diff_options *options,
  12. struct object_id *oid, int diff_header_only, int stable)
  13. {
  14. if (!patch_id_defined(commit))
  15. return -1;
  16. if (commit->parents)
  17. diff_tree_oid(&commit->parents->item->object.oid,
  18. &commit->object.oid, "", options);
  19. else
  20. diff_root_tree_oid(&commit->object.oid, "", options);
  21. diffcore_std(options);
  22. return diff_flush_patch_id(options, oid, diff_header_only, stable);
  23. }
  24. /*
  25. * When we cannot load the full patch-id for both commits for whatever
  26. * reason, the function returns -1 (i.e. return error(...)). Despite
  27. * the "neq" in the name of this function, the caller only cares about
  28. * the return value being zero (a and b are equivalent) or non-zero (a
  29. * and b are different), and returning non-zero would keep both in the
  30. * result, even if they actually were equivalent, in order to err on
  31. * the side of safety. The actual value being negative does not have
  32. * any significance; only that it is non-zero matters.
  33. */
  34. static int patch_id_neq(const void *cmpfn_data,
  35. const struct hashmap_entry *eptr,
  36. const struct hashmap_entry *entry_or_key,
  37. const void *unused_keydata)
  38. {
  39. /* NEEDSWORK: const correctness? */
  40. struct diff_options *opt = (void *)cmpfn_data;
  41. struct patch_id *a, *b;
  42. a = container_of(eptr, struct patch_id, ent);
  43. b = container_of(entry_or_key, struct patch_id, ent);
  44. if (is_null_oid(&a->patch_id) &&
  45. commit_patch_id(a->commit, opt, &a->patch_id, 0, 0))
  46. return error("Could not get patch ID for %s",
  47. oid_to_hex(&a->commit->object.oid));
  48. if (is_null_oid(&b->patch_id) &&
  49. commit_patch_id(b->commit, opt, &b->patch_id, 0, 0))
  50. return error("Could not get patch ID for %s",
  51. oid_to_hex(&b->commit->object.oid));
  52. return !oideq(&a->patch_id, &b->patch_id);
  53. }
  54. int init_patch_ids(struct repository *r, struct patch_ids *ids)
  55. {
  56. memset(ids, 0, sizeof(*ids));
  57. repo_diff_setup(r, &ids->diffopts);
  58. ids->diffopts.detect_rename = 0;
  59. ids->diffopts.flags.recursive = 1;
  60. diff_setup_done(&ids->diffopts);
  61. hashmap_init(&ids->patches, patch_id_neq, &ids->diffopts, 256);
  62. return 0;
  63. }
  64. int free_patch_ids(struct patch_ids *ids)
  65. {
  66. hashmap_free_entries(&ids->patches, struct patch_id, ent);
  67. return 0;
  68. }
  69. static int init_patch_id_entry(struct patch_id *patch,
  70. struct commit *commit,
  71. struct patch_ids *ids)
  72. {
  73. struct object_id header_only_patch_id;
  74. patch->commit = commit;
  75. if (commit_patch_id(commit, &ids->diffopts, &header_only_patch_id, 1, 0))
  76. return -1;
  77. hashmap_entry_init(&patch->ent, oidhash(&header_only_patch_id));
  78. return 0;
  79. }
  80. struct patch_id *has_commit_patch_id(struct commit *commit,
  81. struct patch_ids *ids)
  82. {
  83. struct patch_id patch;
  84. if (!patch_id_defined(commit))
  85. return NULL;
  86. memset(&patch, 0, sizeof(patch));
  87. if (init_patch_id_entry(&patch, commit, ids))
  88. return NULL;
  89. return hashmap_get_entry(&ids->patches, &patch, ent, NULL);
  90. }
  91. struct patch_id *add_commit_patch_id(struct commit *commit,
  92. struct patch_ids *ids)
  93. {
  94. struct patch_id *key;
  95. if (!patch_id_defined(commit))
  96. return NULL;
  97. key = xcalloc(1, sizeof(*key));
  98. if (init_patch_id_entry(key, commit, ids)) {
  99. free(key);
  100. return NULL;
  101. }
  102. hashmap_add(&ids->patches, &key->ent);
  103. return key;
  104. }