Skip to contentSkip to conversation

init: support --import to add all files and commit right after init

PATCH In review Nguyễn Thái Ngọc Duy 5 participants 14 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 init: support --import to add all files and commit right after init

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.

---
 Documentation/git-init.txt |   17 ++++++++++++++-
 builtin-init-db.c          |   49 ++++++++++++++++++++++++++++++++++++++++---
 t/t0001-init.sh            |   44 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 105 insertions(+), 5 deletions(-)
Documentation/git-init.txt +16 -1
index 71749c0..ab75c10 100644
@@ -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|--import [<message>]]
 
 
 OPTIONS
@@ -68,6 +69,20 @@ 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>
+----------------
+
+If no message is given, "Initial commit" will be used.
+This option does not work with `--bare` nor already initialized repository.
+
 --
 
 
builtin-init-db.c +45 -4

96 lines not on this page · raw message

t/t0001-init.sh +44 -0

48 lines not on this page · raw message

Signature · 1 line
1.6.1.446.gc7851
#1 lore raw
Quoted 3 lines · Nguyễn Thái Ngọc Duy

-'git init' [-q | --quiet] [--bare] [--template=<template_directory>] [--shared[=<permissions>]] +'git init' [-q | --quiet] [--bare] [--template=<template_directory>]
+ [--shared[=<permissions>]] [-m|--import [<message>]]

What happened to --import=? Whether or not "--import <arg>" works, the --long-opt= form should always work.

builtin-init-db.c int cmd_init_db(int argc, const char **argv, const char *prefix) · line 427

Quoted 9 lines
builtin-init-db.c +9 -0
+		else if (!strcmp(arg, "--import") || !strcmp(arg, "-m")) {
+			if (i+1 >= argc)
+				import_message = "Initial commit";
+			else {
+				import_message = argv[2];
+				i++;
+				argv++;
+			}
+		}

This is the wrong way to do optional arguments. It means that

  git init --template=foo --import

is different from

  git init --import --template=foo

I think what you want is:

  else if (!strcmp(arg, "-m")) {
    if (i+1 >= argc)
      die("-m requires an import message");
    import_message = argv[2];
    i++;
    argv++;
  }
  else if (!strcmp(arg, "--import"))
    import_message = "Initial commit";
  else if (!prefixcmp(arg, "--import="))
      import_message = arg+9;

That is, --import has a message or not depending on the '=', and "-m" always has a message. If you want "-m" to optionally have a message then it must be used as

git init -mfoo

-Peff

#2 lore raw

2009/3/25 Jeff King <[email protected]>:

Quoted 41 lines · Nguyễn Thái Ngọc Duy
Quoted 4 lines

-'git init' [-q | --quiet] [--bare] [--template=<template_directory>] [--shared[=<permissions>]] +'git init' [-q | --quiet] [--bare] [--template=<template_directory>]
+           [--shared[=<permissions>]] [-m|--import [<message>]]

What happened to --import=? Whether or not "--import <arg>" works, the --long-opt= form should always work.

Quoted 9 lines

+             else if (!strcmp(arg, "--import") || !strcmp(arg, "-m")) {
+                     if (i+1 >= argc)
+                             import_message = "Initial commit";
+                     else {
+                             import_message = argv[2];
+                             i++;
+                             argv++;
+                     }
+             }

This is the wrong way to do optional arguments. It means that

 git init --template=foo --import

is different from

 git init --import --template=foo

I think what you want is:

 else if (!strcmp(arg, "-m")) {
   if (i+1 >= argc)
     die("-m requires an import message");
   import_message = argv[2];
   i++;
   argv++;
 }
 else if (!strcmp(arg, "--import"))
   import_message = "Initial commit";
 else if (!prefixcmp(arg, "--import="))
     import_message = arg+9;

Quoted 5 lines

That is, --import has a message or not depending on the '=', and "-m" always has a message. If you want "-m" to optionally have a message then it must be used as

 git init -mfoo

Right. Should not work late (or send it in the same night). Will rework.

Signature · 1 line
Duy
#3 lore raw
In reply to Nguyễn Thái Ngọc Duy

2009/3/25 Nguyễn Thái Ngọc Duy <[email protected]>:

Documentation/git-init.txt By default, the configuration flag receive.denyNonFastForwards is enabled · line 83

Quoted 12 lines
+-m <message>::
+--import <message>::
Documentation/git-init.txt +10 -0
+
+Commit everything to the newly initialized repository. This is equivalent to:
+
+----------------
+$ git init
+$ git add .
+$ git commit -q -m <message>
+----------------
+
+If no message is given, "Initial commit" will be used.

Why a default message and not running the editor?

Santi

#4 lore raw

Hi,

Quoted 15 lines · Santi Béjar

2009/3/25 Nguyễn Thái Ngọc Duy <[email protected]>:

Documentation/git-init.txt By default, the configuration flag receive.denyNonFastForwards is enabled · line 83

Quoted 12 lines
+-m <message>::
+--import <message>::
Documentation/git-init.txt +10 -0
+
+Commit everything to the newly initialized repository. This is equivalent to:
+
+----------------
+$ git init
+$ git add .
+$ git commit -q -m <message>
+----------------
+
+If no message is given, "Initial commit" will be used.

Why a default message and not running the editor?

Because I would say "Initial commit" anyway.

Ciao,
Dscho

#5 lore raw
Quoted 5 lines · Johannes Schindelin
Quoted 3 lines
Quoted 1 line

+If no message is given, "Initial commit" will be used.

Why a default message and not running the editor?

Because I would say "Initial commit" anyway.

Agreed. This feature is about convenience. If you really want to say something more exciting, then use "git commit" directly. Heck, you can even "git commit --amend" it afterwards.

-Peff

#6 lore raw

2009/3/25 Jeff King <[email protected]>:

Quoted 7 lines · Johannes Schindelin
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

+If no message is given, "Initial commit" will be used.

Why a default message and not running the editor?

Because I would say "Initial commit" anyway.

And I would say "Commit inicial".

Quoted 2 lines

Agreed. This feature is about convenience.

... for project in english.

Quoted 3 lines

If you really want to say
something more exciting, then use "git commit" directly. Heck, you can even "git commit --amend" it afterwards.

I can. But then it is no longer "convenience".

But anyway, it is like the "Merge branch..." commit message for merges. So nothing new.

Santi

#7 lore raw

Santi Béjar, 25.03.2009:

Quoted 10 lines

2009/3/25 Jeff King <[email protected]>:

Quoted 7 lines · Johannes Schindelin
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

+If no message is given, "Initial commit" will be used.

Why a default message and not running the editor?

Because I would say "Initial commit" anyway.

And I would say "Commit inicial".

And I would describe the current state in a few words.

Invoking an editor is more universal and I don't think the majority would be contented with "Initial commit".

Markus

#8 lore raw

Hi,

Quoted 8 lines · Markus Heidelberg

Santi Béjar, 25.03.2009:

Quoted 7 lines

2009/3/25 Jeff King <[email protected]>:

Quoted 6 lines · Johannes Schindelin
Quoted 5 lines
Quoted 4 lines
Quoted 2 lines

+If no message is given, "Initial commit" will be used.

Why a default message and not running the editor?

Quoted 8 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line

Because I would say "Initial commit" anyway.

And I would say "Commit inicial".

And I would describe the current state in a few words.

Invoking an editor is more universal and I don't think the majority would be contented with "Initial commit".

_Again_, as Peff pointed out, you are welcome to use the current method of git init && git add . && git commit, which _does_ launch an editor.

The fact that you want to spend much time (anyway) doing your initial commit does not allow you to inconvenience others.

