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;
66 sub get_archive_dsc () {
67 my $rmad = cmdoutput qw(rmadison -asource),"-s$suite",$package;
68 $rmad =~ m/^ \s*( [^ \t|]+ )\s* \|
71 \s*( [^ \t|]+ )\s* /x or die "$rmad $?";
72 $1 eq $package or die "$rmad $package ?";
74 $3 eq $suite or die "$rmad $suite ?";
75 $4 eq 'source' or die "$rmad ?";
76 # fixme it does not show us the component ?
77 my $prefix = substr($package, 0, $package =~ m/^l/ ? 4 : 1);
78 $dscurl = "$mirror/pool/main/$prefix/$package/${package}_$vsn.dsc";
79 #print DEBUG Dumper($pdodata, $&, $dscurl);
80 $dscdata = url_get($dscurl);
81 my $dscfh = new IO::File \$dscdata, '<' or die $!;
82 #print DEBUG Dumper($dscdata, $dscfh);
83 $dsc = Dpkg::Control::Hash->new(allow_pgp=>1);
84 $dsc->parse($dscfh, 'dsc') or die "parsing of $dscurl failed\n";
85 #print DEBUG Dumper($dsc);
86 my $fmt = $dsc->{Format};
87 die "unsupported format $fmt, sorry\n" unless $fmt eq '1.0';
90 sub check_for_git () {
93 "ssh $alioth_sshtestbodge->[0] '".
94 " set -e; cd /git/dgit-test;".
95 " if test -d $package.git; then echo 1; else echo 0; fi".
97 #print DEBUG "$cmd\n";
98 open P, "$cmd |" or die $!;
100 my $r = <P>; close P;
101 #print STDERR ">$r<\n";
102 die "$r $! $?" unless $r =~ m/^[01]$/;
106 our ($dsc_hash,$lastupl_hash);
108 sub generate_commit_from_dsc () {
109 my $ud = '.git/dgit/unpack';
115 foreach (split /\n/, ($dsc->{'Checksums-Sha256'} || $dsc->{Files})) {
117 m/^\w+ \d+ (\S+)$/ or die "$_ ?";
119 die if $f =~ m#/|^\.|\.dsc$|\.tmp$#;
121 link "../../../$f", $f
125 runcmd qw(dget), @dget_opts, qw(--), $dscurl;
126 foreach my $f (grep { m/\.tar\.gz$/ } @files) {
127 link $f, "../../../$f"
133 $dirs[0] =~ m#^([^/]+)/\.$# or die;
135 chdir $dir or die "$dir $!";
137 die $! unless $!==&ENOENT;
138 runcmd qw(git init -q);
139 rmtree('.git/objects');
140 symlink '../../../../objects','.git/objects' or die $!;
141 runcmd qw(git add -Af);
142 my $tree = cmdoutput qw(git write-tree);
143 chomp $tree; $tree =~ m/^\w+$/ or die "$tree ?";
144 runcmd qw(sh -ec), 'dpkg-parsechangelog >../changelog.tmp';
145 my $clogp = parsecontrol('../changelog.tmp','changelog') or die;
146 my $date = cmdoutput qw(date), '+%s %z', qw(-d),$clogp->{Date};
147 my $author = $clogp->{Maintainer};
148 $author =~ s#,.*##ms;
149 my $authline = "$author $date";
150 $authline =~ m/^[^<>]+ \<\S+\> \d+ [-+]\d+$/ or die $authline;
151 open C, ">../commit.tmp" or die $!;
152 print C "tree $tree\n" or die $!;
153 print C "parent $lastupl_hash\n" or die $! if defined $lastupl_hash;
154 print C <<END or die $!;
160 # imported by dgit from the archive
163 print "synthesised git commit from .dsc $clogp->{Version}\n";
164 my $commithash = cmdoutput qw(git hash-object -w -t commit ../commit.tmp);
165 chdir '../../../..' or die $!;
166 cmdoutput qw(git update-ref -m),"dgit synthesise $clogp->{Version}",
167 'DGIT_ARCHIVE', $commithash;
168 cmdoutput qw(git log -n2), $commithash;
169 # ... gives git a chance to complain if our commit is malformed
170 my $outputhash = $commithash;
171 if (defined $lastupl_hash) {
172 chdir "$ud/$dir" or die $!;
173 runcmd qw(git reset --hard), $lastupl_hash;
174 runcmd qw(sh -ec), 'dpkg-parsechangelog >>../changelogold.tmp';
175 my $oldclogp = Dpkg::Control::Hash->new();
176 $oldclogp->parse('../changelogold.tmp','previous changelog') or die;
178 version_compare_string($oldclogp->{Version}, $clogp->{Version});
180 # git upload/ is earlier vsn than archive, use archive
181 } elsif ($vcmp >= 0) {
182 print STDERR <<END or die $!;
183 Version actually in archive: $clogp->{Version} (older)
184 Last allegedly pushed/uploaded: $oldclogp->{Version} (newer or same)
185 Perhaps the upload is stuck in incoming. Using the version from git.
188 die "version in archive is same as version in git".
189 " to-be-uploaded (upload/) branch but archive".
190 " version hash no commit hash?!\n";
192 chdir '../../../..' or die $!;
198 my $lastupl_ref = "refs/remotes/$remotename/upload/$suite";
200 sub fetch_from_archive () {
201 # ensures that $lastupl_ref is what is actually in the archive,
203 $!=0; $lastupl_hash = `git show-ref --heads $lastupl_ref`;
205 die $? unless ($?==0 && chomp $lastupl_hash)
206 or ($?==128 && !length $lastupl_hash);
208 if (defined $dsc_hash) {
211 $hash = generate_commit_from_dsc();
214 my $mb = cmdoutput qw(git merge-base), $dsc_hash, $lastupl_hash;
215 die "not fast forward on last upload branch!".
216 " (archive's version left in DGIT_ARCHIVE)"
217 unless $mb eq $lastupl_hash;
219 if ($lastupl_ref ne $hash) {
220 cmdoutput qw(git update-ref -m), 'dgit fetch', $lastupl_ref, $hash;
226 $dsc_hash = $dsc->{'Vcs-git-master'};
227 if (defined $dsc_hash) {
228 $dsc_hash =~ m/\w+/ or die "$dsc_hash $?";
230 print "last upload to archive specified git hash\n";
232 print "last upload to archive has NO git hash\n";
234 my $dstdir = "$package";
235 if (check_for_git()) {
236 print "cloning existing git history\n";
237 runcmd qw(git clone --origin),$remotename, qw(-b), $suite, '--',
238 $alioth_git, $dstdir;
239 chdir "$dstdir" or die "$dstdir $!";
240 fetch_from_archive();
241 runcmd qw(git reset --hard), $lastupl_ref;
243 die "missing git history even though dsc has hash" if defined $dsc_hash;
244 print "starting new git history\n";
245 mkdir $dstdir or die "$dstdir $!";
246 chdir "$dstdir" or die "$dstdir $!";
247 runcmd qw(git init -q);
248 open H, "> .git/HEAD" or die $!;
249 print H "ref: refs/heads/$suite\n" or die $!;
251 runcmd qw(git remote add), $remotename, $alioth_git;
252 runcmd "git config branch.$suite.remote $remotename";
253 runcmd "git config branch.$suite.merge refs/heads/$suite";
254 my $newhash = generate_commit_from_dsc();
255 runcmd qw(git reset --hard), $newhash;
257 print "ready for work in $dstdir\n";
268 ($package,$suite) = @ARGV;
276 die if @ARGV && $ARGV[0] =~ m/^\-/;
281 my $cmd = shift @ARGV;
284 { no strict qw(refs); &{"cmd_$cmd"}(); }