]>
Git — Sourcephile - git-remote-gpg.git/blob - git-remote-gpg
2 our $VERSION = '2014.01.28';
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 debug
(sub{'ctx='}, $ctx);
177 debug
(sub{'key='}, $key);
178 $run = sub {return @_} unless defined $run;
179 IPC
::Run
::run
($run->([@{$ctx->{config
}->{gpg
}}
180 , '--batch', '--no-default-keyring', '--keyring', '/dev/null', '--secret-keyring', '/dev/null'
181 , '--passphrase-fd', '3', '--quiet', '--decrypt']
183 or error
("failed to decrypt symmetrically data");
185 sub grg_encrypt_asymmetric
($$;$) {
186 my ($ctx, $clear, $run) = @_;
187 $run = sub {return @_} unless defined $run;
189 ( (map { ('--recipient', '0x'.$_) } (keys %{$ctx->{config
}->{keys}}))
190 , (map { ('--hidden-recipient', '0x'.$_) } (keys %{$ctx->{config
}->{'hidden-keys'}})) );
191 @recipients = ('--default-recipient-self')
193 IPC
::Run
::run
($run->([@{$ctx->{config
}->{gpg
}}
195 , '--compress-algo', 'none'
196 , '--trust-model', 'always'
197 , '--sign', '--encrypt'
198 , ($ctx->{config
}->{signingkey
}->{fpr
} ? ('--local-user', $ctx->{config
}->{signingkey
}->{fpr
}) : ())
201 or error
("failed to encrypt asymmetrically data");
203 sub grg_decrypt_asymmetric
($$;$) {
204 my ($ctx, $run) = @_;
205 my ($clear, $status);
206 $run = sub {return @_} unless defined $run;
207 IPC
::Run
::run
($run->([@{$ctx->{config
}->{gpg
}}
208 , '--batch', '--no-default-keyring',
209 , '--status-fd', '3', '--quiet', '--decrypt']
210 , '>', \
$clear, '3>', \
$status))
211 or error
("failed to decrypt asymmetrically data");
212 debug
(sub{"status=\n$status"});
213 my @lines = split(/\n/,$status);
214 my ($enc_to, $goodsig, $validsig, $validpub, $goodmdc);
215 foreach my $line (@lines) {
216 (not defined $enc_to and (($enc_to) = $line =~ m/^\[GNUPG:\] ENC_TO ([0-9A-F]+).*$/)) or
217 (not defined $goodsig and (($goodsig) = $line =~ m/^\[GNUPG:\] GOODSIG ([0-9A-F]+).*$/)) or
218 (not defined $goodmdc and (($goodmdc) = $line =~ m/^\[GNUPG:\] (GOODMDC)$/)) or
219 (not defined $validsig and not defined $validpub and (($validsig, $validpub)
220 = $line =~ m/^\[GNUPG:\] VALIDSIG ([0-9A-F]+) [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ ([0-9A-F]+).*$/)) or
223 error
("data expected to be encrypted")
225 debug
(sub{"enc_to=$enc_to\n"});
226 error
("data expected to be signed")
228 debug
(sub{"goodsig=$goodsig\n"});
229 error
("modification detection code incorrect")
231 debug
(sub{"good_mdc=$goodmdc\n"});
232 error
("data signature invalid")
233 unless $validsig and $validpub;
234 debug
(sub{"validsig=$validsig\n"});
235 debug
(sub{"validpub=$validpub\n"});
236 error
("data signature refused")
237 unless exists $ctx->{config
}->{keys}->{$validpub}
238 or exists $ctx->{config
}->{'hidden-keys'}->{$validpub};
239 debug
(sub{"accepted:$validpub\n"});
243 sub grg_remote_fetch_file
($) {
245 # NOTE: avoid File::Copy::copy().
246 while (my ($file, undef) = each %{$ctx->{remote
}->{fetch
}}) {
247 my $path = File
::Spec-
>catfile($ctx->{remote
}->{uri
}->file, $file);
249 my $h = $ctx->{remote
}->{fetch
}->{$file};
257 sub grg_remote_fetch_rsync
($) {
259 my $uri = $ctx->{remote
}->{uri
}->clone;
261 if ($uri->opaque =~ m{^//}) {
262 $uri->fragment(undef);
264 @src = map { $uri->path($_); $uri->as_string; }
265 (keys %{$ctx->{remote
}->{fetch
}});
268 my ($authority, $path, $fragment)
269 = $uri->as_string =~ m
|^rsync
:(?:([^/#:]+):)?([^?#]*)(?:#(.*))?$|;
270 @src = map { "$authority:$path/$_" }
271 (keys %{$ctx->{remote
}->{fetch
}});
273 IPC
::Run
::run
([@{$ctx->{config
}->{rsync
}}
274 , '-i', '--ignore-times', '--inplace', '--progress'
276 , $ctx->{'dir-cache'}.'/']
279 sub grg_remote_fetch_sftp
($) {
281 IPC
::Run
::run
([@{$ctx->{config
}->{curl
}}
283 , '--output', File
::Spec-
>catfile($ctx->{'dir-cache'}, '#1')
284 , File
::Spec-
>catfile($ctx->{remote
}->{uri
}->as_string
285 , '{'.join(',', (keys %{$ctx->{remote
}->{fetch
}})).'}') ])
287 sub grg_remote_fetch
($$) {
288 my ($ctx, $files) = @_;
289 debug
(sub{'files='}, $files);
290 my $scheme = $ctx->{remote
}->{uri
}->scheme;
291 $ctx->{remote
}->{fetch
}
293 { path
=> File
::Spec-
>catfile($ctx->{'dir-cache'}, $_)
297 { file
=> \
&grg_remote_fetch_file
298 , rsync
=> \
&grg_remote_fetch_rsync
299 , sftp
=> \
&grg_remote_fetch_sftp
301 error
("URL scheme not supported: `$scheme'")
304 or $ctx->{remote
}->{fetch
} = {};
305 return $ctx->{remote
}->{fetch
};
307 sub grg_remote_init_file
($) {
309 my $dst = $ctx->{remote
}->{uri
}->file;
313 sub grg_remote_init_rsync
($) {
315 my $tmp = File
::Temp-
>tempdir('grg_rsync_XXXXXXXX', CLEANUP
=> 1);
316 my $uri = $ctx->{remote
}->{uri
}->clone;
318 if ($uri->opaque =~ m{^//}) {
319 $uri->fragment(undef);
322 $dst = $uri->as_string;
325 my ($authority, $fragment);
326 ($authority, $path, $fragment)
327 = $uri->as_string =~ m
|^rsync
:(?:([^/#:]+):)?([^?#]*)(?:#(.*))?$|;
328 $dst = "$authority:";
330 &mkdir(File
::Spec-
>catdir($tmp, $path));
331 IPC
::Run
::run
([@{$ctx->{config
}->{rsync
}}
332 , '-i', '--recursive', '--relative'
336 , init
=> sub { chdir $tmp or die $!; })
338 sub grg_remote_init_sftp
($) {
340 my $path = $ctx->{remote
}->{uri
}->path;
341 my $uri = $ctx->{remote
}->{uri
}->clone;
342 $uri->fragment(undef);
345 IPC
::Run
::run
([@{$ctx->{config
}->{curl
}}
346 , '--show-error', '--ftp-create-dirs'
347 , '-Q', "+mkdir ".$path
350 sub grg_remote_init
($) {
352 my $scheme = $ctx->{remote
}->{uri
}->scheme;
354 { file
=> \
&grg_remote_init_file
355 , rsync
=> \
&grg_remote_init_rsync
356 , sftp
=> \
&grg_remote_init_sftp
358 error
("URL scheme not supported: `$scheme'")
361 or error
("remote init failed");
364 sub grg_remote_push_file
($) {
367 foreach my $file (@{$ctx->{remote
}->{push}}) {
368 my $src = File
::Spec-
>catfile($ctx->{'dir-cache'}, $file);
369 my $dst = File
::Spec-
>catfile($ctx->{remote
}->{uri
}->file, $file);
370 debug
(sub{"File::Copy::move('$src', '$dst')\n"});
371 if (not File
::Copy
::move
($src, $dst)) {
378 sub grg_remote_push_rsync
($) {
380 my $uri = $ctx->{remote
}->{uri
}->clone;
381 $uri->fragment(undef);
384 if ($uri->opaque =~ m{^//}) {
385 $uri->fragment(undef);
387 $dst = $uri->as_string;
390 my ($authority, $path, $fragment)
391 = $uri->as_string =~ m
|^rsync
:(?:([^/#:]+):)?([^?#]*)(?:#(.*))?$|;
392 $dst = "$authority:$path/";
394 IPC
::Run
::run
([@{$ctx->{config
}->{rsync
}}
396 , (@{$ctx->{remote
}->{push}})
399 , init
=> sub { chdir $ctx->{'dir-cache'} or die $!; });
401 sub grg_remote_push_sftp
($) {
403 my $uri = $ctx->{remote
}->{uri
}->clone;
406 IPC
::Run
::run
([@{$ctx->{config
}->{curl
}}
407 , '--show-error', '--ftp-create-dirs', '--upload-file'
408 , '{'.join(',', @{$ctx->{remote
}->{push}}).'}'
409 , $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
};
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' => {}
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 debug
(sub{'ctx='}, $ctx);
616 if ($ctx->{command
} eq 'push' or $ctx->{command
} eq 'list for-push') {
617 $ctx->{remote
}->{checked
} = 0;
619 elsif ($ctx->{remote
}->{checking
}) {
623 error
("remote checking failed");
627 sub grg_manifest_push
($) {
629 foreach my $slot (qw(keys hidden-keys)) {
630 $ctx->{manifest
}->{$slot} = {};
631 while (my ($fpr, $uid) = each %{$ctx->{config
}->{$slot}}) {
632 $ctx->{manifest
}->{$slot}->{$fpr} = $uid;
635 my $json = JSON
::encode_json
($ctx->{manifest
})
636 or error
("failed to encode JSON manifest");
637 grg_encrypt_asymmetric
($ctx, $json, sub {
639 , ('--output', File
::Spec-
>catfile($ctx->{'dir-cache'}, $ctx->{'manifest-file'}));
641 push @{$ctx->{remote
}->{push}}, $ctx->{'manifest-file'};
644 sub grg_config_read
($) {
646 my $cfg = $ctx->{config
};
649 foreach my $name (qw(gpg signingkey keys)
650 , grep { !m/^(gpg|signingkey|keys)$/ } (keys %$cfg)) {
652 IPC
::Run
::run
(['git', 'config', '--get', 'remote.'.$ctx->{remote
}->{name
}.'.'.$name, '.+'], '>', \
$value) or
653 IPC
::Run
::run
(['git', 'config', '--get', 'grg.'.$name, '.+'], '>', \
$value) or 1;
654 if ($name eq 'signingkey') {
655 IPC
::Run
::run
(['git', 'config', '--get', 'user.'.$name, '.+'], '>', \
$value)
658 my %keys = gpg_fingerprint
($ctx, $value, ['S']);
659 warning
("signing key ID is not matching a unique key: taking only one")
660 unless scalar(keys %keys) == 1;
661 my ($fpr, $uid) = each %keys;
662 $cfg->{$name} = {fpr
=> $fpr, uid
=> $uid};
664 elsif ($name eq 'keys' or $name eq 'hidden-keys') {
665 IPC
::Run
::run
(['git', 'config', '--get', 'user.'.$name, '.+'], '>', \
$value)
668 my @ids = split(/,/, $value);
670 foreach my $key (@ids) {
671 my %keys = gpg_fingerprint
($ctx, $key, ['E']);
672 while (my ($fpr, $uid) = each %keys) {
673 $cfg->{$name}->{$fpr} = $uid;
678 elsif (grep(/^$name$/, qw(curl gpg rsync))) {
679 IPC
::Run
::run
(['git', 'config', '--get', $name.'.program', '.+'], '>', \
$value)
681 $cfg->{$name} = [split(' ', $value)]
686 $cfg->{$name} = $value
690 error
("no signingkey configured; to do so you may use one of following commands:\n"
691 , "\t\$ git config remote.'$ctx->{remote}->{name}'.signingkey \$your_openpgp_id\n"
692 , "\t\$ git config grg.signingkey \$your_openpgp_id\n"
693 , "\t\$ git config user.signingkey \$your_openpgp_id"
694 ) unless defined $cfg->{signingkey
};
695 if ( (scalar (keys %{$cfg->{keys}}) == 0)
696 and (scalar (keys %{$cfg->{'hidden-keys'}}) == 0) ) {
697 $cfg->{keys} = { $cfg->{signingkey
}->{fpr
} => $cfg->{signingkey
}->{uid
} };
700 debug
(sub{'config='},$cfg);
703 sub grg_connect
($) {
705 grg_config_read
($ctx);
706 grg_manifest_fetch
($ctx);
708 sub grg_disconnect
($) {
710 grg_remote_push
($ctx);
713 sub gpg_command_answer
($) {
715 debug
(sub{join('', @cmd)."\n"});
716 print STDOUT
(@cmd, "\n");
718 sub grg_command_capabilities
($) {
720 $ctx->{command
} = 'capabilities';
721 gpg_command_answer
("fetch");
722 gpg_command_answer
("push");
723 gpg_command_answer
("");
726 sub grg_command_fetch
($$) {
727 my ($ctx, $fetch_refs) = @_;
728 $ctx->{command
} = 'fetch';
729 debug
(sub{"fetch_refs="}, $fetch_refs);
732 my @fetch_objects= ();
733 foreach my $ref (@$fetch_refs) {
734 push @fetch_objects, $ref->{sha1
};
736 grg_pack_fetch
($ctx, \
@fetch_objects);
738 sub grg_command_list
($$) {
739 my ($ctx, $command) = @_;
740 $ctx->{command
} = $command;
742 while (my ($ref, $obj) = each %{$ctx->{manifest
}->{refs
}}) {
743 gpg_command_answer
("$obj $ref");
745 gpg_command_answer
("");
747 sub grg_command_push
($$) {
748 my ($ctx, $push_refs) = @_;
750 $ctx->{command
} = 'push';
751 debug
(sub{"push_refs="}, $push_refs);
754 my @push_objects= ();
755 foreach my $ref (@$push_refs) {
756 IPC
::Run
::run
(['git', 'rev-list', '--ignore-missing', '--max-count=1', $ref->{src
}, '--']
758 or error
("failed to dereference ref to push: ".$ref->{src
});
760 $ref->{src_obj
} = $_;
761 push @push_objects, $_;
763 grg_pack_push
($ctx, \
@push_objects);
764 my $manifest_refs = $ctx->{manifest
}->{refs
};
765 foreach my $ref (@$push_refs) {
766 $manifest_refs->{$ref->{dst
}} = $ref->{src_obj
};
768 $manifest_refs->{HEAD
}
769 = $push_refs->[-1]->{src_obj
}
770 unless exists $manifest_refs->{HEAD
}
772 grg_manifest_push
($ctx);
773 grg_disconnect
($ctx);
775 sub grg_commands
(@) {
779 #STDOUT->autoflush(1);
780 while (defined $line or (not eof(*STDIN
) and
781 (defined($line = readline(*STDIN
)))
783 : error
("readline failed: $!")
785 debug
(sub{"line=\"",$line,"\"\n"});
786 $ctx->{command
} = undef;
787 if ($line eq 'capabilities') {
788 grg_command_capabilities
($ctx);
791 elsif ($line =~ m/^fetch .*$/) {
794 while ((defined $line or (not eof(*STDIN
) and
795 ((defined($line = readline(*STDIN
)))
797 : error
("readline failed: $!")))) and
798 (($sha1, $name) = ($line =~ m/^fetch ([0-9a-f]{40}) (.+)$/))
800 debug
(sub{"fetch line=\"",$line,"\"\n"});
801 push @refs, {sha1
=>$sha1, name
=>$name};
804 error
("failed to parse command: $line")
806 grg_command_fetch
($ctx, \
@refs);
808 elsif ($line eq 'list' or $line eq 'list for-push') {
809 grg_command_list
($ctx, $line);
812 elsif ($line =~ m/^push .*$/) {
814 my ($force, $src, $dst);
815 while ((defined $line or (not eof(*STDIN
) and
816 ((defined($line = readline(*STDIN
)))
818 : error
("readline failed: $!")))) and
819 (($force, $src, $dst) = ($line =~ m/^push (\+)?([^:]+):(.+)$/))
821 debug
(sub{"push line=\"",$line,"\"\n"});
822 push @refs, {force
=>(defined $force), src
=>$src, dst
=>$dst};
825 error
("failed to parse command: $line")
827 grg_command_push
($ctx, \
@refs);
829 elsif ($line =~ m/^$/) {
832 local $SIG{'PIPE'} = 'IGNORE';
833 gpg_command_answer
("");
838 warning
("unsupported command supplied: `$line'");
844 $ENV{GIT_DIR
} = $ENV{GIT_DIR
} || '.git';
845 $ENV{GITCEPTION
} = ($ENV{GITCEPTION
} || '') . '+';
852 , 'hidden-keys' => {}
853 , 'manifest-hash-algo' => 'SHA224' # NOTE: SHA512, SHA384, SHA256, SHA224 supported.
854 , 'pack-filename-size' => 42
855 , 'pack-hash-algo' => 'SHA224' # NOTE: SHA512, SHA384, SHA256, SHA224 supported.
856 , 'pack-key-size' => 64
857 , signingkey
=> undef
860 , 'dir-cache' => undef
862 , 'manifest-file' => undef
871 Getopt
::Long
::Configure
876 Getopt
::Long
::GetOptions
877 ( help
=> sub { Pod
::Usage
::pod2usage
879 , -sections
=> ['SYNOPSIS', 'OPTIONS', 'REMOTES', 'CONFIG']
880 , -verbose
=> 99 ); }
881 , man
=> sub { Pod
::Usage
::pod2usage
(-verbose
=> 2); }
883 $ctx->{remote
}->{checking
} = 1;
886 if (not $ctx->{remote
}->{checking
}) {
887 my $name = shift @ARGV;
888 Pod
::Usage
::pod2usage
(-verbose
=> 1)
889 unless defined $name;
890 ($ctx->{remote
}->{name
}) = ($name =~ m/^((\w|-)+)$/);
891 error
("valid name of remote Git required, got: `$name'")
892 unless $ctx->{remote
}->{name
};
894 my $uri = shift @ARGV;
895 Pod
::Usage
::pod2usage
(-verbose
=> 1)
897 $ctx->{remote
}->{uri
} = URI-
>new($uri);
898 error
("valid URL of remote Git required, got: `$uri'")
899 unless $ctx->{remote
}->{uri
};
900 my $fragment = $ctx->{remote
}->{uri
}->fragment;
902 unless defined $fragment;
903 $ctx->{'manifest-file'} = grg_hash
($ctx
904 , $ctx->{config
}->{'manifest-hash-algo'}
905 , sub { return (@_, '<', \
$fragment); });
906 if (-d
$ENV{GIT_DIR
}) {
907 $ctx->{'dir-cache'} = File
::Spec-
>catdir
908 ( $ENV{GIT_DIR
}, 'cache', 'remotes'
909 , $ctx->{remote
}->{name
}, 'gpg');
910 &mkdir($ctx->{'dir-cache'});
913 $ctx->{'dir-cache'} = File
::Temp-
>tempdir('grg_cache_XXXXXXXX', CLEANUP
=> 1);
915 debug
(sub{"ctx="},$ctx);
926 git-remote-gpg - git-remote-helpers(1) to encrypt remote repository through gpg(1)
930 =item git-remote-gpg $gpg_remote $gpg_url
932 =item git-remote-gpg --check $gpg_url
938 =item B<-h>, B<--help>
948 =item git remote add $remote gpg::rsync:${user:+$user@}$host:$path
950 =item git remote add $remote gpg::rsync://${user:+$user@}$host${port:+:$port}/$path
954 =item git remote add $remote gpg::sftp://${user:+$user@}$host${port:+:$port}/$path
956 =head2 Via File::Copy(3pm)
958 =item git remote add $remote gpg::file://$path
972 =item B<grg.hidden-keys>
974 =item B<grg.manifest-hash-algo>
976 =item B<grg.pack-filename-size>
978 =item B<grg.pack-hash-algo>
980 =item B<grg.pack-key-size>
982 =item B<grg.signingkey>