Skip to content

Commit

Permalink
Merge branch 'np/dreflog'
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Junio C Hamano committed Feb 5, 2007
2 parents 6e2e1cf + 632ac9f commit d1f289c
Show file tree
Hide file tree
Showing 14 changed files with 232 additions and 135 deletions.
8 changes: 5 additions & 3 deletions Documentation/git-show-branch.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----------
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 5 additions & 1 deletion Documentation/git-symbolic-ref.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----------
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion builtin-branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
9 changes: 7 additions & 2 deletions builtin-fsck.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion builtin-init-db.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
17 changes: 8 additions & 9 deletions builtin-reflog.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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;
}
Expand Down Expand Up @@ -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];
Expand Down
5 changes: 4 additions & 1 deletion builtin-show-branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 14 additions & 2 deletions builtin-symbolic-ref.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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);

Expand All @@ -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++;
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
24 changes: 6 additions & 18 deletions git-checkout.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
Expand Down Expand Up @@ -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\""
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions reachable.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit d1f289c

Please sign in to comment.