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.

75 lines
2.0KB

  1. #include "cache.h"
  2. #include "oidmap.h"
  3. #include "object-store.h"
  4. #include "replace-object.h"
  5. #include "refs.h"
  6. #include "repository.h"
  7. #include "commit.h"
  8. static int register_replace_ref(struct repository *r,
  9. const char *refname,
  10. const struct object_id *oid,
  11. int flag, void *cb_data)
  12. {
  13. /* Get sha1 from refname */
  14. const char *slash = strrchr(refname, '/');
  15. const char *hash = slash ? slash + 1 : refname;
  16. struct replace_object *repl_obj = xmalloc(sizeof(*repl_obj));
  17. if (get_oid_hex(hash, &repl_obj->original.oid)) {
  18. free(repl_obj);
  19. warning(_("bad replace ref name: %s"), refname);
  20. return 0;
  21. }
  22. /* Copy sha1 from the read ref */
  23. oidcpy(&repl_obj->replacement, oid);
  24. /* Register new object */
  25. if (oidmap_put(r->objects->replace_map, repl_obj))
  26. die(_("duplicate replace ref: %s"), refname);
  27. return 0;
  28. }
  29. void prepare_replace_object(struct repository *r)
  30. {
  31. if (r->objects->replace_map)
  32. return;
  33. r->objects->replace_map =
  34. xmalloc(sizeof(*r->objects->replace_map));
  35. oidmap_init(r->objects->replace_map, 0);
  36. for_each_replace_ref(r, register_replace_ref, NULL);
  37. }
  38. /* We allow "recursive" replacement. Only within reason, though */
  39. #define MAXREPLACEDEPTH 5
  40. /*
  41. * If a replacement for object oid has been set up, return the
  42. * replacement object's name (replaced recursively, if necessary).
  43. * The return value is either oid or a pointer to a
  44. * permanently-allocated value. This function always respects replace
  45. * references, regardless of the value of read_replace_refs.
  46. */
  47. const struct object_id *do_lookup_replace_object(struct repository *r,
  48. const struct object_id *oid)
  49. {
  50. int depth = MAXREPLACEDEPTH;
  51. const struct object_id *cur = oid;
  52. prepare_replace_object(r);
  53. /* Try to recursively replace the object */
  54. while (depth-- > 0) {
  55. struct replace_object *repl_obj =
  56. oidmap_get(r->objects->replace_map, cur);
  57. if (!repl_obj)
  58. return cur;
  59. cur = &repl_obj->replacement;
  60. }
  61. die(_("replace depth too high for object %s"), oid_to_hex(oid));
  62. }