summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJunio C Hamano <junkio@cox.net>2007-02-05 00:54:47 (GMT)
committerJunio C Hamano <junkio@cox.net>2007-02-05 00:54:47 (GMT)
commitd1f289c5aa52bb77dc4959fb21cef032d954869f (patch)
tree525c95c67c6ae06278a99f244dc5d120c45a8747
parent6e2e1cfb81a6a6de9fc074bd26bed8a45f73251f (diff)
parent632ac9fd12a2d4ff2c1a1fcd63492ce24315221f (diff)
downloadgit-d1f289c5aa52bb77dc4959fb21cef032d954869f.zip
git-d1f289c5aa52bb77dc4959fb21cef032d954869f.tar.gz
git-d1f289c5aa52bb77dc4959fb21cef032d954869f.tar.bz2
Merge branch 'np/dreflog'
* np/dreflog: show-branch -g: default to the current branch. Let git-checkout always drop any detached head Enable HEAD@{...} and make it independent from the current branch scan reflogs independently from refs add reflog when moving HEAD to a new branch create_symref(): do not assume pathname from git_path() persists long enough add logref support to git-symbolic-ref move create_symref() past log_ref_write() add reflog entries for HEAD when detached enable separate reflog for HEAD lock_ref_sha1_basic(): remember the original name of a ref when resolving it make reflog filename independent from struct ref_lock
-rw-r--r--Documentation/git-show-branch.txt8
-rw-r--r--Documentation/git-symbolic-ref.txt6
-rw-r--r--builtin-branch.c3
-rw-r--r--builtin-fsck.c9
-rw-r--r--builtin-init-db.c2
-rw-r--r--builtin-reflog.c17
-rw-r--r--builtin-show-branch.c5
-rw-r--r--builtin-symbolic-ref.c16
-rw-r--r--cache.h2
-rwxr-xr-xgit-checkout.sh24
-rw-r--r--reachable.c4
-rw-r--r--refs.c201
-rw-r--r--refs.h8
-rw-r--r--sha1_name.c62
14 files changed, 232 insertions, 135 deletions
diff --git a/Documentation/git-show-branch.txt b/Documentation/git-show-branch.txt
index b38633c..ba5313d 100644
--- a/Documentation/git-show-branch.txt
+++ b/Documentation/git-show-branch.txt
@@ -11,7 +11,7 @@ SYNOPSIS
'git-show-branch' [--all] [--remotes] [--topo-order] [--current]
[--more=<n> | --list | --independent | --merge-base]
[--no-name | --sha1-name] [--topics] [<rev> | <glob>]...
-'git-show-branch' (-g|--reflog)[=<n>[,<base>]] [--list] <ref>
+'git-show-branch' (-g|--reflog)[=<n>[,<base>]] [--list] [<ref>]
DESCRIPTION
-----------
@@ -97,11 +97,13 @@ OPTIONS
will show the revisions given by "git rev-list {caret}master
topic1 topic2"
---reflog[=<n>[,<base>]] <ref>::
+--reflog[=<n>[,<base>]] [<ref>]::
Shows <n> most recent ref-log entries for the given
ref. If <base> is given, <n> entries going back from
that entry. <base> can be specified as count or date.
- `-g` can be used as a short-hand for this option.
+ `-g` can be used as a short-hand for this option. When
+ no explicit <ref> parameter is given, it defaults to the
+ current branch (or `HEAD` if it is detached).
Note that --more, --list, --independent and --merge-base options
are mutually exclusive.
diff --git a/Documentation/git-symbolic-ref.txt b/Documentation/git-symbolic-ref.txt
index f93b79a..a88f722 100644
--- a/Documentation/git-symbolic-ref.txt
+++ b/Documentation/git-symbolic-ref.txt
@@ -7,7 +7,7 @@ git-symbolic-ref - Read and modify symbolic refs
SYNOPSIS
--------
-'git-symbolic-ref' [-q] <name> [<ref>]
+'git-symbolic-ref' [-q] [-m <reason>] <name> [<ref>]
DESCRIPTION
-----------
@@ -31,6 +31,10 @@ OPTIONS
symbolic ref but a detached HEAD; instead exit with
non-zero status silently.
+-m::
+ Update the reflog for <name> with <reason>. This is valid only
+ when creating or updating a symbolic ref.
+
NOTES
-----
In the past, `.git/HEAD` was a symbolic link pointing at
diff --git a/builtin-branch.c b/builtin-branch.c
index 9b68d3b..2d8d61b 100644
--- a/builtin-branch.c
+++ b/builtin-branch.c
@@ -387,7 +387,8 @@ static void rename_branch(const char *oldname, const char *newname, int force)
if (rename_ref(oldref, newref, logmsg))
die("Branch rename failed");
- if (!strcmp(oldname, head) && create_symref("HEAD", newref))
+ /* no need to pass logmsg here as HEAD didn't really move */
+ if (!strcmp(oldname, head) && create_symref("HEAD", newref, NULL))
die("Branch renamed to %s, but HEAD is not updated!", newname);
}
diff --git a/builtin-fsck.c b/builtin-fsck.c
index f6f1967..6da3814 100644
--- a/builtin-fsck.c
+++ b/builtin-fsck.c
@@ -477,6 +477,12 @@ static int fsck_handle_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
return 0;
}
+static int fsck_handle_reflog(const char *logname, const unsigned char *sha1, int flag, void *cb_data)
+{
+ for_each_reflog_ent(logname, fsck_handle_reflog_ent, NULL);
+ return 0;
+}
+
static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
{
struct object *obj;
@@ -495,14 +501,13 @@ static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int f
obj->used = 1;
mark_reachable(obj, REACHABLE);
- for_each_reflog_ent(refname, fsck_handle_reflog_ent, NULL);
-
return 0;
}
static void get_default_heads(void)
{
for_each_ref(fsck_handle_ref, NULL);
+ for_each_reflog(fsck_handle_reflog, NULL);
/*
* Not having any default heads isn't really fatal, but
diff --git a/builtin-init-db.c b/builtin-init-db.c
index 1865489..12e43d0 100644
--- a/builtin-init-db.c
+++ b/builtin-init-db.c
@@ -231,7 +231,7 @@ static int create_default_files(const char *git_dir, const char *template_path)
strcpy(path + len, "HEAD");
reinit = !read_ref("HEAD", sha1);
if (!reinit) {
- if (create_symref("HEAD", "refs/heads/master") < 0)
+ if (create_symref("HEAD", "refs/heads/master", NULL) < 0)
exit(1);
}
diff --git a/builtin-reflog.c b/builtin-reflog.c
index b443ed9..bfb169a 100644
--- a/builtin-reflog.c
+++ b/builtin-reflog.c
@@ -242,20 +242,18 @@ static int expire_reflog(const char *ref, const unsigned char *sha1, int unused,
struct cmd_reflog_expire_cb *cmd = cb_data;
struct expire_reflog_cb cb;
struct ref_lock *lock;
- char *newlog_path = NULL;
+ char *log_file, *newlog_path = NULL;
int status = 0;
- if (strncmp(ref, "refs/", 5))
- return error("not a ref '%s'", ref);
-
memset(&cb, 0, sizeof(cb));
/* we take the lock for the ref itself to prevent it from
* getting updated.
*/
- lock = lock_ref_sha1(ref + 5, sha1);
+ lock = lock_any_ref_for_update(ref, sha1);
if (!lock)
return error("cannot lock ref '%s'", ref);
- if (!file_exists(lock->log_file))
+ log_file = xstrdup(git_path("logs/%s", ref));
+ if (!file_exists(log_file))
goto finish;
if (!cmd->dry_run) {
newlog_path = xstrdup(git_path("logs/%s.lock", ref));
@@ -271,13 +269,14 @@ static int expire_reflog(const char *ref, const unsigned char *sha1, int unused,
if (fclose(cb.newlog))
status |= error("%s: %s", strerror(errno),
newlog_path);
- if (rename(newlog_path, lock->log_file)) {
+ if (rename(newlog_path, log_file)) {
status |= error("cannot rename %s to %s",
- newlog_path, lock->log_file);
+ newlog_path, log_file);
unlink(newlog_path);
}
}
free(newlog_path);
+ free(log_file);
unlock_ref(lock);
return status;
}
@@ -351,7 +350,7 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
}
if (do_all)
- status |= for_each_ref(expire_reflog, &cb);
+ status |= for_each_reflog(expire_reflog, &cb);
while (i < argc) {
const char *ref = argv[i++];
unsigned char sha1[20];
diff --git a/builtin-show-branch.c b/builtin-show-branch.c
index fa62e48..0d94e40 100644
--- a/builtin-show-branch.c
+++ b/builtin-show-branch.c
@@ -690,7 +690,10 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
if (ac == 0) {
static const char *fake_av[2];
- fake_av[0] = "HEAD";
+ const char *refname;
+
+ refname = resolve_ref("HEAD", sha1, 1, NULL);
+ fake_av[0] = xstrdup(refname);
fake_av[1] = NULL;
av = fake_av;
ac = 1;
diff --git a/builtin-symbolic-ref.c b/builtin-symbolic-ref.c
index 227c9d4..d41b406 100644
--- a/builtin-symbolic-ref.c
+++ b/builtin-symbolic-ref.c
@@ -3,7 +3,7 @@
#include "refs.h"
static const char git_symbolic_ref_usage[] =
-"git-symbolic-ref [-q] name [ref]";
+"git-symbolic-ref [-q] [-m <reason>] name [ref]";
static void check_symref(const char *HEAD, int quiet)
{
@@ -25,6 +25,7 @@ static void check_symref(const char *HEAD, int quiet)
int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
{
int quiet = 0;
+ const char *msg = NULL;
git_config(git_default_config);
@@ -34,6 +35,17 @@ int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
break;
else if (!strcmp("-q", arg))
quiet = 1;
+ else if (!strcmp("-m", arg)) {
+ argc--;
+ argv++;
+ if (argc <= 1)
+ break;
+ msg = argv[1];
+ if (!*msg)
+ die("Refusing to perform update with empty message");
+ if (strchr(msg, '\n'))
+ die("Refusing to perform update with \\n in message");
+ }
else if (!strcmp("--", arg)) {
argc--;
argv++;
@@ -50,7 +62,7 @@ int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
check_symref(argv[1], quiet);
break;
case 3:
- create_symref(argv[1], argv[2]);
+ create_symref(argv[1], argv[2], msg);
break;
default:
usage(git_symbolic_ref_usage);
diff --git a/cache.h b/cache.h
index 9873ee9..201704b 100644
--- a/cache.h
+++ b/cache.h
@@ -302,7 +302,7 @@ extern int read_ref(const char *filename, unsigned char *sha1);
extern const char *resolve_ref(const char *path, unsigned char *sha1, int, int *);
extern int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref);
-extern int create_symref(const char *ref, const char *refs_heads_master);
+extern int create_symref(const char *ref, const char *refs_heads_master, const char *logmsg);
extern int validate_headref(const char *ref);
extern int base_name_compare(const char *name1, int len1, int mode1, const char *name2, int len2, int mode2);
diff --git a/git-checkout.sh b/git-checkout.sh
index 99a81f5..14835a4 100755
--- a/git-checkout.sh
+++ b/git-checkout.sh
@@ -164,22 +164,9 @@ If you want to create a new branch from this checkout, you may do so
(now or later) by using -b with the checkout command again. Example:
git checkout -b <new_branch_name>"
fi
-elif test -z "$oldbranch" && test -n "$branch"
+elif test -z "$oldbranch" && test -z "$quiet"
then
- # Coming back...
- if test -z "$force"
- then
- git show-ref -d -s | grep "$old" >/dev/null || {
- echo >&2 \
-"You are not on any branch and switching to branch '$new_name'
-may lose your changes. At this point, you can do one of two things:
- (1) Decide it is Ok and say 'git checkout -f $new_name';
- (2) Start a new branch from the current commit, by saying
- 'git checkout -b <branch-name>'.
-Leaving your HEAD detached; not switching to branch '$new_name'."
- exit 1;
- }
- fi
+ echo >&2 "Previous HEAD position was $old"
fi
if [ "X$old" = X ]
@@ -257,7 +244,7 @@ if [ "$?" -eq 0 ]; then
fi
if test -n "$branch"
then
- GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD "refs/heads/$branch"
+ GIT_DIR="$GIT_DIR" git-symbolic-ref -m "checkout: moving to $branch" HEAD "refs/heads/$branch"
if test -z "$quiet"
then
echo >&2 "Switched to${newbranch:+ a new} branch \"$branch\""
@@ -270,8 +257,9 @@ if [ "$?" -eq 0 ]; then
# git update-ref --detach HEAD $new
# or something like that...
#
- echo "$detached" >"$GIT_DIR/HEAD.new" &&
- mv "$GIT_DIR/HEAD.new" "$GIT_DIR/HEAD" ||
+ git-rev-parse HEAD >"$GIT_DIR/HEAD.new" &&
+ mv "$GIT_DIR/HEAD.new" "$GIT_DIR/HEAD" &&
+ git-update-ref -m "checkout: moving to $arg" HEAD "$detached" ||
die "Cannot detach HEAD"
if test -n "$detach_warn"
then
diff --git a/reachable.c b/reachable.c
index a6a3348..01760d7 100644
--- a/reachable.c
+++ b/reachable.c
@@ -188,9 +188,9 @@ void mark_reachable_objects(struct rev_info *revs, int mark_reflog)
/* Add all external refs */
for_each_ref(add_one_ref, revs);
- /* Add all reflog info from refs */
+ /* Add all reflog info */
if (mark_reflog)
- for_each_ref(add_one_reflog, revs);
+ for_each_reflog(add_one_reflog, revs);
/*
* Set up the revision walk - this will move all commits
diff --git a/refs.c b/refs.c
index 3db444c..03e8dfe 100644
--- a/refs.c
+++ b/refs.c
@@ -309,53 +309,6 @@ const char *resolve_ref(const char *ref, unsigned char *sha1, int reading, int *
return ref;
}
-int create_symref(const char *ref_target, const char *refs_heads_master)
-{
- const char *lockpath;
- char ref[1000];
- int fd, len, written;
- const char *git_HEAD = git_path("%s", ref_target);
-
-#ifndef NO_SYMLINK_HEAD
- if (prefer_symlink_refs) {
- unlink(git_HEAD);
- if (!symlink(refs_heads_master, git_HEAD))
- return 0;
- fprintf(stderr, "no symlink - falling back to symbolic ref\n");
- }
-#endif
-
- len = snprintf(ref, sizeof(ref), "ref: %s\n", refs_heads_master);
- if (sizeof(ref) <= len) {
- error("refname too long: %s", refs_heads_master);
- return -1;
- }
- lockpath = mkpath("%s.lock", git_HEAD);
- fd = open(lockpath, O_CREAT | O_EXCL | O_WRONLY, 0666);
- if (fd < 0) {
- error("Unable to open %s for writing", lockpath);
- return -5;
- }
- written = write_in_full(fd, ref, len);
- close(fd);
- if (written != len) {
- unlink(lockpath);
- error("Unable to write to %s", lockpath);
- return -2;
- }
- if (rename(lockpath, git_HEAD) < 0) {
- unlink(lockpath);
- error("Unable to create %s", git_HEAD);
- return -3;
- }
- if (adjust_shared_perm(git_HEAD)) {
- unlink(lockpath);
- error("Unable to fix permissions on %s", lockpath);
- return -4;
- }
- return 0;
-}
-
int read_ref(const char *ref, unsigned char *sha1)
{
if (resolve_ref(ref, sha1, 1, NULL))
@@ -680,7 +633,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char
lock->lk = xcalloc(1, sizeof(struct lock_file));
lock->ref_name = xstrdup(ref);
- lock->log_file = xstrdup(git_path("logs/%s", ref));
+ lock->orig_ref_name = xstrdup(orig_ref);
ref_file = git_path("%s", ref);
lock->force_write = lstat(ref_file, &st) && errno == ENOENT;
@@ -778,10 +731,10 @@ int delete_ref(const char *refname, unsigned char *sha1)
*/
ret |= repack_without_ref(refname);
- err = unlink(lock->log_file);
+ err = unlink(git_path("logs/%s", lock->ref_name));
if (err && errno != ENOENT)
fprintf(stderr, "warning: unlink(%s) failed: %s",
- lock->log_file, strerror(errno));
+ git_path("logs/%s", lock->ref_name), strerror(errno));
invalidate_cached_refs();
unlock_ref(lock);
return ret;
@@ -922,47 +875,50 @@ void unlock_ref(struct ref_lock *lock)
rollback_lock_file(lock->lk);
}
free(lock->ref_name);
- free(lock->log_file);
+ free(lock->orig_ref_name);
free(lock);
}
-static int log_ref_write(struct ref_lock *lock,
- const unsigned char *sha1, const char *msg)
+static int log_ref_write(const char *ref_name, const unsigned char *old_sha1,
+ const unsigned char *new_sha1, const char *msg)
{
int logfd, written, oflags = O_APPEND | O_WRONLY;
unsigned maxlen, len;
int msglen;
- char *logrec;
+ char *log_file, *logrec;
const char *committer;
if (log_all_ref_updates < 0)
log_all_ref_updates = !is_bare_repository();
+ log_file = git_path("logs/%s", ref_name);
+
if (log_all_ref_updates &&
- (!strncmp(lock->ref_name, "refs/heads/", 11) ||
- !strncmp(lock->ref_name, "refs/remotes/", 13))) {
- if (safe_create_leading_directories(lock->log_file) < 0)
+ (!strncmp(ref_name, "refs/heads/", 11) ||
+ !strncmp(ref_name, "refs/remotes/", 13) ||
+ !strcmp(ref_name, "HEAD"))) {
+ if (safe_create_leading_directories(log_file) < 0)
return error("unable to create directory for %s",
- lock->log_file);
+ log_file);
oflags |= O_CREAT;
}
- logfd = open(lock->log_file, oflags, 0666);
+ logfd = open(log_file, oflags, 0666);
if (logfd < 0) {
if (!(oflags & O_CREAT) && errno == ENOENT)
return 0;
if ((oflags & O_CREAT) && errno == EISDIR) {
- if (remove_empty_directories(lock->log_file)) {
+ if (remove_empty_directories(log_file)) {
return error("There are still logs under '%s'",
- lock->log_file);
+ log_file);
}
- logfd = open(lock->log_file, oflags, 0666);
+ logfd = open(log_file, oflags, 0666);
}
if (logfd < 0)
return error("Unable to append to %s: %s",
- lock->log_file, strerror(errno));
+ log_file, strerror(errno));
}
msglen = 0;
@@ -984,8 +940,8 @@ static int log_ref_write(struct ref_lock *lock,
maxlen = strlen(committer) + msglen + 100;
logrec = xmalloc(maxlen);
len = sprintf(logrec, "%s %s %s\n",
- sha1_to_hex(lock->old_sha1),
- sha1_to_hex(sha1),
+ sha1_to_hex(old_sha1),
+ sha1_to_hex(new_sha1),
committer);
if (msglen)
len += sprintf(logrec + len - 1, "\t%.*s\n", msglen, msg) - 1;
@@ -993,7 +949,7 @@ static int log_ref_write(struct ref_lock *lock,
free(logrec);
close(logfd);
if (written != len)
- return error("Unable to append to %s", lock->log_file);
+ return error("Unable to append to %s", log_file);
return 0;
}
@@ -1016,7 +972,9 @@ int write_ref_sha1(struct ref_lock *lock,
return -1;
}
invalidate_cached_refs();
- if (log_ref_write(lock, sha1, logmsg) < 0) {
+ if (log_ref_write(lock->ref_name, lock->old_sha1, sha1, logmsg) < 0 ||
+ (strcmp(lock->ref_name, lock->orig_ref_name) &&
+ log_ref_write(lock->orig_ref_name, lock->old_sha1, sha1, logmsg) < 0)) {
unlock_ref(lock);
return -1;
}
@@ -1030,6 +988,65 @@ int write_ref_sha1(struct ref_lock *lock,
return 0;
}
+int create_symref(const char *ref_target, const char *refs_heads_master,
+ const char *logmsg)
+{
+ const char *lockpath;
+ char ref[1000];
+ int fd, len, written;
+ char *git_HEAD = xstrdup(git_path("%s", ref_target));
+ unsigned char old_sha1[20], new_sha1[20];
+
+ if (logmsg && read_ref(ref_target, old_sha1))
+ hashclr(old_sha1);
+
+#ifndef NO_SYMLINK_HEAD
+ if (prefer_symlink_refs) {
+ unlink(git_HEAD);
+ if (!symlink(refs_heads_master, git_HEAD))
+ goto done;
+ fprintf(stderr, "no symlink - falling back to symbolic ref\n");
+ }
+#endif
+
+ len = snprintf(ref, sizeof(ref), "ref: %s\n", refs_heads_master);
+ if (sizeof(ref) <= len) {
+ error("refname too long: %s", refs_heads_master);
+ goto error_free_return;
+ }
+ lockpath = mkpath("%s.lock", git_HEAD);
+ fd = open(lockpath, O_CREAT | O_EXCL | O_WRONLY, 0666);
+ if (fd < 0) {
+ error("Unable to open %s for writing", lockpath);
+ goto error_free_return;
+ }
+ written = write_in_full(fd, ref, len);
+ close(fd);
+ if (written != len) {
+ error("Unable to write to %s", lockpath);
+ goto error_unlink_return;
+ }
+ if (rename(lockpath, git_HEAD) < 0) {
+ error("Unable to create %s", git_HEAD);
+ goto error_unlink_return;
+ }
+ if (adjust_shared_perm(git_HEAD)) {
+ error("Unable to fix permissions on %s", lockpath);
+ error_unlink_return:
+ unlink(lockpath);
+ error_free_return:
+ free(git_HEAD);
+ return -1;
+ }
+
+ done:
+ if (logmsg && !read_ref(refs_heads_master, new_sha1))
+ log_ref_write(ref_target, old_sha1, new_sha1, logmsg);
+
+ free(git_HEAD);
+ return 0;
+}
+
static char *ref_msg(const char *line, const char *endp)
{
const char *ep;
@@ -1186,3 +1203,53 @@ int for_each_reflog_ent(const char *ref, each_reflog_ent_fn fn, void *cb_data)
return ret;
}
+static int do_for_each_reflog(const char *base, each_ref_fn fn, void *cb_data)
+{
+ DIR *dir = opendir(git_path("logs/%s", base));
+ int retval = errno;
+
+ if (dir) {
+ struct dirent *de;
+ int baselen = strlen(base);
+ char *log = xmalloc(baselen + 257);
+
+ memcpy(log, base, baselen);
+ if (baselen && base[baselen-1] != '/')
+ log[baselen++] = '/';
+
+ while ((de = readdir(dir)) != NULL) {
+ struct stat st;
+ int namelen;
+
+ if (de->d_name[0] == '.')
+ continue;
+ namelen = strlen(de->d_name);
+ if (namelen > 255)
+ continue;
+ if (has_extension(de->d_name, ".lock"))
+ continue;
+ memcpy(log + baselen, de->d_name, namelen+1);
+ if (stat(git_path("logs/%s", log), &st) < 0)
+ continue;
+ if (S_ISDIR(st.st_mode)) {
+ retval = do_for_each_reflog(log, fn, cb_data);
+ } else {
+ unsigned char sha1[20];
+ if (!resolve_ref(log, sha1, 0, NULL))
+ retval = error("bad ref for %s", log);
+ else
+ retval = fn(log, sha1, 0, cb_data);
+ }
+ if (retval)
+ break;
+ }
+ free(log);
+ closedir(dir);
+ }
+ return retval;
+}
+
+int for_each_reflog(each_ref_fn fn, void *cb_data)
+{
+ return do_for_each_reflog("", fn, cb_data);
+}
diff --git a/refs.h b/refs.h
index 33450f1..acedffc 100644
--- a/refs.h
+++ b/refs.h
@@ -3,7 +3,7 @@
struct ref_lock {
char *ref_name;
- char *log_file;
+ char *orig_ref_name;
struct lock_file *lk;
unsigned char old_sha1[20];
int lock_fd;
@@ -48,6 +48,12 @@ extern int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned
typedef int each_reflog_ent_fn(unsigned char *osha1, unsigned char *nsha1, const char *, unsigned long, int, const char *, void *);
int for_each_reflog_ent(const char *ref, each_reflog_ent_fn fn, void *cb_data);
+/*
+ * Calls the specified function for each reflog file until it returns nonzero,
+ * and returns the value
+ */
+extern int for_each_reflog(each_ref_fn, void *);
+
/** Returns 0 if target has the right format for a ref. **/
extern int check_ref_format(const char *target);
diff --git a/sha1_name.c b/sha1_name.c
index de8caf8..d77f770 100644
--- a/sha1_name.c
+++ b/sha1_name.c
@@ -235,22 +235,23 @@ static int ambiguous_path(const char *path, int len)
return slash;
}
+static const char *ref_fmt[] = {
+ "%.*s",
+ "refs/%.*s",
+ "refs/tags/%.*s",
+ "refs/heads/%.*s",
+ "refs/remotes/%.*s",
+ "refs/remotes/%.*s/HEAD",
+ NULL
+};
+
int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref)
{
- static const char *fmt[] = {
- "%.*s",
- "refs/%.*s",
- "refs/tags/%.*s",
- "refs/heads/%.*s",
- "refs/remotes/%.*s",
- "refs/remotes/%.*s/HEAD",
- NULL
- };
const char **p, *r;
int refs_found = 0;
*ref = NULL;
- for (p = fmt; *p; p++) {
+ for (p = ref_fmt; *p; p++) {
unsigned char sha1_from_ref[20];
unsigned char *this_result;
@@ -266,6 +267,28 @@ int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref)
return refs_found;
}
+static int dwim_log(const char *str, int len, unsigned char *sha1, char **log)
+{
+ const char **p;
+ int logs_found = 0;
+
+ *log = NULL;
+ for (p = ref_fmt; *p; p++) {
+ struct stat st;
+ char *path = mkpath(*p, len, str);
+ if (!stat(git_path("logs/%s", path), &st) &&
+ S_ISREG(st.st_mode)) {
+ if (!logs_found++) {
+ *log = xstrdup(path);
+ resolve_ref(path, sha1, 0, NULL);
+ }
+ if (!warn_ambiguous_refs)
+ break;
+ }
+ }
+ return logs_found;
+}
+
static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
{
static const char *warning = "warning: refname '%.*s' is ambiguous.\n";
@@ -295,7 +318,9 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
if (!len && reflog_len) {
/* allow "@{...}" to mean the current branch reflog */
refs_found = dwim_ref("HEAD", 4, sha1, &real_ref);
- } else
+ } else if (reflog_len)
+ refs_found = dwim_log(str, len, sha1, &real_ref);
+ else
refs_found = dwim_ref(str, len, sha1, &real_ref);
if (!refs_found)
@@ -310,21 +335,6 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
unsigned long co_time;
int co_tz, co_cnt;
- /*
- * We'll have an independent reflog for "HEAD" eventually
- * which won't be a synonym for the current branch reflog.
- * In the mean time prevent people from getting used to
- * such a synonym until the work is completed.
- */
- if (len && !strncmp("HEAD", str, len) &&
- !strncmp(real_ref, "refs/", 5)) {
- error("reflog for HEAD has not been implemented yet\n"
- "Maybe you could try %s%s instead, "
- "or just %s for current branch..",
- strchr(real_ref+5, '/')+1, str+len, str+len);
- exit(-1);
- }
-
/* Is it asking for N-th entry, or approxidate? */
for (i = nth = 0; 0 <= nth && i < reflog_len; i++) {
char ch = str[at+2+i];