搜索
查看: 171|回复: 1

rip-git.pl

[复制链接]

1839

主题

2255

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
11913
发表于 2015-4-22 13:46:33 | 显示全部楼层 |阅读模式
  1. #!/usr/bin/perl

  2. use strict;

  3. use LWP;
  4. use LWP::UserAgent;
  5. use HTTP::Request;
  6. use Getopt::Long;

  7. my $configfile="$ENV{HOME}/.rip-git";
  8. my %config;
  9. $config{'branch'} = "master";
  10. $config{'gitdir'} = ".git";
  11. $config{'agent'} = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:10.0.2) Gecko/20100101 Firefox/10.0.2';
  12. $config{'verbose'}=0;
  13. $config{'checkout'}=1;

  14. if (-e $configfile) {
  15.         open(CONFIG,"<$configfile") or next;
  16.         while (<CONFIG>) {
  17.             chomp;                  # no newline
  18.             s/#.*//;                # no comments
  19.             s/^\s+//;               # no leading white
  20.             s/\s+$//;               # no trailing white
  21.             next unless length;     # anything left?
  22.             my ($var, $value) = split(/\s*=\s*/, $_, 2);
  23.             $config{$var} = $value;
  24.         }
  25.         close(CONFIG);
  26. }

  27. Getopt::Long::Configure ("bundling");

  28. my $result = GetOptions (
  29.         "a|agent=s" => \$config{'agent'},
  30.         "b|branch=s" => \$config{'branch'},
  31.         "u|url=s" => \$config{'url'},
  32.         "c|checkout!" => \$config{'checkout'},
  33.         "s|verifyssl!" => \$config{'verifyssl'},
  34.         "v|verbose+"  => \$config{'verbose'},
  35.         "h|help" => \&help
  36. );

  37. my @gitfiles=(
  38. "COMMIT_EDITMSG",
  39. "config",
  40. "description",
  41. "HEAD",
  42. "index",
  43. "packed-refs"
  44. );

  45. my @commits;
  46. my $ua = LWP::UserAgent->new;
  47. $ua->agent($config{'agent'});

  48. my $gd=$config{'gitdir'}."/";

  49. mkdir $gd;

  50. print STDERR "[i] Downloading git files from $config{'url'}\n" if ($config{'verbose'}>0);

  51. foreach my $file (@gitfiles) {
  52.         my $furl = $config{'url'}."/".$file;
  53.         getfile($file,$gd.$file);
  54. }

  55. mkdir $gd."logs";
  56. mkdir $gd."logs/refs";
  57. mkdir $gd."logs/refs/heads";
  58. mkdir $gd."logs/refs/remotes";

  59. mkdir $gd."objects";
  60. mkdir $gd."objects/info";
  61. mkdir $gd."objects/pack";

  62. getfile("objects/info/alternates",$gd."objects/info/alternates");

  63. mkdir $gd."info";
  64. getfile("info/grafts",$gd."info/grafts");

  65. my $res = getfile("logs/HEAD",$gd."logs/HEAD");

  66. my @lines = split /\n/, $res->content;
  67. foreach my $line (@lines) {
  68.         my @fields=split(/\s+/, $line);
  69.         my $ref = $fields[1];
  70.         getobject($gd,$ref);
  71. }

  72. mkdir $gd."refs";
  73. mkdir $gd."refs/heads";
  74. my $res = getfile("refs/heads/".$config{'branch'},$gd."refs/heads/".$config{'branch'});
  75. mkdir $gd."refs/remotes";
  76. mkdir $gd."refs/tags";

  77. my $pcount=1;
  78. while ($pcount>0) {
  79.         print STDERR "[i] Running git fsck to check for missing items\n" if ($config{'verbose'}>0);
  80.         open(PIPE,"git fsck |") or die "cannot find git: $!";
  81.         $pcount=0;
  82.         while (<PIPE>) {
  83.                 chomp;
  84.                 if (/^missing/) {
  85.                         my @getref = split (/\s+/);
  86.                         getobject($gd,$getref[2]); # 3rd field is sha1
  87.                         $pcount++;
  88.                 }
  89.         }
  90.         close(PIPE);
  91.         print STDERR "[i] Got items with git fsck: $pcount\n" if ($config{'verbose'}>0);
  92. }

  93. if ($config{'checkout'}) {
  94.         system("git checkout -f");
  95. }


  96. sub getobject {
  97.         my ($gd,$ref) = @_;
  98.         my $rdir = substr ($ref,0,2);
  99.         my $rfile = substr ($ref,2);
  100.         mkdir $gd."objects/$rdir";
  101.         getfile("objects/$rdir/$rfile",$gd."objects/$rdir/$rfile");
  102. }

  103. sub getfile {
  104.         my ($file,$outfile) = @_;
  105.         my $furl = $config{'url'}."/".$file;
  106.         my $req = HTTP::Request->new(GET => $furl);
  107.         # Pass request to the user agent and get a response back
  108.         my $res = $ua->request($req);
  109.         if ($res->is_success) {
  110.                 print STDERR "[d] found $file\n" if ($config{'verbose'}>0);;
  111.                 open (out,">$outfile") or die ("cannot open file: $!");
  112.                 print out $res->content;
  113.                 close (out);
  114.         } else {
  115.                 print STDERR "[!] Not found for $file: ".$res->status_line."\n"
  116.                 if ($config{'verbose'}>0);
  117.         }
  118.         return $res;
  119. }

  120. sub help {
  121.         print "DVCS-Ripper: rip-git.pl. Copyright (C) Kost. Distributed under GPL.\n\n";
  122.         print "Usage: $0 [options] -u [giturl] \n";
  123.         print "\n";
  124.         print " -c        perform 'git checkout -f' on end (default)\n";
  125.         print " -b <s>        Use branch <s> (default: $config{'branch'})\n";
  126.         print " -a <s>        Use agent <s> (default: $config{'agent'})\n";
  127.         print " -s        verify SSL cert\n";
  128.         print " -v        verbose (-vv will be more verbose)\n";
  129.         print "\n";

  130.         print "Example: $0 -v -u http://www.example.com/.git/\n";
  131.         print "Example: $0 # with url and options in $configfile\n";
  132.        
  133.         exit 0;
  134. }
复制代码
过段时间可能会取消签到功能了

1839

主题

2255

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
11913
 楼主| 发表于 2015-4-22 13:47:23 | 显示全部楼层
过段时间可能会取消签到功能了
您需要登录后才可以回帖 登录 | Join BUC

本版积分规则

Powered by Discuz!

© 2012-2015 Baiker Union of China.

快速回复 返回顶部 返回列表