add logref support to git-symbolic-ref

Signed-off-by: Nicolas Pitre <nico@cam.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
This commit is contained in:
Nicolas Pitre 2007-01-26 17:26:10 -05:00 коммит произвёл Junio C Hamano
Родитель 41b625b047
Коммит 8b5157e407
6 изменённых файлов: 35 добавлений и 8 удалений

Просмотреть файл

@ -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

Просмотреть файл

@ -381,7 +381,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);
}

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

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

14
refs.c
Просмотреть файл

@ -986,18 +986,23 @@ int write_ref_sha1(struct ref_lock *lock,
return 0;
}
int create_symref(const char *ref_target, const char *refs_heads_master)
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;
const char *git_HEAD = 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))
return 0;
goto done;
fprintf(stderr, "no symlink - falling back to symbolic ref\n");
}
#endif
@ -1030,6 +1035,11 @@ int create_symref(const char *ref_target, const char *refs_heads_master)
error("Unable to fix permissions on %s", lockpath);
return -4;
}
done:
if (logmsg && !read_ref(refs_heads_master, new_sha1))
log_ref_write(ref_target, old_sha1, new_sha1, logmsg);
return 0;
}