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.

187 lines
5.0KB

  1. #include "cache.h"
  2. #include "config.h"
  3. #include "commit.h"
  4. #include "refs.h"
  5. #include "notes-utils.h"
  6. #include "repository.h"
  7. void create_notes_commit(struct repository *r,
  8. struct notes_tree *t,
  9. struct commit_list *parents,
  10. const char *msg, size_t msg_len,
  11. struct object_id *result_oid)
  12. {
  13. struct object_id tree_oid;
  14. assert(t->initialized);
  15. if (write_notes_tree(t, &tree_oid))
  16. die("Failed to write notes tree to database");
  17. if (!parents) {
  18. /* Deduce parent commit from t->ref */
  19. struct object_id parent_oid;
  20. if (!read_ref(t->ref, &parent_oid)) {
  21. struct commit *parent = lookup_commit(r, &parent_oid);
  22. if (parse_commit(parent))
  23. die("Failed to find/parse commit %s", t->ref);
  24. commit_list_insert(parent, &parents);
  25. }
  26. /* else: t->ref points to nothing, assume root/orphan commit */
  27. }
  28. if (commit_tree(msg, msg_len, &tree_oid, parents, result_oid, NULL,
  29. NULL))
  30. die("Failed to commit notes tree to database");
  31. }
  32. void commit_notes(struct repository *r, struct notes_tree *t, const char *msg)
  33. {
  34. struct strbuf buf = STRBUF_INIT;
  35. struct object_id commit_oid;
  36. if (!t)
  37. t = &default_notes_tree;
  38. if (!t->initialized || !t->update_ref || !*t->update_ref)
  39. die(_("Cannot commit uninitialized/unreferenced notes tree"));
  40. if (!t->dirty)
  41. return; /* don't have to commit an unchanged tree */
  42. /* Prepare commit message and reflog message */
  43. strbuf_addstr(&buf, msg);
  44. strbuf_complete_line(&buf);
  45. create_notes_commit(r, t, NULL, buf.buf, buf.len, &commit_oid);
  46. strbuf_insert(&buf, 0, "notes: ", 7); /* commit message starts at index 7 */
  47. update_ref(buf.buf, t->update_ref, &commit_oid, NULL, 0,
  48. UPDATE_REFS_DIE_ON_ERR);
  49. strbuf_release(&buf);
  50. }
  51. int parse_notes_merge_strategy(const char *v, enum notes_merge_strategy *s)
  52. {
  53. if (!strcmp(v, "manual"))
  54. *s = NOTES_MERGE_RESOLVE_MANUAL;
  55. else if (!strcmp(v, "ours"))
  56. *s = NOTES_MERGE_RESOLVE_OURS;
  57. else if (!strcmp(v, "theirs"))
  58. *s = NOTES_MERGE_RESOLVE_THEIRS;
  59. else if (!strcmp(v, "union"))
  60. *s = NOTES_MERGE_RESOLVE_UNION;
  61. else if (!strcmp(v, "cat_sort_uniq"))
  62. *s = NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ;
  63. else
  64. return -1;
  65. return 0;
  66. }
  67. static combine_notes_fn parse_combine_notes_fn(const char *v)
  68. {
  69. if (!strcasecmp(v, "overwrite"))
  70. return combine_notes_overwrite;
  71. else if (!strcasecmp(v, "ignore"))
  72. return combine_notes_ignore;
  73. else if (!strcasecmp(v, "concatenate"))
  74. return combine_notes_concatenate;
  75. else if (!strcasecmp(v, "cat_sort_uniq"))
  76. return combine_notes_cat_sort_uniq;
  77. else
  78. return NULL;
  79. }
  80. static int notes_rewrite_config(const char *k, const char *v, void *cb)
  81. {
  82. struct notes_rewrite_cfg *c = cb;
  83. if (starts_with(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) {
  84. c->enabled = git_config_bool(k, v);
  85. return 0;
  86. } else if (!c->mode_from_env && !strcmp(k, "notes.rewritemode")) {
  87. if (!v)
  88. return config_error_nonbool(k);
  89. c->combine = parse_combine_notes_fn(v);
  90. if (!c->combine) {
  91. error(_("Bad notes.rewriteMode value: '%s'"), v);
  92. return 1;
  93. }
  94. return 0;
  95. } else if (!c->refs_from_env && !strcmp(k, "notes.rewriteref")) {
  96. /* note that a refs/ prefix is implied in the
  97. * underlying for_each_glob_ref */
  98. if (starts_with(v, "refs/notes/"))
  99. string_list_add_refs_by_glob(c->refs, v);
  100. else
  101. warning(_("Refusing to rewrite notes in %s"
  102. " (outside of refs/notes/)"), v);
  103. return 0;
  104. }
  105. return 0;
  106. }
  107. struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd)
  108. {
  109. struct notes_rewrite_cfg *c = xmalloc(sizeof(struct notes_rewrite_cfg));
  110. const char *rewrite_mode_env = getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT);
  111. const char *rewrite_refs_env = getenv(GIT_NOTES_REWRITE_REF_ENVIRONMENT);
  112. c->cmd = cmd;
  113. c->enabled = 1;
  114. c->combine = combine_notes_concatenate;
  115. c->refs = xcalloc(1, sizeof(struct string_list));
  116. c->refs->strdup_strings = 1;
  117. c->refs_from_env = 0;
  118. c->mode_from_env = 0;
  119. if (rewrite_mode_env) {
  120. c->mode_from_env = 1;
  121. c->combine = parse_combine_notes_fn(rewrite_mode_env);
  122. if (!c->combine)
  123. /*
  124. * TRANSLATORS: The first %s is the name of
  125. * the environment variable, the second %s is
  126. * its value.
  127. */
  128. error(_("Bad %s value: '%s'"), GIT_NOTES_REWRITE_MODE_ENVIRONMENT,
  129. rewrite_mode_env);
  130. }
  131. if (rewrite_refs_env) {
  132. c->refs_from_env = 1;
  133. string_list_add_refs_from_colon_sep(c->refs, rewrite_refs_env);
  134. }
  135. git_config(notes_rewrite_config, c);
  136. if (!c->enabled || !c->refs->nr) {
  137. string_list_clear(c->refs, 0);
  138. free(c->refs);
  139. free(c);
  140. return NULL;
  141. }
  142. c->trees = load_notes_trees(c->refs, NOTES_INIT_WRITABLE);
  143. string_list_clear(c->refs, 0);
  144. free(c->refs);
  145. return c;
  146. }
  147. int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
  148. const struct object_id *from_obj, const struct object_id *to_obj)
  149. {
  150. int ret = 0;
  151. int i;
  152. for (i = 0; c->trees[i]; i++)
  153. ret = copy_note(c->trees[i], from_obj, to_obj, 1, c->combine) || ret;
  154. return ret;
  155. }
  156. void finish_copy_notes_for_rewrite(struct repository *r,
  157. struct notes_rewrite_cfg *c,
  158. const char *msg)
  159. {
  160. int i;
  161. for (i = 0; c->trees[i]; i++) {
  162. commit_notes(r, c->trees[i], msg);
  163. free_notes(c->trees[i]);
  164. }
  165. free(c->trees);
  166. free(c);
  167. }