2007-01-11 18:47:48 +08:00
|
|
|
#include "cache.h"
|
|
|
|
#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;
|
2009-10-19 23:48:10 +08:00
|
|
|
const char *short_ref;
|
2007-01-11 18:47:48 +08:00
|
|
|
struct reflog_info {
|
|
|
|
unsigned char osha1[20], nsha1[20];
|
|
|
|
char *email;
|
|
|
|
unsigned long timestamp;
|
|
|
|
int tz;
|
|
|
|
char *message;
|
|
|
|
} *items;
|
|
|
|
int nr, alloc;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int read_one_reflog(unsigned char *osha1, unsigned char *nsha1,
|
|
|
|
const char *email, unsigned long timestamp, int tz,
|
|
|
|
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;
|
2014-03-03 17:39:59 +08:00
|
|
|
hashcpy(item->osha1, osha1);
|
|
|
|
hashcpy(item->nsha1, nsha1);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
unsigned char sha1[20];
|
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,
|
|
|
|
sha1, 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,
|
|
|
|
unsigned long timestamp)
|
|
|
|
{
|
|
|
|
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_info_lifo {
|
|
|
|
struct commit_info {
|
|
|
|
struct commit *commit;
|
|
|
|
void *util;
|
|
|
|
} *items;
|
|
|
|
int nr, alloc;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct commit_info *get_commit_info(struct commit *commit,
|
|
|
|
struct commit_info_lifo *lifo, int pop)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < lifo->nr; i++)
|
|
|
|
if (lifo->items[i].commit == commit) {
|
|
|
|
struct commit_info *result = &lifo->items[i];
|
|
|
|
if (pop) {
|
|
|
|
if (i + 1 < lifo->nr)
|
|
|
|
memmove(lifo->items + i,
|
|
|
|
lifo->items + i + 1,
|
|
|
|
(lifo->nr - i) *
|
|
|
|
sizeof(struct commit_info));
|
|
|
|
lifo->nr--;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void add_commit_info(struct commit *commit, void *util,
|
|
|
|
struct commit_info_lifo *lifo)
|
|
|
|
{
|
|
|
|
struct commit_info *info;
|
2014-03-04 06:31:57 +08:00
|
|
|
ALLOC_GROW(lifo->items, lifo->nr + 1, lifo->alloc);
|
2007-01-11 18:47:48 +08:00
|
|
|
info = lifo->items + lifo->nr;
|
|
|
|
info->commit = commit;
|
|
|
|
info->util = util;
|
|
|
|
lifo->nr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
struct commit_info_lifo reflogs;
|
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
|
|
|
{
|
2014-05-26 23:33:54 +08:00
|
|
|
*info = xcalloc(1, sizeof(struct reflog_walk_info));
|
2007-01-11 18:47:48 +08:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
unsigned long timestamp = 0;
|
|
|
|
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)
|
|
|
|
die ("Cannot walk reflogs for %s", name);
|
|
|
|
|
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') {
|
|
|
|
unsigned char sha1[20];
|
|
|
|
free(branch);
|
2014-07-16 03:59:36 +08:00
|
|
|
branch = resolve_refdup("HEAD", 0, sha1, NULL);
|
2011-12-13 22:17:48 +08:00
|
|
|
if (!branch)
|
|
|
|
die ("No current branch");
|
|
|
|
|
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) {
|
|
|
|
unsigned char sha1[20];
|
|
|
|
char *b;
|
|
|
|
if (dwim_log(branch, strlen(branch), sha1, &b) == 1) {
|
|
|
|
if (reflogs) {
|
|
|
|
free(reflogs->ref);
|
|
|
|
free(reflogs);
|
|
|
|
}
|
|
|
|
free(branch);
|
|
|
|
branch = b;
|
|
|
|
reflogs = read_complete_reflog(branch);
|
|
|
|
}
|
|
|
|
}
|
2007-01-11 18:47:48 +08:00
|
|
|
if (!reflogs || reflogs->nr == 0)
|
2007-07-24 07:39:50 +08:00
|
|
|
return -1;
|
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;
|
|
|
|
}
|
|
|
|
|
2014-05-26 23:33:54 +08:00
|
|
|
commit_reflog = xcalloc(1, sizeof(struct commit_reflog));
|
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(branch);
|
|
|
|
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;
|
|
|
|
|
|
|
|
add_commit_info(commit, commit_reflog, &info->reflogs);
|
2007-07-24 07:39:50 +08:00
|
|
|
return 0;
|
2007-01-11 18:47:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void fake_reflog_parent(struct reflog_walk_info *info, struct commit *commit)
|
|
|
|
{
|
|
|
|
struct commit_info *commit_info =
|
|
|
|
get_commit_info(commit, &info->reflogs, 0);
|
|
|
|
struct commit_reflog *commit_reflog;
|
2016-01-06 05:12:10 +08:00
|
|
|
struct object *logobj;
|
2007-01-11 18:47:48 +08:00
|
|
|
struct reflog_info *reflog;
|
|
|
|
|
|
|
|
info->last_commit_reflog = NULL;
|
|
|
|
if (!commit_info)
|
|
|
|
return;
|
|
|
|
|
|
|
|
commit_reflog = commit_info->util;
|
|
|
|
if (commit_reflog->recno < 0) {
|
|
|
|
commit->parents = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
info->last_commit_reflog = commit_reflog;
|
2016-01-06 05:12:10 +08:00
|
|
|
|
|
|
|
do {
|
|
|
|
reflog = &commit_reflog->reflogs->items[commit_reflog->recno];
|
|
|
|
commit_reflog->recno--;
|
|
|
|
logobj = parse_object(reflog->osha1);
|
|
|
|
} while (commit_reflog->recno && (logobj && logobj->type != OBJ_COMMIT));
|
|
|
|
|
2016-06-04 04:42:35 +08:00
|
|
|
if (!logobj && commit_reflog->recno >= 0 && is_null_sha1(reflog->osha1)) {
|
|
|
|
/* a root commit, but there are still more entries to show */
|
|
|
|
reflog = &commit_reflog->reflogs->items[commit_reflog->recno];
|
|
|
|
logobj = parse_object(reflog->nsha1);
|
|
|
|
}
|
|
|
|
|
2016-01-06 05:12:10 +08:00
|
|
|
if (!logobj || logobj->type != OBJ_COMMIT) {
|
|
|
|
commit_info->commit = NULL;
|
2007-01-11 18:47:48 +08:00
|
|
|
commit->parents = NULL;
|
|
|
|
return;
|
|
|
|
}
|
2016-01-06 05:12:10 +08:00
|
|
|
commit_info->commit = (struct commit *)logobj;
|
2007-01-11 18:47:48 +08:00
|
|
|
|
2014-05-26 23:33:54 +08:00
|
|
|
commit->parents = xcalloc(1, sizeof(struct commit_list));
|
2007-01-11 18:47:48 +08:00
|
|
|
commit->parents->item = commit_info->commit;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|