Enable reflogs by default in any repository with a working directory.

New and experienced Git users alike are finding out too late that
they forgot to enable reflogs in the current repository, and cannot
use the information stored within it to recover from an incorrectly
entered command such as `git reset --hard HEAD^^^` when they really
meant HEAD^^ (aka HEAD~2).

So enable reflogs by default in all future versions of Git, unless
the user specifically disables it with:

  [core]
    logAllRefUpdates = false

in their .git/config or ~/.gitconfig.

We only enable reflogs in repositories that have a working directory
associated with them, as shared/bare repositories do not have
an easy means to prune away old log entries, or may fail logging
entirely if the user's gecos information is not valid during a push.
This heuristic was suggested on the mailing list by Junio.

Documentation was also updated to indicate the new default behavior.
We probably should start to teach usuing the reflog to recover
from mistakes in some of the tutorial material, as new users are
likely to make a few along the way and will feel better knowing
they can recover from them quickly and easily, without fsck-objects'
lost+found features.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
This commit is contained in:
Shawn O. Pearce 2006-12-14 17:41:17 -05:00 committed by Junio C Hamano
parent ef0a89a604
commit 0bee591869
5 changed files with 21 additions and 4 deletions

View File

@ -79,8 +79,11 @@ core.logAllRefUpdates::
file is automatically created for branch heads. file is automatically created for branch heads.
This information can be used to determine what commit This information can be used to determine what commit
was the tip of a branch "2 days ago". This value is was the tip of a branch "2 days ago".
false by default (no automated creation of log files).
This value is true by default in a repository that has
a working directory associated with it, and false by
default in a bare repository.
core.repositoryFormatVersion:: core.repositoryFormatVersion::
Internal variable identifying the repository format and layout Internal variable identifying the repository format and layout

View File

@ -242,6 +242,9 @@ static int create_default_files(const char *git_dir, const char *template_path)
filemode ? "true" : "false"); filemode ? "true" : "false");
} }
/* Enable logAllRefUpdates if a working tree is attached */
if (!is_bare_git_dir(git_dir))
git_config_set("core.logallrefupdates", "true");
return reinit; return reinit;
} }

View File

@ -123,6 +123,7 @@ extern int cache_errno;
#define INDEX_ENVIRONMENT "GIT_INDEX_FILE" #define INDEX_ENVIRONMENT "GIT_INDEX_FILE"
#define GRAFT_ENVIRONMENT "GIT_GRAFT_FILE" #define GRAFT_ENVIRONMENT "GIT_GRAFT_FILE"
extern int is_bare_git_dir(const char *dir);
extern const char *get_git_dir(void); extern const char *get_git_dir(void);
extern char *get_object_directory(void); extern char *get_object_directory(void);
extern char *get_refs_directory(void); extern char *get_refs_directory(void);

View File

@ -48,6 +48,16 @@ static void setup_git_env(void)
git_graft_file = getenv(GRAFT_ENVIRONMENT); git_graft_file = getenv(GRAFT_ENVIRONMENT);
if (!git_graft_file) if (!git_graft_file)
git_graft_file = xstrdup(git_path("info/grafts")); git_graft_file = xstrdup(git_path("info/grafts"));
log_all_ref_updates = !is_bare_git_dir(git_dir);
}
int is_bare_git_dir (const char *dir)
{
const char *s;
if (!strcmp(dir, DEFAULT_GIT_DIR_ENVIRONMENT))
return 0;
s = strrchr(dir, '/');
return !s || strcmp(s + 1, DEFAULT_GIT_DIR_ENVIRONMENT);
} }
const char *get_git_dir(void) const char *get_git_dir(void)

View File

@ -63,8 +63,8 @@ test_expect_failure \
"test $B"' = $(cat .git/'"$m"')' "test $B"' = $(cat .git/'"$m"')'
rm -f .git/$m rm -f .git/$m
mkdir -p .git/logs/refs/heads : a repository with working tree always has reflog these days...
touch .git/logs/refs/heads/master : >.git/logs/refs/heads/master
test_expect_success \ test_expect_success \
"create $m (logged by touch)" \ "create $m (logged by touch)" \
'GIT_COMMITTER_DATE="2005-05-26 23:30" \ 'GIT_COMMITTER_DATE="2005-05-26 23:30" \