Git development
 help / color / mirror / Atom feed
From: Johannes Schindelin <Johannes.Schindelin@gmx.de>
To: "Edward Z. Yang" <edwardzyang@thewritingpot.com>
Cc: git@vger.kernel.org
Subject: [TEASER PATCH] write .gitmodules according to core.autocrlf
Date: Thu, 26 Jun 2008 12:58:20 +0100 (BST)	[thread overview]
Message-ID: <alpine.DEB.1.00.0806261257460.9925@racer> (raw)
In-Reply-To: <g3vaaq$pm1$1@ger.gmane.org>


This patch introduces the option "--crlf=<bool>" to git-config, which
tells git-config to write with or without CR/LF line endings.  (Empty
means 'false'.)

This option is then used by git-submodule to write .gitmodules with
the correct line-endings according to core.autocrlf.

NOTE: this patch is _not_ meant for inclusion, but as a starting point.
I have _no_ desire to continue working on this topic.  It is just a
proof of concept, and it is _your_ responsibility to get it into
submittable form.

This would involve:

	- giving the option a better name (--crlf does not imply that
	  it is only for writing),

	- move the declaration of the config_endl variable to somewhere
	  more public, such as cache.h,

	- possibly even move the definition of config_endl to
	  environment.c,

	- split the patch into the config-related and the
	  submodule-related part,

	- add documentation both for the config-related as well as for
	  the submodule-related part,

	- add tests,

	- submit it, and be quick to fix whatever is criticized on the
	  Git mailing list.

If you do that, would be nice to give me some credit, too.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 builtin-config.c |    5 +++++
 config.c         |   14 +++++++++-----
 git-submodule.sh |   15 ++++++++++-----
 3 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/builtin-config.c b/builtin-config.c
index 3a441ef..6ab191f 100644
--- a/builtin-config.c
+++ b/builtin-config.c
@@ -17,6 +17,7 @@ static char delim = '=';
 static char key_delim = ' ';
 static char term = '\n';
 static enum { T_RAW, T_INT, T_BOOL, T_BOOL_OR_INT } type = T_RAW;
+extern const char *config_endl;
 
 static int show_all_config(const char *key_, const char *value_, void *cb)
 {
@@ -334,6 +335,10 @@ int cmd_config(int argc, const char **argv, const char *prefix)
 			delim = '\n';
 			key_delim = '\n';
 		}
+		else if (!prefixcmp(argv[1], "--crlf="))
+			config_endl = argv[1][7] &&
+				git_config_bool("crlf", argv[1] + 7) ?
+				"\r\n" : "\n";
 		else if (!strcmp(argv[1], "--rename-section")) {
 			int ret;
 			if (argc != 4)
diff --git a/config.c b/config.c
index 58749bf..da9213e 100644
--- a/config.c
+++ b/config.c
@@ -15,6 +15,7 @@ static const char *config_file_name;
 static int config_linenr;
 static int config_file_eof;
 static int zlib_compression_seen;
+const char *config_endl = "\n";
 
 static int get_next_char(void)
 {
@@ -749,9 +750,10 @@ static int store_write_section(int fd, const char* key)
 				strbuf_addch(&sb, '\\');
 			strbuf_addch(&sb, key[i]);
 		}
-		strbuf_addstr(&sb, "\"]\n");
+		strbuf_addstr(&sb, "\"]");
+		strbuf_addstr(&sb, config_endl);
 	} else {
-		strbuf_addf(&sb, "[%.*s]\n", store.baselen, key);
+		strbuf_addf(&sb, "[%.*s]%s", store.baselen, key, config_endl);
 	}
 
 	success = write_in_full(fd, sb.buf, sb.len) == sb.len;
@@ -789,7 +791,7 @@ static int store_write_pair(int fd, const char* key, const char* value)
 	for (i = 0; value[i]; i++)
 		switch (value[i]) {
 		case '\n':
-			strbuf_addstr(&sb, "\\n");
+			strbuf_addstr(&sb, config_endl);
 			break;
 		case '\t':
 			strbuf_addstr(&sb, "\\t");
@@ -801,7 +803,7 @@ static int store_write_pair(int fd, const char* key, const char* value)
 			strbuf_addch(&sb, value[i]);
 			break;
 		}
-	strbuf_addf(&sb, "%s\n", quote);
+	strbuf_addf(&sb, "%s%s", quote, config_endl);
 
 	success = write_in_full(fd, sb.buf, sb.len) == sb.len;
 	strbuf_release(&sb);
@@ -1047,7 +1049,9 @@ int git_config_set_multivar(const char* key, const char* value,
 				    copy_end - copy_begin)
 					goto write_err_out;
 				if (new_line &&
-				    write_in_full(fd, "\n", 1) != 1)
+				    write_in_full(fd, config_endl,
+					    strlen(config_endl)) !=
+				    strlen(config_endl))
 					goto write_err_out;
 			}
 			copy_begin = store.offset[i];
diff --git a/git-submodule.sh b/git-submodule.sh
index 3eb78cc..c551c74 100755
--- a/git-submodule.sh
+++ b/git-submodule.sh
@@ -27,6 +27,11 @@ say()
 	fi
 }
 
+gitmodules_config ()
+{
+	git config -f .gitmodules --crlf=$(git config --bool core.autocrlf) "$@"
+}
+
 # NEEDSWORK: identical function exists in get_repo_base in clone.sh
 get_repo_base() {
 	(
@@ -74,8 +79,8 @@ module_name()
 {
 	# Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
 	re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
-	name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
-		sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
+	name=$(gitmodules_config '^submodule\..*\.path$' |
+		sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p')
        test -z "$name" &&
        die "No submodule mapping found in .gitmodules for path '$path'"
        echo "$name"
@@ -198,8 +203,8 @@ cmd_add()
 	git add "$path" ||
 	die "Failed to add submodule '$path'"
 
-	git config -f .gitmodules submodule."$path".path "$path" &&
-	git config -f .gitmodules submodule."$path".url "$repo" &&
+	gitmodules_config submodule."$path".path "$path" &&
+	gitmodules_config submodule."$path".url "$repo" &&
 	git add .gitmodules ||
 	die "Failed to register submodule '$path'"
 }
@@ -240,7 +245,7 @@ cmd_init()
 		url=$(git config submodule."$name".url)
 		test -z "$url" || continue
 
-		url=$(git config -f .gitmodules submodule."$name".url)
+		url=$(gitmodules_config submodule."$name".url)
 		test -z "$url" &&
 		die "No url found for submodule path '$path' in .gitmodules"
 
-- 
1.5.6.173.gde14c

      parent reply	other threads:[~2008-06-26 12:01 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-06-26  5:43 [Bug?] git submodule add doesn't respect core.autocrlf Edward Z. Yang
2008-06-26  8:40 ` Lars Hjemli
2008-06-26 11:58 ` Johannes Schindelin [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.DEB.1.00.0806261257460.9925@racer \
    --to=johannes.schindelin@gmx.de \
    --cc=edwardzyang@thewritingpot.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox