3 # Integration between git and Debian-style archives
5 # Copyright (C)2013 Ian Jackson
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
25 use Dpkg::Control::Hash;
29 open DEBUG, ">&STDERR" or die $!;
31 our $mirror = 'http://mirror.relativity.greenend.org.uk/mirror/debian-ftp/';
35 our $aliothname = 'iwj@git.debian.org';
36 our $aliothpath = '/git/dgit-test';
37 our $alioth_git = "git+ssh://$aliothname/$aliothpath";
38 our $alioth_sshtestbodge = [$aliothname,$aliothpath];
40 our (@dget_opts) = qw(-u);
42 our $remotename = 'dgit';
44 sub mainbranch () { return "dgit/$suite"; }
45 sub uploadbranch () { return "dgit/$suite.upload"; }
46 sub lref ($) { return "refs/heads/$_[0]"; }
47 sub rref ($) { return "refs/remotes/$remotename/$_[0]"; }
53 $ua = LWP::UserAgent->new();
56 print DEBUG "fetching @_...\n";
57 my $r = $ua->get(@_) or die $!;
58 die "$_[0]: ".$r->status_line."; failed.\n" unless $r->is_success;
59 return $r->decoded_content();
62 our ($dscdata,$dscurl,$dsc);
66 die "@_ $! $?" if system @_;
70 open P, "-|", @_ or die $!;
73 { local $/ = undef; $d = <P>; }
75 close P or die "@_ $? $!";
81 my $c = Dpkg::Control::Hash->new();
82 $c->load(@_) or return undef;
87 my $c = Dpkg::Control::Hash->new();
88 my $p = new IO::File '-|', qw(dpkg-parsechangelog) or die $!;
90 $?=0; $!=0; close $p or die "$! $?";
94 sub get_archive_dsc () {
95 my $rmad = cmdoutput qw(rmadison -asource),"-s$suite",$package;
96 $rmad =~ m/^ \s*( [^ \t|]+ )\s* \|
99 \s*( [^ \t|]+ )\s* /x or die "$rmad $?";
100 $1 eq $package or die "$rmad $package ?";
102 $3 eq $suite or die "$rmad $suite ?";
103 $4 eq 'source' or die "$rmad ?";
104 # fixme it does not show us the component ?
105 my $prefix = substr($package, 0, $package =~ m/^l/ ? 4 : 1);
106 $dscurl = "$mirror/pool/main/$prefix/$package/${package}_$vsn.dsc";
107 #print DEBUG Dumper($pdodata, $&, $dscurl);
108 $dscdata = url_get($dscurl);
109 my $dscfh = new IO::File \$dscdata, '<' or die $!;
110 #print DEBUG Dumper($dscdata, $dscfh);
111 $dsc = Dpkg::Control::Hash->new(allow_pgp=>1);
112 $dsc->parse($dscfh, 'dsc') or die "parsing of $dscurl failed\n";
113 #print DEBUG Dumper($dsc);
114 my $fmt = $dsc->{Format};
115 die "unsupported format $fmt, sorry\n" unless $fmt eq '1.0';
118 sub check_for_git () {
121 "ssh $alioth_sshtestbodge->[0] '".
122 " set -e; cd /git/dgit-test;".
123 " if test -d $package.git; then echo 1; else echo 0; fi".
125 #print DEBUG "$cmd\n";
126 open P, "$cmd |" or die $!;
128 my $r = <P>; close P;
129 #print STDERR ">$r<\n";
130 die "$r $! $?" unless $r =~ m/^[01]$/;
134 our ($dsc_hash,$lastupl_hash);
136 our $ud = '.git/dgit/unpack';
144 sub mktree_in_ud_from_only_subdir () {
145 # changes into the subdir
148 $dirs[0] =~ m#^([^/]+)/\.$# or die;
150 chdir $dir or die "$dir $!";
152 die $! unless $!==&ENOENT;
153 runcmd qw(git init -q);
154 rmtree('.git/objects');
155 symlink '../../../../objects','.git/objects' or die $!;
156 runcmd qw(git add -Af);
157 my $tree = cmdoutput qw(git write-tree);
158 chomp $tree; $tree =~ m/^\w+$/ or die "$tree ?";
162 sub generate_commit_from_dsc () {
166 foreach (split /\n/, ($dsc->{'Checksums-Sha256'} || $dsc->{Files})) {
168 m/^\w+ \d+ (\S+)$/ or die "$_ ?";
170 die if $f =~ m#/|^\.|\.dsc$|\.tmp$#;
172 link "../../../$f", $f
176 runcmd qw(dget), @dget_opts, qw(--), $dscurl;
177 foreach my $f (grep { m/\.tar\.gz$/ } @files) {
178 link $f, "../../../$f"
182 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
183 runcmd qw(sh -ec), 'dpkg-parsechangelog >../changelog.tmp';
184 my $clogp = parsecontrol('../changelog.tmp','changelog') or die;
185 my $date = cmdoutput qw(date), '+%s %z', qw(-d),$clogp->{Date};
186 my $author = $clogp->{Maintainer};
187 $author =~ s#,.*##ms;
188 my $authline = "$author $date";
189 $authline =~ m/^[^<>]+ \<\S+\> \d+ [-+]\d+$/ or die $authline;
190 open C, ">../commit.tmp" or die $!;
191 print C "tree $tree\n" or die $!;
192 print C "parent $lastupl_hash\n" or die $! if defined $lastupl_hash;
193 print C <<END or die $!;
199 # imported by dgit from the archive
202 print "synthesised git commit from .dsc $clogp->{Version}\n";
203 my $commithash = cmdoutput qw(git hash-object -w -t commit ../commit.tmp);
204 chdir '../../../..' or die $!;
205 cmdoutput qw(git update-ref -m),"dgit synthesise $clogp->{Version}",
206 'DGIT_ARCHIVE', $commithash;
207 cmdoutput qw(git log -n2), $commithash;
208 # ... gives git a chance to complain if our commit is malformed
209 my $outputhash = $commithash;
210 if (defined $lastupl_hash) {
211 chdir "$ud/$dir" or die $!;
212 runcmd qw(git reset --hard), $lastupl_hash;
213 runcmd qw(sh -ec), 'dpkg-parsechangelog >>../changelogold.tmp';
214 my $oldclogp = Dpkg::Control::Hash->new();
215 $oldclogp->parse('../changelogold.tmp','previous changelog') or die;
217 version_compare_string($oldclogp->{Version}, $clogp->{Version});
219 # git upload/ is earlier vsn than archive, use archive
220 } elsif ($vcmp >= 0) {
221 print STDERR <<END or die $!;
222 Version actually in archive: $clogp->{Version} (older)
223 Last allegedly pushed/uploaded: $oldclogp->{Version} (newer or same)
224 Perhaps the upload is stuck in incoming. Using the version from git.
227 die "version in archive is same as version in git".
228 " to-be-uploaded (upload/) branch but archive".
229 " version hash no commit hash?!\n";
231 chdir '../../../..' or die $!;
238 return cmdoutput qw(git rev-parse --), "$_[0]~0";
241 sub is_fast_fwd ($$) {
242 my ($ancestor,$child) = @_;
243 my $mb = cmdoutput qw(git merge-base), $dsc_hash, $lastupl_hash;
244 return rev_parse($mb) eq rev_parse($ancestor);
247 sub fetch_from_archive () {
248 # ensures that rref(uploadbranch()) is what is actually in the archive,
250 my $lastupl_ref = rref(uploadbranch());
251 $!=0; $lastupl_hash = `git show-ref --heads $lastupl_ref`;
253 die $? unless ($?==0 && chomp $lastupl_hash)
254 or ($?==128 && !length $lastupl_hash);
256 if (defined $dsc_hash) {
259 $hash = generate_commit_from_dsc();
262 die "not fast forward on last upload branch!".
263 " (archive's version left in DGIT_ARCHIVE)"
264 unless is_fast_fwd($lastupl_hash, $dsc_hash);
266 if ($lastupl_ref ne $hash) {
267 cmdoutput qw(git update-ref -m), 'dgit fetch', $lastupl_ref, $hash;
273 $dsc_hash = $dsc->{'Vcs-git-master'};
274 if (defined $dsc_hash) {
275 $dsc_hash =~ m/\w+/ or die "$dsc_hash $?";
277 print "last upload to archive specified git hash\n";
279 print "last upload to archive has NO git hash\n";
281 my $dstdir = "$package";
282 if (check_for_git()) {
283 print "cloning existing git history\n";
284 runcmd qw(git clone --origin),$remotename, qw(-b), $suite, '--',
285 $alioth_git, $dstdir;
286 chdir "$dstdir" or die "$dstdir $!";
287 fetch_from_archive();
288 runcmd qw(git reset --hard), rref(uploadbranch());
290 die "missing git history even though dsc has hash" if defined $dsc_hash;
291 print "starting new git history\n";
292 mkdir $dstdir or die "$dstdir $!";
293 chdir "$dstdir" or die "$dstdir $!";
294 runcmd qw(git init -q);
295 open H, "> .git/HEAD" or die $!;
296 print H "ref: ".lref(mainbranch())."\n" or die $!;
298 runcmd qw(git remote add), $remotename, $alioth_git;
299 runcmd "git config branch.$suite.remote $remotename";
300 runcmd "git config branch.$suite.merge ".lref(mainbranch());
301 my $newhash = generate_commit_from_dsc();
302 runcmd qw(git reset --hard), $newhash;
304 print "ready for work in $dstdir\n";
312 runcmd qw(git diff --quiet HEAD);
313 my $clogp = parsechangelog();
314 $package = $clogp->{Source};
315 my $dscfn = "${package}_$clogp->{Version}.dsc";
316 stat $dscfn or die "$dscfn $!";
317 $dsc = parsecontrol("../$dscfn");
320 print "checking that $dscfn corresponds to HEAD\n";
321 runcmd qw(dpkg-source -x --), "../../../../$dscfn";
322 my $tree = mktree_in_ud_from_only_subdir();
323 chdir '../../../..' or die $!;
324 runcmd qw(git diff --exit-code), $tree;
325 runcmd qw(git fetch), $alioth_git,
326 map { lref($_).":".rref($_) }
327 (mainbranch(), uploadbranch());
329 if (!is_fast_fwd(mainbranch
330 xxx introduce remote_lref
331 xxx use remote_lref everywhere
334 do fast forward check and maybe fake merge
345 ($package,$suite) = @ARGV;
353 die if @ARGV && $ARGV[0] =~ m/^\-/;
358 my $cmd = shift @ARGV;
361 { no strict qw(refs); &{"cmd_$cmd"}(); }