2023-02-24 08:09:24 +08:00
|
|
|
#include "git-compat-util.h"
|
|
|
|
#include "alloc.h"
|
2007-01-11 18:47:48 +08:00
|
|
|
#include "commit.h"
|
|
|
|
#include "refs.h"
|
|
|
|
#include "diff.h"
|
|
|
|
#include "revision.h"
|
2008-07-22 02:03:49 +08:00
|
|
|
#include "string-list.h"
|
2007-01-20 16:47:34 +08:00
|
|
|
#include "reflog-walk.h"
|
2007-01-11 18:47:48 +08:00
|
|
|
|
|
|
|
struct complete_reflogs {
|
|
|
|
char *ref;
|
2022-04-14 04:01:52 +08:00
|
|
|
char *short_ref;
|
2007-01-11 18:47:48 +08:00
|
|
|
struct reflog_info {
|
2017-02-22 07:47:31 +08:00
|
|
|
struct object_id ooid, noid;
|
2007-01-11 18:47:48 +08:00
|
|
|
char *email;
|
2017-04-27 03:29:31 +08:00
|
|
|
timestamp_t timestamp;
|
2007-01-11 18:47:48 +08:00
|
|
|
int tz;
|
|
|
|
char *message;
|
|
|
|
} *items;
|
|
|
|
int nr, alloc;
|
|
|
|
};
|
|
|
|
|
2017-02-22 07:47:32 +08:00
|
|
|
static int read_one_reflog(struct object_id *ooid, struct object_id *noid,
|
2017-04-27 03:29:31 +08:00
|
|
|
const char *email, timestamp_t timestamp, int tz,
|
2007-01-11 18:47:48 +08:00
|
|
|
const char *message, void *cb_data)
|
|
|
|
{
|
|
|
|
struct complete_reflogs *array = cb_data;
|
|
|
|
struct reflog_info *item;
|
|
|
|
|
2014-03-04 06:31:57 +08:00
|
|
|
ALLOC_GROW(array->items, array->nr + 1, array->alloc);
|
2007-01-11 18:47:48 +08:00
|
|
|
item = array->items + array->nr;
|
2017-02-22 07:47:32 +08:00
|
|
|
oidcpy(&item->ooid, ooid);
|
|
|
|
oidcpy(&item->noid, noid);
|
2007-01-11 18:47:48 +08:00
|
|
|
item->email = xstrdup(email);
|
|
|
|
item->timestamp = timestamp;
|
|
|
|
item->tz = tz;
|
|
|
|
item->message = xstrdup(message);
|
|
|
|
array->nr++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-07 16:43:16 +08:00
|
|
|
static void free_complete_reflog(struct complete_reflogs *array)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!array)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < array->nr; i++) {
|
|
|
|
free(array->items[i].email);
|
|
|
|
free(array->items[i].message);
|
|
|
|
}
|
|
|
|
free(array->items);
|
|
|
|
free(array->ref);
|
2022-04-14 04:01:52 +08:00
|
|
|
free(array->short_ref);
|
2017-07-07 16:43:16 +08:00
|
|
|
free(array);
|
|
|
|
}
|
|
|
|
|
2022-10-18 09:05:32 +08:00
|
|
|
static void complete_reflogs_clear(void *util, const char *str UNUSED)
|
2022-04-14 04:01:52 +08:00
|
|
|
{
|
|
|
|
struct complete_reflogs *array = util;
|
|
|
|
free_complete_reflog(array);
|
|
|
|
}
|
|
|
|
|
2007-01-11 18:47:48 +08:00
|
|
|
static struct complete_reflogs *read_complete_reflog(const char *ref)
|
|
|
|
{
|
|
|
|
struct complete_reflogs *reflogs =
|
2014-05-26 23:33:54 +08:00
|
|
|
xcalloc(1, sizeof(struct complete_reflogs));
|
2007-01-11 18:47:48 +08:00
|
|
|
reflogs->ref = xstrdup(ref);
|
|
|
|
for_each_reflog_ent(ref, read_one_reflog, reflogs);
|
|
|
|
if (reflogs->nr == 0) {
|
2011-12-13 22:17:48 +08:00
|
|
|
const char *name;
|
|
|
|
void *name_to_free;
|
2014-07-16 03:59:36 +08:00
|
|
|
name = name_to_free = resolve_refdup(ref, RESOLVE_REF_READING,
|
2017-10-01 15:29:03 +08:00
|
|
|
NULL, NULL);
|
2011-11-13 18:22:15 +08:00
|
|
|
if (name) {
|
2007-01-11 18:47:48 +08:00
|
|
|
for_each_reflog_ent(name, read_one_reflog, reflogs);
|
2011-12-13 22:17:48 +08:00
|
|
|
free(name_to_free);
|
2011-11-13 18:22:15 +08:00
|
|
|
}
|
2007-01-11 18:47:48 +08:00
|
|
|
}
|
|
|
|
if (reflogs->nr == 0) {
|
2015-09-25 05:07:03 +08:00
|
|
|
char *refname = xstrfmt("refs/%s", ref);
|
2007-01-11 18:47:48 +08:00
|
|
|
for_each_reflog_ent(refname, read_one_reflog, reflogs);
|
|
|
|
if (reflogs->nr == 0) {
|
2015-09-25 05:07:03 +08:00
|
|
|
free(refname);
|
|
|
|
refname = xstrfmt("refs/heads/%s", ref);
|
2007-01-11 18:47:48 +08:00
|
|
|
for_each_reflog_ent(refname, read_one_reflog, reflogs);
|
|
|
|
}
|
|
|
|
free(refname);
|
|
|
|
}
|
|
|
|
return reflogs;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_reflog_recno_by_time(struct complete_reflogs *array,
|
2017-04-27 03:29:31 +08:00
|
|
|
timestamp_t timestamp)
|
2007-01-11 18:47:48 +08:00
|
|
|
{
|
|
|
|
int i;
|
2007-01-20 17:49:15 +08:00
|
|
|
for (i = array->nr - 1; i >= 0; i--)
|
2007-01-11 18:47:48 +08:00
|
|
|
if (timestamp >= array->items[i].timestamp)
|
|
|
|
return i;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct commit_reflog {
|
2012-05-04 13:26:26 +08:00
|
|
|
int recno;
|
|
|
|
enum selector_type {
|
|
|
|
SELECTOR_NONE,
|
|
|
|
SELECTOR_INDEX,
|
|
|
|
SELECTOR_DATE
|
|
|
|
} selector;
|
2007-01-11 18:47:48 +08:00
|
|
|
struct complete_reflogs *reflogs;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct reflog_walk_info {
|
reflog-walk: stop using fake parents
The reflog-walk system works by putting a ref's tip into the
pending queue, and then "traversing" the reflog by
pretending that the parent of each commit is the previous
reflog entry.
This causes a number of user-visible oddities, as documented
in t1414 (and the commit message which introduced it). We
can fix all of them in one go by replacing the fake-reflog
system with a much simpler one: just keeping a list of
reflogs to show, and walking through them entry by entry.
The implementation is fairly straight-forward, but there are
a few items to note:
1. We obviously must skip calling add_parents_to_list()
when we are traversing reflogs, since we do not want to
walk the original parents at all. As a result, we must call
try_to_simplify_commit() ourselves.
There are other parts of add_parents_to_list() we skip,
as well, but none of them should matter for a reflog
traversal:
- We do not allow UNINTERESTING commits, nor
symmetric ranges (and we bail when these are used
with "-g").
- Using --source makes no sense, since we aren't
traversing. The reflog selector shows the same
information with more detail.
- Using --first-parent is still sensible, since you
may want to see the first-parent diff for each
entry. But since we're not traversing, we don't
need to cull the parent list here.
2. Since we now just walk the reflog entries themselves,
rather than starting with the ref tip, we now look at
the "new" field of each entry rather than the "old"
(i.e., we are showing entries, not faking parents).
This removes all of the tricky logic around skipping
past root commits.
But note that we have no way to show an entry with the
null sha1 in its "new" field (because such a commit
obviously does not exist). Normally this would not
happen, since we delete reflogs along with refs, but
there is one special case. When we rename the currently
checked out branch, we write two reflog entries into
the HEAD log: one where the commit goes away, and
another where it comes back.
Prior to this commit, we show both entries with
identical reflog messages. After this commit, we show
only the "comes back" entry. See the update in t3200
which demonstrates this.
Arguably either is fine, as the whole double-entry
thing is a bit hacky in the first place. And until a
recent fix, we truncated the traversal in such a case
anyway, which was _definitely_ wrong.
3. We show individual reflogs in order, but choose which
reflog to show at each stage based on which has the
most recent timestamp. This interleaves the output
from multiple reflogs based on date order, which is
probably what you'd want with limiting like "-n 30".
Note that the implementation aims for simplicity. It
does a linear walk over the reflog queue for each
commit it pulls, which may perform badly if you
interleave an enormous number of reflogs. That seems
like an unlikely use case; if we did want to handle it,
we could probably keep a priority queue of reflogs,
ordered by the timestamp of their current tip entry.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2017-07-07 17:14:07 +08:00
|
|
|
struct commit_reflog **logs;
|
|
|
|
size_t nr, alloc;
|
2008-07-22 02:03:49 +08:00
|
|
|
struct string_list complete_reflogs;
|
2007-01-11 18:47:48 +08:00
|
|
|
struct commit_reflog *last_commit_reflog;
|
|
|
|
};
|
|
|
|
|
2014-09-01 04:11:31 +08:00
|
|
|
void init_reflog_walk(struct reflog_walk_info **info)
|
2007-01-11 18:47:48 +08:00
|
|
|
{
|
2021-03-14 00:17:22 +08:00
|
|
|
CALLOC_ARRAY(*info, 1);
|
reflog-walk: duplicate strings in complete_reflogs list
As part of the add_reflog_to_walk() function, we keep a
string_list mapping refnames to their reflog contents. This
serves as a cache so that accessing the same reflog twice
requires only a single copy of the log in memory.
The string_list is initialized via xcalloc, meaning its
strdup_strings field is set to 0. But after inserting a
string into the list, we unconditionally call free() on the
string, leaving the list pointing to freed memory. If
another reflog is added (e.g., "git log -g HEAD HEAD"), then
the second one may have unpredictable results.
The extra free was added by 5026b47175 (add_reflog_for_walk:
avoid memory leak, 2017-05-04). Though if you look
carefully, you can see that the code was buggy even before
then. If we tried to read the reflogs by time but came up
with no entries, we exited with an error, freeing the string
in that code path. So the bug was harder to trigger, but
still there.
We can fix it by just asking the string list to make a copy
of the string. Technically we could fix the problem by not
calling free() on our string (and just handing over
ownership to the string list), but there are enough
conditionals that it's quite hard to figure out which code
paths need the free and which do not. Simpler is better
here.
The new test reliably shows the problem when run with
--valgrind or ASAN.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2017-07-07 16:39:50 +08:00
|
|
|
(*info)->complete_reflogs.strdup_strings = 1;
|
2007-01-11 18:47:48 +08:00
|
|
|
}
|
|
|
|
|
2022-04-14 04:01:52 +08:00
|
|
|
void reflog_walk_info_release(struct reflog_walk_info *info)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (!info)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < info->nr; i++)
|
|
|
|
free(info->logs[i]);
|
|
|
|
string_list_clear_func(&info->complete_reflogs,
|
|
|
|
complete_reflogs_clear);
|
|
|
|
free(info->logs);
|
|
|
|
free(info);
|
|
|
|
}
|
|
|
|
|
2007-07-24 07:39:50 +08:00
|
|
|
int add_reflog_for_walk(struct reflog_walk_info *info,
|
2007-01-11 18:47:48 +08:00
|
|
|
struct commit *commit, const char *name)
|
|
|
|
{
|
2017-04-27 03:29:31 +08:00
|
|
|
timestamp_t timestamp = 0;
|
2007-01-11 18:47:48 +08:00
|
|
|
int recno = -1;
|
2008-07-22 02:03:49 +08:00
|
|
|
struct string_list_item *item;
|
2007-01-11 18:47:48 +08:00
|
|
|
struct complete_reflogs *reflogs;
|
|
|
|
char *branch, *at = strchr(name, '@');
|
|
|
|
struct commit_reflog *commit_reflog;
|
2012-05-04 13:26:26 +08:00
|
|
|
enum selector_type selector = SELECTOR_NONE;
|
2007-01-11 18:47:48 +08:00
|
|
|
|
2007-01-20 10:28:19 +08:00
|
|
|
if (commit->object.flags & UNINTERESTING)
|
2018-07-21 15:49:19 +08:00
|
|
|
die("cannot walk reflogs for %s", name);
|
2007-01-20 10:28:19 +08:00
|
|
|
|
2007-01-11 18:47:48 +08:00
|
|
|
branch = xstrdup(name);
|
|
|
|
if (at && at[1] == '{') {
|
|
|
|
char *ep;
|
|
|
|
branch[at - name] = '\0';
|
|
|
|
recno = strtoul(at + 2, &ep, 10);
|
|
|
|
if (*ep != '}') {
|
|
|
|
recno = -1;
|
|
|
|
timestamp = approxidate(at + 2);
|
2012-05-04 13:26:26 +08:00
|
|
|
selector = SELECTOR_DATE;
|
2007-01-11 18:47:48 +08:00
|
|
|
}
|
2012-05-04 13:26:26 +08:00
|
|
|
else
|
|
|
|
selector = SELECTOR_INDEX;
|
2007-01-11 18:47:48 +08:00
|
|
|
} else
|
|
|
|
recno = 0;
|
|
|
|
|
2010-06-26 07:41:37 +08:00
|
|
|
item = string_list_lookup(&info->complete_reflogs, branch);
|
2007-01-11 18:47:48 +08:00
|
|
|
if (item)
|
|
|
|
reflogs = item->util;
|
|
|
|
else {
|
2007-02-02 07:07:24 +08:00
|
|
|
if (*branch == '\0') {
|
|
|
|
free(branch);
|
2017-10-01 15:29:03 +08:00
|
|
|
branch = resolve_refdup("HEAD", 0, NULL, NULL);
|
2011-12-13 22:17:48 +08:00
|
|
|
if (!branch)
|
2018-07-21 15:49:19 +08:00
|
|
|
die("no current branch");
|
2011-12-13 22:17:48 +08:00
|
|
|
|
2007-02-02 07:07:24 +08:00
|
|
|
}
|
2007-01-11 18:47:48 +08:00
|
|
|
reflogs = read_complete_reflog(branch);
|
2007-02-09 08:28:23 +08:00
|
|
|
if (!reflogs || reflogs->nr == 0) {
|
|
|
|
char *b;
|
2017-05-04 21:58:42 +08:00
|
|
|
int ret = dwim_log(branch, strlen(branch),
|
2021-08-23 19:36:08 +08:00
|
|
|
NULL, &b);
|
2017-05-04 21:58:42 +08:00
|
|
|
if (ret > 1)
|
|
|
|
free(b);
|
|
|
|
else if (ret == 1) {
|
2017-07-07 16:43:16 +08:00
|
|
|
free_complete_reflog(reflogs);
|
2007-02-09 08:28:23 +08:00
|
|
|
free(branch);
|
|
|
|
branch = b;
|
|
|
|
reflogs = read_complete_reflog(branch);
|
|
|
|
}
|
|
|
|
}
|
2017-05-04 21:58:42 +08:00
|
|
|
if (!reflogs || reflogs->nr == 0) {
|
2017-07-07 16:43:16 +08:00
|
|
|
free_complete_reflog(reflogs);
|
2017-05-04 21:58:42 +08:00
|
|
|
free(branch);
|
2007-07-24 07:39:50 +08:00
|
|
|
return -1;
|
2017-05-04 21:58:42 +08:00
|
|
|
}
|
2010-06-26 07:41:35 +08:00
|
|
|
string_list_insert(&info->complete_reflogs, branch)->util
|
2007-01-11 18:47:48 +08:00
|
|
|
= reflogs;
|
|
|
|
}
|
2017-05-04 21:58:42 +08:00
|
|
|
free(branch);
|
2007-01-11 18:47:48 +08:00
|
|
|
|
2021-03-14 00:17:22 +08:00
|
|
|
CALLOC_ARRAY(commit_reflog, 1);
|
2007-01-11 18:47:48 +08:00
|
|
|
if (recno < 0) {
|
|
|
|
commit_reflog->recno = get_reflog_recno_by_time(reflogs, timestamp);
|
|
|
|
if (commit_reflog->recno < 0) {
|
|
|
|
free(commit_reflog);
|
2007-07-24 07:39:50 +08:00
|
|
|
return -1;
|
2007-01-11 18:47:48 +08:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
commit_reflog->recno = reflogs->nr - recno - 1;
|
2012-05-04 13:26:26 +08:00
|
|
|
commit_reflog->selector = selector;
|
2007-01-11 18:47:48 +08:00
|
|
|
commit_reflog->reflogs = reflogs;
|
|
|
|
|
reflog-walk: stop using fake parents
The reflog-walk system works by putting a ref's tip into the
pending queue, and then "traversing" the reflog by
pretending that the parent of each commit is the previous
reflog entry.
This causes a number of user-visible oddities, as documented
in t1414 (and the commit message which introduced it). We
can fix all of them in one go by replacing the fake-reflog
system with a much simpler one: just keeping a list of
reflogs to show, and walking through them entry by entry.
The implementation is fairly straight-forward, but there are
a few items to note:
1. We obviously must skip calling add_parents_to_list()
when we are traversing reflogs, since we do not want to
walk the original parents at all. As a result, we must call
try_to_simplify_commit() ourselves.
There are other parts of add_parents_to_list() we skip,
as well, but none of them should matter for a reflog
traversal:
- We do not allow UNINTERESTING commits, nor
symmetric ranges (and we bail when these are used
with "-g").
- Using --source makes no sense, since we aren't
traversing. The reflog selector shows the same
information with more detail.
- Using --first-parent is still sensible, since you
may want to see the first-parent diff for each
entry. But since we're not traversing, we don't
need to cull the parent list here.
2. Since we now just walk the reflog entries themselves,
rather than starting with the ref tip, we now look at
the "new" field of each entry rather than the "old"
(i.e., we are showing entries, not faking parents).
This removes all of the tricky logic around skipping
past root commits.
But note that we have no way to show an entry with the
null sha1 in its "new" field (because such a commit
obviously does not exist). Normally this would not
happen, since we delete reflogs along with refs, but
there is one special case. When we rename the currently
checked out branch, we write two reflog entries into
the HEAD log: one where the commit goes away, and
another where it comes back.
Prior to this commit, we show both entries with
identical reflog messages. After this commit, we show
only the "comes back" entry. See the update in t3200
which demonstrates this.
Arguably either is fine, as the whole double-entry
thing is a bit hacky in the first place. And until a
recent fix, we truncated the traversal in such a case
anyway, which was _definitely_ wrong.
3. We show individual reflogs in order, but choose which
reflog to show at each stage based on which has the
most recent timestamp. This interleaves the output
from multiple reflogs based on date order, which is
probably what you'd want with limiting like "-n 30".
Note that the implementation aims for simplicity. It
does a linear walk over the reflog queue for each
commit it pulls, which may perform badly if you
interleave an enormous number of reflogs. That seems
like an unlikely use case; if we did want to handle it,
we could probably keep a priority queue of reflogs,
ordered by the timestamp of their current tip entry.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2017-07-07 17:14:07 +08:00
|
|
|
ALLOC_GROW(info->logs, info->nr + 1, info->alloc);
|
|
|
|
info->logs[info->nr++] = commit_reflog;
|
2007-01-11 18:47:48 +08:00
|
|
|
|
reflog-walk: stop using fake parents
The reflog-walk system works by putting a ref's tip into the
pending queue, and then "traversing" the reflog by
pretending that the parent of each commit is the previous
reflog entry.
This causes a number of user-visible oddities, as documented
in t1414 (and the commit message which introduced it). We
can fix all of them in one go by replacing the fake-reflog
system with a much simpler one: just keeping a list of
reflogs to show, and walking through them entry by entry.
The implementation is fairly straight-forward, but there are
a few items to note:
1. We obviously must skip calling add_parents_to_list()
when we are traversing reflogs, since we do not want to
walk the original parents at all. As a result, we must call
try_to_simplify_commit() ourselves.
There are other parts of add_parents_to_list() we skip,
as well, but none of them should matter for a reflog
traversal:
- We do not allow UNINTERESTING commits, nor
symmetric ranges (and we bail when these are used
with "-g").
- Using --source makes no sense, since we aren't
traversing. The reflog selector shows the same
information with more detail.
- Using --first-parent is still sensible, since you
may want to see the first-parent diff for each
entry. But since we're not traversing, we don't
need to cull the parent list here.
2. Since we now just walk the reflog entries themselves,
rather than starting with the ref tip, we now look at
the "new" field of each entry rather than the "old"
(i.e., we are showing entries, not faking parents).
This removes all of the tricky logic around skipping
past root commits.
But note that we have no way to show an entry with the
null sha1 in its "new" field (because such a commit
obviously does not exist). Normally this would not
happen, since we delete reflogs along with refs, but
there is one special case. When we rename the currently
checked out branch, we write two reflog entries into
the HEAD log: one where the commit goes away, and
another where it comes back.
Prior to this commit, we show both entries with
identical reflog messages. After this commit, we show
only the "comes back" entry. See the update in t3200
which demonstrates this.
Arguably either is fine, as the whole double-entry
thing is a bit hacky in the first place. And until a
recent fix, we truncated the traversal in such a case
anyway, which was _definitely_ wrong.
3. We show individual reflogs in order, but choose which
reflog to show at each stage based on which has the
most recent timestamp. This interleaves the output
from multiple reflogs based on date order, which is
probably what you'd want with limiting like "-n 30".
Note that the implementation aims for simplicity. It
does a linear walk over the reflog queue for each
commit it pulls, which may perform badly if you
interleave an enormous number of reflogs. That seems
like an unlikely use case; if we did want to handle it,
we could probably keep a priority queue of reflogs,
ordered by the timestamp of their current tip entry.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2017-07-07 17:14:07 +08:00
|
|
|
return 0;
|
2007-01-11 18:47:48 +08:00
|
|
|
}
|
|
|
|
|
2009-10-19 23:48:09 +08:00
|
|
|
void get_reflog_selector(struct strbuf *sb,
|
|
|
|
struct reflog_walk_info *reflog_info,
|
convert "enum date_mode" into a struct
In preparation for adding date modes that may carry extra
information beyond the mode itself, this patch converts the
date_mode enum into a struct.
Most of the conversion is fairly straightforward; we pass
the struct as a pointer and dereference the type field where
necessary. Locations that declare a date_mode can use a "{}"
constructor. However, the tricky case is where we use the
enum labels as constants, like:
show_date(t, tz, DATE_NORMAL);
Ideally we could say:
show_date(t, tz, &{ DATE_NORMAL });
but of course C does not allow that. Likewise, we cannot
cast the constant to a struct, because we need to pass an
actual address. Our options are basically:
1. Manually add a "struct date_mode d = { DATE_NORMAL }"
definition to each caller, and pass "&d". This makes
the callers uglier, because they sometimes do not even
have their own scope (e.g., they are inside a switch
statement).
2. Provide a pre-made global "date_normal" struct that can
be passed by address. We'd also need "date_rfc2822",
"date_iso8601", and so forth. But at least the ugliness
is defined in one place.
3. Provide a wrapper that generates the correct struct on
the fly. The big downside is that we end up pointing to
a single global, which makes our wrapper non-reentrant.
But show_date is already not reentrant, so it does not
matter.
This patch implements 3, along with a minor macro to keep
the size of the callers sane.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-06-26 00:55:02 +08:00
|
|
|
const struct date_mode *dmode, int force_date,
|
2009-10-19 23:48:10 +08:00
|
|
|
int shorten)
|
2009-10-19 23:48:09 +08:00
|
|
|
{
|
|
|
|
struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
|
|
|
|
struct reflog_info *info;
|
2009-10-19 23:48:10 +08:00
|
|
|
const char *printed_ref;
|
2009-10-19 23:48:09 +08:00
|
|
|
|
|
|
|
if (!commit_reflog)
|
|
|
|
return;
|
|
|
|
|
2009-10-19 23:48:10 +08:00
|
|
|
if (shorten) {
|
|
|
|
if (!commit_reflog->reflogs->short_ref)
|
|
|
|
commit_reflog->reflogs->short_ref
|
|
|
|
= shorten_unambiguous_ref(commit_reflog->reflogs->ref, 0);
|
|
|
|
printed_ref = commit_reflog->reflogs->short_ref;
|
|
|
|
} else {
|
|
|
|
printed_ref = commit_reflog->reflogs->ref;
|
|
|
|
}
|
|
|
|
|
|
|
|
strbuf_addf(sb, "%s@{", printed_ref);
|
reflog-walk: always make HEAD@{0} show indexed selectors
When we are showing reflog selectors during a walk, we infer
from context whether the user wanted to see the index in
each selector, or the reflog date. The current rules are:
1. if the user asked for an explicit date format in the
output, show the date
2. if the user asked for ref@{now}, show the date
3. if neither is true, show the index
However, if we see "ref@{0}", that should be a strong clue
that the user wants to see the counted version. In fact, it
should be much stronger than the date format in (1). The
user may have been setting the date format to use in another
part of the output (e.g., in --format="%gd (%ad)", they may
have wanted to influence the author date).
This patch flips the rules to:
1. if the user asked for ref@{0}, always show the index
2. if the user asked for ref@{now}, always show the date
3. otherwise, we have just "ref"; show them counted by
default, but respect the presence of "--date" as a clue
that the user wanted them date-based
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2012-05-04 13:27:25 +08:00
|
|
|
if (commit_reflog->selector == SELECTOR_DATE ||
|
2012-05-08 05:11:32 +08:00
|
|
|
(commit_reflog->selector == SELECTOR_NONE && force_date)) {
|
2009-10-19 23:48:09 +08:00
|
|
|
info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
|
|
|
|
strbuf_addstr(sb, show_date(info->timestamp, info->tz, dmode));
|
|
|
|
} else {
|
|
|
|
strbuf_addf(sb, "%d", commit_reflog->reflogs->nr
|
|
|
|
- 2 - commit_reflog->recno);
|
|
|
|
}
|
|
|
|
|
|
|
|
strbuf_addch(sb, '}');
|
|
|
|
}
|
|
|
|
|
2009-10-19 23:48:10 +08:00
|
|
|
void get_reflog_message(struct strbuf *sb,
|
|
|
|
struct reflog_walk_info *reflog_info)
|
|
|
|
{
|
|
|
|
struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
|
|
|
|
struct reflog_info *info;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if (!commit_reflog)
|
|
|
|
return;
|
|
|
|
|
|
|
|
info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
|
|
|
|
len = strlen(info->message);
|
|
|
|
if (len > 0)
|
|
|
|
len--; /* strip away trailing newline */
|
|
|
|
strbuf_add(sb, info->message, len);
|
|
|
|
}
|
|
|
|
|
2011-12-16 19:40:24 +08:00
|
|
|
const char *get_reflog_ident(struct reflog_walk_info *reflog_info)
|
|
|
|
{
|
|
|
|
struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
|
|
|
|
struct reflog_info *info;
|
|
|
|
|
|
|
|
if (!commit_reflog)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
|
|
|
|
return info->email;
|
|
|
|
}
|
|
|
|
|
2017-07-07 17:16:21 +08:00
|
|
|
timestamp_t get_reflog_timestamp(struct reflog_walk_info *reflog_info)
|
|
|
|
{
|
|
|
|
struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
|
|
|
|
struct reflog_info *info;
|
|
|
|
|
|
|
|
if (!commit_reflog)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
|
|
|
|
return info->timestamp;
|
|
|
|
}
|
|
|
|
|
2009-10-19 23:48:09 +08:00
|
|
|
void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,
|
convert "enum date_mode" into a struct
In preparation for adding date modes that may carry extra
information beyond the mode itself, this patch converts the
date_mode enum into a struct.
Most of the conversion is fairly straightforward; we pass
the struct as a pointer and dereference the type field where
necessary. Locations that declare a date_mode can use a "{}"
constructor. However, the tricky case is where we use the
enum labels as constants, like:
show_date(t, tz, DATE_NORMAL);
Ideally we could say:
show_date(t, tz, &{ DATE_NORMAL });
but of course C does not allow that. Likewise, we cannot
cast the constant to a struct, because we need to pass an
actual address. Our options are basically:
1. Manually add a "struct date_mode d = { DATE_NORMAL }"
definition to each caller, and pass "&d". This makes
the callers uglier, because they sometimes do not even
have their own scope (e.g., they are inside a switch
statement).
2. Provide a pre-made global "date_normal" struct that can
be passed by address. We'd also need "date_rfc2822",
"date_iso8601", and so forth. But at least the ugliness
is defined in one place.
3. Provide a wrapper that generates the correct struct on
the fly. The big downside is that we end up pointing to
a single global, which makes our wrapper non-reentrant.
But show_date is already not reentrant, so it does not
matter.
This patch implements 3, along with a minor macro to keep
the size of the callers sane.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-06-26 00:55:02 +08:00
|
|
|
const struct date_mode *dmode, int force_date)
|
2007-01-11 18:47:48 +08:00
|
|
|
{
|
2009-10-19 23:48:09 +08:00
|
|
|
if (reflog_info && reflog_info->last_commit_reflog) {
|
|
|
|
struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
|
2007-01-11 18:47:48 +08:00
|
|
|
struct reflog_info *info;
|
2009-10-19 23:48:09 +08:00
|
|
|
struct strbuf selector = STRBUF_INIT;
|
2007-01-11 18:47:48 +08:00
|
|
|
|
2007-01-20 16:51:41 +08:00
|
|
|
info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
|
2012-05-08 05:11:32 +08:00
|
|
|
get_reflog_selector(&selector, reflog_info, dmode, force_date, 0);
|
2007-01-20 16:51:41 +08:00
|
|
|
if (oneline) {
|
2009-10-19 23:48:09 +08:00
|
|
|
printf("%s: %s", selector.buf, info->message);
|
2007-01-20 16:51:41 +08:00
|
|
|
}
|
|
|
|
else {
|
2009-10-19 23:48:09 +08:00
|
|
|
printf("Reflog: %s (%s)\nReflog message: %s",
|
|
|
|
selector.buf, info->email, info->message);
|
2007-01-20 16:51:41 +08:00
|
|
|
}
|
2009-10-19 23:48:09 +08:00
|
|
|
|
|
|
|
strbuf_release(&selector);
|
2007-01-11 18:47:48 +08:00
|
|
|
}
|
|
|
|
}
|
2017-07-07 17:08:30 +08:00
|
|
|
|
|
|
|
int reflog_walk_empty(struct reflog_walk_info *info)
|
|
|
|
{
|
reflog-walk: stop using fake parents
The reflog-walk system works by putting a ref's tip into the
pending queue, and then "traversing" the reflog by
pretending that the parent of each commit is the previous
reflog entry.
This causes a number of user-visible oddities, as documented
in t1414 (and the commit message which introduced it). We
can fix all of them in one go by replacing the fake-reflog
system with a much simpler one: just keeping a list of
reflogs to show, and walking through them entry by entry.
The implementation is fairly straight-forward, but there are
a few items to note:
1. We obviously must skip calling add_parents_to_list()
when we are traversing reflogs, since we do not want to
walk the original parents at all. As a result, we must call
try_to_simplify_commit() ourselves.
There are other parts of add_parents_to_list() we skip,
as well, but none of them should matter for a reflog
traversal:
- We do not allow UNINTERESTING commits, nor
symmetric ranges (and we bail when these are used
with "-g").
- Using --source makes no sense, since we aren't
traversing. The reflog selector shows the same
information with more detail.
- Using --first-parent is still sensible, since you
may want to see the first-parent diff for each
entry. But since we're not traversing, we don't
need to cull the parent list here.
2. Since we now just walk the reflog entries themselves,
rather than starting with the ref tip, we now look at
the "new" field of each entry rather than the "old"
(i.e., we are showing entries, not faking parents).
This removes all of the tricky logic around skipping
past root commits.
But note that we have no way to show an entry with the
null sha1 in its "new" field (because such a commit
obviously does not exist). Normally this would not
happen, since we delete reflogs along with refs, but
there is one special case. When we rename the currently
checked out branch, we write two reflog entries into
the HEAD log: one where the commit goes away, and
another where it comes back.
Prior to this commit, we show both entries with
identical reflog messages. After this commit, we show
only the "comes back" entry. See the update in t3200
which demonstrates this.
Arguably either is fine, as the whole double-entry
thing is a bit hacky in the first place. And until a
recent fix, we truncated the traversal in such a case
anyway, which was _definitely_ wrong.
3. We show individual reflogs in order, but choose which
reflog to show at each stage based on which has the
most recent timestamp. This interleaves the output
from multiple reflogs based on date order, which is
probably what you'd want with limiting like "-n 30".
Note that the implementation aims for simplicity. It
does a linear walk over the reflog queue for each
commit it pulls, which may perform badly if you
interleave an enormous number of reflogs. That seems
like an unlikely use case; if we did want to handle it,
we could probably keep a priority queue of reflogs,
ordered by the timestamp of their current tip entry.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2017-07-07 17:14:07 +08:00
|
|
|
return !info || !info->nr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct commit *next_reflog_commit(struct commit_reflog *log)
|
|
|
|
{
|
|
|
|
for (; log->recno >= 0; log->recno--) {
|
|
|
|
struct reflog_info *entry = &log->reflogs->items[log->recno];
|
2018-06-29 09:21:51 +08:00
|
|
|
struct object *obj = parse_object(the_repository,
|
|
|
|
&entry->noid);
|
reflog-walk: stop using fake parents
The reflog-walk system works by putting a ref's tip into the
pending queue, and then "traversing" the reflog by
pretending that the parent of each commit is the previous
reflog entry.
This causes a number of user-visible oddities, as documented
in t1414 (and the commit message which introduced it). We
can fix all of them in one go by replacing the fake-reflog
system with a much simpler one: just keeping a list of
reflogs to show, and walking through them entry by entry.
The implementation is fairly straight-forward, but there are
a few items to note:
1. We obviously must skip calling add_parents_to_list()
when we are traversing reflogs, since we do not want to
walk the original parents at all. As a result, we must call
try_to_simplify_commit() ourselves.
There are other parts of add_parents_to_list() we skip,
as well, but none of them should matter for a reflog
traversal:
- We do not allow UNINTERESTING commits, nor
symmetric ranges (and we bail when these are used
with "-g").
- Using --source makes no sense, since we aren't
traversing. The reflog selector shows the same
information with more detail.
- Using --first-parent is still sensible, since you
may want to see the first-parent diff for each
entry. But since we're not traversing, we don't
need to cull the parent list here.
2. Since we now just walk the reflog entries themselves,
rather than starting with the ref tip, we now look at
the "new" field of each entry rather than the "old"
(i.e., we are showing entries, not faking parents).
This removes all of the tricky logic around skipping
past root commits.
But note that we have no way to show an entry with the
null sha1 in its "new" field (because such a commit
obviously does not exist). Normally this would not
happen, since we delete reflogs along with refs, but
there is one special case. When we rename the currently
checked out branch, we write two reflog entries into
the HEAD log: one where the commit goes away, and
another where it comes back.
Prior to this commit, we show both entries with
identical reflog messages. After this commit, we show
only the "comes back" entry. See the update in t3200
which demonstrates this.
Arguably either is fine, as the whole double-entry
thing is a bit hacky in the first place. And until a
recent fix, we truncated the traversal in such a case
anyway, which was _definitely_ wrong.
3. We show individual reflogs in order, but choose which
reflog to show at each stage based on which has the
most recent timestamp. This interleaves the output
from multiple reflogs based on date order, which is
probably what you'd want with limiting like "-n 30".
Note that the implementation aims for simplicity. It
does a linear walk over the reflog queue for each
commit it pulls, which may perform badly if you
interleave an enormous number of reflogs. That seems
like an unlikely use case; if we did want to handle it,
we could probably keep a priority queue of reflogs,
ordered by the timestamp of their current tip entry.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2017-07-07 17:14:07 +08:00
|
|
|
|
|
|
|
if (obj && obj->type == OBJ_COMMIT)
|
|
|
|
return (struct commit *)obj;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static timestamp_t log_timestamp(struct commit_reflog *log)
|
|
|
|
{
|
|
|
|
return log->reflogs->items[log->recno].timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct commit *next_reflog_entry(struct reflog_walk_info *walk)
|
|
|
|
{
|
|
|
|
struct commit_reflog *best = NULL;
|
|
|
|
struct commit *best_commit = NULL;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < walk->nr; i++) {
|
|
|
|
struct commit_reflog *log = walk->logs[i];
|
|
|
|
struct commit *commit = next_reflog_commit(log);
|
|
|
|
|
|
|
|
if (!commit)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!best || log_timestamp(log) > log_timestamp(best)) {
|
|
|
|
best = log;
|
|
|
|
best_commit = commit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (best) {
|
|
|
|
best->recno--;
|
|
|
|
walk->last_commit_reflog = best;
|
|
|
|
return best_commit;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2017-07-07 17:08:30 +08:00
|
|
|
}
|