branch: record creation of renamed branch in HEAD's log

Renaming the current branch adds an event to the current branch's log
and to HEAD's log.  However, the logged entries differ.  The entry in
the branch's log represents the entire renaming operation (the old and
new hash are identical), whereas the entry in HEAD's log represents
the deletion only (the new sha1 is null).

Extend replace_each_worktree_head_symref(), whose only caller is
branch_rename(), to take a reflog message argument.  This allows the
creation of the new ref to be recorded in HEAD's log.  As a result,
the renaming event is represented by two entries (a deletion and a
creation entry) in HEAD's log.

It's a bit unfortunate that the branch's log and HEAD's log now
represent the renaming event in different ways.  Given that the
renaming operation is not atomic, the two-entry form is a more
accurate representation of the operation and is more useful for
debugging purposes if a failure occurs between the deletion and
creation events.  It would make sense to move the branch's log to the
two-entry form, but this would involve changes to how the rename is
carried out and to how the update flags and reflogs are processed for
deletions, so it may not be worth the effort.

Based-on-patch-by: Jeff King <peff@peff.net>
Signed-off-by: Kyle Meyer <kyle@kyleam.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Kyle Meyer 2017-02-20 20:10:35 -05:00 коммит произвёл Junio C Hamano
Родитель 893dbf5ba1
Коммит 39ee4c6c2f
6 изменённых файлов: 15 добавлений и 10 удалений

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

@ -345,7 +345,8 @@ void die_if_checked_out(const char *branch, int ignore_current_worktree)
branch, wt->path);
}
int replace_each_worktree_head_symref(const char *oldref, const char *newref)
int replace_each_worktree_head_symref(const char *oldref, const char *newref,
const char *logmsg)
{
int ret = 0;
struct worktree **worktrees = get_worktrees(0);
@ -358,7 +359,7 @@ int replace_each_worktree_head_symref(const char *oldref, const char *newref)
continue;
if (set_worktree_head_symref(get_worktree_git_dir(worktrees[i]),
newref)) {
newref, logmsg)) {
ret = -1;
error(_("HEAD of working tree %s is not updated"),
worktrees[i]->path);

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

@ -71,6 +71,7 @@ extern void die_if_checked_out(const char *branch, int ignore_current_worktree);
* This will be used when renaming a branch. Returns 0 if successful, non-zero
* otherwise.
*/
extern int replace_each_worktree_head_symref(const char *oldref, const char *newref);
extern int replace_each_worktree_head_symref(const char *oldref, const char *newref,
const char *logmsg);
#endif

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

@ -579,14 +579,15 @@ static void rename_branch(const char *oldname, const char *newname, int force)
if (rename_ref(oldref.buf, newref.buf, logmsg.buf))
die(_("Branch rename failed"));
strbuf_release(&logmsg);
if (recovery)
warning(_("Renamed a misnamed branch '%s' away"), oldref.buf + 11);
if (replace_each_worktree_head_symref(oldref.buf, newref.buf))
if (replace_each_worktree_head_symref(oldref.buf, newref.buf, logmsg.buf))
die(_("Branch renamed to %s, but HEAD is not updated!"), newname);
strbuf_release(&logmsg);
strbuf_addf(&oldsection, "branch.%s", oldref.buf + 11);
strbuf_release(&oldref);
strbuf_addf(&newsection, "branch.%s", newref.buf + 11);

3
refs.h
Просмотреть файл

@ -334,7 +334,8 @@ int create_symref(const char *refname, const char *target, const char *logmsg);
* $GIT_DIR points to.
* Return 0 if successful, non-zero otherwise.
* */
int set_worktree_head_symref(const char *gitdir, const char *target);
int set_worktree_head_symref(const char *gitdir, const char *target,
const char *logmsg);
enum action_on_err {
UPDATE_REFS_MSG_ON_ERR,

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

@ -3055,7 +3055,7 @@ static int files_create_symref(struct ref_store *ref_store,
return ret;
}
int set_worktree_head_symref(const char *gitdir, const char *target)
int set_worktree_head_symref(const char *gitdir, const char *target, const char *logmsg)
{
static struct lock_file head_lock;
struct ref_lock *lock;
@ -3083,7 +3083,7 @@ int set_worktree_head_symref(const char *gitdir, const char *target)
lock->lk = &head_lock;
lock->ref_name = xstrdup(head_rel);
ret = create_symref_locked(lock, head_rel, target, NULL);
ret = create_symref_locked(lock, head_rel, target, logmsg);
unlock_ref(lock); /* will free lock */
strbuf_release(&head_path);

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

@ -139,9 +139,10 @@ test_expect_success 'git branch -M baz bam should succeed when baz is checked ou
test $(git rev-parse --abbrev-ref HEAD) = bam
'
test_expect_success 'git branch -M baz bam should add entry to .git/logs/HEAD' '
test_expect_success 'git branch -M baz bam should add entries to .git/logs/HEAD' '
msg="Branch: renamed refs/heads/baz to refs/heads/bam" &&
grep " 0\{40\}.*$msg$" .git/logs/HEAD
grep " 0\{40\}.*$msg$" .git/logs/HEAD &&
grep "^0\{40\}.*$msg$" .git/logs/HEAD
'
test_expect_success 'git branch -M baz bam should succeed when baz is checked out as linked working tree' '