{"thread":{"id":"6337","subject":"[PATCH] Teach the revision walker to walk by reflogs with --walk-reflogs","startedAt":"2007-01-11T10:47:48Z","lastAt":"2007-01-11T10:57:13Z","messageCount":2,"participants":["Johannes Schindelin"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"31461","messageId":"Pine.LNX.4.63.0701111147070.22628@wbgn013.biozentrum.uni-wuerzburg.de","threadId":"6337","inReplyTo":null,"subject":"[PATCH] Teach the revision walker to walk by reflogs with --walk-reflogs","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2007-01-11T10:47:48Z","receivedAt":"2007-01-11T10:47:48Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"\nWhen called with \"--walk-reflogs\", as long as there are reflogs\navailable, the walker will take this information into account, rather\nthan the parent information in the commit object.\n\nSigned-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>\n---\n\n\tSecond take. No longer fall back to commit parent traversal when\n\tasked to traverse reflogs.\n\n Makefile      |    2 +-\n log-tree.c    |    3 +\n reflog-walk.c |  233 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n reflog-walk.h |   11 +++\n revision.c    |   11 +++\n revision.h    |    2 +\n 6 files changed, 261 insertions(+), 1 deletions(-)\n\ndiff --git a/Makefile b/Makefile\nindex 719ef12..ffffcb5 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -257,7 +257,7 @@ LIB_OBJS = \\\n \tinterpolate.o \\\n \tlockfile.o \\\n \tobject.o pack-check.o patch-delta.o path.o pkt-line.o sideband.o \\\n-\treachable.o \\\n+\treachable.o reflog-walk.o \\\n \tquote.o read-cache.o refs.o run-command.o dir.o object-refs.o \\\n \tserver-info.o setup.o sha1_file.o sha1_name.o strbuf.o \\\n \ttag.o tree.o usage.o config.o environment.o ctype.o copy.o \\\ndiff --git a/log-tree.c b/log-tree.c\nindex 35be33a..f043ad3 100644\n--- a/log-tree.c\n+++ b/log-tree.c\n@@ -2,6 +2,7 @@\n #include \"diff.h\"\n #include \"commit.h\"\n #include \"log-tree.h\"\n+#include \"reflog-walk.h\"\n \n static void show_parents(struct commit *commit, int abbrev)\n {\n@@ -223,6 +224,8 @@ void show_log(struct rev_info *opt, const char *sep)\n \t\tprintf(\"%s\",\n \t\t       diff_get_color(opt->diffopt.color_diff, DIFF_RESET));\n \t\tputchar(opt->commit_format == CMIT_FMT_ONELINE ? ' ' : '\\n');\n+\t\tif (opt->reflog_info)\n+\t\t\tshow_reflog_message(opt->reflog_info);\n \t}\n \n \t/*\ndiff --git a/reflog-walk.c b/reflog-walk.c\nnew file mode 100644\nindex 0000000..658cba9\n--- /dev/null\n+++ b/reflog-walk.c\n@@ -0,0 +1,233 @@\n+#include \"cache.h\"\n+#include \"commit.h\"\n+#include \"refs.h\"\n+#include \"diff.h\"\n+#include \"revision.h\"\n+#include \"path-list.h\"\n+\n+struct complete_reflogs {\n+\tchar *ref;\n+\tstruct reflog_info {\n+\t\tunsigned char osha1[20], nsha1[20];\n+\t\tchar *email;\n+\t\tunsigned long timestamp;\n+\t\tint tz;\n+\t\tchar *message;\n+\t} *items;\n+\tint nr, alloc;\n+};\n+\n+static int read_one_reflog(unsigned char *osha1, unsigned char *nsha1,\n+\t\tconst char *email, unsigned long timestamp, int tz,\n+\t\tconst char *message, void *cb_data)\n+{\n+\tstruct complete_reflogs *array = cb_data;\n+\tstruct reflog_info *item;\n+\n+\tif (array->nr >= array->alloc) {\n+\t\tarray->alloc = alloc_nr(array->nr + 1);\n+\t\tarray->items = xrealloc(array->items, array->alloc *\n+\t\t\tsizeof(struct reflog_info));\n+\t}\n+\titem = array->items + array->nr;\n+\tmemcpy(item->osha1, osha1, 20);\n+\tmemcpy(item->nsha1, nsha1, 20);\n+\titem->email = xstrdup(email);\n+\titem->timestamp = timestamp;\n+\titem->tz = tz;\n+\titem->message = xstrdup(message);\n+\tarray->nr++;\n+\treturn 0;\n+}\n+\n+static struct complete_reflogs *read_complete_reflog(const char *ref)\n+{\n+\tstruct complete_reflogs *reflogs =\n+\t\txcalloc(sizeof(struct complete_reflogs), 1);\n+\treflogs->ref = xstrdup(ref);\n+\tfor_each_reflog_ent(ref, read_one_reflog, reflogs);\n+\tif (reflogs->nr == 0) {\n+\t\tunsigned char sha1[20];\n+\t\tconst char *name = resolve_ref(ref, sha1, 1, NULL);\n+\t\tif (name)\n+\t\t\tfor_each_reflog_ent(name, read_one_reflog, reflogs);\n+\t}\n+\tif (reflogs->nr == 0) {\n+\t\tint len = strlen(ref);\n+\t\tchar *refname = xmalloc(len + 12);\n+\t\tsprintf(refname, \"refs/%s\", ref);\n+\t\tfor_each_reflog_ent(refname, read_one_reflog, reflogs);\n+\t\tif (reflogs->nr == 0) {\n+\t\t\tsprintf(refname, \"refs/heads/%s\", ref);\n+\t\t\tfor_each_reflog_ent(refname, read_one_reflog, reflogs);\n+\t\t}\n+\t\tfree(refname);\n+\t}\n+\treturn reflogs;\n+}\n+\n+static int get_reflog_recno_by_time(struct complete_reflogs *array,\n+\tunsigned long timestamp)\n+{\n+\tint i;\n+\tfor (i = array->nr - 1; i >= 0; i++)\n+\t\tif (timestamp >= array->items[i].timestamp)\n+\t\t\treturn i;\n+\treturn -1;\n+}\n+\n+struct commit_info_lifo {\n+\tstruct commit_info {\n+\t\tstruct commit *commit;\n+\t\tvoid *util;\n+\t} *items;\n+\tint nr, alloc;\n+};\n+\n+static struct commit_info *get_commit_info(struct commit *commit,\n+\t\tstruct commit_info_lifo *lifo, int pop)\n+{\n+\tint i;\n+\tfor (i = 0; i < lifo->nr; i++)\n+\t\tif (lifo->items[i].commit == commit) {\n+\t\t\tstruct commit_info *result = &lifo->items[i];\n+\t\t\tif (pop) {\n+\t\t\t\tif (i + 1 < lifo->nr)\n+\t\t\t\t\tmemmove(lifo->items + i,\n+\t\t\t\t\t\tlifo->items + i + 1,\n+\t\t\t\t\t\t(lifo->nr - i) *\n+\t\t\t\t\t\tsizeof(struct commit_info));\n+\t\t\t\tlifo->nr--;\n+\t\t\t}\n+\t\t\treturn result;\n+\t\t}\n+\treturn NULL;\n+}\n+\n+static void add_commit_info(struct commit *commit, void *util,\n+\t\tstruct commit_info_lifo *lifo)\n+{\n+\tstruct commit_info *info;\n+\tif (lifo->nr >= lifo->alloc) {\n+\t\tlifo->alloc = alloc_nr(lifo->nr + 1);\n+\t\tlifo->items = xrealloc(lifo->items,\n+\t\t\tlifo->alloc * sizeof(struct commit_info));\n+\t}\n+\tinfo = lifo->items + lifo->nr;\n+\tinfo->commit = commit;\n+\tinfo->util = util;\n+\tlifo->nr++;\n+}\n+\n+struct commit_reflog {\n+\tint flag, recno;\n+\tstruct complete_reflogs *reflogs;\n+};\n+\n+struct reflog_walk_info {\n+\tstruct commit_info_lifo reflogs;\n+\tstruct path_list complete_reflogs;\n+\tstruct commit_reflog *last_commit_reflog;\n+};\n+\n+void init_reflog_walk(struct reflog_walk_info** info)\n+{\n+\t*info = xcalloc(sizeof(struct reflog_walk_info), 1);\n+}\n+\n+void add_reflog_for_walk(struct reflog_walk_info *info,\n+\t\tstruct commit *commit, const char *name)\n+{\n+\tunsigned long timestamp = 0;\n+\tint recno = -1;\n+\tstruct path_list_item *item;\n+\tstruct complete_reflogs *reflogs;\n+\tchar *branch, *at = strchr(name, '@');\n+\tstruct commit_reflog *commit_reflog;\n+\n+\tbranch = xstrdup(name);\n+\tif (at && at[1] == '{') {\n+\t\tchar *ep;\n+\t\tbranch[at - name] = '\\0';\n+\t\trecno = strtoul(at + 2, &ep, 10);\n+\t\tif (*ep != '}') {\n+\t\t\trecno = -1;\n+\t\t\ttimestamp = approxidate(at + 2);\n+\t\t}\n+\t} else\n+\t\trecno = 0;\n+\n+\titem = path_list_lookup(branch, &info->complete_reflogs);\n+\tif (item)\n+\t\treflogs = item->util;\n+\telse {\n+\t\treflogs = read_complete_reflog(branch);\n+\t\tif (!reflogs || reflogs->nr == 0)\n+\t\t\tdie(\"No reflogs found for '%s'\", branch);\n+\t\tpath_list_insert(branch, &info->complete_reflogs)->util\n+\t\t\t= reflogs;\n+\t}\n+\n+\tcommit_reflog = xcalloc(sizeof(struct commit_reflog), 1);\n+\tif (recno < 0) {\n+\t\tcommit_reflog->flag = 1;\n+\t\tcommit_reflog->recno = get_reflog_recno_by_time(reflogs, timestamp);\n+\t\tif (commit_reflog->recno < 0) {\n+\t\t\tfree(branch);\n+\t\t\tfree(commit_reflog);\n+\t\t\treturn;\n+\t\t}\n+\t} else\n+\t\tcommit_reflog->recno = reflogs->nr - recno - 1;\n+\tcommit_reflog->reflogs = reflogs;\n+\n+\tadd_commit_info(commit, commit_reflog, &info->reflogs);\n+}\n+\n+void fake_reflog_parent(struct reflog_walk_info *info, struct commit *commit)\n+{\n+\tstruct commit_info *commit_info =\n+\t\tget_commit_info(commit, &info->reflogs, 0);\n+\tstruct commit_reflog *commit_reflog;\n+\tstruct reflog_info *reflog;\n+\n+\tinfo->last_commit_reflog = NULL;\n+\tif (!commit_info)\n+\t\treturn;\n+\n+\tcommit_reflog = commit_info->util;\n+\tif (commit_reflog->recno < 0) {\n+\t\tcommit->parents = NULL;\n+\t\treturn;\n+\t}\n+\n+\treflog = &commit_reflog->reflogs->items[commit_reflog->recno];\n+\tinfo->last_commit_reflog = commit_reflog;\n+\tcommit_info->commit = (struct commit *)parse_object(reflog->osha1);\n+\tif (!commit_info->commit)\n+\t\treturn;\n+\n+\tcommit->parents = xcalloc(sizeof(struct commit_list), 1);\n+\tcommit->parents->item = commit_info->commit;\n+\tcommit->object.flags &= ~(ADDED | SEEN | SHOWN);\n+\tcommit_reflog->recno--;\n+}\n+\n+void show_reflog_message(struct reflog_walk_info* info)\n+{\n+\tif (info && info->last_commit_reflog) {\n+\t\tstruct commit_reflog *commit_reflog = info->last_commit_reflog;\n+\t\tstruct reflog_info *info;\n+\n+\t\tprintf(\"Reflog: %s@{\", commit_reflog->reflogs->ref);\n+\t\tinfo = &commit_reflog->reflogs->items[commit_reflog->recno + 1];\n+\t\tif (commit_reflog->flag)\n+\t\t\tprintf(\"%s\", show_rfc2822_date(info->timestamp,\n+\t\t\t\t\t\tinfo->tz));\n+\t\telse\n+\t\t\tprintf(\"%d\", commit_reflog->reflogs->nr\n+\t\t\t\t\t- 2 - commit_reflog->recno);\n+\t\tprintf(\"} (%s)\\nReflog message: %s\",\n+\t\t\tinfo->email, info->message);\n+\t}\n+}\ndiff --git a/reflog-walk.h b/reflog-walk.h\nnew file mode 100644\nindex 0000000..787996b\n--- /dev/null\n+++ b/reflog-walk.h\n@@ -0,0 +1,11 @@\n+#ifndef REFLOG_WALK_H\n+#define REFLOG_WALK_H\n+\n+extern void init_reflog_walk(struct reflog_walk_info** info);\n+extern void add_reflog_for_walk(struct reflog_walk_info *info,\n+\t\tstruct commit *commit, const char *name);\n+extern void fake_reflog_parent(struct reflog_walk_info *info,\n+\t\tstruct commit *commit);\n+extern void show_reflog_message(struct reflog_walk_info* info);\n+\n+#endif\ndiff --git a/revision.c b/revision.c\nindex 1e3b29a..3d0f061 100644\n--- a/revision.c\n+++ b/revision.c\n@@ -7,6 +7,7 @@\n #include \"refs.h\"\n #include \"revision.h\"\n #include \"grep.h\"\n+#include \"reflog-walk.h\"\n \n static char *path_name(struct name_path *path, const char *name)\n {\n@@ -116,6 +117,9 @@ void mark_parents_uninteresting(struct commit *commit)\n void add_pending_object(struct rev_info *revs, struct object *obj, const char *name)\n {\n \tadd_object_array(obj, name, &revs->pending);\n+\tif (revs->reflog_info && obj->type == OBJ_COMMIT)\n+\t\tadd_reflog_for_walk(revs->reflog_info,\n+\t\t\t\t(struct commit *)obj, name);\n }\n \n static struct object *get_reference(struct rev_info *revs, const char *name, const unsigned char *sha1, unsigned int flags)\n@@ -864,6 +868,10 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, const ch\n \t\t\t\thandle_reflog(revs, flags);\n \t\t\t\tcontinue;\n \t\t\t}\n+\t\t\tif (!strcmp(arg, \"--walk-reflogs\")) {\n+\t\t\t\tinit_reflog_walk(&revs->reflog_info);\n+\t\t\t\tcontinue;\n+\t\t\t}\n \t\t\tif (!strcmp(arg, \"--not\")) {\n \t\t\t\tflags ^= UNINTERESTING;\n \t\t\t\tcontinue;\n@@ -1208,6 +1216,9 @@ static struct commit *get_revision_1(struct rev_info *revs)\n \t\trevs->commits = entry->next;\n \t\tfree(entry);\n \n+\t\tif (revs->reflog_info)\n+\t\t\tfake_reflog_parent(revs->reflog_info, commit);\n+\n \t\t/*\n \t\t * If we haven't done the list limiting, we need to look at\n \t\t * the parents here. We also need to do the date-based limiting\ndiff --git a/revision.h b/revision.h\nindex 8f7907d..d93481f 100644\n--- a/revision.h\n+++ b/revision.h\n@@ -89,6 +89,8 @@ struct rev_info {\n \n \ttopo_sort_set_fn_t topo_setter;\n \ttopo_sort_get_fn_t topo_getter;\n+\n+\tstruct reflog_walk_info *reflog_info;\n };\n \n #define REV_TREE_SAME\t\t0\n-- \n1.4.4.4.g49ef2-dirty\n"},{"id":"31463","messageId":"Pine.LNX.4.63.0701111156510.22628@wbgn013.biozentrum.uni-wuerzburg.de","threadId":"6337","inReplyTo":"Pine.LNX.4.63.0701111147070.22628@wbgn013.biozentrum.uni-wuerzburg.de","subject":"Re: [PATCH] Teach the revision walker to walk by reflogs with --walk-reflogs","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2007-01-11T10:57:13Z","receivedAt":"2007-01-11T10:57:13Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"... and an --amend:\n\n---\n\n reflog-walk.c |    6 ++++--\n 1 files changed, 4 insertions(+), 2 deletions(-)\n\ndiff --git a/reflog-walk.c b/reflog-walk.c\nindex 658cba9..d4b49c7 100644\n--- a/reflog-walk.c\n+++ b/reflog-walk.c\n@@ -203,14 +203,16 @@ void fake_reflog_parent(struct reflog_walk_info *info, struct commit *commit)\n \n \treflog = &commit_reflog->reflogs->items[commit_reflog->recno];\n \tinfo->last_commit_reflog = commit_reflog;\n+\tcommit_reflog->recno--;\n \tcommit_info->commit = (struct commit *)parse_object(reflog->osha1);\n-\tif (!commit_info->commit)\n+\tif (!commit_info->commit) {\n+\t\tcommit->parents = NULL;\n \t\treturn;\n+\t}\n \n \tcommit->parents = xcalloc(sizeof(struct commit_list), 1);\n \tcommit->parents->item = commit_info->commit;\n \tcommit->object.flags &= ~(ADDED | SEEN | SHOWN);\n-\tcommit_reflog->recno--;\n }\n \n void show_reflog_message(struct reflog_walk_info* info)\n"}]}