Skip to content

Commit 8b5157e

Browse files
Nicolas PitreJunio C Hamano
authored andcommitted
add logref support to git-symbolic-ref
Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent 41b625b commit 8b5157e

File tree

6 files changed

+35
-8
lines changed

6 files changed

+35
-8
lines changed

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
@@ -381,7 +381,8 @@ static void rename_branch(const char *oldname, const char *newname, int force)
381381
if (rename_ref(oldref, newref, logmsg))
382382
die("Branch rename failed");
383383

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

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-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);

refs.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -986,18 +986,23 @@ int write_ref_sha1(struct ref_lock *lock,
986986
return 0;
987987
}
988988

989-
int create_symref(const char *ref_target, const char *refs_heads_master)
989+
int create_symref(const char *ref_target, const char *refs_heads_master,
990+
const char *logmsg)
990991
{
991992
const char *lockpath;
992993
char ref[1000];
993994
int fd, len, written;
994995
const char *git_HEAD = git_path("%s", ref_target);
996+
unsigned char old_sha1[20], new_sha1[20];
997+
998+
if (logmsg && read_ref(ref_target, old_sha1))
999+
hashclr(old_sha1);
9951000

9961001
#ifndef NO_SYMLINK_HEAD
9971002
if (prefer_symlink_refs) {
9981003
unlink(git_HEAD);
9991004
if (!symlink(refs_heads_master, git_HEAD))
1000-
return 0;
1005+
goto done;
10011006
fprintf(stderr, "no symlink - falling back to symbolic ref\n");
10021007
}
10031008
#endif
@@ -1030,6 +1035,11 @@ int create_symref(const char *ref_target, const char *refs_heads_master)
10301035
error("Unable to fix permissions on %s", lockpath);
10311036
return -4;
10321037
}
1038+
1039+
done:
1040+
if (logmsg && !read_ref(refs_heads_master, new_sha1))
1041+
log_ref_write(ref_target, old_sha1, new_sha1, logmsg);
1042+
10331043
return 0;
10341044
}
10351045

0 commit comments

Comments
 (0)