[Bps-public-commit] Config-GitLike branch, master, updated. 1.16-2-g9126aa6

Alex Vandiver alexmv at bestpractical.com
Sun Jul 16 21:15:43 EDT 2017


The branch, master has been updated
       via  9126aa6c2c94808e8f7686064bc84c85edee095a (commit)
       via  e2a8ddbee2ea856b82ff29ffcab95534a5861e98 (commit)
      from  b216a660b0a85f6e65d3abdc877a89e374205cd2 (commit)

Summary of changes:
 Makefile.PL           |  1 +
 lib/Config/GitLike.pm | 26 ++++++++++++++++++++++++++
 t/dos.conf            |  5 +++++
 t/mac.conf            |  1 +
 t/platforms.t         | 19 +++++++++++++++++++
 t/unix.conf           |  5 +++++
 6 files changed, 57 insertions(+)
 create mode 100644 t/dos.conf
 create mode 100644 t/mac.conf
 create mode 100644 t/platforms.t
 create mode 100644 t/unix.conf

- Log -----------------------------------------------------------------
commit e2a8ddbee2ea856b82ff29ffcab95534a5861e98
Author: Alex Vandiver <alex at chmrr.net>
Date:   Sun Jul 16 17:09:55 2017 -0700

    Add "." to @INC in Makefile.PL, for 5.28 compatibility

diff --git a/Makefile.PL b/Makefile.PL
index e021ab4..6b76104 100644
--- a/Makefile.PL
+++ b/Makefile.PL
@@ -1,3 +1,4 @@
+BEGIN { push @INC, '.' }
 use inc::Module::Install;
 name('Config-GitLike');
 all_from('lib/Config/GitLike.pm');

commit 9126aa6c2c94808e8f7686064bc84c85edee095a
Author: Alex Vandiver <alex at chmrr.net>
Date:   Sun Jul 16 18:02:33 2017 -0700

    Allow reading and writing non-UNIX-newline config files
    
    Standard `git` assumes that all files have UNIX line endings -- that
    is, "\n".  It transforms "\r\n" into "\n" upon read, but upon write
    still assumes "\n" is the correct line ending -- creating files with
    mixed line endings.  While those files will parse correctly by git,
    such files may give editors trouble.
    
    `git` has no support for Mac-style files with "\r" line endings,
    treating them as one long line:
    ```
    $ git config -f t/mac.conf -l
    core.engine=pg  topdir = sql [deploy]  verify = true
    ```
    
    Extend Config::GitLike to also be lenient to "\r\n" line endings upon
    read, but to also remember this data for changes applied to the file.
    Also, detect "\n"-less files that do contain "\r"s, and assume them to
    be Mac-style files with "\r" style line endings during both reading
    and writing.
    
    This increases interoperability of files between different systems.
    It does cause Config::GitLike to be more lenient than `git` --
    however, this is merely one more small case of such, and not the
    first.
    
    Tests supplied by David E. Wheeler <david at justatheory.com>
    
    Closes GH-13.

diff --git a/lib/Config/GitLike.pm b/lib/Config/GitLike.pm
index 81e99de..dbdd812 100644
--- a/lib/Config/GitLike.pm
+++ b/lib/Config/GitLike.pm
@@ -70,6 +70,12 @@ has 'encoding' => (
     isa => Maybe[Str],
 );
 
+has 'newlines' => (
+    is => 'rw',
+    isa => HashRef,
+    default => sub { +{} },
+);
+
 has 'include' => (
     is => 'rw',
     isa => Str,
@@ -165,6 +171,19 @@ sub _read_config {
 
     my $c = do {local $/; <$fh>};
 
+    my $newlines = "\n";
+    if ($c =~ m/\r\n/) {
+        # Convert from DOS; `git` applies this on read always, and
+        # simply mangles files on write.
+        $newlines = "\r\n";
+        $c =~ s/\r\n/\n/g;
+    } elsif ($c !~ /\n/ and $c =~ /\r/) {
+        # Best-guess convert from Mac.
+        $newlines = "\r";
+        $c =~ s/\r/\n/g;
+    }
+    $self->newlines->{$filename} = $newlines;
+
     $c =~ s/\n*$/\n/; # Ensure it ends with a newline
 
     return $c;
@@ -1077,6 +1096,8 @@ sub _write_config {
     my $self = shift;
     my($filename, $content) = @_;
 
+    my $newlines = $self->newlines->{$filename} || "\n";
+    $content =~ s/\n/$newlines/g if $newlines ne "\n";
     # allow nested symlinks but only within reason
     my $max_depth = 5;
 
@@ -1902,6 +1923,11 @@ Git just rejects them.
 We don't support NUL-terminating output (the --null flag to
 git-config). Who needs it?
 
+Git only supports reading UNIX- and DOS-style newlines ("\n" and
+"\r\n"), and always uses "\n" when modifying files.  We also support
+reading Mac-style newlines ("\r"), and write updates to files using
+the same newlines as they were read with.
+
 =head1 BUGS
 
 If you find any bugs in this module, report them at:
diff --git a/t/dos.conf b/t/dos.conf
new file mode 100644
index 0000000..2d279d7
--- /dev/null
+++ b/t/dos.conf
@@ -0,0 +1,5 @@
+[core]
+	engine = pg
+	topdir = sql
+[deploy]
+	verify = true
diff --git a/t/mac.conf b/t/mac.conf
new file mode 100644
index 0000000..81918bd
--- /dev/null
+++ b/t/mac.conf
@@ -0,0 +1 @@
+[core]	engine = pg	topdir = sql[deploy]	verify = true
\ No newline at end of file
diff --git a/t/platforms.t b/t/platforms.t
new file mode 100644
index 0000000..bc3ffc9
--- /dev/null
+++ b/t/platforms.t
@@ -0,0 +1,19 @@
+use strict;
+use warnings;
+
+use Test::More;
+use Config::GitLike;
+use File::Spec;
+
+for my $platform (qw(unix dos mac)) {
+    my $config_filename = File::Spec->catfile('t', "$platform.conf");
+    ok my $data = Config::GitLike->load_file($config_filename),
+        "Load $platform config";
+    is_deeply $data, {
+        'core.engine' => 'pg',
+        'core.topdir' => 'sql',
+        'deploy.verify' => 'true',
+    }, "Should have proper config for $platform file";
+}
+
+done_testing;
diff --git a/t/unix.conf b/t/unix.conf
new file mode 100644
index 0000000..c0488e5
--- /dev/null
+++ b/t/unix.conf
@@ -0,0 +1,5 @@
+[core]
+	engine = pg
+	topdir = sql
+[deploy]
+	verify = true

-----------------------------------------------------------------------


More information about the Bps-public-commit mailing list