]> git.madduck.net Git - code/myrepos.git/commitdiff

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

implemented mr config
authorJoey Hess <joey@kodama.kitenet.net>
Fri, 12 Oct 2007 22:35:03 +0000 (18:35 -0400)
committerJoey Hess <joey@kodama.kitenet.net>
Fri, 12 Oct 2007 22:35:03 +0000 (18:35 -0400)
TODO
mr

diff --git a/TODO b/TODO
index 6621db6f90ed9718e838136757f22b7513b8360a..000d5abbe7376caa7ed5efb0b745aaa530835df0 100644 (file)
--- a/TODO
+++ b/TODO
@@ -18,7 +18,7 @@
   closest mrconfig file.
 
   mr register would be implemented as a shell command that then calls
-  mr config with flags that make it actually edit the mrconfig file:
+  mr config with flags that make it edit the mrconfig file:
 
   if [ -d "$MR_REPO/.svn" ]; then
        url=$(svn info "$MR_REPO" | grep -i ^URL: | cut -d ' ' -f 2)
diff --git a/mr b/mr
index 4243194e84b506e9f8caff635e8fe38c32e61ed5..235c17518ad546c868086b8dcb73e931d7c95794 100755 (executable)
--- a/mr
+++ b/mr
@@ -18,6 +18,8 @@ B<mr> [options] diff
 
 B<mr> [options] log
 
+B<mr> [options] config section [parameter=[value] ...]
+
 B<mr> [options] action [params ...]
 
 =head1 DESCRIPTION
@@ -74,6 +76,15 @@ Show the commit log.
 
 List the repositories that mr will act on.
 
+=item config
+
+Modifies the mrconfig file. The next parameter is the name of the section
+to add or modify, and it is followed by one or more instances of
+"parameter=value". Use "parameter=" to remove a parameter. For example,
+to register a new svn repository in src/foo:
+
+  mr config src/foo checkout="svn co svn://example.com/foo/trunk"
+
 =item help
 
 Displays this help.
@@ -250,6 +261,23 @@ if (! exists $knownactions{$action}) {
 if ($action eq 'help') {
        exec($config{''}{DEFAULT}{help});
 }
+elsif ($action eq 'config') {
+       if (@ARGV < 2) {
+               die "mr config: not enough parameters\n";
+       }
+       my $section=shift;
+       my %fields;
+       foreach (@ARGV) {
+               if (/^([^=]+)=(.*)$/) {
+                       $fields{$1}=$2;
+               }
+               else {
+                       die "mr config: expected parameter=value, not \"$_\"\n";
+               }
+       }
+       modifyconfig($config, $section, %fields);
+       exit 0;
+}
 
 # work out what repos to act on
 my @repos;
@@ -493,6 +521,85 @@ sub loadconfig {
        }
 }
 
+sub modifyconfig {
+       my $f=shift;
+       # the section to modify or add
+       my $targetsection=shift;
+       # fields to change in the section
+       # To remove a field, set its value to "".
+       my %changefields=@_;
+
+       my @lines;
+       if (-e $f) {
+               open(my $in, "<", $f) || die "mr: open $f: $!\n";
+               @lines=<$in>;
+               close $in;
+       }
+
+       my $section;
+       my @out;
+       while (@lines) {
+               $_=shift(@lines);
+
+               if (/^\s*\#/ || /^\s*$/) {
+                       push @out, $_;
+               }
+               elsif (/^\s*\[([^\]]*)\]\s*$/) {
+                       if (defined $section && 
+                           $section eq $targetsection) {
+                               my @blanks;
+                               while ($out[$#out] =~ /^\s*$/) {
+                                       unshift @blanks, pop @out;
+                               }
+                               foreach my $field (sort keys %changefields) {
+                                       if (length $changefields{$field}) {
+                                               push @out, "$field = $changefields{$field}\n";
+                                       }
+                               }
+                               push @out, @blanks;
+                       }
+
+                       $section=$1;
+
+                       push @out, $_;
+               }
+               elsif (/^\s*(\w+)\s*=\s(.*)/) {
+                       my $parameter=$1;
+                       my $value=$2;
+
+                       # continuation line
+                       while ($value=~/(.*\\)$/s) {
+                               $value=$1."\n".shift(@lines);
+                               chomp $value;
+                       }
+
+                       if ($section eq $targetsection) {
+                               if (exists $changefields{$parameter}) {
+                                       if (length $changefields{$parameter}) {
+                                               $value=$changefields{$parameter};
+                                       }
+                                       delete $changefields{$parameter};
+                               }
+                       }
+
+                       push @out, "$parameter = $value\n";
+               }
+       }
+
+       if (%changefields) {
+               push @out, "\n[$targetsection]\n";
+               foreach my $field (sort keys %changefields) {
+                       if (length $changefields{$field}) {
+                               push @out, "$field = $changefields{$field}\n";
+                       }
+               }
+       }
+
+       open(my $out, ">", $f) || die "mr: write $f: $!\n";
+       print $out @out;
+       close $out;     
+}
+
 # Finally, some useful actions that mr knows about by default.
 # These can be overridden in ~/.mrconfig.
 __DATA__
@@ -569,7 +676,7 @@ log =                                                       \
                error "unknown repo type"               \
        fi
 list = true
-
+config = 
 help =                                                 \
        if [ ! -e "$MR_PATH" ]; then                    \
                error "cannot find program path"        \