Skip to content

Commit 5f856dd

Browse files
author
Junio C Hamano
committed
fix reflog entries for "git-branch"
Even when -l is not given from the command line, the repository may have the configuration variable core.logallrefupdates set, or an old-timer might have done ": >.git/logs/refs/heads/new" before running "git branch new". In these cases, the code gave an uninitialized msg[] from the stack to be written out as the reflog message. This also passes a different message when '-f' option is used. Saying "git branch -f branch some-commit" is a moral equilvalent of doing "git-reset some-commit" while on the branch. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 505739f commit 5f856dd

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

builtin-branch.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ static void create_branch(const char *name, const char *start_name,
316316
struct commit *commit;
317317
unsigned char sha1[20];
318318
char ref[PATH_MAX], msg[PATH_MAX + 20];
319+
int forcing = 0;
319320

320321
snprintf(ref, sizeof ref, "refs/heads/%s", name);
321322
if (check_ref_format(ref))
@@ -326,6 +327,7 @@ static void create_branch(const char *name, const char *start_name,
326327
die("A branch named '%s' already exists.", name);
327328
else if (!is_bare_repository() && !strcmp(head, name))
328329
die("Cannot force update the current branch.");
330+
forcing = 1;
329331
}
330332

331333
if (start_sha1)
@@ -342,11 +344,15 @@ static void create_branch(const char *name, const char *start_name,
342344
if (!lock)
343345
die("Failed to lock ref for update: %s.", strerror(errno));
344346

345-
if (reflog) {
347+
if (reflog)
346348
log_all_ref_updates = 1;
349+
350+
if (forcing)
351+
snprintf(msg, sizeof msg, "branch: Reset from %s",
352+
start_name);
353+
else
347354
snprintf(msg, sizeof msg, "branch: Created from %s",
348355
start_name);
349-
}
350356

351357
if (write_ref_sha1(lock, sha1, msg) < 0)
352358
die("Failed to write ref: %s.", strerror(errno));

0 commit comments

Comments
 (0)