Skip to content

Commit d1f289c

Browse files
author
Junio C Hamano
committed
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
2 parents 6e2e1cf + 632ac9f commit d1f289c

14 files changed

+232
-135
lines changed

Documentation/git-show-branch.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ SYNOPSIS
1111
'git-show-branch' [--all] [--remotes] [--topo-order] [--current]
1212
[--more=<n> | --list | --independent | --merge-base]
1313
[--no-name | --sha1-name] [--topics] [<rev> | <glob>]...
14-
'git-show-branch' (-g|--reflog)[=<n>[,<base>]] [--list] <ref>
14+
'git-show-branch' (-g|--reflog)[=<n>[,<base>]] [--list] [<ref>]
1515

1616
DESCRIPTION
1717
-----------
@@ -97,11 +97,13 @@ OPTIONS
9797
will show the revisions given by "git rev-list {caret}master
9898
topic1 topic2"
9999

100-
--reflog[=<n>[,<base>]] <ref>::
100+
--reflog[=<n>[,<base>]] [<ref>]::
101101
Shows <n> most recent ref-log entries for the given
102102
ref. If <base> is given, <n> entries going back from
103103
that entry. <base> can be specified as count or date.
104-
`-g` can be used as a short-hand for this option.
104+
`-g` can be used as a short-hand for this option. When
105+
no explicit <ref> parameter is given, it defaults to the
106+
current branch (or `HEAD` if it is detached).
105107

106108
Note that --more, --list, --independent and --merge-base options
107109
are mutually exclusive.

Documentation/git-symbolic-ref.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ git-symbolic-ref - Read and modify symbolic refs
77

88
SYNOPSIS
99
--------
10-
'git-symbolic-ref' [-q] <name> [<ref>]
10+
'git-symbolic-ref' [-q] [-m <reason>] <name> [<ref>]
1111

1212
DESCRIPTION
1313
-----------
@@ -31,6 +31,10 @@ OPTIONS
3131
symbolic ref but a detached HEAD; instead exit with
3232
non-zero status silently.
3333

34+
-m::
35+
Update the reflog for <name> with <reason>. This is valid only
36+
when creating or updating a symbolic ref.
37+
3438
NOTES
3539
-----
3640
In the past, `.git/HEAD` was a symbolic link pointing at

builtin-branch.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,8 @@ static void rename_branch(const char *oldname, const char *newname, int force)
387387
if (rename_ref(oldref, newref, logmsg))
388388
die("Branch rename failed");
389389

390-
if (!strcmp(oldname, head) && create_symref("HEAD", newref))
390+
/* no need to pass logmsg here as HEAD didn't really move */
391+
if (!strcmp(oldname, head) && create_symref("HEAD", newref, NULL))
391392
die("Branch renamed to %s, but HEAD is not updated!", newname);
392393
}
393394

builtin-fsck.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,12 @@ static int fsck_handle_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
477477
return 0;
478478
}
479479

480+
static int fsck_handle_reflog(const char *logname, const unsigned char *sha1, int flag, void *cb_data)
481+
{
482+
for_each_reflog_ent(logname, fsck_handle_reflog_ent, NULL);
483+
return 0;
484+
}
485+
480486
static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
481487
{
482488
struct object *obj;
@@ -495,14 +501,13 @@ static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int f
495501
obj->used = 1;
496502
mark_reachable(obj, REACHABLE);
497503

498-
for_each_reflog_ent(refname, fsck_handle_reflog_ent, NULL);
499-
500504
return 0;
501505
}
502506

503507
static void get_default_heads(void)
504508
{
505509
for_each_ref(fsck_handle_ref, NULL);
510+
for_each_reflog(fsck_handle_reflog, NULL);
506511

507512
/*
508513
* Not having any default heads isn't really fatal, but

builtin-init-db.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ static int create_default_files(const char *git_dir, const char *template_path)
231231
strcpy(path + len, "HEAD");
232232
reinit = !read_ref("HEAD", sha1);
233233
if (!reinit) {
234-
if (create_symref("HEAD", "refs/heads/master") < 0)
234+
if (create_symref("HEAD", "refs/heads/master", NULL) < 0)
235235
exit(1);
236236
}
237237

builtin-reflog.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -242,20 +242,18 @@ static int expire_reflog(const char *ref, const unsigned char *sha1, int unused,
242242
struct cmd_reflog_expire_cb *cmd = cb_data;
243243
struct expire_reflog_cb cb;
244244
struct ref_lock *lock;
245-
char *newlog_path = NULL;
245+
char *log_file, *newlog_path = NULL;
246246
int status = 0;
247247

248-
if (strncmp(ref, "refs/", 5))
249-
return error("not a ref '%s'", ref);
250-
251248
memset(&cb, 0, sizeof(cb));
252249
/* we take the lock for the ref itself to prevent it from
253250
* getting updated.
254251
*/
255-
lock = lock_ref_sha1(ref + 5, sha1);
252+
lock = lock_any_ref_for_update(ref, sha1);
256253
if (!lock)
257254
return error("cannot lock ref '%s'", ref);
258-
if (!file_exists(lock->log_file))
255+
log_file = xstrdup(git_path("logs/%s", ref));
256+
if (!file_exists(log_file))
259257
goto finish;
260258
if (!cmd->dry_run) {
261259
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,
271269
if (fclose(cb.newlog))
272270
status |= error("%s: %s", strerror(errno),
273271
newlog_path);
274-
if (rename(newlog_path, lock->log_file)) {
272+
if (rename(newlog_path, log_file)) {
275273
status |= error("cannot rename %s to %s",
276-
newlog_path, lock->log_file);
274+
newlog_path, log_file);
277275
unlink(newlog_path);
278276
}
279277
}
280278
free(newlog_path);
279+
free(log_file);
281280
unlock_ref(lock);
282281
return status;
283282
}
@@ -351,7 +350,7 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
351350
}
352351

