clone/update: mark project changed on failure
[girocco.git] / jobd / git-fetch-q-progress.sh
bloba7feefb44f25d0e61d03768ed3483ed003e98def
1 #!/bin/sh
3 # git-fetch-q-progress.sh - fetch with progress but not ref noise
4 # Copyright (C) 2017,2018 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.1
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 } elsif ($_[0] =~ /^error:/) {
73 flush_last;
74 print STDERR $_[0];
75 } else {
76 flush_last;
77 $sawref=0, flush_results if $sawref;
78 $last = $_[0];
81 do {
82 my ($rin, $win, $ein);
83 $rin = $win = "";
84 vec($rin, fileno(STDIN), 1) = 1;
85 $ein = $rin;
86 select($rin, $win, $ein, undef);
87 $cnt = read(STDIN, $data, 1024, length($data));
88 my $idx = rindex($data, "\r");
89 if ($idx >= 0) {
90 ++$idx;
91 flush_last;
92 print substr($data, 0, $idx);
93 substr($data, 0, $idx) = "";
95 while (($idx = index($data, "\n")) >= 0) {
96 ++$idx;
97 handle_line(substr($data, 0, $idx));
98 substr($data, 0, $idx) = "";
100 } while $cnt;
101 flush_last;
102 $data eq "" or print $data;
103 flush_results
105 exec 3>&1
107 read -r ec <<EOT || :
109 exec 4>&3 3>&1 1>&4 4>&-
110 { ec=0 && git fetch --progress "$@" 3>&- || ec=$?; echo $ec >&3; } 2>&1 |
111 { export PERLPROG && perl -e 'eval $ENV{PERLPROG}'; } >&2
114 exec 3>&-
115 exit ${ec:-1}