update.sh: tame the wild show_progress=1 beast
[girocco/readme.git] / jobd / git-fetch-q-progress.sh
blob84e065c44de2791a5e8e2ea641762d6a2f8ae792
1 #!/bin/sh
3 # git-fetch-q-progress.sh - fetch with progress but not ref noise
4 # Copyright (C) 2017 Kyle J. McKay. All rights reserved.
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 # Version 1.0
22 set -e
23 git rev-parse --git-dir >/dev/null || exit
24 PERLPROG='
25 #!perl
26 #line 27 "git-fetch-q-progress.sh"
27 use strict;
28 use warnings;
29 use Fcntl;
30 $| = 1;
31 my $flags = fcntl(STDIN, F_GETFL, 0);
32 defined($flags) or die "fcntl failed: $!";
33 fcntl(STDIN, F_SETFL, $flags | O_NONBLOCK) or die "fcntl failed: $!";
34 my $data = "";
35 my $cnt;
36 my $last = undef;
37 sub flush_last { print($last) and $last = undef if defined($last); }
38 my ($total, $creates, $deletes, $forces) = (0,0,0,0);
39 sub flush_results {
40 if ($total) {
41 my $msg = $total . " ref";
42 $msg .= "s" unless $total == 1;
43 $msg .= " affected";
44 if ($creates) {
45 $msg .= ", " . $creates . " creation";
46 $msg .= "s" unless $creates == 1;
48 if ($deletes) {
49 $msg .= ", " . $deletes . " deletion";
50 $msg .= "s" unless $deletes == 1;
52 if ($forces) {
53 $msg .= ", " . $forces . " forced update";
54 $msg .= "s" unless $forces == 1;
56 print $msg, "\n";
58 $total = $creates = $deletes = $forces = 0;
60 my $sawref;
61 sub handle_line {
62 if ($_[0] =~ /^ ([*=!tX +-]) /) {
63 ++$total unless $1 eq "!" || $1 eq "=" || $1 eq "X";
64 if ($1 eq "-") {
65 ++$deletes;
66 } elsif ($1 eq "*") {
67 ++$creates;
68 } elsif ($1 eq "+") {
69 ++$forces;
71 $sawref = 1, $last = undef if !$sawref;
72 } else {
73 flush_last;
74 $sawref=0, flush_results if $sawref;
75 $last = $_[0];
78 do {
79 my ($rin, $win, $ein);
80 $rin = $win = "";
81 vec($rin, fileno(STDIN), 1) = 1;
82 $ein = $rin;
83 select($rin, $win, $ein, undef);
84 $cnt = read(STDIN, $data, 1024, length($data));
85 my $idx = rindex($data, "\r");
86 if ($idx >= 0) {
87 ++$idx;
88 flush_last;
89 print substr($data, 0, $idx);
90 substr($data, 0, $idx) = "";
92 while (($idx = index($data, "\n")) >= 0) {
93 ++$idx;
94 handle_line(substr($data, 0, $idx));
95 substr($data, 0, $idx) = "";
97 } while $cnt;
98 flush_last;
99 $data eq "" or print $data;
100 flush_results
102 exec 3>&1
104 read -r ec <<EOT || :
106 exec 4>&3 3>&1 1>&4 4>&-
107 { ec=0 && git fetch --progress "$@" 3>&- || ec=$?; echo $ec >&3; } 2>&1 |
108 { export PERLPROG && perl -e 'eval $ENV{PERLPROG}'; } >&2
111 exec 3>&-
112 exit ${ec:-1}