353352
if (do_all)
354-
status |= for_each_ref(expire_reflog, &cb);
353+
status |= for_each_reflog(expire_reflog, &cb);
355354
while (i < argc) {
356355
const char *ref = argv[i++];
357356
unsigned char sha1[20];

builtin-show-branch.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,10 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
690690

691691
if (ac == 0) {
692692
static const char *fake_av[2];
693-
fake_av[0] = "HEAD";
693+
const char *refname;
694+
695+
refname = resolve_ref("HEAD", sha1, 1, NULL);
696+
fake_av[0] = xstrdup(refname);
694697
fake_av[1] = NULL;
695698
av = fake_av;
696699
ac = 1;

builtin-symbolic-ref.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "refs.h"
44

55
static const char git_symbolic_ref_usage[] =
6-
"git-symbolic-ref [-q] name [ref]";
6+
"git-symbolic-ref [-q] [-m <reason>] name [ref]";
77

88
static void check_symref(const char *HEAD, int quiet)
99
{
@@ -25,6 +25,7 @@ static void check_symref(const char *HEAD, int quiet)
2525
int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
2626
{
2727
int quiet = 0;
28+
const char *msg = NULL;
2829

2930
git_config(git_default_config);
3031

@@ -34,6 +35,17 @@ int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
3435
break;
3536
else if (!strcmp("-q", arg))
3637
quiet = 1;
38+
else if (!strcmp("-m", arg)) {
39+
argc--;
40+
argv++;
41+
if (argc <= 1)
42+
break;
43+
msg = argv[1];
44+
if (!*msg)
45+
die("Refusing to perform update with empty message");
46+
if (strchr(msg, '\n'))
47+
die("Refusing to perform update with \\n in message");
48+
}
3749
else if (!strcmp("--", arg)) {
3850
argc--;
3951
argv++;
@@ -50,7 +62,7 @@ int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
5062
check_symref(argv[1], quiet);
5163
break;
5264
case 3:
53-
create_symref(argv[1], argv[2]);
65+
create_symref(argv[1], argv[2], msg);
5466
break;
5567
default:
5668
usage(git_symbolic_ref_usage);

cache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ extern int read_ref(const char *filename, unsigned char *sha1);
302302
extern const char *resolve_ref(const char *path, unsigned char *sha1, int, int *);
303303
extern int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref);
304304

305-
extern int create_symref(const char *ref, const char *refs_heads_master);
305+
extern int create_symref(const char *ref, const char *refs_heads_master, const char *logmsg);
306306
extern int validate_headref(const char *ref);
307307

308308
extern int base_name_compare(const char *name1, int len1, int mode1, const char *name2, int len2, int mode2);

git-checkout.sh

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -164,22 +164,9 @@ If you want to create a new branch from this checkout, you may do so
164164
(now or later) by using -b with the checkout command again. Example:
165165
git checkout -b <new_branch_name>"
166166
fi
167-
elif test -z "$oldbranch" && test -n "$branch"
167+
elif test -z "$oldbranch" && test -z "$quiet"
168168
then
169-
# Coming back...
170-
if test -z "$force"
171-
then
172-
git show-ref -d -s | grep "$old" >/dev/null || {
173-
echo >&2 \
174-
"You are not on any branch and switching to branch '$new_name'
175-
may lose your changes. At this point, you can do one of two things:
176-
(1) Decide it is Ok and say 'git checkout -f $new_name';
177-
(2) Start a new branch from the current commit, by saying
178-
'git checkout -b <branch-name>'.
179-
Leaving your HEAD detached; not switching to branch '$new_name'."
180-
exit 1;
181-
}
182-
fi
169+
echo >&2 "Previous HEAD position was $old"
183170
fi
184171

185172
if [ "X$old" = X ]
@@ -257,7 +244,7 @@ if [ "$?" -eq 0 ]; then
257244
fi
258245
if test -n "$branch"
259246
then
260-
GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD "refs/heads/$branch"
247+
GIT_DIR="$GIT_DIR" git-symbolic-ref -m "checkout: moving to $branch" HEAD "refs/heads/$branch"
261248
if test -z "$quiet"
262249
then
263250
echo >&2 "Switched to${newbranch:+ a new} branch \"$branch\""
@@ -270,8 +257,9 @@ if [ "$?" -eq 0 ]; then
270257
# git update-ref --detach HEAD $new
271258
# or something like that...
272259
#
273-
echo "$detached" >"$GIT_DIR/HEAD.new" &&
274-
mv "$GIT_DIR/HEAD.new" "$GIT_DIR/HEAD" ||
260+
git-rev-parse HEAD >"$GIT_DIR/HEAD.new" &&
261+
mv "$GIT_DIR/HEAD.new" "$GIT_DIR/HEAD" &&
262+
git-update-ref -m "checkout: moving to $arg" HEAD "$detached" ||
275263
die "Cannot detach HEAD"
276264
if test -n "$detach_warn"
277265
then

0 commit comments

Comments
 (0)