summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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-symbolic-ref.c16
-rw-r--r--cache.h2
-rwxr-xr-xgit-checkout.sh7
-rw-r--r--reachable.c4
-rw-r--r--refs.c201
-rw-r--r--refs.h8
11 files changed, 185 insertions, 90 deletions
diff --git a/Documentation/git-symbolic-ref.txt b/Documentation/git-symbolic-ref.txt
index f93b79a85e..a88f722860 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 9b68d3b69f..2d8d61b453 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 f6f1967449..6da3814d59 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 1865489381..12e43d0db4 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 b443ed9ef6..bfb169ac04 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-symbolic-ref.c b/builtin-symbolic-ref.c
index 227c9d4a62..d41b40640b 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 9873ee97d9..201704bacf 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 99a81f509a..2c8cbe43a6 100755
--- a/git-checkout.sh
+++ b/git-checkout.sh
@@ -257,7 +257,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 +270,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 a6a334822a..01760d7046 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 3db444cad2..03e8dfec9f 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 33450f13e7..acedffc0e4 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);