* [PATCH] init-db: support --import to add all files and commit right after init
@ 2009-03-25 2:09 Nguyễn Thái Ngọc Duy
2009-03-25 2:14 ` Shawn O. Pearce
` (2 more replies)
0 siblings, 3 replies; 10+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2009-03-25 2:09 UTC (permalink / raw)
To: git; +Cc: Nguyễn Thái Ngọc Duy
This is equivalent to "git init;git add .;git commit -q -m blah".
I find myself doing that too many times, hence this shortcut.
In future, --fast-import support would also be nice if the import
directory has a lot of files.
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
I'm not really sure about document format as I failed to install
xmlto on my machine. Html output looked fine though.
Documentation/git-init.txt | 16 +++++++++++++-
builtin-init-db.c | 49 ++++++++++++++++++++++++++++++++++++++++---
t/t0001-init.sh | 32 ++++++++++++++++++++++++++++
3 files changed, 92 insertions(+), 5 deletions(-)
diff --git a/Documentation/git-init.txt b/Documentation/git-init.txt
index 71749c0..52af645 100644
--- a/Documentation/git-init.txt
+++ b/Documentation/git-init.txt
@@ -8,7 +8,8 @@ git-init - Create an empty git repository or reinitialize an existing one
SYNOPSIS
--------
-'git init' [-q | --quiet] [--bare] [--template=<template_directory>] [--shared[=<permissions>]]
+'git init' [-q | --quiet] [--bare] [--template=<template_directory>]
+ [--shared[=<permissions>]] [-m <message> | --import=<message>]
OPTIONS
@@ -68,6 +69,19 @@ By default, the configuration flag receive.denyNonFastForwards is enabled
in shared repositories, so that you cannot force a non fast-forwarding push
into it.
+-m <message>::
+--import=<message>::
+
+Commit everything to the newly initialized repository. This is equivalent to:
+
+----------------
+$ git init
+$ git add .
+$ git commit -q -m <message>
+----------------
+
+This option does not work with `--bare` nor already initialized repository.
+
--
diff --git a/builtin-init-db.c b/builtin-init-db.c
index ee3911f..5bf563a 100644
--- a/builtin-init-db.c
+++ b/builtin-init-db.c
@@ -6,6 +6,7 @@
#include "cache.h"
#include "builtin.h"
#include "exec_cmd.h"
+#include "run-command.h"
#ifndef DEFAULT_GIT_TEMPLATE_DIR
#define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
@@ -19,6 +20,7 @@
static int init_is_bare_repository = 0;
static int init_shared_repository = -1;
+static int reinit;
static void safe_create_dir(const char *dir, int share)
{
@@ -279,7 +281,7 @@ int init_db(const char *template_dir, unsigned int flags)
{
const char *sha1_dir;
char *path;
- int len, reinit;
+ int len;
safe_create_dir(get_git_dir(), 0);
@@ -363,8 +365,29 @@ static int guess_repository_type(const char *git_dir)
return 1;
}
+static int import_files(const char *import_message)
+{
+ const char *args[6];
+ int i = 0;
+ int ret;
+
+ args[i++] = "add";
+ args[i++] = ".";
+ args[i] = NULL;
+ ret = run_command_v_opt(args, RUN_GIT_CMD);
+ if (ret)
+ return ret;
+ i = 0;
+ args[i++] = "commit";
+ args[i++] = "-q";
+ args[i++] = "-m";
+ args[i++] = import_message;
+ args[i] = NULL;
+ return run_command_v_opt(args, RUN_GIT_CMD);
+}
+
static const char init_db_usage[] =
-"git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]]";
+"git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]] [-m <message> | --import=<message>]";
/*
* If you want to, you can share the DB area with any number of branches.
@@ -377,7 +400,8 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
const char *git_dir;
const char *template_dir = NULL;
unsigned int flags = 0;
- int i;
+ const char *import_message = NULL;
+ int ret, i;
for (i = 1; i < argc; i++, argv++) {
const char *arg = argv[1];
@@ -392,12 +416,24 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
init_shared_repository = PERM_GROUP;
else if (!prefixcmp(arg, "--shared="))
init_shared_repository = git_config_perm("arg", arg+9);
+ else if (!strcmp(arg, "--import") || !strcmp(arg, "-m")) {
+ if (i+1 >= argc)
+ die("--import requires an import message");
+ import_message = argv[2];
+ i++;
+ argv++;
+ }
+ else if (!prefixcmp(arg, "--import="))
+ import_message = arg+9;
else if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet"))
flags |= INIT_DB_QUIET;
else
usage(init_db_usage);
}
+ if (import_message && is_bare_repository_cfg == 1)
+ die("--import does not work with --bare");
+
/*
* GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
* without --bare. Catch the error early.
@@ -440,5 +476,10 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
set_git_dir(make_absolute_path(git_dir));
- return init_db(template_dir, flags);
+ ret = init_db(template_dir, flags);
+ if (ret || !import_message)
+ return ret;
+ if (reinit)
+ die("--import does not work with already initialized repository");
+ return import_files(import_message);
}
diff --git a/t/t0001-init.sh b/t/t0001-init.sh
index 5ac0a27..86414f0 100755
--- a/t/t0001-init.sh
+++ b/t/t0001-init.sh
@@ -199,4 +199,36 @@ test_expect_success 'init honors global core.sharedRepository' '
x`git config -f shared-honor-global/.git/config core.sharedRepository`
'
+test_expect_success 'init --import with --bare' '
+ (
+ unset GIT_DIR GIT_WORK_TREE GIT_CONFIG
+ mkdir init-import-bare.git &&
+ cd init-import-bare.git &&
+ ! git init --bare --import test
+ )
+
+'
+
+test_expect_success 'init --import' '
+ (
+ test_tick
+ unset GIT_DIR GIT_WORK_TREE GIT_CONFIG
+ mkdir init-import &&
+ cd init-import &&
+ touch foo bar &&
+ git init --import test &&
+ test "$(git rev-parse HEAD)" = 758aa5a579e42200a6fd4e4964c7e1dc1875d67d
+ )
+'
+
+test_expect_success 'reinit --import' '
+ (
+ test_tick
+ unset GIT_DIR GIT_WORK_TREE GIT_CONFIG
+ cd init-import &&
+ ! git init --import testagain &&
+ test "$(git rev-parse HEAD)" = 758aa5a579e42200a6fd4e4964c7e1dc1875d67d
+ )
+'
+
test_done
--
1.6.1.446.gc7851
^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [PATCH] init-db: support --import to add all files and commit right after init
2009-03-25 2:09 [PATCH] init-db: support --import to add all files and commit right after init Nguyễn Thái Ngọc Duy
@ 2009-03-25 2:14 ` Shawn O. Pearce
2009-03-25 2:27 ` Nguyen Thai Ngoc Duy
2009-03-25 3:51 ` Johannes Schindelin
2009-03-25 4:19 ` Jeff King
2 siblings, 1 reply; 10+ messages in thread
From: Shawn O. Pearce @ 2009-03-25 2:14 UTC (permalink / raw)
To: Nguy???n Th??i Ng???c Duy; +Cc: git
Nguy???n Th??i Ng???c Duy <pclouds@gmail.com> wrote:
> This is equivalent to "git init;git add .;git commit -q -m blah".
> I find myself doing that too many times, hence this shortcut.
Why not:
git config --global alias.init-import '!git init && git add . && git commit -q'
?
--
Shawn.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] init-db: support --import to add all files and commit right after init
2009-03-25 2:14 ` Shawn O. Pearce
@ 2009-03-25 2:27 ` Nguyen Thai Ngoc Duy
0 siblings, 0 replies; 10+ messages in thread
From: Nguyen Thai Ngoc Duy @ 2009-03-25 2:27 UTC (permalink / raw)
To: Shawn O. Pearce; +Cc: git
On Wed, Mar 25, 2009 at 1:14 PM, Shawn O. Pearce <spearce@spearce.org> wrote:
> Nguy???n Th??i Ng???c Duy <pclouds@gmail.com> wrote:
>> This is equivalent to "git init;git add .;git commit -q -m blah".
>> I find myself doing that too many times, hence this shortcut.
>
> Why not:
>
> git config --global alias.init-import '!git init && git add . && git commit -q'
1. less setup on new machine
2. fast-import support (not yet though)
--
Duy
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] init-db: support --import to add all files and commit right after init
2009-03-25 2:09 [PATCH] init-db: support --import to add all files and commit right after init Nguyễn Thái Ngọc Duy
2009-03-25 2:14 ` Shawn O. Pearce
@ 2009-03-25 3:51 ` Johannes Schindelin
2009-03-25 4:25 ` Nguyen Thai Ngoc Duy
2009-03-25 4:19 ` Jeff King
2 siblings, 1 reply; 10+ messages in thread
From: Johannes Schindelin @ 2009-03-25 3:51 UTC (permalink / raw)
To: Nguyễn Thái Ngọc Duy; +Cc: git
[-- Attachment #1: Type: TEXT/PLAIN, Size: 551 bytes --]
Hi,
On Wed, 25 Mar 2009, Nguyễn Thái Ngọc Duy wrote:
> This is equivalent to "git init;git add .;git commit -q -m blah".
> I find myself doing that too many times, hence this shortcut.
>
> In future, --fast-import support would also be nice if the import
> directory has a lot of files.
>
> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
I wanted to have this for a _long_ time!
But could you please say "init" in the subject instead of "init-db"? The
latter is just a historical wart we have to carry around.
Thanks,
Dscho
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] init-db: support --import to add all files and commit right after init
2009-03-25 2:09 [PATCH] init-db: support --import to add all files and commit right after init Nguyễn Thái Ngọc Duy
2009-03-25 2:14 ` Shawn O. Pearce
2009-03-25 3:51 ` Johannes Schindelin
@ 2009-03-25 4:19 ` Jeff King
2009-03-25 4:32 ` Nguyen Thai Ngoc Duy
2009-03-31 9:09 ` Miles Bader
2 siblings, 2 replies; 10+ messages in thread
From: Jeff King @ 2009-03-25 4:19 UTC (permalink / raw)
To: Nguyễn Thái Ngọc Duy; +Cc: git
On Wed, Mar 25, 2009 at 01:09:56PM +1100, Nguyễn Thái Ngọc Duy wrote:
> This is equivalent to "git init;git add .;git commit -q -m blah".
> I find myself doing that too many times, hence this shortcut.
I think I would find this handy. Shawn suggested an alias, but I think
this is perhaps "universal enough" to merit an actual command-line
option.
> +-m <message>::
> +--import=<message>::
> +
> +Commit everything to the newly initialized repository. This is equivalent to:
Maybe this is being too lazy, but I think it would be useful to allow
just "--import" without a message to default to "Initial commit",
"initial import from `basename $PWD`", or something like that.
> + else if (!strcmp(arg, "--import") || !strcmp(arg, "-m")) {
> + if (i+1 >= argc)
> + die("--import requires an import message");
> + import_message = argv[2];
> + i++;
> + argv++;
> + }
I seem to recall that we were phasing out "--long-option <arg>" at some
point, and that all long-options should use "--long-option=". But maybe
I am mis-remembering.
-Peff
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] init-db: support --import to add all files and commit right after init
2009-03-25 3:51 ` Johannes Schindelin
@ 2009-03-25 4:25 ` Nguyen Thai Ngoc Duy
0 siblings, 0 replies; 10+ messages in thread
From: Nguyen Thai Ngoc Duy @ 2009-03-25 4:25 UTC (permalink / raw)
To: Johannes Schindelin; +Cc: git
2009/3/25 Johannes Schindelin <Johannes.Schindelin@gmx.de>:
> But could you please say "init" in the subject instead of "init-db"? The
> latter is just a historical wart we have to carry around.
OK. No problem.
--
Duy
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] init-db: support --import to add all files and commit right after init
2009-03-25 4:19 ` Jeff King
@ 2009-03-25 4:32 ` Nguyen Thai Ngoc Duy
2009-03-25 4:49 ` Jeff King
2009-03-31 9:09 ` Miles Bader
1 sibling, 1 reply; 10+ messages in thread
From: Nguyen Thai Ngoc Duy @ 2009-03-25 4:32 UTC (permalink / raw)
To: Jeff King; +Cc: git
2009/3/25 Jeff King <peff@peff.net>:
>> +-m <message>::
>> +--import=<message>::
>> +
>> +Commit everything to the newly initialized repository. This is equivalent to:
>
> Maybe this is being too lazy, but I think it would be useful to allow
> just "--import" without a message to default to "Initial commit",
> "initial import from `basename $PWD`", or something like that.
I tend to do "commit -q -m i", not a big deal for two more keystrokes.
My initial version allowed "git init /path/to/workdir" too so it might
be ambiguous for optional message. So all vote for "Initial commit"?
Dynamic message seems overkill because most of the cases, this is a
throw away repository.
>> + else if (!strcmp(arg, "--import") || !strcmp(arg, "-m")) {
>> + if (i+1 >= argc)
>> + die("--import requires an import message");
>> + import_message = argv[2];
>> + i++;
>> + argv++;
>> + }
>
> I seem to recall that we were phasing out "--long-option <arg>" at some
> point, and that all long-options should use "--long-option=". But maybe
> I am mis-remembering.
I did not know too. Any reference?
--
Duy
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] init-db: support --import to add all files and commit right after init
2009-03-25 4:32 ` Nguyen Thai Ngoc Duy
@ 2009-03-25 4:49 ` Jeff King
0 siblings, 0 replies; 10+ messages in thread
From: Jeff King @ 2009-03-25 4:49 UTC (permalink / raw)
To: Nguyen Thai Ngoc Duy; +Cc: git
On Wed, Mar 25, 2009 at 03:32:17PM +1100, Nguyen Thai Ngoc Duy wrote:
> I tend to do "commit -q -m i", not a big deal for two more keystrokes.
> My initial version allowed "git init /path/to/workdir" too so it might
> be ambiguous for optional message. So all vote for "Initial commit"?
> Dynamic message seems overkill because most of the cases, this is a
> throw away repository.
Initial commit is fine with me. I plan to use this mostly for throw-away
repositories. I was thinking it might be useful for this:
tar xzf package-0.1.tar.gz
cd package-0.1
git init --import
so that later you remember it came from package-0.1 (when you are
looking at package 0.2). OTOH, if we take the information from the
directory, then the fact that it came from 0.1 is by definition already
in the directory name. So it is pretty pointless unless you later
rename the directory to just "package".
> > I seem to recall that we were phasing out "--long-option <arg>" at some
> > point, and that all long-options should use "--long-option=". But maybe
> > I am mis-remembering.
>
> I did not know too. Any reference?
I was, in fact, mis-remembering. The problem is not about long versus
short options, but rather about options which have an optional value.
See "Separating argument from the option" in "git help cli".
However, what we are discussing above _is_ making it an optional value,
in which case "--import <arg>" would have no value for "--import".
-Peff
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] init-db: support --import to add all files and commit right after init
2009-03-25 4:19 ` Jeff King
2009-03-25 4:32 ` Nguyen Thai Ngoc Duy
@ 2009-03-31 9:09 ` Miles Bader
2009-03-31 9:27 ` Jeff King
1 sibling, 1 reply; 10+ messages in thread
From: Miles Bader @ 2009-03-31 9:09 UTC (permalink / raw)
To: Jeff King; +Cc: Nguyễn Thái Ngọc Duy, git
Jeff King <peff@peff.net> writes:
> I seem to recall that we were phasing out "--long-option <arg>" at some
> point, and that all long-options should use "--long-option=". But maybe
> I am mis-remembering.
Surely it should support both where possible, since both are standard
syntaxes for passing args to "--"-style long options (so users expect
both to work).
-Miles
--
Occam's razor split hairs so well, I bought the whole argument!
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] init-db: support --import to add all files and commit right after init
2009-03-31 9:09 ` Miles Bader
@ 2009-03-31 9:27 ` Jeff King
0 siblings, 0 replies; 10+ messages in thread
From: Jeff King @ 2009-03-31 9:27 UTC (permalink / raw)
To: Miles Bader; +Cc: Nguyễn Thái Ngọc Duy, git
On Tue, Mar 31, 2009 at 06:09:43PM +0900, Miles Bader wrote:
> Jeff King <peff@peff.net> writes:
> > I seem to recall that we were phasing out "--long-option <arg>" at some
> > point, and that all long-options should use "--long-option=". But maybe
> > I am mis-remembering.
>
> Surely it should support both where possible, since both are standard
> syntaxes for passing args to "--"-style long options (so users expect
> both to work).
Yes. See later in the thread where I correctly remember the issue.
-Peff
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2009-03-31 9:30 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-03-25 2:09 [PATCH] init-db: support --import to add all files and commit right after init Nguyễn Thái Ngọc Duy
2009-03-25 2:14 ` Shawn O. Pearce
2009-03-25 2:27 ` Nguyen Thai Ngoc Duy
2009-03-25 3:51 ` Johannes Schindelin
2009-03-25 4:25 ` Nguyen Thai Ngoc Duy
2009-03-25 4:19 ` Jeff King
2009-03-25 4:32 ` Nguyen Thai Ngoc Duy
2009-03-25 4:49 ` Jeff King
2009-03-31 9:09 ` Miles Bader
2009-03-31 9:27 ` Jeff King
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).