]> git.madduck.net Git - code/myrepos.git/blobdiff - mr

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:

add support for getting config settings
[code/myrepos.git] / mr
diff --git a/mr b/mr
index 8eb0374babbca924fb65e2191f9c7a7ed64a707b..db209d27c38409b4dfdc09ba93cb7e85a96bbc00 100755 (executable)
--- a/mr
+++ b/mr
@@ -18,7 +18,7 @@ B<mr> [options] diff
 
 B<mr> [options] log
 
-B<mr> [options] register repository
+B<mr> [options] register [repository]
 
 B<mr> [options] config section [parameter=[value] ...]
 
@@ -80,14 +80,17 @@ List the repositories that mr will act on.
 
 =item register
 
-The next parameter is the directory of an existing repository. The
-repository will be registered in the mrconfig file.
+Register an existing repository in the mrconfig file. By default, the
+epository in the current directory is registered, or you can specify a
+directory to register.
 
 =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. 
+Adds, modifies, removed, or prints a value from the mrconfig file. The next
+parameter is the name of the section the value is in. To add or modify a
+value, follow it by one or more instances of "parameter=value". Use
+"parameter=" to remove a parameter. Use just "parameter" to get the value
+of a parameter.
 
 For example, to add (or edit) a repository in src/foo:
 
@@ -281,16 +284,21 @@ elsif ($action eq 'config') {
                        $section=$1;
                }
        }
-       my %fields;
+       my %changefields;
        foreach (@ARGV) {
                if (/^([^=]+)=(.*)$/) {
-                       $fields{$1}=$2;
+                       $changefields{$1}=$2;
                }
                else {
-                       die "mr config: expected parameter=value, not \"$_\"\n";
+                       foreach my $topdir (sort keys %config) {
+                               if (exists $config{$topdir}{$section} &&
+                                   exists $config{$topdir}{$section}{$_}) {
+                                       print $config{$topdir}{$section}{$_}."\n";
+                               }
+                       }
                }
        }
-       modifyconfig($config, $section, %fields);
+       modifyconfig($config, $section, %changefields) if %changefields;
        exit 0;
 }
 elsif ($action eq 'register') {
@@ -346,7 +354,7 @@ sub action {
 
        if (exists $config{$topdir}{$subdir}{deleted}) {
                if (! -d $dir) {
-                       next;
+                       return;
                }
                else {
                        my $test="set -e;".$lib.$config{$topdir}{$subdir}{deleted};
@@ -463,6 +471,10 @@ sub loadconfig {
                $dir="";
        }
        else {
+               if (! -e $f) {
+                       return;
+               }
+
                my $absf=abs_path($f);
                if ($loaded{$absf}) {
                        return;
@@ -551,14 +563,28 @@ sub modifyconfig {
        my %changefields=@_;
 
        my @lines;
+       my @out;
+
        if (-e $f) {
                open(my $in, "<", $f) || die "mr: open $f: $!\n";
                @lines=<$in>;
                close $in;
        }
 
+       my $addfields=sub {
+               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;
+       };
+
        my $section;
-       my @out;
        while (@lines) {
                $_=shift(@lines);
 
@@ -568,16 +594,7 @@ sub modifyconfig {
                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;
+                               $addfields->();
                        }
 
                        $section=$1;
@@ -607,7 +624,11 @@ sub modifyconfig {
                }
        }
 
-       if (%changefields) {
+       if (defined $section && 
+           $section eq $targetsection) {
+                       $addfields->();
+       }
+       elsif (%changefields) {
                push @out, "\n[$targetsection]\n";
                foreach my $field (sort keys %changefields) {
                        if (length $changefields{$field}) {
@@ -697,13 +718,12 @@ log =                                                     \
                error "unknown repo type"               \
        fi
 register =                                                             \
-       if [ -z "$1" ]; then                                            \
-               error "repository directory not specified"              \
+       if [ -n "$1" ]; then                                            \
+               cd "$1"                                                 \
        fi                                                              \
-       cd "$1"                                                         \
        basedir="$(basename $(pwd))"                                    \
        if [ -d .svn ]; then                                            \
-               url=$(svn info . |                                      \
+               url=$(LANG=C svn info . |                               \
                      grep -i ^URL: | cut -d ' ' -f 2)                  \
                if [ -z "$url" ]; then                                  \
                        error "cannot determine svn url"                \
@@ -711,12 +731,19 @@ register =                                                                \
                echo "Registering svn url: $url"                        \
                mr config "$(pwd)" checkout="svn co $url $basedir"      \
        elif [ -d .git ]; then                                          \
-               url=$(git-config --get remote.origin.url)               \
+               url=$(LANG=C git-config --get remote.origin.url)        \
                if [ -z "$url" ]; then                                  \
                        error "cannot determine git url"                \
                fi                                                      \
                echo "Registering git url: $url"                        \
                mr config "$(pwd)" checkout="git clone $url $basedir"   \
+       elif [ -d .bzr ]; then                                          \
+               url=$(cat .bzr/branch/parent)                           \
+               if [ -z "$url" ]; then                                  \
+                       error "cannot determine bzr url"                \
+               fi                                                      \
+               echo "Registering bzr url: $url"                        \
+               mr config "$(pwd)" checkout="bzr clone $url $basedir"   \
        else                                                            \
                error "unable to register this repo type"               \
        fi