chiark / gitweb /
Dgit.pm: Move parsecontrol etc. out of dgit
[dgit.git] / Debian / Dgit.pm
index 960f505aa1579036d27f3d7374b18d74490f6e37..8f069f7575e64739be6d9a25237d2f0640f3dff6 100644 (file)
@@ -47,11 +47,12 @@ BEGIN {
                       fail failmsg ensuredir must_getcwd executable_on_path
                       waitstatusmsg failedcmd_waitstatus
                      failedcmd_report_cmd failedcmd
-                      runcmd cmdoutput cmdoutput_errok
+                      runcmd shell_cmd cmdoutput cmdoutput_errok
                       git_rev_parse git_cat_file
                      git_get_ref git_get_symref git_for_each_ref
                       git_for_each_tag_referring is_fast_fwd
                      git_check_unmodified
+                     git_reflog_action_msg  git_update_ref_cmd
                       $package_re $component_re $deliberately_re
                      $distro_re $versiontag_re $series_filename_re
                      $extra_orig_namepart_re
@@ -65,6 +66,8 @@ BEGIN {
                       $negate_harmful_gitattrs
                      changedir git_slurp_config_src
                      gdr_ffq_prev_branchinfo
+                     parsecontrolfh parsecontrol parsechangelog
+                     getfield
                      playtree_setup);
     # implicitly uses $main::us
     %EXPORT_TAGS = ( policyflags => [qw(NOFFCHECK FRESHREPO NOCOMMITCHECK)],
@@ -101,7 +104,9 @@ sub NOCOMMITCHECK () { return 0x8; }
 our $debugprefix;
 our $debuglevel = 0;
 
-our $negate_harmful_gitattrs = "-text -eol -crlf -ident -filter";
+our $negate_harmful_gitattrs =
+    "-text -eol -crlf -ident -filter -working-tree-encoding";
+    # ^ when updating this, alter the regexp in dgit:is_gitattrs_setup
 
 our $forkcheck_mainprocess;
 
@@ -223,11 +228,11 @@ sub _us () {
 }
 
 sub failmsg {
-    my $s = "@_\n";
+    my $s = "error: @_\n";
     $s =~ s/\n\n$/\n/;
     my $prefix = _us().": ";
     $s =~ s/^/$prefix/gm;
-    return $s;
+    return "\n".$s;
 }
 
 sub fail {
@@ -313,6 +318,11 @@ sub runcmd {
     failedcmd @_ if system @_;
 }
 
+sub shell_cmd {
+    my ($first_shell, @cmd) = @_;
+    return qw(sh -ec), $first_shell.'; exec "$@"', 'x', @cmd;
+}
+
 sub cmdoutput_errok {
     confess Dumper(\@_)." ?" if grep { !defined } @_;
     debugcmd "|",@_;
@@ -485,6 +495,21 @@ sub is_fast_fwd ($$) {
     }
 }
 
+sub git_reflog_action_msg ($) {
+    my ($msg) = @_;
+    my $rla = $ENV{GIT_REFLOG_ACTION};
+    $msg = "$rla: $msg" if length $rla;
+    return $msg;
+}
+
+sub git_update_ref_cmd {
+    # returns  qw(git update-ref), qw(-m), @_
+    # except that message may be modified to honour GIT_REFLOG_ACTION
+    my $msg = shift @_;
+    $msg = git_reflog_action_msg $msg;
+    return qw(git update-ref -m), $msg, @_;
+}
+
 sub changedir ($) {
     my ($newdir) = @_;
     printdebug "CD $newdir\n";
@@ -531,6 +556,59 @@ sub gdr_ffq_prev_branchinfo ($) {
     return ('branch', undef, $symref, $ffq_prev, $gdrlast);
 }
 
+sub parsecontrolfh ($$;$) {
+    my ($fh, $desc, $allowsigned) = @_;
+    our $dpkgcontrolhash_noissigned;
+    my $c;
+    for (;;) {
+       my %opts = ('name' => $desc);
+       $opts{allow_pgp}= $allowsigned || !$dpkgcontrolhash_noissigned;
+       $c = Dpkg::Control::Hash->new(%opts);
+       $c->parse($fh,$desc) or die "parsing of $desc failed";
+       last if $allowsigned;
+       last if $dpkgcontrolhash_noissigned;
+       my $issigned= $c->get_option('is_pgp_signed');
+       if (!defined $issigned) {
+           $dpkgcontrolhash_noissigned= 1;
+           seek $fh, 0,0 or die "seek $desc: $!";
+       } elsif ($issigned) {
+           fail "control file $desc is (already) PGP-signed. ".
+               " Note that dgit push needs to modify the .dsc and then".
+               " do the signature itself";
+       } else {
+           last;
+       }
+    }
+    return $c;
+}
+
+sub parsecontrol {
+    my ($file, $desc, $allowsigned) = @_;
+    my $fh = new IO::Handle;
+    open $fh, '<', $file or die "$file: $!";
+    my $c = parsecontrolfh($fh,$desc,$allowsigned);
+    $fh->error and die $!;
+    close $fh;
+    return $c;
+}
+
+sub parsechangelog {
+    my $c = Dpkg::Control::Hash->new(name => 'parsed changelog');
+    my $p = new IO::Handle;
+    my @cmd = (qw(dpkg-parsechangelog), @_);
+    open $p, '-|', @cmd or die $!;
+    $c->parse($p);
+    $?=0; $!=0; close $p or failedcmd @cmd;
+    return $c;
+}
+
+sub getfield ($$) {
+    my ($dctrl,$field) = @_;
+    my $v = $dctrl->{$field};
+    return $v if defined $v;
+    fail "missing field $field in ".$dctrl->get_option('name');
+}
+
 # ========== playground handling ==========
 
 # terminology: