7 use Dpkg::Control::Hash;
11 open DEBUG, ">&STDERR" or die $!;
13 our $mirror = 'http://mirror.relativity.greenend.org.uk/mirror/debian-ftp/';
17 our $aliothname = 'iwj@git.debian.org';
18 our $aliothpath = '/git/dgit-test';
19 our $alioth_git = "git+ssh://$aliothname/$aliothpath";
20 our $alioth_sshtestbodge = [$aliothname,$aliothpath];
22 our (@dget_opts) = qw(-u);
24 our $remotename = 'dgit';
26 sub mainbranch () { return "$suite"; }
27 sub uploadbranch () { return "upload/$suite"; }
33 $ua = LWP::UserAgent->new();
36 print DEBUG "fetching @_...\n";
37 my $r = $ua->get(@_) or die $!;
38 die "$_[0]: ".$r->status_line."; failed.\n" unless $r->is_success;
39 return $r->decoded_content();
42 our ($dscdata,$dscurl,$dsc);
46 die "@_ $! $?" if system @_;
50 open P, "-|", @_ or die $!;
53 { local $/ = undef; $d = <P>; }
55 close P or die "@_ $? $!";
61 my $c = Dpkg::Control::Hash->new();
62 $c->load(@_) or return undef;
67 my $c = Dpkg::Control::Hash->new();
68 my $p = new IO::File '-|', qw(dpkg-parsechangelog) or die $!;
70 $?=0; $!=0; close $p or die "$! $?";
74 sub get_archive_dsc () {
75 my $rmad = cmdoutput qw(rmadison -asource),"-s$suite",$package;
76 $rmad =~ m/^ \s*( [^ \t|]+ )\s* \|
79 \s*( [^ \t|]+ )\s* /x or die "$rmad $?";
80 $1 eq $package or die "$rmad $package ?";
82 $3 eq $suite or die "$rmad $suite ?";
83 $4 eq 'source' or die "$rmad ?";
84 # fixme it does not show us the component ?
85 my $prefix = substr($package, 0, $package =~ m/^l/ ? 4 : 1);
86 $dscurl = "$mirror/pool/main/$prefix/$package/${package}_$vsn.dsc";
87 #print DEBUG Dumper($pdodata, $&, $dscurl);
88 $dscdata = url_get($dscurl);
89 my $dscfh = new IO::File \$dscdata, '<' or die $!;
90 #print DEBUG Dumper($dscdata, $dscfh);
91 $dsc = Dpkg::Control::Hash->new(allow_pgp=>1);
92 $dsc->parse($dscfh, 'dsc') or die "parsing of $dscurl failed\n";
93 #print DEBUG Dumper($dsc);
94 my $fmt = $dsc->{Format};
95 die "unsupported format $fmt, sorry\n" unless $fmt eq '1.0';
98 sub check_for_git () {
101 "ssh $alioth_sshtestbodge->[0] '".
102 " set -e; cd /git/dgit-test;".
103 " if test -d $package.git; then echo 1; else echo 0; fi".
105 #print DEBUG "$cmd\n";
106 open P, "$cmd |" or die $!;
108 my $r = <P>; close P;
109 #print STDERR ">$r<\n";
110 die "$r $! $?" unless $r =~ m/^[01]$/;
114 our ($dsc_hash,$lastupl_hash);
116 our $ud = '.git/dgit/unpack';
124 sub mktree_in_ud_from_only_subdir () {
125 # changes into the subdir
128 $dirs[0] =~ m#^([^/]+)/\.$# or die;
130 chdir $dir or die "$dir $!";
132 die $! unless $!==&ENOENT;
133 runcmd qw(git init -q);
134 rmtree('.git/objects');
135 symlink '../../../../objects','.git/objects' or die $!;
136 runcmd qw(git add -Af);
137 my $tree = cmdoutput qw(git write-tree);
138 chomp $tree; $tree =~ m/^\w+$/ or die "$tree ?";
142 sub generate_commit_from_dsc () {
146 foreach (split /\n/, ($dsc->{'Checksums-Sha256'} || $dsc->{Files})) {
148 m/^\w+ \d+ (\S+)$/ or die "$_ ?";
150 die if $f =~ m#/|^\.|\.dsc$|\.tmp$#;
152 link "../../../$f", $f
156 runcmd qw(dget), @dget_opts, qw(--), $dscurl;
157 foreach my $f (grep { m/\.tar\.gz$/ } @files) {
158 link $f, "../../../$f"
162 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
163 runcmd qw(sh -ec), 'dpkg-parsechangelog >../changelog.tmp';
164 my $clogp = parsecontrol('../changelog.tmp','changelog') or die;
165 my $date = cmdoutput qw(date), '+%s %z', qw(-d),$clogp->{Date};
166 my $author = $clogp->{Maintainer};
167 $author =~ s#,.*##ms;
168 my $authline = "$author $date";
169 $authline =~ m/^[^<>]+ \<\S+\> \d+ [-+]\d+$/ or die $authline;
170 open C, ">../commit.tmp" or die $!;
171 print C "tree $tree\n" or die $!;
172 print C "parent $lastupl_hash\n" or die $! if defined $lastupl_hash;
173 print C <<END or die $!;
179 # imported by dgit from the archive
182 print "synthesised git commit from .dsc $clogp->{Version}\n";
183 my $commithash = cmdoutput qw(git hash-object -w -t commit ../commit.tmp);
184 chdir '../../../..' or die $!;
185 cmdoutput qw(git update-ref -m),"dgit synthesise $clogp->{Version}",
186 'DGIT_ARCHIVE', $commithash;
187 cmdoutput qw(git log -n2), $commithash;
188 # ... gives git a chance to complain if our commit is malformed
189 my $outputhash = $commithash;
190 if (defined $lastupl_hash) {
191 chdir "$ud/$dir" or die $!;
192 runcmd qw(git reset --hard), $lastupl_hash;
193 runcmd qw(sh -ec), 'dpkg-parsechangelog >>../changelogold.tmp';
194 my $oldclogp = Dpkg::Control::Hash->new();
195 $oldclogp->parse('../changelogold.tmp','previous changelog') or die;
197 version_compare_string($oldclogp->{Version}, $clogp->{Version});
199 # git upload/ is earlier vsn than archive, use archive
200 } elsif ($vcmp >= 0) {
201 print STDERR <<END or die $!;
202 Version actually in archive: $clogp->{Version} (older)
203 Last allegedly pushed/uploaded: $oldclogp->{Version} (newer or same)
204 Perhaps the upload is stuck in incoming. Using the version from git.
207 die "version in archive is same as version in git".
208 " to-be-uploaded (upload/) branch but archive".
209 " version hash no commit hash?!\n";
211 chdir '../../../..' or die $!;
217 my $lastupl_ref = "refs/remotes/$remotename/upload/$suite";
220 return cmdoutput qw(git rev-parse --), "$_[0]~0";
223 sub is_fast_fwd ($$) {
224 my ($ancestor,$child) = @_;
225 my $mb = cmdoutput qw(git merge-base), $dsc_hash, $lastupl_hash;
226 return rev_parse($mb) eq rev_parse($ancestor);
229 sub fetch_from_archive () {
230 # ensures that $lastupl_ref is what is actually in the archive,
232 $!=0; $lastupl_hash = `git show-ref --heads $lastupl_ref`;
234 die $? unless ($?==0 && chomp $lastupl_hash)
235 or ($?==128 && !length $lastupl_hash);
237 if (defined $dsc_hash) {
240 $hash = generate_commit_from_dsc();
243 die "not fast forward on last upload branch!".
244 " (archive's version left in DGIT_ARCHIVE)"
245 unless is_fast_fwd($lastupl_hash, $dsc_hash);
247 if ($lastupl_ref ne $hash) {
248 cmdoutput qw(git update-ref -m), 'dgit fetch', $lastupl_ref, $hash;
254 $dsc_hash = $dsc->{'Vcs-git-master'};
255 if (defined $dsc_hash) {
256 $dsc_hash =~ m/\w+/ or die "$dsc_hash $?";
258 print "last upload to archive specified git hash\n";
260 print "last upload to archive has NO git hash\n";
262 my $dstdir = "$package";
263 if (check_for_git()) {
264 print "cloning existing git history\n";
265 runcmd qw(git clone --origin),$remotename, qw(-b), $suite, '--',
266 $alioth_git, $dstdir;
267 chdir "$dstdir" or die "$dstdir $!";
268 fetch_from_archive();
269 runcmd qw(git reset --hard), $lastupl_ref;
271 die "missing git history even though dsc has hash" if defined $dsc_hash;
272 print "starting new git history\n";
273 mkdir $dstdir or die "$dstdir $!";
274 chdir "$dstdir" or die "$dstdir $!";
275 runcmd qw(git init -q);
276 open H, "> .git/HEAD" or die $!;
277 print H "ref: refs/heads/$suite\n" or die $!;
279 runcmd qw(git remote add), $remotename, $alioth_git;
280 runcmd "git config branch.$suite.remote $remotename";
281 runcmd "git config branch.$suite.merge refs/heads/$suite";
282 my $newhash = generate_commit_from_dsc();
283 runcmd qw(git reset --hard), $newhash;
285 print "ready for work in $dstdir\n";
293 runcmd qw(git diff --quiet HEAD);
294 my $clogp = parsechangelog();
295 $package = $clogp->{Source};
296 my $dscfn = "${package}_$clogp->{Version}.dsc";
297 stat $dscfn or die "$dscfn $!";
298 $dsc = parsecontrol("../$dscfn");
301 print "checking that $dscfn corresponds to HEAD\n";
302 runcmd qw(dpkg-source -x --), "../../../../$dscfn";
303 my $tree = mktree_in_ud_from_only_subdir();
304 chdir '../../../..' or die $!;
305 runcmd qw(git diff --exit-code), $tree;
306 runcmd qw(git fetch), $alioth_git,
307 map { "$_:refs/remotes/$remotename/$_" }
308 (mainbranch(), uploadbranch());
312 do fast forward check and maybe fake merge
323 ($package,$suite) = @ARGV;
331 die if @ARGV && $ARGV[0] =~ m/^\-/;
336 my $cmd = shift @ARGV;
339 { no strict qw(refs); &{"cmd_$cmd"}(); }