THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 line
2.3KB

  1. #include "cache.h"
  2. #include "notes-cache.h"
  3. #include "object-store.h"
  4. #include "repository.h"
  5. #include "commit.h"
  6. #include "refs.h"
  7. static int notes_cache_match_validity(struct repository *r,
  8. const char *ref,
  9. const char *validity)
  10. {
  11. struct object_id oid;
  12. struct commit *commit;
  13. struct pretty_print_context pretty_ctx;
  14. struct strbuf msg = STRBUF_INIT;
  15. int ret;
  16. if (read_ref(ref, &oid) < 0)
  17. return 0;
  18. commit = lookup_commit_reference_gently(r, &oid, 1);
  19. if (!commit)
  20. return 0;
  21. memset(&pretty_ctx, 0, sizeof(pretty_ctx));
  22. format_commit_message(commit, "%s", &msg, &pretty_ctx);
  23. strbuf_trim(&msg);
  24. ret = !strcmp(msg.buf, validity);
  25. strbuf_release(&msg);
  26. return ret;
  27. }
  28. void notes_cache_init(struct repository *r, struct notes_cache *c,
  29. const char *name, const char *validity)
  30. {
  31. struct strbuf ref = STRBUF_INIT;
  32. int flags = NOTES_INIT_WRITABLE;
  33. memset(c, 0, sizeof(*c));
  34. c->validity = xstrdup(validity);
  35. strbuf_addf(&ref, "refs/notes/%s", name);
  36. if (!notes_cache_match_validity(r, ref.buf, validity))
  37. flags |= NOTES_INIT_EMPTY;
  38. init_notes(&c->tree, ref.buf, combine_notes_overwrite, flags);
  39. strbuf_release(&ref);
  40. }
  41. int notes_cache_write(struct notes_cache *c)
  42. {
  43. struct object_id tree_oid, commit_oid;
  44. if (!c || !c->tree.initialized || !c->tree.update_ref ||
  45. !*c->tree.update_ref)
  46. return -1;
  47. if (!c->tree.dirty)
  48. return 0;
  49. if (write_notes_tree(&c->tree, &tree_oid))
  50. return -1;
  51. if (commit_tree(c->validity, strlen(c->validity), &tree_oid, NULL,
  52. &commit_oid, NULL, NULL) < 0)
  53. return -1;
  54. if (update_ref("update notes cache", c->tree.update_ref, &commit_oid,
  55. NULL, 0, UPDATE_REFS_QUIET_ON_ERR) < 0)
  56. return -1;
  57. return 0;
  58. }
  59. char *notes_cache_get(struct notes_cache *c, struct object_id *key_oid,
  60. size_t *outsize)
  61. {
  62. const struct object_id *value_oid;
  63. enum object_type type;
  64. char *value;
  65. unsigned long size;
  66. value_oid = get_note(&c->tree, key_oid);
  67. if (!value_oid)
  68. return NULL;
  69. value = read_object_file(value_oid, &type, &size);
  70. *outsize = size;
  71. return value;
  72. }
  73. int notes_cache_put(struct notes_cache *c, struct object_id *key_oid,
  74. const char *data, size_t size)
  75. {
  76. struct object_id value_oid;
  77. if (write_object_file(data, size, "blob", &value_oid) < 0)
  78. return -1;
  79. return add_note(&c->tree, key_oid, &value_oid, NULL);
  80. }