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.

132 lines
2.4KB

  1. /*
  2. * Copyright (C) 2005 Junio C Hamano
  3. */
  4. #include "cache.h"
  5. #include "diff.h"
  6. #include "diffcore.h"
  7. static char **order;
  8. static int order_cnt;
  9. static void prepare_order(const char *orderfile)
  10. {
  11. int cnt, pass;
  12. struct strbuf sb = STRBUF_INIT;
  13. void *map;
  14. char *cp, *endp;
  15. ssize_t sz;
  16. if (order)
  17. return;
  18. sz = strbuf_read_file(&sb, orderfile, 0);
  19. if (sz < 0)
  20. die_errno(_("failed to read orderfile '%s'"), orderfile);
  21. map = strbuf_detach(&sb, NULL);
  22. endp = (char *) map + sz;
  23. for (pass = 0; pass < 2; pass++) {
  24. cnt = 0;
  25. cp = map;
  26. while (cp < endp) {
  27. char *ep;
  28. for (ep = cp; ep < endp && *ep != '\n'; ep++)
  29. ;
  30. /* cp to ep has one line */
  31. if (*cp == '\n' || *cp == '#')
  32. ; /* comment */
  33. else if (pass == 0)
  34. cnt++;
  35. else {
  36. if (*ep == '\n') {
  37. *ep = 0;
  38. order[cnt] = cp;
  39. } else {
  40. order[cnt] = xmemdupz(cp, ep - cp);
  41. }
  42. cnt++;
  43. }
  44. if (ep < endp)
  45. ep++;
  46. cp = ep;
  47. }
  48. if (pass == 0) {
  49. order_cnt = cnt;
  50. ALLOC_ARRAY(order, cnt);
  51. }
  52. }
  53. }
  54. static int match_order(const char *path)
  55. {
  56. int i;
  57. static struct strbuf p = STRBUF_INIT;
  58. for (i = 0; i < order_cnt; i++) {
  59. strbuf_reset(&p);
  60. strbuf_addstr(&p, path);
  61. while (p.buf[0]) {
  62. char *cp;
  63. if (!wildmatch(order[i], p.buf, 0))
  64. return i;
  65. cp = strrchr(p.buf, '/');
  66. if (!cp)
  67. break;
  68. *cp = 0;
  69. }
  70. }
  71. return order_cnt;
  72. }
  73. static int compare_objs_order(const void *a_, const void *b_)
  74. {
  75. struct obj_order const *a, *b;
  76. a = (struct obj_order const *)a_;
  77. b = (struct obj_order const *)b_;
  78. if (a->order != b->order)
  79. return a->order - b->order;
  80. return a->orig_order - b->orig_order;
  81. }
  82. void order_objects(const char *orderfile, obj_path_fn_t obj_path,
  83. struct obj_order *objs, int nr)
  84. {
  85. int i;
  86. if (!nr)
  87. return;
  88. prepare_order(orderfile);
  89. for (i = 0; i < nr; i++) {
  90. objs[i].orig_order = i;
  91. objs[i].order = match_order(obj_path(objs[i].obj));
  92. }
  93. QSORT(objs, nr, compare_objs_order);
  94. }
  95. static const char *pair_pathtwo(void *obj)
  96. {
  97. struct diff_filepair *pair = (struct diff_filepair *)obj;
  98. return pair->two->path;
  99. }
  100. void diffcore_order(const char *orderfile)
  101. {
  102. struct diff_queue_struct *q = &diff_queued_diff;
  103. struct obj_order *o;
  104. int i;
  105. if (!q->nr)
  106. return;
  107. ALLOC_ARRAY(o, q->nr);
  108. for (i = 0; i < q->nr; i++)
  109. o[i].obj = q->queue[i];
  110. order_objects(orderfile, pair_pathtwo, o, q->nr);
  111. for (i = 0; i < q->nr; i++)
  112. q->queue[i] = o[i].obj;
  113. free(o);
  114. return;
  115. }