{"thread":{"id":"2667","subject":"[RFC/PATCH 3/3] Check repository format version.","startedAt":"2005-11-24T03:36:53Z","lastAt":"2005-11-24T03:36:53Z","messageCount":1,"participants":["Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":3},"messages":[{"id":"12663","messageId":"7v8xvezpgq.fsf@assigned-by-dhcp.cox.net","threadId":"2667","inReplyTo":null,"subject":"[RFC/PATCH 3/3] Check repository format version.","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2005-11-24T03:36:53Z","receivedAt":"2005-11-24T03:36:53Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Add check_repository_format() that is to be called when we know\nwhere the configuration file is and die if the format version\nis too new to be understood by us.\n\nUse it in enter_repo(), setup_git_directory(), and git-find-git\nto prevent the commands that use them to corrupt future git\nrepositories.  What this patch does not cover are commands that\nimplicitly assumes it is always run from the toplevel, or the\nuser tells us where the git directory is with the GIT_DIR\nenvironment variable.\n\nsetup_git_directory() users that could corrupt future repository\nwithout this change are:\n\n\tconfig-set symbolic-ref update-index update-ref\n\nand that could yield incorrect results are:\n\n\tcat-file diff-files diff-index diff-stages diff-tree\n\tls-files name-rev rev-list rev-parse show-branch\n\nenter_repo() users are:\n\n\tdaemon receive-pack upload-pack\n\ngit-find-git is used by git-sh-setup, which means most of the\nbarebone Porcelain scripts are covered with this change upfront.\nThis might be enough for everyday use to futureproof us.\n\nThe repository format check code was originally done by Martin\nAtukunda.\n\nSigned-off-by: Junio C Hamano <junkio@cox.net>\n\n---\n\n * The checks in this version is less intrusive compared to\n   Martin's version, and tries to check only once when we know\n   where the configuration file should be.\n\n cache.h         |    2 +-\n find-git.c      |    5 +++--\n git-sh-setup.sh |    2 +-\n path.c          |    1 +\n setup.c         |   24 ++++++++++++++++++++++--\n 5 files changed, 28 insertions(+), 6 deletions(-)\n\napplies-to: cfcbe85286bb9bfb4f04169269c543640626218d\n08279559b1d63107c4e25abb4c882ad21e1ad43b\ndiff --git a/cache.h b/cache.h\nindex 7cde341..c3e46f3 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -185,7 +185,7 @@ extern int diff_rename_limit_default;\n \n #define GIT_REPO_VERSION 0\n extern int repository_format_version;\n-extern int check_repo_format(void);\n+extern int check_repository_format(void);\n \n #define MTIME_CHANGED\t0x0001\n #define CTIME_CHANGED\t0x0002\ndiff --git a/find-git.c b/find-git.c\nindex e063425..430ba7c 100644\n--- a/find-git.c\n+++ b/find-git.c\n@@ -14,9 +14,10 @@ int main(int ac, char **av)\n \t */\n \tgit_config(git_default_config);\n \n-\t/* Later we check repository version and exit with non-zero\n-\t * status after issuing an error message here.\n+\t/* This would die after issuing an error message if we \n+\t * do not understand the repository format.\n \t */\n+\tcheck_repository_format();\n \n \tif (!prefix)\n \t\t/* we are at the project toplevel or GIT_DIR is set.\ndiff --git a/git-sh-setup.sh b/git-sh-setup.sh\nindex 8e30bf6..2485bb9 100755\n--- a/git-sh-setup.sh\n+++ b/git-sh-setup.sh\n@@ -37,6 +37,6 @@ case \"$?\" in\n \n \t# (128) git-find-git died -- malformed configuration and that\n \t# is really fatal.  We have already given the error message.\n-\n+\texit $?\n \t;;\n esac\ndiff --git a/path.c b/path.c\nindex 4d88947..2c077c0 100644\n--- a/path.c\n+++ b/path.c\n@@ -199,6 +199,7 @@ char *enter_repo(char *path, int strict)\n \tif(access(\"objects\", X_OK) == 0 && access(\"refs\", X_OK) == 0 &&\n \t   validate_symref(\"HEAD\") == 0) {\n \t\tputenv(\"GIT_DIR=.\");\n+\t\tcheck_repository_format();\n \t\treturn current_dir();\n \t}\n \ndiff --git a/setup.c b/setup.c\nindex f64ca23..48be931 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -73,7 +73,7 @@ const char **get_pathspec(const char *pr\n }\n \n /*\n- * Test it it looks like we're at the top\n+ * Test if it looks like we're at the top\n  * level git directory. We want to see a\n  *\n  *  - either a .git/objects/ directory _or_ the proper\n@@ -135,5 +135,25 @@ const char *setup_git_directory_gently(i\n \n const char *setup_git_directory(void)\n {\n-\treturn setup_git_directory_gently(NULL);\n+\tconst char *retval = setup_git_directory_gently(NULL);\n+\tcheck_repository_format();\n+\treturn retval;\n+}\n+\n+static int check_repository_format_version(const char *var, const char *value)\n+{\n+       if (strcmp(var, \"core.repositoryformatversion\") == 0) {\n+               repository_format_version = git_config_int(var, value);\n+       }\n+       return 0;\n+}\n+\n+int check_repository_format(void)\n+{\n+\tif (git_config(check_repository_format_version) == -1)\n+\t\treturn -1;\n+\tif (GIT_REPO_VERSION < repository_format_version)\n+\t\tdie(\"Expected git repo version <= %d, found %d\",\n+\t\t    GIT_REPO_VERSION, repository_format_version);\n+\treturn 0;\n }\n---\n0.99.9.GIT\n"}]}