{"thread":{"id":"18568","subject":"[PATCH v3] init: support --import to add all files and commit right after init","startedAt":"2009-03-26T09:49:03Z","lastAt":"2009-03-26T09:57:16Z","messageCount":2,"participants":["Nguyễn Thái Ngọc Duy","Jeff King"],"isPatch":true,"patchVersion":3,"patchTotal":null},"messages":[{"id":"109515","messageId":"1238060943-4694-1-git-send-email-pclouds@gmail.com","threadId":"18568","inReplyTo":null,"subject":"[PATCH v3] init: support --import to add all files and commit right after init","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2009-03-26T09:49:03Z","receivedAt":"2009-03-26T09:49:03Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"This is equivalent to \"git init;git add .;git commit -q -m blah\".\nI find myself doing that too many times, hence this shortcut.\n\nIn future, --fast-import support would also be nice if the import\ndirectory has a lot of files.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n Hopefully I don't make any mistake this time. -m will not take any argument\n for two reasons:\n\n  - optional argument for short options does not appear anywhere in git AFAIK.\n    let's not make parse-opt migration more difficult with -mfoo or -m=foo\n  - you want it short, so make it short :) if you want your own message, you\n    probably won't mind typing a few more: --import=blah\n\n Documentation/git-init.txt |   17 ++++++++++++++++-\n builtin-init-db.c          |   44 ++++++++++++++++++++++++++++++++++++++++----\n t/t0001-init.sh            |   44 ++++++++++++++++++++++++++++++++++++++++++++\n 3 files changed, 100 insertions(+), 5 deletions(-)\n\ndiff --git a/Documentation/git-init.txt b/Documentation/git-init.txt\nindex 71749c0..52bc34a 100644\n--- a/Documentation/git-init.txt\n+++ b/Documentation/git-init.txt\n@@ -8,7 +8,8 @@ git-init - Create an empty git repository or reinitialize an existing one\n \n SYNOPSIS\n --------\n-'git init' [-q | --quiet] [--bare] [--template=<template_directory>] [--shared[=<permissions>]]\n+'git init' [-q | --quiet] [--bare] [--template=<template_directory>]\n+           [--shared[=<permissions>]] [-m | --import[=<message>]]\n \n \n OPTIONS\n@@ -68,6 +69,20 @@ By default, the configuration flag receive.denyNonFastForwards is enabled\n in shared repositories, so that you cannot force a non fast-forwarding push\n into it.\n \n+-m::\n+--import[=<message>]::\n+\n+Commit everything to the newly initialized repository. This is equivalent to:\n+\n+----------------\n+$ git init\n+$ git add .\n+$ git commit -q -m <message>\n+----------------\n+\n+If `-m` is used or no message is given to `--import`, \"Initial commit\" will be used.\n+This option does not work with `--bare` nor already initialized repository.\n+\n --\n \n \ndiff --git a/builtin-init-db.c b/builtin-init-db.c\nindex ee3911f..c787909 100644\n--- a/builtin-init-db.c\n+++ b/builtin-init-db.c\n@@ -6,6 +6,7 @@\n #include \"cache.h\"\n #include \"builtin.h\"\n #include \"exec_cmd.h\"\n+#include \"run-command.h\"\n \n #ifndef DEFAULT_GIT_TEMPLATE_DIR\n #define DEFAULT_GIT_TEMPLATE_DIR \"/usr/share/git-core/templates\"\n@@ -19,6 +20,7 @@\n \n static int init_is_bare_repository = 0;\n static int init_shared_repository = -1;\n+static int reinit;\n \n static void safe_create_dir(const char *dir, int share)\n {\n@@ -279,7 +281,7 @@ int init_db(const char *template_dir, unsigned int flags)\n {\n \tconst char *sha1_dir;\n \tchar *path;\n-\tint len, reinit;\n+\tint len;\n \n \tsafe_create_dir(get_git_dir(), 0);\n \n@@ -363,8 +365,29 @@ static int guess_repository_type(const char *git_dir)\n \treturn 1;\n }\n \n+static int import_files(const char *import_message)\n+{\n+\tconst char *args[6];\n+\tint i = 0;\n+\tint ret;\n+\n+\targs[i++] = \"add\";\n+\targs[i++] = \".\";\n+\targs[i] = NULL;\n+\tret = run_command_v_opt(args, RUN_GIT_CMD);\n+\tif (ret)\n+\t\treturn ret;\n+\ti = 0;\n+\targs[i++] = \"commit\";\n+\targs[i++] = \"-q\";\n+\targs[i++] = \"-m\";\n+\targs[i++] = import_message;\n+\targs[i] = NULL;\n+\treturn run_command_v_opt(args, RUN_GIT_CMD);\n+}\n+\n static const char init_db_usage[] =\n-\"git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]]\";\n+\"git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]] [-m|--import[=<message>]]\";\n \n /*\n  * If you want to, you can share the DB area with any number of branches.\n@@ -377,7 +400,8 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)\n \tconst char *git_dir;\n \tconst char *template_dir = NULL;\n \tunsigned int flags = 0;\n-\tint i;\n+\tconst char *import_message = NULL;\n+\tint ret, i;\n \n \tfor (i = 1; i < argc; i++, argv++) {\n \t\tconst char *arg = argv[1];\n@@ -392,12 +416,19 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)\n \t\t\tinit_shared_repository = PERM_GROUP;\n \t\telse if (!prefixcmp(arg, \"--shared=\"))\n \t\t\tinit_shared_repository = git_config_perm(\"arg\", arg+9);\n+\t\telse if (!strcmp(arg, \"--import\") || !strcmp(arg, \"-m\"))\n+\t\t\timport_message = \"Initial commit\";\n+\t\telse if (!prefixcmp(arg, \"--import=\"))\n+\t\t\timport_message = arg+9;\n \t\telse if (!strcmp(arg, \"-q\") || !strcmp(arg, \"--quiet\"))\n \t\t\tflags |= INIT_DB_QUIET;\n \t\telse\n \t\t\tusage(init_db_usage);\n \t}\n \n+\tif (import_message && is_bare_repository_cfg == 1)\n+\t\tdie(\"--import does not work with --bare\");\n+\n \t/*\n \t * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR\n \t * without --bare.  Catch the error early.\n@@ -440,5 +471,10 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)\n \n \tset_git_dir(make_absolute_path(git_dir));\n \n-\treturn init_db(template_dir, flags);\n+\tret = init_db(template_dir, flags);\n+\tif (ret || !import_message)\n+\t\treturn ret;\n+\tif (reinit)\n+\t\tdie(\"--import does not work with already initialized repository\");\n+\treturn import_files(import_message);\n }\ndiff --git a/t/t0001-init.sh b/t/t0001-init.sh\nindex 5ac0a27..b8e8bfc 100755\n--- a/t/t0001-init.sh\n+++ b/t/t0001-init.sh\n@@ -199,4 +199,48 @@ test_expect_success 'init honors global core.sharedRepository' '\n \tx`git config -f shared-honor-global/.git/config core.sharedRepository`\n '\n \n+test_expect_success 'init --import does not work with --bare' '\n+\t(\n+\t\tunset GIT_DIR GIT_WORK_TREE GIT_CONFIG\n+\t\tmkdir init-import-bare.git &&\n+\t\tcd init-import-bare.git &&\n+\t\t! git init --bare --import\n+\t)\n+\n+'\n+\n+test_expect_success 'init --import no message' '\n+\t(\n+\t\ttest_tick\n+\t\tunset GIT_DIR GIT_WORK_TREE GIT_CONFIG\n+\t\tmkdir init-import-nomsg &&\n+\t\tcd init-import-nomsg &&\n+\t\ttouch foo bar &&\n+\t\tgit init --import &&\n+\t\ttest \"$(git rev-parse HEAD)\" = 4ff955458fd61a7b5d798b81e28c9249e8ebb5df\n+\t)\n+'\n+\n+test_expect_success 'init --import=msg' '\n+\t(\n+\t\ttest_tick\n+\t\tunset GIT_DIR GIT_WORK_TREE GIT_CONFIG\n+\t\tmkdir init-import &&\n+\t\tcd init-import &&\n+\t\ttouch foo bar &&\n+\t\tgit init --import=test &&\n+\t\ttest \"$(git rev-parse HEAD)\" = 758aa5a579e42200a6fd4e4964c7e1dc1875d67d\n+\t)\n+'\n+\n+test_expect_success 'init --import on already init(ed) should fail' '\n+\t(\n+\t\ttest_tick\n+\t\tunset GIT_DIR GIT_WORK_TREE GIT_CONFIG\n+\t\tcd init-import &&\n+\t\t! git init --import=testagain &&\n+\t\ttest \"$(git rev-parse HEAD)\" = 758aa5a579e42200a6fd4e4964c7e1dc1875d67d\n+\t)\n+'\n+\n test_done\n-- \n1.6.1.446.gc7851\n"},{"id":"109516","messageId":"20090326095716.GD14292@coredump.intra.peff.net","threadId":"18568","inReplyTo":"1238060943-4694-1-git-send-email-pclouds@gmail.com","subject":"Re: [PATCH v3] init: support --import to add all files and commit right after init","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2009-03-26T09:57:16Z","receivedAt":"2009-03-26T09:57:16Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Thu, Mar 26, 2009 at 08:49:03PM +1100, Nguyễn Thái Ngọc Duy wrote:\n\n> This is equivalent to \"git init;git add .;git commit -q -m blah\".\n> I find myself doing that too many times, hence this shortcut.\n\nThis version looks OK to me, but I have a few comments:\n\n>  Hopefully I don't make any mistake this time. -m will not take any argument\n>  for two reasons:\n> \n>   - optional argument for short options does not appear anywhere in git AFAIK.\n>     let's not make parse-opt migration more difficult with -mfoo or -m=foo\n\nshortlog's -w has optional arguments (and is the option that spawned the\ndiscussion that led to Pierre writing the gitcli(7) text, IIRC). So\nparse-options does handle it, but you _cannot_ say \"-m foo\" anymore.\n\n> +-m::\n> +--import[=<message>]::\n\nNit: -m made sense when it specified a message. But now that it doesn't\ntake a message, maybe \"-i\" would be more appropriate?\n\n> +If `-m` is used or no message is given to `--import`, \"Initial commit\" will be used.\n\nStyle nit: long line.\n\n-Peff\n"}]}