{"thread":{"id":"2824","subject":"[PATCH] repost: add git-nntp-post for posting git commit diffs as news messages","startedAt":"2005-12-13T19:55:09Z","lastAt":"2005-12-13T19:55:09Z","messageCount":1,"participants":["Artem Khodush"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"13576","messageId":"40b2b7d90512131155g588cb41bu145437e80ee31de1@mail.gmail.com","threadId":"2824","inReplyTo":null,"subject":"[PATCH] repost: add git-nntp-post for posting git commit diffs as news messages","fromName":"Artem Khodush","fromEmail":"greenkaa@gmail.com","sentAt":"2005-12-13T19:55:09Z","receivedAt":"2005-12-13T19:55:09Z","isPatch":true,"sender":{"key":"greenkaa@gmail.com","avatar":null},"body":"Sorry for posting again, the previous post has broken long lines in the patch.\n\nIn case anyone is still interested, here is a script\nthat exports git repository to a nntp server,\nas prompted in http://marc.theaimsgroup.com/?l=git&m=113385203614980&w=2\nTested with INN on localhost with a copy of git repository\n(newsgroups must be created beforehand, and inn.conf artcutoff:\nand expire.ctl /remember/: parameters better set to never).\n\nI'm not sure if I got this one right, though:\n\n> A merge commit would probably become a multipart with usually 2\n> attachments (but N attachments for a N-way octopus), showing\n> diff from each branch.\n\nCurrently for merges, it posts diff with parent[0] as first attachment,\nand diff between parent[i] and git-merge-base parent[i] parent[0]\nas each next attachment.\n\n\n\n\nSigned-off-by: Artem Khodush <greenkaa@gmail.com>\n\n---\n\n Makefile           |    3\n git-nntp-post.perl |  492 ++++++++++++++++++++++++++++++++++++++++++++++++++++\n 2 files changed, 494 insertions(+), 1 deletions(-)\n create mode 100755 git-nntp-post.perl\n\n676443e459c0689a8b1ec6c6d46423c4f1bbac8e\ndiff --git a/Makefile b/Makefile\nindex 00521fe..2cb2a07 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -97,7 +97,8 @@ SCRIPT_SH = \\\n SCRIPT_PERL = \\\n  git-archimport.perl git-cvsimport.perl git-relink.perl \\\n  git-shortlog.perl git-fmt-merge-msg.perl \\\n- git-svnimport.perl git-mv.perl git-cvsexportcommit.perl\n+ git-svnimport.perl git-mv.perl git-cvsexportcommit.perl \\\n+ git-nntp-post.perl\n\n SCRIPT_PYTHON = \\\n  git-merge-recursive.py\ndiff --git a/git-nntp-post.perl b/git-nntp-post.perl\nnew file mode 100755\nindex 0000000..5a23faf\n--- /dev/null\n+++ b/git-nntp-post.perl\n@@ -0,0 +1,492 @@\n+#!/usr/bin/perl -w\n+#\n+# Copyright (C) 2005 Artem Khodush <greenkaa@gmail.com>\n+#\n+# This program contains parts from gitweb.cgi,\n+# (C) 2005, Kay Sievers <kay.sievers@vrfy.org>\n+# (C) 2005, Christian Gierke <ch@gierke.de>\n+\n+# This program is licensed under the GPL v2, or a later version\n+\n+use warnings;\n+use strict;\n+use Net::NNTP;\n+use HTTP::Date;\n+use MIME::Lite;\n+#use MIME::Entity; # interchangeable with the above, but slower\n+\n+BEGIN {\n+       if( $^V ge v5.8.0 ) {\n+               require Encode; import Encode;\n+       }else {\n+               no strict \"refs\";\n+               *{\"Encode::encode\"}=sub { my ($a,$s,$b)=@_; return $s; };\n+       }\n+}\n+\n+# Read commits, starting from heads given on the command line,\n+# (or all repository heads), and post each commit diff\n+# to a newsgroup, starting with earliest commit.\n+# Commits from different branches go to different newsgroups,\n+# named after the head names.\n+# The order of heads on the command line is important:\n+# when branching point is encountered, the branch specified\n+# earlier is considered to be the trunk, continuing into the past.\n+\n+# For commits with more than one parent, multipart message\n+# is posted with first part containing diff with parent[0], and each\n+# other part containing diff between parent[i] and\n+# git-merge-base parent[i] parent[0] (thus showing summary change\n+# followed by what was merged from each branch)\n+\n+# Subset of the git-rev-list options (--max-age, --min-age and\n+# commits starting with ^) is accepted to limit the number of\n+# commits posted.\n+# Exactly one (the latest) commit may be posted with --one option.\n+\n+# By default, it posts to localhost nntp port (119), this may\n+# be changed by --nntp-host and --nntp-port.\n+\n+# The newsgroup names are formed by taking the GIT_DIR\n+# without the trailing .git, replacing / with ., and appending\n+# the head name. This can be overriden with --newsgroup-prefix.\n+\n+# Each posted message gets an id of the form\n+# <commit.SHA1@kernel.org>, and the nntp server is relied upon\n+# to reject duplicate posts, and to keep the posting order intact.\n+# Hence, with ordinary news servers, each commit\n+# can appear only in the newsgroups it was originally posted\n+# (currently only one), unless someone care to implement\n+# logic for rewriting posts, or a special nntp server.\n+\n+sub usage() {\n+       print STDERR <<EOT;\n+$0 [OPTIONS] [<head> ...] [^<commit> ...]\n+--nntp-host <host>\n+--nntp-port <port>\n+--newsgroup-prefix <prefix>\n+--max-age <epoch>\n+--min-age <epoch>\n+--one\n+--print-newsgroup-names\n+--print-messages\n+EOT\n+       exit(1);\n+}\n+\n+my $GIT_DIR = `git rev-parse --git-dir`;\n+chop $GIT_DIR;\n+exit 1 if $?; # rev-parse would have given \"not a git dir\" message.\n+chomp($GIT_DIR);\n+\n+my $DOMAIN_ID=\"kernel.org\"; # sufficiently unique string for Path\n+ #and Message-Id headers\n+\n+my $NNTP_HOST=\"localhost\";\n+my $NNTP_PORT=\"119\";\n+my $NEWSGROUP_PREFIX;\n+my $MAX_AGE;\n+my $MIN_AGE;\n+my $ONLY_ONE;\n+my %HEADS;\n+my @STOP_COMMITS;\n+my $PRINT_NEWSGROUP_NAMES;\n+my $PRINT_MESSAGES;\n+\n+my $HEAD_COUNT=0;\n+\n+while( my $arg=shift ) {\n+       if( $arg eq \"-h\" || $arg eq \"--nntp-host\" ) {\n+               $NNTP_HOST=shift or usage();\n+       }elsif( $arg=~/^--nntp-host=(.+)$/ ) {\n+               $NNTP_HOST=$1;\n+       }elsif( $arg eq \"-p\" || $arg eq \"--nntp-port\" ) {\n+               $NNTP_PORT=shift or usage();\n+       }elsif( $arg=~/^--nntp-port=(.+)$/ ) {\n+               $NNTP_PORT=$1;\n+       }elsif( $arg eq \"-n\" || $arg eq \"--newsgroup-prefix\" ) {\n+               $NEWSGROUP_PREFIX=shift or usage();\n+       }elsif( $arg=~/^--newsgroup-prefix=(.+)$/ ) {\n+               $NEWSGROUP_PREFIX=$1;\n+       }elsif( $arg eq \"-a\" || $arg eq \"--max-age\" ) {\n+               $MAX_AGE=shift or usage();\n+       }elsif( $arg=~/^--max-age=(.+)$/ ) {\n+               $MAX_AGE=$1;\n+       }elsif( $arg eq \"-i\" || $arg eq \"--min-age\" ) {\n+               $MIN_AGE=shift or usage();\n+       }elsif( $arg=~/^--min-age=(.+)$/ ) {\n+               $MIN_AGE=$1;\n+       }elsif( $arg eq \"-1\" || $arg eq \"--one\" ) {\n+               $ONLY_ONE=1;\n+       }elsif( $arg eq \"--print-newsgroup-names\" ) {\n+               $PRINT_NEWSGROUP_NAMES=1;\n+       }elsif( $arg eq \"--print-messages\" ) {\n+               $PRINT_MESSAGES=1;\n+       }elsif( $arg=~/^\\^/ ) {\n+               push @STOP_COMMITS, $arg;\n+       }else {\n+               if( \"HEAD\" eq $arg ) {\n+                       (undef,$arg)=split( \" \", `git-name-rev HEAD` );\n+               }\n+               $HEADS{$arg}->{order}=++$HEAD_COUNT;\n+       }\n+}\n+\n+&verify_heads;\n+&read_commits;\n+&propagate_branches;\n+&post_commits;\n+exit( 0 );\n+\n+sub verify_heads()\n+{\n+       my $repo=$GIT_DIR;\n+       my $read_all_heads= 0==$HEAD_COUNT;\n+       open my $fd, \"-|\", \"git-peek-remote $repo\"\n+        or die \"$0: error running git-peek-remote: $!\";\n+       while( my $line=<$fd> ) {\n+               my ($id,$name)=split ' ', $line;\n+               my $is_head=0;\n+               if( \"HEAD\" eq $name ) {\n+                       $is_head=1;\n+                       (undef,$name)=split( \" \", `git-name-rev HEAD` );\n+               }\n+               if( $name=~s/^refs\\/heads\\/// || $is_head ) {\n+ # if there were heads given on the command line,\n+ # take only those. Otherwise, with --one, take only\n+ # HEAD, without --one, take all heads.\n+                       if( exists( $HEADS{$name} )\n+   || ($read_all_heads && ($is_head || !$ONLY_ONE)) ) {\n+                               $HEADS{$name}->{id}=$id;\n+                       }\n+               }\n+       }\n+       close $fd or die \"$0: git_get_type: unable to close fd: $!\";\n+       if( $read_all_heads ) {\n+               #sort them\n+               my $n=0;\n+               for my $name (sort keys %HEADS) {\n+                       $HEADS{$name}->{order}=++$n;\n+               }\n+               # make master the first\n+               $HEADS{\"master\"}->{order}=0 if exists $HEADS{\"master\"};\n+       }\n+       if( $ONLY_ONE && scalar( keys %HEADS )!=1 ) {\n+               die \"$0: --one requires exactly one head, but \"\n+        . scalar( keys %HEADS ) . \" were given\\n\";\n+       }\n+       if( $PRINT_NEWSGROUP_NAMES ) { # just print them\n+               for my $head (keys %HEADS) {\n+                       print &make_newsgroup_name( $head ) . \"\\n\";\n+               }\n+               exit( 0 );\n+       }\n+}\n+\n+my %COMMITS;\n+\n+sub read_commits\n+{\n+       my $args=\"\";\n+       $args.=\" --max-age $MAX_AGE\" if $MAX_AGE;\n+       $args.=\" \" . join( \" \", keys %HEADS );\n+       $args.=\" \" . join( \" \", @STOP_COMMITS );\n+       $args.=\" --max-count=1\" if $ONLY_ONE;\n+       $/ = \"\\0\";\n+       open my $fd, \"-|\", \"git-rev-list --header --parents $args\"\n+        or die \"$0: error running git-rev-list: $!\";\n+       while( my $commit_line=<$fd> ) {\n+               $commit_line =~ s/\\r$//;\n+               my @commit_lines = split '\\n', $commit_line;\n+               pop @commit_lines;\n+               my %co;\n+\n+               my $header = shift @commit_lines;\n+               if (!($header =~ m/^[0-9a-fA-F]{40}/)) {\n+                       next;\n+               }\n+               ($co{'id'}, my @parents) = split ' ', $header;\n+               $co{'parents'} = \\@parents;\n+               while (my $line = shift @commit_lines) {\n+                       last if $line eq \"\\n\";\n+                       if ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {\n+                               $co{'author'} = $1;\n+                               $co{'author_epoch'} = $2;\n+                               $co{'author_tz'} = $3;\n+                       }elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {\n+                               $co{'committer'} = $1;\n+                               $co{'committer_epoch'} = $2;\n+                               $co{'committer_tz'} = $3;\n+                       }\n+               }\n+               $co{'title'}=\"\";\n+               $co{'title_short'}=\"\";\n+               foreach my $title (@commit_lines) {\n+                       if ($title ne \"\") {\n+                               $title=~s/^\\s+//;\n+                               $co{'title'}=chop_str($title, 80, 5);\n+                               $co{'title_short'}=make_title_short( $title );\n+                               last;\n+                       }\n+               }\n+               # remove added spaces\n+               foreach my $line (@commit_lines) {\n+                       $line =~ s/^    //;\n+               }\n+               $co{'comment'} = \\@commit_lines;\n+               $COMMITS{$co{'id'}}=\\%co;\n+       }\n+       close $fd or die \"$0: git_read_commit: unable to close fd: $!\";\n+       $/ = \"\\n\";\n+}\n+\n+sub make_title_short\n+{\n+ my $title=shift;\n+       # remove leading stuff of merges\n+       # to make the interesting part visible\n+       if (length($title) > 50) {\n+        $title =~ s/^Automatic //;\n+        $title =~ s/^merge (of|with) /Merge ... /i;\n+        if (length($title) > 50) {\n+        $title =~ s/(http|rsync):\\/\\///;\n+        }\n+        if (length($title) > 50) {\n+        $title =~ s/(master|www|rsync)\\.//;\n+        }\n+        if (length($title) > 50) {\n+        $title =~ s/kernel.org:?//;\n+        }\n+        if (length($title) > 50) {\n+        $title =~ s/\\/pub\\/scm//;\n+        }\n+       }\n+ return chop_str($title, 50, 5)\n+}\n+\n+sub propagate_branches\n+{\n+       for my $head (sort { $HEADS{$a}->{order} - $HEADS{$b}->{order} }\n+        keys %HEADS) {\n+               my $child_id=undef;\n+               my $id=$HEADS{$head}->{id};\n+               while( $id && exists $COMMITS{$id}\n+            && !exists $COMMITS{$id}->{branch} ) {\n+                       my $co=$COMMITS{$id};\n+                       $co->{branch}=$head;\n+                       $co->{child}=$child_id;\n+                       $HEADS{$head}->{start_id}=$id;\n+                       $child_id=$id;\n+                       $id=$co->{parents}->[0];\n+               }\n+       }\n+}\n+\n+sub make_message_id\n+{\n+       my $id=shift;\n+       return \"<commit.$id\\@$DOMAIN_ID>\",\n+}\n+\n+sub make_commit_body\n+{\n+       my ($id,$other_id,$from_branch)=@_;\n+       my $body=\"\";\n+       my ($author, $author_epoch, $committer, $committer_epoch);\n+       my $comment=\"\";\n+       if( exists $COMMITS{$id} ) {\n+        # if we have it already, spare git-cat-file\n+               my $co=$COMMITS{$id};\n+               $author=$co->{author};\n+        $author_epoch=$co->{author_epoch};\n+        $committer=$co->{committer};\n+        $committer_epoch=$co->{committer_epoch};\n+               $comment=join( \"\\n\", @{$co->{comment}} );\n+       }else {\n+               open my $fd0, \"-|\", \"git-cat-file commit $id\"\n+        or die \"$0: error running git-cat-file: $!\";\n+               my $header=1;\n+               while( <$fd0> ) {\n+                       if( !$header ) {\n+                               $comment.=$_;\n+                       }elsif( m/^\\s+$/ ) {\n+                               $header=0;\n+                       }elsif( m/^author (.*) ([0-9]+) (.*)$/ ) {\n+                               $author=$1;\n+                               $author_epoch=$2;\n+                       }elsif( m/^committer (.*) ([0-9]+) (.*)$/ ) {\n+                               $committer=$1;\n+                               $committer_epoch=$2;\n+                       }\n+               }\n+               close $fd0;\n+       }\n+       if( $from_branch ) {\n+               my $merge_base=`git-merge-base $id $other_id`;\n+               chop $merge_base;\n+               $body.=\"merged from $id\\n\\n---\\n\";\n+               $other_id=$merge_base if $merge_base;\n+       }else {\n+               my ($author_name,$author_email)=split( \" \", $author );\n+               my ($committer_name,$committer_email)=split( \" \", $committer );\n+               $body.=\"committer $committer \".time2str($committer_epoch).\"\\n\"\n+        unless $author_name eq $committer_name;\n+               $body.=$comment;\n+               $body.=\"\\n---\\n\\n\";\n+       }\n+       if( $other_id ) {\n+               open my $fd1, \"-|\",\n+ \"git-diff-tree -p $other_id $id | git-apply --stat --summary\"\n+ or die \"$0: error running git-diff-tree: $!\";\n+               while( <$fd1> ) {\n+                       $body.=$_;\n+               }\n+               close $fd1;\n+               $body.=\"\\n\";\n+               open my $fd2, \"-|\", \"git-diff-tree -p $other_id $id\"\n+        or die \"$0: error running git-diff-tree again: $!\";\n+               while( <$fd2> ) {\n+                       $body.=$_;\n+               }\n+               close $fd2;\n+       }\n+       return $body;\n+}\n+\n+sub make_commit_message\n+{\n+       my ($newsgroup,$co)=@_;\n+       my $n_parents=scalar( @{$co->{parents}} );\n+       my $parents=join(\" \", map( make_message_id( $_ ),  @{$co->{parents}}));\n+       my $from=$co->{author};\n+       $from=$co->{committer} unless $from;\n+       $from=\"unknown author\" unless $from;\n+       my $subject=$co->{title_short};\n+       $subject=\"undescribed patch\" unless $subject;\n+       my $msg=MIME::Lite->new(\n+               From=>join( \" \", map( Encode::encode( \"MIME-Q\", $_ ),\n+        split( \" \", $from ) ) ),\n+               Subject=>join( \" \", map( Encode::encode( \"MIME-Q\", $_ ),\n+        split( \" \", $subject ) ) ),\n+               Date=>time2str( $co->{author_epoch} ),\n+               \"Message-Id\"=>make_message_id( $co->{id} ),\n+               ($n_parents>0 ? (References=>$parents) : ()),\n+\n+               ($n_parents>1 ? (Type=>\"multipart/mixed\")\n+                       :(\n+                       Type=>\"text/plain; charset=utf8\",\n+                       Encoding=>\"quoted-printable\",\n+                       Data=>make_commit_body( $co->{id},\n+ $co->{parents}->[0],\n+ 0)\n+                       )\n+               )\n+       );\n+       $msg->add( Path=>$DOMAIN_ID );\n+       $msg->add( Newsgroups=>$newsgroup );\n+       if( $n_parents>1 ) {\n+               $msg->attach(\n+                       Type=>\"text/plain; charset=utf8\",\n+                       Encoding=>\"quoted-printable\",\n+                       Data=>make_commit_body( $co->{id},\n+        $co->{parents}->[0],\n+        0 )\n+               );\n+               for my $n (1..$n_parents-1) {\n+                       $msg->attach(\n+                               Type=>\"text/plain; charset=utf8\",\n+                               Encoding=>\"quoted-printable\",\n+ Data=>make_commit_body( $co->{parents}->[$n],\n+        $co->{parents}->[0],\n+        1 )\n+                       );\n+               }\n+       }\n+       return $msg;\n+}\n+\n+sub post_commit\n+{\n+       my ($nntp,$newsgroup,$co)=@_;\n+       if( $PRINT_MESSAGES ) { # just print them\n+               print make_commit_message( $newsgroup, $co )->as_string();\n+               print \"\\0\";\n+       }else {\n+               if( !$nntp->ihave( make_message_id( $co->{id} ) ) ) {\n+                       my $reason=$nntp->message();\n+                       unless( $reason=~m/got it/i\n+     || $reason=~m/duplicate/i ) {\n+                               print STDERR \"unexpected responce in attempt \"\n+        . \"to post $co->{id}: $reason\"\n+                       }\n+               }else {\n+        my$msg=make_commit_message(  $newsgroup, $co );\n+                       $nntp->datasend( $msg->as_string() );\n+                       if( !$nntp->dataend() ) {\n+                               print STDERR \"error posting $co->{id}: \"\n+        . $nntp->message() . \"\\n\";\n+                       }\n+               }\n+       }\n+}\n+\n+sub make_newsgroup_name\n+{\n+       my $nhead=shift;\n+       $nhead=~s/\\/+$//;\n+       $nhead=~s/^\\/+//;\n+       $nhead=~s/\\./-/g;\n+       $nhead=~s/\\//\\./g;\n+       my $nprefix=$NEWSGROUP_PREFIX;\n+       if( !defined( $nprefix ) ) {\n+               $nprefix=$GIT_DIR;\n+               $nprefix=~s/\\/\\.git\\/?\\s*$//;\n+               $nprefix=~s/\\/+$//;\n+               $nprefix=~s/^\\/+//;\n+               $nprefix=~s/\\./-/g;\n+               $nprefix=~s/\\//\\./g;\n+       }\n+       return \"$nprefix.$nhead\";\n+}\n+\n+sub post_commits\n+{\n+       my $nntp;\n+       unless( $PRINT_MESSAGES ) {\n+               $nntp=Net::NNTP->new( $NNTP_HOST,\n+        Port=>$NNTP_PORT,\n+ Reader=>0 );\n+               die \"unable to connect to nntp host \"\n+        .\"${NNTP_HOST}:${NNTP_PORT}\"\n+        unless $nntp;\n+       }\n+       while( my ($head,$h)=each %HEADS ) {\n+               my $id=$h->{start_id};\n+               my $newsgroup_name=make_newsgroup_name( $head );\n+               while( $id ) {\n+                       my $co=$COMMITS{$id};\n+                       $id=$co->{child};\n+                       next if defined( $MIN_AGE )\n+        && $co->{committer_epoch} > $MIN_AGE;\n+                       post_commit( $nntp, $newsgroup_name, $co );\n+               }\n+       }\n+}\n+\n+sub chop_str {\n+       my $str = shift;\n+       my $len = shift;\n+       my $add_len = shift || 10;\n+\n+       # allow only $len chars, but don't cut a word if it would fit\n+       # in $add_len. If it doesn't fit, cut it if it's still longer\n+       # than the dots we would add\n+       $str =~ m/^(.{0,$len}[^ \\/\\-_:\\.@]{0,$add_len})(.*)/;\n+       my $body = $1;\n+       my $tail = $2;\n+       if (length($tail) > 4) {\n+               $tail = \" ...\";\n+       }\n+       return \"$body$tail\";\n+}\n+\n--\n0.99.9.GIT\n"}]}