Others who want to have a quick way to work safely with something they might need to change, and might then want to use the full power of Git to see what they changed. Without any need for a "nice" first commit.

Ciao,
Dscho

#9 lore raw
Quoted 5 lines · Johannes Schindelin

_Again_, as Peff pointed out, you are welcome to use the current method of git init && git add . && git commit, which _does_ launch an editor.

The fact that you want to spend much time (anyway) doing your initial commit does not allow you to inconvenience others.

Another option would be a patch on top of the original to allow

  git config --global init.importmessage 'Commit inicial'

or

  git config --global init.importeditor true

I have no interest in writing such a patch, but I don't see a reason to reject it.

-Peff

#10 lore raw
Quoted 10 lines · Jeff King

Another option would be a patch on top of the original to allow

  git config --global init.importmessage 'Commit inicial'

or

  git config --global init.importeditor true

I have no interest in writing such a patch, but I don't see a reason to reject it.

Actually, there is one possible reason to reject it: scripts could not rely on the behavior of "--import" without it. But I think it is OK to make a conscious decision that this is a feature for _humans_, and that scripts can use "init && add && commit" (or they can be happy with dealing with the human's choice of editor or not).

-Peff

#11 lore raw
In reply to Johannes Schindelin

Johannes Schindelin, 27.03.2009:

Quoted 3 lines

Others who want to have a quick way to work safely with something they might need to change, and might then want to use the full power of Git to see what they changed. Without any need for a "nice" first commit.

What's the difference between the first commit and the others? I don't see the reason, not to have a short description for it.

Markus

#12 lore raw

Hi,

Quoted 7 lines · Markus Heidelberg

Johannes Schindelin, 27.03.2009:

Quoted 3 lines

Others who want to have a quick way to work safely with something they might need to change, and might then want to use the full power of Git to see what they changed. Without any need for a "nice" first commit.

What's the difference between the first commit and the others? I don't see the reason, not to have a short description for it.

Maybe you can learn a new trick here:

$ tar xf /some/random/project.tar
$ git init
$ git add .
$ git commit -m initial

and now one of two work flows:

# get the thing to work properly, or add a new feature, or clean up...
$ git diff > diff.patch
# send the diff to the maintainer, without ever committing

or

# make a patch series, use rebase -i to clean up after it
# send the patch series to the maintainer of the random project

See? The initial commit does not matter at all.

I do this so often that it stops being funny having to type three commands.

And having to edit a commit message I do not care about anyway everytime, just to please you, would not make it any funnier ;-)

I'll just repeat one of my favorite mantras: optimize for the common case, not for the uncommon case.

Ciao,
Dscho

#13 lore raw

Johannes Schindelin, 28.03.2009:

Quoted 25 lines

Hi,

Quoted 7 lines · Markus Heidelberg

Johannes Schindelin, 27.03.2009:

Quoted 3 lines

Others who want to have a quick way to work safely with something they might need to change, and might then want to use the full power of Git to see what they changed. Without any need for a "nice" first commit.

What's the difference between the first commit and the others? I don't see the reason, not to have a short description for it.

Maybe you can learn a new trick here:

$ tar xf /some/random/project.tar
$ git init
$ git add .
$ git commit -m initial

and now one of two work flows:

# get the thing to work properly, or add a new feature, or clean up...
$ git diff > diff.patch
# send the diff to the maintainer, without ever committing
Quoted 6 lines

or

# make a patch series, use rebase -i to clean up after it
# send the patch series to the maintainer of the random project

See? The initial commit does not matter at all.

Yep, I only thought about own projects and didn't take this workflow into account. Although I have already used it myself and of course the initial commit is not interesting then.

Quoted 5 lines

I do this so often that it stops being funny having to type three commands.

And having to edit a commit message I do not care about anyway everytime, just to please you, would not make it any funnier ;-)

Understood :)
But note, that my second mail was only about writing an initial commit message or not. I don't have objections against the default commit message with --import any more.

Markus