]>
Git — Sourcephile - git-remote-gpg.git/blob - git-remote-gpg
2 our $VERSION = '2014.04.29';
4 # This file is a git-remote-helpers(1) to use a gpg(1)
5 # as a cryptographic layer below git(1)'s objects.
6 # Copyright (C) 2014 Julien Moutinho
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published
10 # by the Free Software Foundation, either version 3 of the License,
11 # or any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty
15 # of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 # See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 use warnings FATAL
=> qw(all);
28 use File
::Spec
::Functions
qw(:ALL);
32 # NOTE: to debug: IPCRUNDEBUG=basic|data|details|gory
35 use POSIX
qw(WNOHANG);
42 foreach my $msg (@_) {
48 my $call = (caller(1))[3];
52 , "\e[30m\e[1m.", join('.', $call."\e[m")
56 : Data
::Dumper
::Dumper
($_)
63 my $call = (caller(1))[3];
66 , "\e[30m\e[1m.", join('.', $call."\e[m")
67 , " ", (ref $_ eq 'CODE'?(join("\n ", $_->()), "\n"):(@_, "\n"))
71 local $Carp::CarpLevel
= 1;
72 carp
("\e[33mWARNING\e[m ", @_, "\n\t");
75 local $Carp::CarpLevel
= 1;
76 croak
("\e[31mERROR\e[m ", @_, "\n\t");
80 foreach my $file (@_) {
81 debug
(sub{"file=$file\n"});
89 foreach my $dir (@_) {
90 debug
(sub{"dir=$dir\n"});
91 File
::Path
::make_path
($dir, {verbose
=>0, error
=> \
my $error});
93 for my $diag (@$error) {
94 my ($dir, $message) = %$diag;
95 error
("dir=$dir: $message");
102 my ($ctx, $size) = @_;
104 IPC
::Run
::run
([@{$ctx->{config
}->{gpg
}}
105 , '--armor', '--gen-rand', '1', $size]
107 or error
("failed to get random bits");
111 sub grg_hash
($$;$) {
112 my ($ctx, $algo, $run) = @_;
113 $run = sub {return @_} unless defined $run;
115 IPC
::Run
::run
($run->([@{$ctx->{config
}->{gpg
}}
116 , '--with-colons', '--print-md', $algo]
118 or error
("failed to hash data");
119 return ((split(':', $hash))[2]);
121 sub gpg_fingerprint
($$$) {
122 my ($ctx, $id, $caps_needed) = @_;
125 if (IPC
::Run
::run
([@{$ctx->{config
}->{gpg
}}
126 , '--fixed-list-mode', '--with-colons', '--with-fingerprint', '--list-keys', $id]
128 my @lines = split(/\n/,$output);
129 while (my $line = shift @lines) {
130 if (my ($longkeyid, $caps) = $line =~ m/^pub:[^:]*:[^:]*:[^:]*:([^:]*):[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:([^:]+):.*$/) {
132 foreach my $cap (@$caps_needed) {
133 if (not ($caps =~ m/$cap/)) {
134 warning
("skipping key 0x$longkeyid which has not usable capability: $cap, but matches: `$id'");
141 while ((not defined $fpr or not defined $uid)
142 and $line = shift @lines) {
143 (not defined $fpr and (($fpr) = $line =~ m/^fpr:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:([0-9A-F]+):.*$/)) or
144 (not defined $uid and (($uid) = $line =~ m/^uid:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:([^:]+):.*$/)) or
147 error
("unable to extract fingerprint and user ID")
155 error
("unable to find any OpenPGP key with usable capability: ".join('', @$caps_needed)." for: `$id'")
156 unless scalar(%h) gt 0;
157 debug
(sub{"$id -> "}, \
%h);
160 sub grg_encrypt_symmetric
($$$;$) {
161 my ($ctx, $clear, $key, $run) = @_;
162 $run = sub {return @_} unless defined $run;
163 IPC
::Run
::run
($run->([@{$ctx->{config
}->{gpg
}}
165 , '--compress-algo', 'none'
167 , '--passphrase-fd', '3'
169 , '--trust-model', 'always'
171 , '<', \
$clear, '3<', \
$key))
172 or error
("failed to encrypt symmetrically data");
174 sub grg_decrypt_symmetric
($$$;$) {
175 my ($ctx, $key, $run) = @_;
176 $run = sub {return @_} unless defined $run;
177 IPC
::Run
::run
($run->([@{$ctx->{config
}->{gpg
}}
178 , '--batch', '--no-default-keyring', '--keyring', '/dev/null', '--secret-keyring', '/dev/null'
179 , '--passphrase-fd', '3', '--quiet', '--decrypt']
181 or error
("failed to decrypt symmetrically data");
183 sub grg_encrypt_asymmetric
($$;$) {
184 my ($ctx, $clear, $run) = @_;
185 $run = sub {return @_} unless defined $run;
187 ( (map { ('--recipient', '0x'.$_) } (keys %{$ctx->{config
}->{keys}}))
188 , (map { ('--hidden-recipient', '0x'.$_) } (keys %{$ctx->{config
}->{'hidden-keys'}})) );
189 @recipients = ('--default-recipient-self')
191 IPC
::Run
::run
($run->([@{$ctx->{config
}->{gpg
}}
193 , '--compress-algo', 'none'
194 , '--trust-model', 'always'
195 , '--sign', '--encrypt'
196 , ($ctx->{config
}->{signingkey
}->{fpr
} ? ('--local-user', $ctx->{config
}->{signingkey
}->{fpr
}) : ())
199 or error
("failed to encrypt asymmetrically data");
201 sub grg_decrypt_asymmetric
($$;$) {
202 my ($ctx, $run) = @_;
203 my ($clear, $status);
204 $run = sub {return @_} unless defined $run;
205 IPC
::Run
::run
($run->([@{$ctx->{config
}->{gpg
}}
206 , '--batch', '--no-default-keyring',
207 , '--status-fd', '3', '--quiet', '--decrypt']
208 , '>', \
$clear, '3>', \
$status))
209 or error
("failed to decrypt asymmetrically data");
210 debug
(sub{"status=\n$status"});
211 my @lines = split(/\n/,$status);
212 my ($enc_to, $goodsig, $validsig, $validpub, $goodmdc);
213 foreach my $line (@lines) {
214 (not defined $enc_to and (($enc_to) = $line =~ m/^\[GNUPG:\] ENC_TO ([0-9A-F]+).*$/)) or
215 (not defined $goodsig and (($goodsig) = $line =~ m/^\[GNUPG:\] GOODSIG ([0-9A-F]+).*$/)) or
216 (not defined $goodmdc and (($goodmdc) = $line =~ m/^\[GNUPG:\] (GOODMDC)$/)) or
217 (not defined $validsig and not defined $validpub and (($validsig, $validpub)
218 = $line =~ m/^\[GNUPG:\] VALIDSIG ([0-9A-F]+) [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ ([0-9A-F]+).*$/)) or
221 error
("data expected to be encrypted")
223 debug
(sub{"enc_to=$enc_to\n"});
224 error
("data expected to be signed")
226 debug
(sub{"goodsig=$goodsig\n"});
227 error
("modification detection code incorrect")
229 debug
(sub{"good_mdc=$goodmdc\n"});
230 error
("data signature invalid")
231 unless $validsig and $validpub;
232 debug
(sub{"validsig=$validsig\n"});
233 debug
(sub{"validpub=$validpub\n"});
234 error
("data signature refused")
235 unless exists $ctx->{config
}->{keys}->{$validpub}
236 or exists $ctx->{config
}->{'hidden-keys'}->{$validpub};
237 debug
(sub{"accepted:$validpub\n"});
241 sub grg_remote_fetch_file
($) {
243 # NOTE: avoid File::Copy::copy().
244 while (my ($file, undef) = each %{$ctx->{remote
}->{fetch
}}) {
245 my $path = File
::Spec-
>catfile($ctx->{remote
}->{uri
}->file, $file);
247 my $h = $ctx->{remote
}->{fetch
}->{$file};
255 sub grg_remote_fetch_rsync
($) {
257 my $uri = $ctx->{remote
}->{uri
}->clone;
259 if ($uri->opaque =~ m{^//}) {
260 $uri->fragment(undef);
262 @src = map { $uri->path($_); $uri->as_string; }
263 (keys %{$ctx->{remote
}->{fetch
}});
266 my ($authority, $path, $fragment)
267 = $uri->as_string =~ m
|^rsync
:(?:([^/#:]+):)?([^?#]*)(?:#(.*))?$|;
268 @src = map { "$authority:$path/$_" }
269 (keys %{$ctx->{remote
}->{fetch
}});
271 IPC
::Run
::run
([@{$ctx->{config
}->{rsync
}}
272 , '-i', '--ignore-times', '--inplace', '--progress'
274 , $ctx->{'dir-cache'}.'/']
277 sub grg_remote_fetch_sftp
($) {
279 IPC
::Run
::run
([@{$ctx->{config
}->{curl
}}
281 , '--output', File
::Spec-
>catfile($ctx->{'dir-cache'}, '#1')
282 , $ctx->{remote
}->{uri
}->as_string.'/'.'{'.join(',', (keys %{$ctx->{remote
}->{fetch
}})).'}' ]
285 sub grg_remote_fetch
($$) {
286 my ($ctx, $files) = @_;
287 debug
(sub{'files='}, $files);
288 my $scheme = $ctx->{remote
}->{uri
}->scheme;
289 $ctx->{remote
}->{fetch
}
291 { path
=> File
::Spec-
>catfile($ctx->{'dir-cache'}, $_)
295 { file
=> \
&grg_remote_fetch_file
296 , rsync
=> \
&grg_remote_fetch_rsync
297 , sftp
=> \
&grg_remote_fetch_sftp
299 error
("URL scheme not supported: `$scheme'")
302 or $ctx->{remote
}->{fetch
} = {};
303 return $ctx->{remote
}->{fetch
};
305 sub grg_remote_init_file
($) {
307 my $dst = $ctx->{remote
}->{uri
}->file;
311 sub grg_remote_init_rsync
($) {
313 my $tmp = File
::Temp-
>tempdir('grg_rsync_XXXXXXXX', CLEANUP
=> 1);
314 my $uri = $ctx->{remote
}->{uri
}->clone;
316 if ($uri->opaque =~ m{^//}) {
317 $uri->fragment(undef);
320 $dst = $uri->as_string;
323 my ($authority, $fragment);
324 ($authority, $path, $fragment)
325 = $uri->as_string =~ m
|^rsync
:(?:([^/#:]+):)?([^?#]*)(?:#(.*))?$|;
326 $dst = "$authority:";
328 &mkdir(File
::Spec-
>catdir($tmp, $path));
329 IPC
::Run
::run
([@{$ctx->{config
}->{rsync
}}
330 , '-i', '--recursive', '--relative'
334 , init
=> sub { chdir $tmp or die $!; })
336 sub grg_remote_init_sftp
($) {
338 my $uri = $ctx->{remote
}->{uri
}->clone;
339 my ($path) = $uri->path =~ m
|^/?(.*)$|;
340 $uri->fragment(undef);
343 IPC
::Run
::run
([@{$ctx->{config
}->{curl
}}
344 , '--show-error', '--ftp-create-dirs'
345 , '-Q', "+mkdir ".$path
349 sub grg_remote_init
($) {
351 my $scheme = $ctx->{remote
}->{uri
}->scheme;
353 { file
=> \
&grg_remote_init_file
354 , rsync
=> \
&grg_remote_init_rsync
355 , sftp
=> \
&grg_remote_init_sftp
357 error
("URL scheme not supported: `$scheme'")
360 or error
("remote init failed");
363 sub grg_remote_push_file
($) {
366 foreach my $file (@{$ctx->{remote
}->{push}}) {
367 my $src = File
::Spec-
>catfile($ctx->{'dir-cache'}, $file);
368 my $dst = File
::Spec-
>catfile($ctx->{remote
}->{uri
}->file, $file);
369 debug
(sub{"File::Copy::move('$src', '$dst')\n"});
370 if (not File
::Copy
::move
($src, $dst)) {
377 sub grg_remote_push_rsync
($) {
379 my $uri = $ctx->{remote
}->{uri
}->clone;
380 $uri->fragment(undef);
383 if ($uri->opaque =~ m{^//}) {
384 $uri->fragment(undef);
386 $dst = $uri->as_string;
389 my ($authority, $path, $fragment)
390 = $uri->as_string =~ m
|^rsync
:(?:([^/#:]+):)?([^?#]*)(?:#(.*))?$|;
391 $dst = "$authority:$path/";
393 IPC
::Run
::run
([@{$ctx->{config
}->{rsync
}}
395 , (@{$ctx->{remote
}->{push}})
398 , init
=> sub { chdir $ctx->{'dir-cache'} or die $!; });
400 sub grg_remote_push_sftp
($) {
402 my $uri = $ctx->{remote
}->{uri
}->clone;
403 $uri->fragment(undef);
405 IPC
::Run
::run
([@{$ctx->{config
}->{curl
}}
406 , '--show-error', '--ftp-create-dirs', '--upload-file'
407 , File
::Spec-
>catfile($ctx->{'dir-cache'},'{'.join(',', @{$ctx->{remote
}->{push}}).'}')
408 , $uri->as_string.'/']
411 sub grg_remote_push
($) {
413 my $scheme = $ctx->{remote
}->{uri
}->scheme;
414 grg_remote_init
($ctx)
415 unless $ctx->{remote
}->{checked
};
417 if @{$ctx->{remote
}->{push}} == 0;
419 { file
=> \
&grg_remote_push_file
420 , rsync
=> \
&grg_remote_push_rsync
421 , sftp
=> \
&grg_remote_push_sftp
423 error
("URL scheme not supported: `$scheme'")
426 or error
("remote push failed");
427 rm
(map {File
::Spec-
>catfile($ctx->{'dir-cache'}, $_)} @{$ctx->{remote
}->{push}});
430 sub grg_remote_remove
($) {
432 #my $scheme = $ctx->{remote}->{uri}->scheme;
435 # File::Copy::remove_tree
436 # ( map { File::Spec->catfile($ctx->{remote}->{uri}->path, $_) } @$files
440 # IPC::Run::run([@{$ctx->{config}->{rsync}}
441 # , '--verbose', '--ignore-times', '--recursive', '--delete'
443 # , $ctx->{remote}->{uri}])
446 # IPC::Run::run([@{$ctx->{config}->{curl}}
448 # , map { ('-Q', 'rm '.$_) } @$files
449 # , $ctx->{remote}->{uri}])
452 #error("URL scheme not supported: `$scheme'")
454 #$fct->($ctx, $ctx->{remote}->{remove})
455 # or error("remote remove failed");
459 sub grg_pack_fetch
($$) {
460 my ($ctx, $fetch_objects) = @_;
463 my %remote_objects = ();
464 while (my ($pack_id, $pack) = each %{$ctx->{manifest
}->{packs
}}) {
465 foreach my $obj (@{$pack->{objects
}}) {
466 $remote_objects{$obj} = $pack_id;
470 my %packs_to_fetch = ();
471 foreach my $obj (@$fetch_objects) {
472 my @packs = ($remote_objects{$obj});
473 while (my $pack_id = shift @packs) {
474 if (not exists $packs_to_fetch{$pack_id}) {
475 $packs_to_fetch{$pack_id} = 1;
476 my $manifest_pack = $ctx->{manifest
}->{packs
}->{$pack_id};
477 error
("manifest is missing a dependency pack: $pack_id")
478 unless defined $manifest_pack;
479 @packs = (@packs, @{$manifest_pack->{deps
}});
483 my @packs_to_fetch = keys %packs_to_fetch;
484 my $packs_fetched = grg_remote_fetch
($ctx, [@packs_to_fetch]);
485 foreach my $pack_id (@packs_to_fetch) {
487 = exists $packs_fetched->{$pack_id}
488 ? $packs_fetched->{$pack_id}
489 : {path
=> File
::Spec-
>catfile($ctx->{'dir-cache'}, $pack_id), preserve
=> 0};
490 my $manifest_pack = $ctx->{manifest
}->{packs
}->{$pack_id};
491 my $pack_key = $manifest_pack->{key
};
493 grg_decrypt_symmetric
($ctx, $pack_key, sub {
494 push @{$_[0]}, ($pack_fetched->{path
});
495 return (@_, '>', \
$pack_data);
497 my $pack_hash_algo = $manifest_pack->{hash_algo
};
498 my $pack_hash = grg_hash
($ctx
500 , sub { return (@_, '<', \
$pack_data); });
501 error
("pack data hash differs from pack manifest hash")
502 unless $pack_hash eq $manifest_pack->{hash
};
503 rm
($pack_fetched->{path
})
504 unless $pack_fetched->{preserve
};
505 IPC
::Run
::run
(['git', 'index-pack', '-v', '--stdin']
510 sub grg_pack_push
($$) {
511 my ($ctx, $push_objects) = @_;
513 debug
(sub{"push_objects=\n"}, $push_objects);
515 my %remote_objects = ();
516 while (my ($pack_id, $pack) = each %{$ctx->{manifest
}->{packs
}}) {
517 foreach my $obj (@{$pack->{objects
}}) {
518 $remote_objects{$obj} = $pack_id;
522 IPC
::Run
::run
(['git', 'cat-file', '--batch-check']
523 , '<', \
join("\n", keys %remote_objects)
525 or error
("failed to query local git objects");
528 if ($_ =~ m/ missing$/) { () }
531 # @pack_objects, @pack_deps_objects
532 IPC
::Run
::run
(['git', 'rev-list', '--objects-edge', '--stdin', '--']
533 , '<', \
join("\n", ((map {'^'.$_} @common_objects), @$push_objects))
535 or error
("failed to query objects to pack");
536 my @pack_objects_edge = split(/\n/, $_);
537 foreach (@pack_objects_edge) {s/ .*//}
538 my @pack_objects = grep {m/^[^-]/} @pack_objects_edge;
539 my @pack_deps_objects = grep {s/^-//} @pack_objects_edge;
542 foreach my $obj (@pack_deps_objects) {
543 my $pack = $remote_objects{$obj};
544 error
("manifest is missing object dependencies")
545 unless defined $pack;
546 $pack_deps{$pack} = 1;
548 if (@pack_objects > 0) {
552 while (not defined $pack_id
553 or exists $ctx->{manifest
}->{packs
}->{$pack_id}) {
554 $pack_id = grg_rand
($ctx, $ctx->{config
}->{'pack-filename-size'});
555 $pack_id =~ s{/}{-}g;
556 error
("failed to pick an unused random pack filename after 512 tries; retry or increase grg.pack-filename-size")
557 if $pack_id_try++ >= 512;
559 my $pack_key = grg_rand
($ctx, $ctx->{config
}->{'pack-key-size'});
561 IPC
::Run
::run
(['git', 'pack-objects', '--stdout']
562 , '<', \
join("\n", @pack_objects)
564 or error
("failed to pack objects to push");
565 my $pack_hash = grg_hash
($ctx
566 , $ctx->{config
}->{'pack-hash-algo'}
567 , sub { return (@_, '<', \
$pack_data); });
568 grg_encrypt_symmetric
($ctx, $pack_data, $pack_key, sub {
569 push @{$_[0]}, ('--output', File
::Spec-
>catfile($ctx->{'dir-cache'}, $pack_id));
572 push @{$ctx->{remote
}->{push}}, $pack_id;
573 $ctx->{manifest
}->{packs
}->{$pack_id} =
574 { deps
=> [keys %pack_deps]
576 , hash_algo
=> $ctx->{config
}->{'pack-hash-algo'}
578 , objects
=> \
@pack_objects
583 sub grg_manifest_fetch
($) {
586 { 'hidden-keys' => {}
590 , version
=> $VERSION
592 my $fetched = grg_remote_fetch
($ctx, [$ctx->{'manifest-file'}]);
593 my $crypt = $fetched->{$ctx->{'manifest-file'}}->{path
};
594 if (defined $crypt) {
595 $ctx->{remote
}->{checked
} = 1;
597 grg_decrypt_asymmetric
($ctx, sub {
598 push @{$_[0]}, $crypt;
599 return (@_, '>', \
$json); });
600 rm
($fetched->{$ctx->{'manifest-file'}}->{path
})
601 unless $fetched->{$ctx->{'manifest-file'}}->{preserve
};
603 ($manifest = JSON
::decode_json
($json) and ref $manifest eq 'HASH')
604 or error
("failed to decode JSON manifest");
605 $ctx->{manifest
} = {%{$ctx->{manifest
}}, %$manifest};
606 foreach my $slot (qw(keys hidden-keys)) {
607 while (my ($fpr, $uid) = each %{$ctx->{manifest
}->{$slot}}) {
608 my %keys = gpg_fingerprint
($ctx, '0x'.$fpr, ['E']);
609 my ($fpr, $uid) = each %keys;
610 $ctx->{config
}->{$slot}->{$fpr} = $uid;
615 if ($ctx->{command
} eq 'push' or $ctx->{command
} eq 'list for-push') {
616 $ctx->{remote
}->{checked
} = 0;
618 elsif ($ctx->{remote
}->{checking
}) {
622 error
("remote checking failed");
626 sub grg_manifest_push
($) {
628 foreach my $slot (qw(keys hidden-keys)) {
629 $ctx->{manifest
}->{$slot} = {};
630 while (my ($fpr, $uid) = each %{$ctx->{config
}->{$slot}}) {
631 $ctx->{manifest
}->{$slot}->{$fpr} = $uid;
634 my $json = JSON
::encode_json
($ctx->{manifest
})
635 or error
("failed to encode JSON manifest");
636 grg_encrypt_asymmetric
($ctx, $json, sub {
638 , ('--output', File
::Spec-
>catfile($ctx->{'dir-cache'}, $ctx->{'manifest-file'}));
640 push @{$ctx->{remote
}->{push}}, $ctx->{'manifest-file'};
643 sub grg_config_read
($) {
645 my $cfg = $ctx->{config
};
648 foreach my $name (qw(gpg signingkey keys)
649 , grep { !m/^(gpg|signingkey|keys)$/ } (keys %$cfg)) {
651 IPC
::Run
::run
(['git', 'config', '--get', 'remote.'.$ctx->{remote
}->{name
}.'.'.$name, '.+'], '>', \
$value) or
652 IPC
::Run
::run
(['git', 'config', '--get', 'grg.'.$name, '.+'], '>', \
$value) or 1;
653 if ($name eq 'signingkey') {
654 IPC
::Run
::run
(['git', 'config', '--get', 'user.'.$name, '.+'], '>', \
$value)
657 my %keys = gpg_fingerprint
($ctx, $value, ['S']);
658 warning
("signing key ID is not matching a unique key: taking only one")
659 unless scalar(keys %keys) == 1;
660 my ($fpr, $uid) = each %keys;
661 $cfg->{$name} = {fpr
=> $fpr, uid
=> $uid};
663 elsif ($name eq 'keys' or $name eq 'hidden-keys') {
664 IPC
::Run
::run
(['git', 'config', '--get', 'user.'.$name, '.+'], '>', \
$value)
667 my @ids = split(/,/, $value);
669 foreach my $key (@ids) {
670 my %keys = gpg_fingerprint
($ctx, $key, ['E']);
671 while (my ($fpr, $uid) = each %keys) {
672 $cfg->{$name}->{$fpr} = $uid;
677 elsif (grep(/^$name$/, qw(curl gpg rsync))) {
678 IPC
::Run
::run
(['git', 'config', '--get', $name.'.program', '.+'], '>', \
$value)
680 $cfg->{$name} = [split(' ', $value)]
685 $cfg->{$name} = $value
689 error
("no signingkey configured; to do so you may use one of following commands:\n"
690 , "\t\$ git config remote.'$ctx->{remote}->{name}'.signingkey \$your_openpgp_id\n"
691 , "\t\$ git config grg.signingkey \$your_openpgp_id\n"
692 , "\t\$ git config user.signingkey \$your_openpgp_id"
693 ) unless defined $cfg->{signingkey
};
694 if ( (scalar (keys %{$cfg->{keys}}) == 0)
695 and (scalar (keys %{$cfg->{'hidden-keys'}}) == 0) ) {
696 $cfg->{keys} = { $cfg->{signingkey
}->{fpr
} => $cfg->{signingkey
}->{uid
} };
699 debug
(sub{'config='},$cfg);
702 sub grg_connect
($) {
704 grg_config_read
($ctx);
705 grg_manifest_fetch
($ctx);
707 sub grg_disconnect
($) {
709 grg_remote_push
($ctx);
712 sub gpg_command_answer
($) {
714 debug
(sub{join('', @cmd)."\n"});
715 print STDOUT
(@cmd, "\n");
717 sub grg_command_capabilities
($) {
719 $ctx->{command
} = 'capabilities';
720 gpg_command_answer
("fetch");
721 gpg_command_answer
("push");
722 gpg_command_answer
("");
725 sub grg_command_fetch
($$) {
726 my ($ctx, $fetch_refs) = @_;
727 $ctx->{command
} = 'fetch';
728 debug
(sub{"fetch_refs="}, $fetch_refs);
731 my @fetch_objects= ();
732 foreach my $ref (@$fetch_refs) {
733 push @fetch_objects, $ref->{sha1
};
735 grg_pack_fetch
($ctx, \
@fetch_objects);
737 sub grg_command_list
($$) {
738 my ($ctx, $command) = @_;
739 $ctx->{command
} = $command;
741 my $manifest_refs = $ctx->{manifest
}->{refs
};
742 while (my ($ref, $obj) = each %$manifest_refs) {
743 if ($obj =~ m
|^ref: *(.*) *$|) {
744 $obj = $manifest_refs->{$1};
746 gpg_command_answer
("$obj $ref")
749 gpg_command_answer
("");
751 sub grg_command_push
($$) {
752 my ($ctx, $push_refs) = @_;
754 $ctx->{command
} = 'push';
755 debug
(sub{"push_refs="}, $push_refs);
758 my @push_objects= ();
759 foreach my $ref (@$push_refs) {
760 IPC
::Run
::run
(['git', 'rev-list', '--ignore-missing', '--max-count=1', $ref->{src
}, '--']
762 or error
("failed to dereference ref to push: ".$ref->{src
});
764 $ref->{src_obj
} = $_;
765 push @push_objects, $_;
767 grg_pack_push
($ctx, \
@push_objects);
768 my $manifest_refs = $ctx->{manifest
}->{refs
};
769 foreach my $ref (@$push_refs) {
770 $manifest_refs->{$ref->{dst
}} = $ref->{src_obj
};
772 $manifest_refs->{HEAD
} = 'ref: refs/heads/master'
773 unless defined $manifest_refs->{HEAD
};
774 grg_manifest_push
($ctx);
775 grg_disconnect
($ctx);
777 sub grg_commands
(@) {
781 #STDOUT->autoflush(1);
782 while (defined $line or (not eof(*STDIN
) and
783 (defined($line = readline(*STDIN
)))
785 : error
("readline failed: $!")
787 debug
(sub{"line=\"",$line,"\"\n"});
788 $ctx->{command
} = undef;
789 if ($line eq 'capabilities') {
790 grg_command_capabilities
($ctx);
793 elsif ($line =~ m/^fetch .*$/) {
796 while ((defined $line or (not eof(*STDIN
) and
797 ((defined($line = readline(*STDIN
)))
799 : error
("readline failed: $!")))) and
800 (($sha1, $name) = ($line =~ m/^fetch ([0-9a-f]{40}) (.+)$/))
802 debug
(sub{"fetch line=\"",$line,"\"\n"});
803 push @refs, {sha1
=>$sha1, name
=>$name};
806 error
("failed to parse command: $line")
808 grg_command_fetch
($ctx, \
@refs);
810 elsif ($line eq 'list' or $line eq 'list for-push') {
811 grg_command_list
($ctx, $line);
814 elsif ($line =~ m/^push .*$/) {
816 my ($force, $src, $dst);
817 while ((defined $line or (not eof(*STDIN
) and
818 ((defined($line = readline(*STDIN
)))
820 : error
("readline failed: $!")))) and
821 (($force, $src, $dst) = ($line =~ m/^push (\+)?([^:]+):(.+)$/))
823 debug
(sub{"push line=\"",$line,"\"\n"});
824 push @refs, {force
=>(defined $force), src
=>$src, dst
=>$dst};
827 error
("failed to parse command: $line")
829 grg_command_push
($ctx, \
@refs);
831 elsif ($line =~ m/^$/) {
834 local $SIG{'PIPE'} = 'IGNORE';
835 gpg_command_answer
("");
840 warning
("unsupported command supplied: `$line'");
846 $ENV{GIT_DIR
} = $ENV{GIT_DIR
} || '.git';
847 $ENV{GITCEPTION
} = ($ENV{GITCEPTION
} || '') . '+';
854 , 'hidden-keys' => {}
855 , 'manifest-hash-algo' => 'SHA224' # NOTE: SHA512, SHA384, SHA256, SHA224 supported.
856 , 'pack-filename-size' => 42
857 , 'pack-hash-algo' => 'SHA224' # NOTE: SHA512, SHA384, SHA256, SHA224 supported.
858 , 'pack-key-size' => 64
859 , signingkey
=> undef
862 , 'dir-cache' => undef
864 , 'manifest-file' => undef
873 Getopt
::Long
::Configure
878 Getopt
::Long
::GetOptions
879 ( help
=> sub { Pod
::Usage
::pod2usage
881 , -sections
=> ['SYNOPSIS', 'OPTIONS', 'REMOTES', 'CONFIG']
882 , -verbose
=> 99 ); }
883 , man
=> sub { Pod
::Usage
::pod2usage
(-verbose
=> 2); }
885 $ctx->{remote
}->{checking
} = 1;
888 if (not $ctx->{remote
}->{checking
}) {
889 my $name = shift @ARGV;
890 Pod
::Usage
::pod2usage
(-verbose
=> 1)
891 unless defined $name;
892 ($ctx->{remote
}->{name
}) = ($name =~ m/^((\w|-)+)$/);
893 error
("valid name of remote Git required, got: `$name'")
894 unless $ctx->{remote
}->{name
};
896 my $uri = shift @ARGV;
897 Pod
::Usage
::pod2usage
(-verbose
=> 1)
899 $ctx->{remote
}->{uri
} = URI-
>new($uri);
900 error
("valid URL of remote Git required, got: `$uri'")
901 unless $ctx->{remote
}->{uri
};
902 my $fragment = $ctx->{remote
}->{uri
}->fragment;
904 unless defined $fragment;
905 $ctx->{'manifest-file'} = grg_hash
($ctx
906 , $ctx->{config
}->{'manifest-hash-algo'}
907 , sub { return (@_, '<', \
$fragment); });
908 if (-d
$ENV{GIT_DIR
}) {
909 $ctx->{'dir-cache'} = File
::Spec-
>catdir
910 ( $ENV{GIT_DIR
}, 'cache', 'remotes'
911 , $ctx->{remote
}->{name
}, 'gpg');
912 &mkdir($ctx->{'dir-cache'});
915 $ctx->{'dir-cache'} = File
::Temp-
>tempdir('grg_cache_XXXXXXXX', CLEANUP
=> 1);
917 debug
(sub{"ctx="},$ctx);
928 git-remote-gpg - git-remote-helpers(1) to encrypt remote repository through gpg(1)
932 =item git-remote-gpg $gpg_remote $gpg_url
934 =item git-remote-gpg --check $gpg_url
940 =item B<-h>, B<--help>
950 =item git remote add $remote gpg::rsync:${user:+$user@}$host:$path
952 =item git remote add $remote gpg::rsync://${user:+$user@}$host${port:+:$port}/$path
956 =item git remote add $remote gpg::sftp://${user:+$user@}$host${port:+:$port}/$path
958 =head2 Via File::Copy(3pm)
960 =item git remote add $remote gpg::file://$path
968 =item B<grg.curl>, B<remote.$remote.curl>
970 =item B<grg.gpg>, B<remote.$remote.gpg>
972 =item B<grg.keys>, B<remote.$remote.keys>
974 =item B<grg.hidden-keys>, B<remote.$remote.hidden-keys>
976 =item B<grg.manifest-hash-algo>, B<remote.$remote.manifest-hash-algo>
978 =item B<grg.pack-filename-size>, B<remote.$remote.pack-filename-size>
980 =item B<grg.pack-hash-algo>, B<remote.$remote.pack-hash-algo>
982 =item B<grg.pack-key-size>, B<remote.$remote.pack-key-size>
984 =item B<grg.signingkey>, B<remote.$remote.signingkey>
986 =item B<grg.rsync>, B<remote.$remote.rsync>