@matheusbsbs / matheus-bagliano / commits / 5f856dd47d
Fork 0

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 <junkio@cox.net>

Junio C Hamano committed Jan 19, 2007 at 11:51 UTC 5f856dd47dacb30fb9f605b4b7e1fa577ada7d26
1 file changed +8 -2
builtin-branch.c
+8 -2
@@ -316,6 +316,7 @@ static void create_branch(const char *name, const char *start_name,
316 struct commit *commit;
317 unsigned char sha1[20];
318 char ref[PATH_MAX], msg[PATH_MAX + 20];
319 + int forcing = 0;
320
321 snprintf(ref, sizeof ref, "refs/heads/%s", name);
322 if (check_ref_format(ref))
@@ -326,6 +327,7 @@ static void create_branch(const char *name, const char *start_name,
327 die("A branch named '%s' already exists.", name);
328 else if (!is_bare_repository() && !strcmp(head, name))
329 die("Cannot force update the current branch.");
330 + forcing = 1;
331 }
332
333 if (start_sha1)
@@ -342,11 +344,15 @@ static void create_branch(const char *name, const char *start_name,
344 if (!lock)
345 die("Failed to lock ref for update: %s.", strerror(errno));
346
345 - if (reflog) {
347 + if (reflog)
348 log_all_ref_updates = 1;
349 +
350 + if (forcing)
351 + snprintf(msg, sizeof msg, "branch: Reset from %s",
352 + start_name);
353 + else
354 snprintf(msg, sizeof msg, "branch: Created from %s",
355 start_name);
349 - }
356
357 if (write_ref_sha1(lock, sha1, msg) < 0)
358 die("Failed to write ref: %s.", strerror(errno));