2007-01-11 10:47:48 +00:00
|
|
|
#include "cache.h"
|
|
|
|
#include "commit.h"
|
|
|
|
#include "refs.h"
|
|
|
|
#include "diff.h"
|
|
|
|
#include "revision.h"
|
2008-07-21 18:03:49 +00:00
|
|
|
#include "string-list.h"
|
2007-01-20 08:47:34 +00:00
|
|
|
#include "reflog-walk.h"
|
2007-01-11 10:47:48 +00:00
|
|
|
|
|
|
|
struct complete_reflogs {
|
|
|
|
char *ref;
|
2009-10-19 15:48:10 +00:00
|
|
|
const char *short_ref;
|
2007-01-11 10:47:48 +00: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-03 22:31:57 +00:00
|
|
|
ALLOC_GROW(array->items, array->nr + 1, array->alloc);
|
2007-01-11 10:47:48 +00:00
|
|
|
item = array->items + array->nr;
|
2014-03-03 09:39:59 +00:00
|
|
|
hashcpy(item->osha1, osha1);
|
|
|
|
hashcpy(item->nsha1, nsha1);
|
2007-01-11 10:47:48 +00: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 =
|
|
|
|
xcalloc(sizeof(struct complete_reflogs), 1);
|
|
|
|
reflogs->ref = xstrdup(ref);
|
|
|
|
for_each_reflog_ent(ref, read_one_reflog, reflogs);
|
|
|
|
if (reflogs->nr == 0) {
|
|
|
|
unsigned char sha1[20];
|
2011-12-13 14:17:48 +00:00
|
|
|
const char *name;
|
|
|
|
void *name_to_free;
|
|
|
|
name = name_to_free = resolve_refdup(ref, sha1, 1, NULL);
|
2011-11-13 10:22:15 +00:00
|
|
|
if (name) {
|
2007-01-11 10:47:48 +00:00
|
|
|
for_each_reflog_ent(name, read_one_reflog, reflogs);
|
2011-12-13 14:17:48 +00:00
|
|
|
free(name_to_free);
|
2011-11-13 10:22:15 +00:00
|
|
|
}
|
2007-01-11 10:47:48 +00:00
|
|
|
}
|
|
|
|
if (reflogs->nr == 0) {
|
|
|
|
int len = strlen(ref);
|
|
|
|
char *refname = xmalloc(len + 12);
|
|
|
|
sprintf(refname, "refs/%s", ref);
|
|
|
|
for_each_reflog_ent(refname, read_one_reflog, reflogs);
|
|
|
|
if (reflogs->nr == 0) {
|
|
|
|
sprintf(refname, "refs/heads/%s", ref);
|
|
|
|
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 09:49:15 +00:00
|
|
|
for (i = array->nr - 1; i >= 0; i--)
|
2007-01-11 10:47:48 +00: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-03 22:31:57 +00:00
|
|
|
ALLOC_GROW(lifo->items, lifo->nr + 1, lifo->alloc);
|
2007-01-11 10:47:48 +00:00
|
|
|
info = lifo->items + lifo->nr;
|
|
|
|
info->commit = commit;
|
|
|
|
info->util = util;
|
|
|
|
lifo->nr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct commit_reflog {
|
2012-05-04 05:26:26 +00:00
|
|
|
int recno;
|
|
|
|
enum selector_type {
|
|
|
|
SELECTOR_NONE,
|
|
|
|
SELECTOR_INDEX,
|
|
|
|
SELECTOR_DATE
|
|
|
|
} selector;
|
2007-01-11 10:47:48 +00:00
|
|
|
struct complete_reflogs *reflogs;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct reflog_walk_info {
|
|
|
|
struct commit_info_lifo reflogs;
|
2008-07-21 18:03:49 +00:00
|
|
|
struct string_list complete_reflogs;
|
2007-01-11 10:47:48 +00:00
|
|
|
struct commit_reflog *last_commit_reflog;
|
|
|
|
};
|
|
|
|
|
|
|
|
void init_reflog_walk(struct reflog_walk_info** info)
|
|
|
|
{
|
|
|
|
*info = xcalloc(sizeof(struct reflog_walk_info), 1);
|
|
|
|
}
|
|
|
|
|
2007-07-23 23:39:50 +00:00
|
|
|
int add_reflog_for_walk(struct reflog_walk_info *info,
|
2007-01-11 10:47:48 +00:00
|
|
|
struct commit *commit, const char *name)
|
|
|
|
{
|
|
|
|
unsigned long timestamp = 0;
|
|
|
|
int recno = -1;
|
2008-07-21 18:03:49 +00:00
|
|
|
struct string_list_item *item;
|
2007-01-11 10:47:48 +00:00
|
|
|
struct complete_reflogs *reflogs;
|
|
|
|
char *branch, *at = strchr(name, '@');
|
|
|
|
struct commit_reflog *commit_reflog;
|
2012-05-04 05:26:26 +00:00
|
|
|
enum selector_type selector = SELECTOR_NONE;
|
2007-01-11 10:47:48 +00:00
|
|
|
|
2007-01-20 02:28:19 +00:00
|
|
|
if (commit->object.flags & UNINTERESTING)
|
|
|
|
die ("Cannot walk reflogs for %s", name);
|
|
|
|
|
2007-01-11 10:47:48 +00: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 05:26:26 +00:00
|
|
|
selector = SELECTOR_DATE;
|
2007-01-11 10:47:48 +00:00
|
|
|
}
|
2012-05-04 05:26:26 +00:00
|
|
|
else
|
|
|
|
selector = SELECTOR_INDEX;
|
2007-01-11 10:47:48 +00:00
|
|
|
} else
|
|
|
|
recno = 0;
|
|
|
|
|
2010-06-25 23:41:37 +00:00
|
|
|
item = string_list_lookup(&info->complete_reflogs, branch);
|
2007-01-11 10:47:48 +00:00
|
|
|
if (item)
|
|
|
|
reflogs = item->util;
|
|
|
|
else {
|
2007-02-01 23:07:24 +00:00
|
|
|
if (*branch == '\0') {
|
|
|
|
unsigned char sha1[20];
|
|
|
|
free(branch);
|
2011-12-13 14:17:48 +00:00
|
|
|
branch = resolve_refdup("HEAD", sha1, 0, NULL);
|
|
|
|
if (!branch)
|
|
|
|
die ("No current branch");
|
|
|
|
|
2007-02-01 23:07:24 +00:00
|
|
|
}
|
2007-01-11 10:47:48 +00:00
|
|
|
reflogs = read_complete_reflog(branch);
|
2007-02-09 00:28:23 +00: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 10:47:48 +00:00
|
|
|
if (!reflogs || reflogs->nr == 0)
|
2007-07-23 23:39:50 +00:00
|
|
|
return -1;
|
2010-06-25 23:41:35 +00:00
|
|
|
string_list_insert(&info->complete_reflogs, branch)->util
|
2007-01-11 10:47:48 +00:00
|
|
|
= reflogs;
|
|
|
|
}
|
|
|
|
|
|
|
|
commit_reflog = xcalloc(sizeof(struct commit_reflog), 1);
|
|
|
|
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-23 23:39:50 +00:00
|
|
|
return -1;
|
2007-01-11 10:47:48 +00:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
commit_reflog->recno = reflogs->nr - recno - 1;
|
2012-05-04 05:26:26 +00:00
|
|
|
commit_reflog->selector = selector;
|
2007-01-11 10:47:48 +00:00
|
|
|
commit_reflog->reflogs = reflogs;
|
|
|
|
|
|
|
|
add_commit_info(commit, commit_reflog, &info->reflogs);
|
2007-07-23 23:39:50 +00:00
|
|
|
return 0;
|
2007-01-11 10:47:48 +00: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;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
reflog = &commit_reflog->reflogs->items[commit_reflog->recno];
|
|
|
|
info->last_commit_reflog = commit_reflog;
|
|
|
|
commit_reflog->recno--;
|
|
|
|
commit_info->commit = (struct commit *)parse_object(reflog->osha1);
|
|
|
|
if (!commit_info->commit) {
|
|
|
|
commit->parents = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
commit->parents = xcalloc(sizeof(struct commit_list), 1);
|
|
|
|
commit->parents->item = commit_info->commit;
|
|
|
|
}
|
|
|
|
|
2009-10-19 15:48:09 +00:00
|
|
|
void get_reflog_selector(struct strbuf *sb,
|
|
|
|
struct reflog_walk_info *reflog_info,
|
2012-05-07 21:11:32 +00:00
|
|
|
enum date_mode dmode, int force_date,
|
2009-10-19 15:48:10 +00:00
|
|
|
int shorten)
|
2009-10-19 15:48:09 +00:00
|
|
|
{
|
|
|
|
struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
|
|
|
|
struct reflog_info *info;
|
2009-10-19 15:48:10 +00:00
|
|
|
const char *printed_ref;
|
2009-10-19 15:48:09 +00:00
|
|
|
|
|
|
|
if (!commit_reflog)
|
|
|
|
return;
|
|
|
|
|
2009-10-19 15:48:10 +00: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 05:27:25 +00:00
|
|
|
if (commit_reflog->selector == SELECTOR_DATE ||
|
2012-05-07 21:11:32 +00:00
|
|
|
(commit_reflog->selector == SELECTOR_NONE && force_date)) {
|
2009-10-19 15:48:09 +00: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 15:48:10 +00: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 11:40:24 +00: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 15:48:09 +00:00
|
|
|
void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,
|
2012-05-07 21:11:32 +00:00
|
|
|
enum date_mode dmode, int force_date)
|
2007-01-11 10:47:48 +00:00
|
|
|
{
|
2009-10-19 15:48:09 +00:00
|
|
|
if (reflog_info && reflog_info->last_commit_reflog) {
|
|
|
|
struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
|
2007-01-11 10:47:48 +00:00
|
|
|
struct reflog_info *info;
|
2009-10-19 15:48:09 +00:00
|
|
|
struct strbuf selector = STRBUF_INIT;
|
2007-01-11 10:47:48 +00:00
|
|
|
|
2007-01-20 08:51:41 +00:00
|
|
|
info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
|
2012-05-07 21:11:32 +00:00
|
|
|
get_reflog_selector(&selector, reflog_info, dmode, force_date, 0);
|
2007-01-20 08:51:41 +00:00
|
|
|
if (oneline) {
|
2009-10-19 15:48:09 +00:00
|
|
|
printf("%s: %s", selector.buf, info->message);
|
2007-01-20 08:51:41 +00:00
|
|
|
}
|
|
|
|
else {
|
2009-10-19 15:48:09 +00:00
|
|
|
printf("Reflog: %s (%s)\nReflog message: %s",
|
|
|
|
selector.buf, info->email, info->message);
|
2007-01-20 08:51:41 +00:00
|
|
|
}
|
2009-10-19 15:48:09 +00:00
|
|
|
|
|
|
|
strbuf_release(&selector);
|
2007-01-11 10:47:48 +00:00
|
|
|
}
|
|
|
|
}
|