And a new version that does the right thing for output to non-terminals.

-- 
bye,
pabs

http://wiki.debian.org/PaulWise
From 11169c0f9c1a32b5f066f889a4561d62473ab77a Mon Sep 17 00:00:00 2001
From: Paul Wise <p...@debian.org>
Date: Sat, 2 Feb 2013 13:41:58 +0800
Subject: [PATCH] Make quiet mode show output if there is some. Closes: #694031

---
 debian/changelog |  4 ++++
 debian/control   |  2 +-
 mr               | 64 +++++++++++++++++++++++++++++++++++---------------------
 3 files changed, 45 insertions(+), 25 deletions(-)

diff --git a/debian/changelog b/debian/changelog
index 1c93976..80e813a 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,9 +1,13 @@
 mr (1.14) UNRELEASED; urgency=low
 
+  [ Joey Hess ]
   * Added a fetch command. Closes: #480580
   * status: Now includes information about unpushed changes,
     for git, git-svn, hg, and bzr. Closes: #693021
 
+  [ Paul Wise ]
+  * Make quiet mode show output if there is some. Closes: #694031
+
  -- Joey Hess <jo...@debian.org>  Sun, 11 Nov 2012 11:33:05 -0400
 
 mr (1.13) unstable; urgency=low
diff --git a/debian/control b/debian/control
index 7189dc4..2700380 100644
--- a/debian/control
+++ b/debian/control
@@ -12,7 +12,7 @@ Architecture: all
 Section: vcs
 Depends: ${misc:Depends}
 Suggests: subversion, git-core | git (>= 1:1.7), cvs, bzr, mercurial, darcs, fossil, vcsh, liburi-perl, curl
-Recommends: libwww-perl, libhtml-parser-perl, perl
+Recommends: libwww-perl, libio-pty-easy-perl, libhtml-parser-perl, perl
 Description: Multiple Repository management tool
  The mr(1) command can checkout, update, or perform other actions on
  a set of repositories as if they were one combined respository. It
diff --git a/mr b/mr
index 7cce39f..6de6ce2 100755
--- a/mr
+++ b/mr
@@ -240,9 +240,8 @@ Be verbose.
 
 =item --quiet
 
-Be quiet. This suppresses mr's usual output, as well as any output from
-commands that are run (including stderr output). If a command fails,
-the output will be shown.
+Be quiet. This suppresses mr's usual output. If a command fails or there is any
+output then the usual output will be shown.
 
 =item -k
 
@@ -692,6 +691,32 @@ sub fulldir {
 	return $subdir =~ /^\// ? $subdir : $topdir.$subdir;
 }
 
+sub terminal_friendly_spawn {
+	my $actionmsg = shift;
+	my $sh = shift;
+	my $output = "";
+	if (-t STDOUT && eval{use IO::Pty::Easy;1;} eq 1) {
+		my $pty = IO::Pty::Easy->new;
+		$pty->spawn($sh);
+		while ($pty->is_active) {
+			my $data = $pty->read(0);
+			$output .= $data if defined $data;
+		}
+		$pty->close;
+	} else {
+		$output = qx/$sh 2>&1/;
+	}
+	my $ret = $?;
+	if ($ret != 0) {
+		print "$actionmsg\n" if $actionmsg;;
+		print STDERR $output;
+	} elsif ($output) {
+		print "$actionmsg\n" if $actionmsg;
+		print $output;
+	}
+	return ($ret, $output ? 1 : 0);
+}
+
 sub action {
 	my ($action, $dir, $topdir, $subdir, $force_checkout) = @_;
 	my $fulldir=fulldir($topdir, $subdir);
@@ -790,17 +815,12 @@ sub action {
 		my $hookret=hook("pre_$action", $topdir, $subdir);
 		return $hookret if $hookret != OK;
 
-		my $ret=runsh $action, $topdir, $subdir,
+		my ($ret, $out)=runsh $action, $topdir, $subdir,
 			$command, \@ARGV, sub {
 				my $sh=shift;
 				if ($quiet) {
-					my $output = qx/$sh 2>&1/;
-					my $ret = $?;
-					if ($ret != 0) {
-						print "$actionmsg\n";
-						print STDERR $output;
-					}
-					return $ret;
+					return terminal_friendly_spawn($actionmsg, $sh);
+
 				}
 				else {
 					system($sh);
@@ -838,7 +858,7 @@ sub action {
 				return FAILED;
 			}
 
-			my $ret=hook("post_$action", $topdir, $subdir);
+			my ($ret, $hook_out)=hook("post_$action", $topdir, $subdir);
 			return $ret if $ret != OK;
 			
 			if ($is_checkout || $is_update) {
@@ -852,7 +872,7 @@ sub action {
 				return $ret if $ret != OK;
 			}
 			
-			return OK;
+			return (OK, $out || $hook_out);
 		}
 	}
 }
@@ -862,15 +882,10 @@ sub hook {
 
 	my $command=$config{$topdir}{$subdir}{$hook};
 	return OK unless defined $command;
-	my $ret=runsh $hook, $topdir, $subdir, $command, [], sub {
+	my ($ret,$out)=runsh $hook, $topdir, $subdir, $command, [], sub {
 			my $sh=shift;
 			if ($quiet) {
-				my $output = qx/$sh 2>&1/;
-				my $ret = $?;
-				if ($ret != 0) {
-					print STDERR $output;
-				}
-				return $ret;
+				return terminal_friendly_spawn(undef, $sh);
 			}
 			else {
 				system($sh);
@@ -890,7 +905,7 @@ sub hook {
 		}
 	}
 
-	return OK;
+	return (OK, $out);
 }
 
 # run actions on multiple repos, in parallel
@@ -918,7 +933,7 @@ sub mrs {
 				close CHILD_STDERR;
 				close $outfh;
 				close $errfh;
-				exit action($action, @$repo);
+				exit +(action($action, @$repo))[0];
 			}
 			close CHILD_STDOUT;
 			close CHILD_STDERR;
@@ -966,10 +981,11 @@ sub mrs {
 sub record {
 	my $dir=shift()->[0];
 	my $ret=shift;
+	my $out=shift;
 
 	if ($ret == OK) {
 		push @ok, $dir;
-		print "\n" unless $quiet;
+		print "\n" unless $quiet && !$out;
 	}
 	elsif ($ret == FAILED) {
 		if ($interactive) {
@@ -978,7 +994,7 @@ sub record {
 			system((getpwuid($<))[8], "-i");
 		}
 		push @failed, $dir;
-		print "\n" unless $quiet;
+		print "\n";
 	}
 	elsif ($ret == SKIPPED) {
 		push @skipped, $dir;
-- 
1.8.1.2

Attachment: signature.asc
Description: This is a digitally signed message part

Reply via email to