{"thread":{"id":"18517","subject":"[PATCH] init-db: support --import to add all files and commit right after init","startedAt":"2009-03-25T02:09:56Z","lastAt":"2009-03-31T09:27:31Z","messageCount":10,"participants":["Nguyễn Thái Ngọc Duy","Shawn O. Pearce","Nguyen Thai Ngoc Duy","Johannes Schindelin","Jeff King","Miles Bader"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"109290","messageId":"1237946996-5287-1-git-send-email-pclouds@gmail.com","threadId":"18517","inReplyTo":null,"subject":"[PATCH] init-db: 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-25T02:09:56Z","receivedAt":"2009-03-25T02:09:56Z","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 I'm not really sure about document format as I failed to install\n xmlto on my machine. Html output looked fine though.\n\n Documentation/git-init.txt |   16 +++++++++++++-\n builtin-init-db.c          |   49 ++++++++++++++++++++++++++++++++++++++++---\n t/t0001-init.sh            |   32 ++++++++++++++++++++++++++++\n 3 files changed, 92 insertions(+), 5 deletions(-)\n\ndiff --git a/Documentation/git-init.txt b/Documentation/git-init.txt\nindex 71749c0..52af645 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 <message> | --import=<message>]\n \n \n OPTIONS\n@@ -68,6 +69,19 @@ 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 <message>::\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+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..5bf563a 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 <message> | --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,24 @@ 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\tif (i+1 >= argc)\n+\t\t\t\tdie(\"--import requires an import message\");\n+\t\t\timport_message = argv[2];\n+\t\t\ti++;\n+\t\t\targv++;\n+\t\t}\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 +476,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..86414f0 100755\n--- a/t/t0001-init.sh\n+++ b/t/t0001-init.sh\n@@ -199,4 +199,36 @@ 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 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 test\n+\t)\n+\n+'\n+\n+test_expect_success 'init --import' '\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 'reinit --import' '\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":"109293","messageId":"20090325021458.GY23521@spearce.org","threadId":"18517","inReplyTo":"1237946996-5287-1-git-send-email-pclouds@gmail.com","subject":"Re: [PATCH] init-db: support --import to add all files and commit right after init","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2009-03-25T02:14:58Z","receivedAt":"2009-03-25T02:14:58Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"Nguy???n Th??i Ng???c Duy <pclouds@gmail.com> wrote:\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\nWhy not:\n\n  git config --global alias.init-import '!git init && git add . && git commit -q'\n\n?\n\n-- \nShawn.\n"},{"id":"109295","messageId":"fcaeb9bf0903241927h5b4d20b9r30fd67c26e63c6f5@mail.gmail.com","threadId":"18517","inReplyTo":"20090325021458.GY23521@spearce.org","subject":"Re: [PATCH] init-db: support --import to add all files and commit right after init","fromName":"Nguyen Thai Ngoc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2009-03-25T02:27:52Z","receivedAt":"2009-03-25T02:27:52Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Wed, Mar 25, 2009 at 1:14 PM, Shawn O. Pearce <spearce@spearce.org> wrote:\n> Nguy???n Th??i Ng???c Duy <pclouds@gmail.com> wrote:\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>\n> Why not:\n>\n>  git config --global alias.init-import '!git init && git add . && git commit -q'\n\n1. less setup on new machine\n2. fast-import support (not yet though)\n-- \nDuy\n"},{"id":"109304","messageId":"alpine.DEB.1.00.0903250450240.10279@pacific.mpi-cbg.de","threadId":"18517","inReplyTo":"1237946996-5287-1-git-send-email-pclouds@gmail.com","subject":"Re: [PATCH] init-db: support --import to add all files and commit right after init","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2009-03-25T03:51:22Z","receivedAt":"2009-03-25T03:51:22Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi,\n\nOn Wed, 25 Mar 2009, 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> \n> In future, --fast-import support would also be nice if the import\n> directory has a lot of files.\n> \n> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n\nI wanted to have this for a _long_ time!\n\nBut could you please say \"init\" in the subject instead of \"init-db\"?  The \nlatter is just a historical wart we have to carry around.\n\nThanks,\nDscho"},{"id":"109308","messageId":"20090325041934.GA15524@coredump.intra.peff.net","threadId":"18517","inReplyTo":"1237946996-5287-1-git-send-email-pclouds@gmail.com","subject":"Re: [PATCH] init-db: support --import to add all files and commit right after init","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2009-03-25T04:19:35Z","receivedAt":"2009-03-25T04:19:35Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Wed, Mar 25, 2009 at 01:09:56PM +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\nI think I would find this handy. Shawn suggested an alias, but I think\nthis is perhaps \"universal enough\" to merit an actual command-line\noption.\n\n> +-m <message>::\n> +--import=<message>::\n> +\n> +Commit everything to the newly initialized repository. This is equivalent to:\n\nMaybe this is being too lazy, but I think it would be useful to allow\njust \"--import\" without a message to default to \"Initial commit\",\n\"initial import from `basename $PWD`\", or something like that.\n\n> +\t\telse if (!strcmp(arg, \"--import\") || !strcmp(arg, \"-m\")) {\n> +\t\t\tif (i+1 >= argc)\n> +\t\t\t\tdie(\"--import requires an import message\");\n> +\t\t\timport_message = argv[2];\n> +\t\t\ti++;\n> +\t\t\targv++;\n> +\t\t}\n\nI seem to recall that we were phasing out \"--long-option <arg>\" at some\npoint, and that all long-options should use \"--long-option=\". But maybe\nI am mis-remembering.\n\n-Peff\n"},{"id":"109312","messageId":"fcaeb9bf0903242125kf6281acp9f1289a1c0e3e79b@mail.gmail.com","threadId":"18517","inReplyTo":"alpine.DEB.1.00.0903250450240.10279@pacific.mpi-cbg.de","subject":"Re: [PATCH] init-db: support --import to add all files and commit right after init","fromName":"Nguyen Thai Ngoc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2009-03-25T04:25:44Z","receivedAt":"2009-03-25T04:25:44Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"2009/3/25 Johannes Schindelin <Johannes.Schindelin@gmx.de>:\n> But could you please say \"init\" in the subject instead of \"init-db\"?  The\n> latter is just a historical wart we have to carry around.\n\nOK. No problem.\n-- \nDuy\n"},{"id":"109314","messageId":"fcaeb9bf0903242132k71e78219xfda5553854df4a2c@mail.gmail.com","threadId":"18517","inReplyTo":"20090325041934.GA15524@coredump.intra.peff.net","subject":"Re: [PATCH] init-db: support --import to add all files and commit right after init","fromName":"Nguyen Thai Ngoc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2009-03-25T04:32:17Z","receivedAt":"2009-03-25T04:32:17Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"2009/3/25 Jeff King <peff@peff.net>:\n>> +-m <message>::\n>> +--import=<message>::\n>> +\n>> +Commit everything to the newly initialized repository. This is equivalent to:\n>\n> Maybe this is being too lazy, but I think it would be useful to allow\n> just \"--import\" without a message to default to \"Initial commit\",\n> \"initial import from `basename $PWD`\", or something like that.\n\nI tend to do \"commit -q -m i\", not a big deal for two more keystrokes.\nMy initial version allowed \"git init /path/to/workdir\" too so it might\nbe ambiguous for optional message. So all vote for \"Initial commit\"?\nDynamic message seems overkill because most of the cases, this is a\nthrow away repository.\n\n>> +             else if (!strcmp(arg, \"--import\") || !strcmp(arg, \"-m\")) {\n>> +                     if (i+1 >= argc)\n>> +                             die(\"--import requires an import message\");\n>> +                     import_message = argv[2];\n>> +                     i++;\n>> +                     argv++;\n>> +             }\n>\n> I seem to recall that we were phasing out \"--long-option <arg>\" at some\n> point, and that all long-options should use \"--long-option=\". But maybe\n> I am mis-remembering.\n\nI did not know too. Any reference?\n-- \nDuy\n"},{"id":"109315","messageId":"20090325044909.GA21311@coredump.intra.peff.net","threadId":"18517","inReplyTo":"fcaeb9bf0903242132k71e78219xfda5553854df4a2c@mail.gmail.com","subject":"Re: [PATCH] init-db: support --import to add all files and commit right after init","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2009-03-25T04:49:09Z","receivedAt":"2009-03-25T04:49:09Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Wed, Mar 25, 2009 at 03:32:17PM +1100, Nguyen Thai Ngoc Duy wrote:\n\n> I tend to do \"commit -q -m i\", not a big deal for two more keystrokes.\n> My initial version allowed \"git init /path/to/workdir\" too so it might\n> be ambiguous for optional message. So all vote for \"Initial commit\"?\n> Dynamic message seems overkill because most of the cases, this is a\n> throw away repository.\n\nInitial commit is fine with me. I plan to use this mostly for throw-away\nrepositories. I was thinking it might be useful for this:\n\n  tar xzf package-0.1.tar.gz\n  cd package-0.1\n  git init --import\n\nso that later you remember it came from package-0.1 (when you are\nlooking at package 0.2). OTOH, if we take the information from the\ndirectory, then the fact that it came from 0.1 is by definition already\nin the directory name.  So it is pretty pointless unless you later\nrename the directory to just \"package\".\n\n> > I seem to recall that we were phasing out \"--long-option <arg>\" at some\n> > point, and that all long-options should use \"--long-option=\". But maybe\n> > I am mis-remembering.\n> \n> I did not know too. Any reference?\n\nI was, in fact, mis-remembering. The problem is not about long versus\nshort options, but rather about options which have an optional value.\nSee \"Separating argument from the option\" in \"git help cli\".\n\nHowever, what we are discussing above _is_ making it an optional value,\nin which case \"--import <arg>\" would have no value for \"--import\".\n\n-Peff\n"},{"id":"109984","messageId":"buomyb2cb3c.fsf@dhlpc061.dev.necel.com","threadId":"18517","inReplyTo":"20090325041934.GA15524@coredump.intra.peff.net","subject":"Re: [PATCH] init-db: support --import to add all files and commit right after init","fromName":"Miles Bader","fromEmail":"miles@gnu.org","sentAt":"2009-03-31T09:09:43Z","receivedAt":"2009-03-31T09:09:43Z","isPatch":true,"sender":{"key":"miles@gnu.org","avatar":"https://gravatar.com/avatar/01069b69593af7bff28e2f97afeb3644ae6fe2f5f56cb3a8cf34c5fb8c36efe5?d=mp&s=160"},"body":"Jeff King <peff@peff.net> writes:\n> I seem to recall that we were phasing out \"--long-option <arg>\" at some\n> point, and that all long-options should use \"--long-option=\". But maybe\n> I am mis-remembering.\n\nSurely it should support both where possible, since both are standard\nsyntaxes for passing args to \"--\"-style long options (so users expect\nboth to work).\n\n-Miles\n\n-- \nOccam's razor split hairs so well, I bought the whole argument!\n"},{"id":"109986","messageId":"20090331092731.GA1276@coredump.intra.peff.net","threadId":"18517","inReplyTo":"buomyb2cb3c.fsf@dhlpc061.dev.necel.com","subject":"Re: [PATCH] init-db: support --import to add all files and commit right after init","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2009-03-31T09:27:31Z","receivedAt":"2009-03-31T09:27:31Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Tue, Mar 31, 2009 at 06:09:43PM +0900, Miles Bader wrote:\n\n> Jeff King <peff@peff.net> writes:\n> > I seem to recall that we were phasing out \"--long-option <arg>\" at some\n> > point, and that all long-options should use \"--long-option=\". But maybe\n> > I am mis-remembering.\n> \n> Surely it should support both where possible, since both are standard\n> syntaxes for passing args to \"--\"-style long options (so users expect\n> both to work).\n\nYes.  See later in the thread where I correctly remember the issue.\n\n-Peff\n"}]}