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.

349 lines
8.2KB

  1. #include "cache.h"
  2. #include "commit.h"
  3. #include "refs.h"
  4. #include "diff.h"
  5. #include "revision.h"
  6. #include "string-list.h"
  7. #include "reflog-walk.h"
  8. struct complete_reflogs {
  9. char *ref;
  10. const char *short_ref;
  11. struct reflog_info {
  12. struct object_id ooid, noid;
  13. char *email;
  14. timestamp_t timestamp;
  15. int tz;
  16. char *message;
  17. } *items;
  18. int nr, alloc;
  19. };
  20. static int read_one_reflog(struct object_id *ooid, struct object_id *noid,
  21. const char *email, timestamp_t timestamp, int tz,
  22. const char *message, void *cb_data)
  23. {
  24. struct complete_reflogs *array = cb_data;
  25. struct reflog_info *item;
  26. ALLOC_GROW(array->items, array->nr + 1, array->alloc);
  27. item = array->items + array->nr;
  28. oidcpy(&item->ooid, ooid);
  29. oidcpy(&item->noid, noid);
  30. item->email = xstrdup(email);
  31. item->timestamp = timestamp;
  32. item->tz = tz;
  33. item->message = xstrdup(message);
  34. array->nr++;
  35. return 0;
  36. }
  37. static void free_complete_reflog(struct complete_reflogs *array)
  38. {
  39. int i;
  40. if (!array)
  41. return;
  42. for (i = 0; i < array->nr; i++) {
  43. free(array->items[i].email);
  44. free(array->items[i].message);
  45. }
  46. free(array->items);
  47. free(array->ref);
  48. free(array);
  49. }
  50. static struct complete_reflogs *read_complete_reflog(const char *ref)
  51. {
  52. struct complete_reflogs *reflogs =
  53. xcalloc(1, sizeof(struct complete_reflogs));
  54. reflogs->ref = xstrdup(ref);
  55. for_each_reflog_ent(ref, read_one_reflog, reflogs);
  56. if (reflogs->nr == 0) {
  57. const char *name;
  58. void *name_to_free;
  59. name = name_to_free = resolve_refdup(ref, RESOLVE_REF_READING,
  60. NULL, NULL);
  61. if (name) {
  62. for_each_reflog_ent(name, read_one_reflog, reflogs);
  63. free(name_to_free);
  64. }
  65. }
  66. if (reflogs->nr == 0) {
  67. char *refname = xstrfmt("refs/%s", ref);
  68. for_each_reflog_ent(refname, read_one_reflog, reflogs);
  69. if (reflogs->nr == 0) {
  70. free(refname);
  71. refname = xstrfmt("refs/heads/%s", ref);
  72. for_each_reflog_ent(refname, read_one_reflog, reflogs);
  73. }
  74. free(refname);
  75. }
  76. return reflogs;
  77. }
  78. static int get_reflog_recno_by_time(struct complete_reflogs *array,
  79. timestamp_t timestamp)
  80. {
  81. int i;
  82. for (i = array->nr - 1; i >= 0; i--)
  83. if (timestamp >= array->items[i].timestamp)
  84. return i;
  85. return -1;
  86. }
  87. struct commit_reflog {
  88. int recno;
  89. enum selector_type {
  90. SELECTOR_NONE,
  91. SELECTOR_INDEX,
  92. SELECTOR_DATE
  93. } selector;
  94. struct complete_reflogs *reflogs;
  95. };
  96. struct reflog_walk_info {
  97. struct commit_reflog **logs;
  98. size_t nr, alloc;
  99. struct string_list complete_reflogs;
  100. struct commit_reflog *last_commit_reflog;
  101. };
  102. void init_reflog_walk(struct reflog_walk_info **info)
  103. {
  104. *info = xcalloc(1, sizeof(struct reflog_walk_info));
  105. (*info)->complete_reflogs.strdup_strings = 1;
  106. }
  107. int add_reflog_for_walk(struct reflog_walk_info *info,
  108. struct commit *commit, const char *name)
  109. {
  110. timestamp_t timestamp = 0;
  111. int recno = -1;
  112. struct string_list_item *item;
  113. struct complete_reflogs *reflogs;
  114. char *branch, *at = strchr(name, '@');
  115. struct commit_reflog *commit_reflog;
  116. enum selector_type selector = SELECTOR_NONE;
  117. if (commit->object.flags & UNINTERESTING)
  118. die("cannot walk reflogs for %s", name);
  119. branch = xstrdup(name);
  120. if (at && at[1] == '{') {
  121. char *ep;
  122. branch[at - name] = '\0';
  123. recno = strtoul(at + 2, &ep, 10);
  124. if (*ep != '}') {
  125. recno = -1;
  126. timestamp = approxidate(at + 2);
  127. selector = SELECTOR_DATE;
  128. }
  129. else
  130. selector = SELECTOR_INDEX;
  131. } else
  132. recno = 0;
  133. item = string_list_lookup(&info->complete_reflogs, branch);
  134. if (item)
  135. reflogs = item->util;
  136. else {
  137. if (*branch == '\0') {
  138. free(branch);
  139. branch = resolve_refdup("HEAD", 0, NULL, NULL);
  140. if (!branch)
  141. die("no current branch");
  142. }
  143. reflogs = read_complete_reflog(branch);
  144. if (!reflogs || reflogs->nr == 0) {
  145. struct object_id oid;
  146. char *b;
  147. int ret = dwim_log(branch, strlen(branch),
  148. &oid, &b);
  149. if (ret > 1)
  150. free(b);
  151. else if (ret == 1) {
  152. free_complete_reflog(reflogs);
  153. free(branch);
  154. branch = b;
  155. reflogs = read_complete_reflog(branch);
  156. }
  157. }
  158. if (!reflogs || reflogs->nr == 0) {
  159. free_complete_reflog(reflogs);
  160. free(branch);
  161. return -1;
  162. }
  163. string_list_insert(&info->complete_reflogs, branch)->util
  164. = reflogs;
  165. }
  166. free(branch);
  167. commit_reflog = xcalloc(1, sizeof(struct commit_reflog));
  168. if (recno < 0) {
  169. commit_reflog->recno = get_reflog_recno_by_time(reflogs, timestamp);
  170. if (commit_reflog->recno < 0) {
  171. free(commit_reflog);
  172. return -1;
  173. }
  174. } else
  175. commit_reflog->recno = reflogs->nr - recno - 1;
  176. commit_reflog->selector = selector;
  177. commit_reflog->reflogs = reflogs;
  178. ALLOC_GROW(info->logs, info->nr + 1, info->alloc);
  179. info->logs[info->nr++] = commit_reflog;
  180. return 0;
  181. }
  182. void get_reflog_selector(struct strbuf *sb,
  183. struct reflog_walk_info *reflog_info,
  184. const struct date_mode *dmode, int force_date,
  185. int shorten)
  186. {
  187. struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
  188. struct reflog_info *info;
  189. const char *printed_ref;
  190. if (!commit_reflog)
  191. return;
  192. if (shorten) {
  193. if (!commit_reflog->reflogs->short_ref)
  194. commit_reflog->reflogs->short_ref
  195. = shorten_unambiguous_ref(commit_reflog->reflogs->ref, 0);
  196. printed_ref = commit_reflog->reflogs->short_ref;
  197. } else {
  198. printed_ref = commit_reflog->reflogs->ref;
  199. }
  200. strbuf_addf(sb, "%s@{", printed_ref);
  201. if (commit_reflog->selector == SELECTOR_DATE ||
  202. (commit_reflog->selector == SELECTOR_NONE && force_date)) {
  203. info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
  204. strbuf_addstr(sb, show_date(info->timestamp, info->tz, dmode));
  205. } else {
  206. strbuf_addf(sb, "%d", commit_reflog->reflogs->nr
  207. - 2 - commit_reflog->recno);
  208. }
  209. strbuf_addch(sb, '}');
  210. }
  211. void get_reflog_message(struct strbuf *sb,
  212. struct reflog_walk_info *reflog_info)
  213. {
  214. struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
  215. struct reflog_info *info;
  216. size_t len;
  217. if (!commit_reflog)
  218. return;
  219. info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
  220. len = strlen(info->message);
  221. if (len > 0)
  222. len--; /* strip away trailing newline */
  223. strbuf_add(sb, info->message, len);
  224. }
  225. const char *get_reflog_ident(struct reflog_walk_info *reflog_info)
  226. {
  227. struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
  228. struct reflog_info *info;
  229. if (!commit_reflog)
  230. return NULL;
  231. info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
  232. return info->email;
  233. }
  234. timestamp_t get_reflog_timestamp(struct reflog_walk_info *reflog_info)
  235. {
  236. struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
  237. struct reflog_info *info;
  238. if (!commit_reflog)
  239. return 0;
  240. info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
  241. return info->timestamp;
  242. }
  243. void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,
  244. const struct date_mode *dmode, int force_date)
  245. {
  246. if (reflog_info && reflog_info->last_commit_reflog) {
  247. struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
  248. struct reflog_info *info;
  249. struct strbuf selector = STRBUF_INIT;
  250. info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
  251. get_reflog_selector(&selector, reflog_info, dmode, force_date, 0);
  252. if (oneline) {
  253. printf("%s: %s", selector.buf, info->message);
  254. }
  255. else {
  256. printf("Reflog: %s (%s)\nReflog message: %s",
  257. selector.buf, info->email, info->message);
  258. }
  259. strbuf_release(&selector);
  260. }
  261. }
  262. int reflog_walk_empty(struct reflog_walk_info *info)
  263. {
  264. return !info || !info->nr;
  265. }
  266. static struct commit *next_reflog_commit(struct commit_reflog *log)
  267. {
  268. for (; log->recno >= 0; log->recno--) {
  269. struct reflog_info *entry = &log->reflogs->items[log->recno];
  270. struct object *obj = parse_object(the_repository,
  271. &entry->noid);
  272. if (obj && obj->type == OBJ_COMMIT)
  273. return (struct commit *)obj;
  274. }
  275. return NULL;
  276. }
  277. static timestamp_t log_timestamp(struct commit_reflog *log)
  278. {
  279. return log->reflogs->items[log->recno].timestamp;
  280. }
  281. struct commit *next_reflog_entry(struct reflog_walk_info *walk)
  282. {
  283. struct commit_reflog *best = NULL;
  284. struct commit *best_commit = NULL;
  285. size_t i;
  286. for (i = 0; i < walk->nr; i++) {
  287. struct commit_reflog *log = walk->logs[i];
  288. struct commit *commit = next_reflog_commit(log);
  289. if (!commit)
  290. continue;
  291. if (!best || log_timestamp(log) > log_timestamp(best)) {
  292. best = log;
  293. best_commit = commit;
  294. }
  295. }
  296. if (best) {
  297. best->recno--;
  298. walk->last_commit_reflog = best;
  299. return best_commit;
  300. }
  301. return NULL;
  302. }