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.

72 lines
1.9KB

  1. #include "cache.h"
  2. #include "remote.h"
  3. #include "refspec.h"
  4. #include "checkout.h"
  5. #include "config.h"
  6. struct tracking_name_data {
  7. /* const */ char *src_ref;
  8. char *dst_ref;
  9. struct object_id *dst_oid;
  10. int num_matches;
  11. const char *default_remote;
  12. char *default_dst_ref;
  13. struct object_id *default_dst_oid;
  14. };
  15. #define TRACKING_NAME_DATA_INIT { NULL, NULL, NULL, 0, NULL, NULL, NULL }
  16. static int check_tracking_name(struct remote *remote, void *cb_data)
  17. {
  18. struct tracking_name_data *cb = cb_data;
  19. struct refspec_item query;
  20. memset(&query, 0, sizeof(struct refspec_item));
  21. query.src = cb->src_ref;
  22. if (remote_find_tracking(remote, &query) ||
  23. get_oid(query.dst, cb->dst_oid)) {
  24. free(query.dst);
  25. return 0;
  26. }
  27. cb->num_matches++;
  28. if (cb->default_remote && !strcmp(remote->name, cb->default_remote)) {
  29. struct object_id *dst = xmalloc(sizeof(*cb->default_dst_oid));
  30. cb->default_dst_ref = xstrdup(query.dst);
  31. oidcpy(dst, cb->dst_oid);
  32. cb->default_dst_oid = dst;
  33. }
  34. if (cb->dst_ref) {
  35. free(query.dst);
  36. return 0;
  37. }
  38. cb->dst_ref = query.dst;
  39. return 0;
  40. }
  41. const char *unique_tracking_name(const char *name, struct object_id *oid,
  42. int *dwim_remotes_matched)
  43. {
  44. struct tracking_name_data cb_data = TRACKING_NAME_DATA_INIT;
  45. const char *default_remote = NULL;
  46. if (!git_config_get_string_const("checkout.defaultremote", &default_remote))
  47. cb_data.default_remote = default_remote;
  48. cb_data.src_ref = xstrfmt("refs/heads/%s", name);
  49. cb_data.dst_oid = oid;
  50. for_each_remote(check_tracking_name, &cb_data);
  51. if (dwim_remotes_matched)
  52. *dwim_remotes_matched = cb_data.num_matches;
  53. free(cb_data.src_ref);
  54. free((char *)default_remote);
  55. if (cb_data.num_matches == 1) {
  56. free(cb_data.default_dst_ref);
  57. free(cb_data.default_dst_oid);
  58. return cb_data.dst_ref;
  59. }
  60. free(cb_data.dst_ref);
  61. if (cb_data.default_dst_ref) {
  62. oidcpy(oid, cb_data.default_dst_oid);
  63. free(cb_data.default_dst_oid);
  64. return cb_data.default_dst_ref;
  65. }
  66. return NULL;
  67. }