2 # Copyright 2005, Ryan Anderson <ryan@michonline.com>
3 # Distribution permitted under the GPL v2, as distributed
4 # by the Free Software Foundation.
5 # Later versions of the GPL at the discretion of Linus Torvalds
7 # Scan two git object-trees, and hardlink any common objects between them.
14 sub get_canonical_form
($);
15 sub do_scan_directory
($$$);
16 sub compare_two_files
($$);
18 sub link_two_files
($$);
22 my $total_already = 0;
23 my ($linked,$already);
25 my $fail_on_different_sizes = 0;
27 GetOptions
("safe" => \
$fail_on_different_sizes,
34 usage
() if (!defined $dirs[0] || !defined $dirs[1]);
36 $_ = get_canonical_form
($_) foreach (@dirs);
38 my $master_dir = pop @dirs;
40 opendir(D
,$master_dir . "objects/")
41 or die "Failed to open $master_dir/objects/ : $!";
43 my @hashdirs = grep { ($_ eq 'pack') || /^[0-9a-f]{2}$/ } readdir(D
);
45 foreach my $repo (@dirs) {
48 printf("Searching '%s' and '%s' for common objects and hardlinking them...\n",
51 foreach my $hashdir (@hashdirs) {
52 do_scan_directory
($master_dir, $hashdir, $repo);
55 printf("Linked %d files, %d were already linked.\n",$linked, $already);
57 $total_linked += $linked;
58 $total_already += $already;
61 printf("Totals: Linked %d files, %d were already linked.\n",
62 $total_linked, $total_already);
65 sub do_scan_directory
($$$) {
66 my ($srcdir, $subdir, $dstdir) = @_;
68 my $sfulldir = sprintf("%sobjects/%s/",$srcdir,$subdir);
69 my $dfulldir = sprintf("%sobjects/%s/",$dstdir,$subdir);
72 or die "Failed to opendir $sfulldir: $!";
74 foreach my $file (grep(!/\.{1,2}$/, readdir(S
))) {
75 my $sfilename = $sfulldir . $file;
76 my $dfilename = $dfulldir . $file;
78 compare_two_files
($sfilename,$dfilename);
84 sub compare_two_files
($$) {
85 my ($sfilename, $dfilename) = @_;
87 # Perl's stat returns relevant information as follows:
91 my @sstatinfo = stat($sfilename);
92 my @dstatinfo = stat($dfilename);
94 if (@sstatinfo == 0 && @dstatinfo == 0) {
95 die sprintf("Stat of both %s and %s failed: %s\n",$sfilename, $dfilename, $!);
97 } elsif (@dstatinfo == 0) {
101 if ( ($sstatinfo[0] == $dstatinfo[0]) &&
102 ($sstatinfo[1] != $dstatinfo[1])) {
103 if ($sstatinfo[7] == $dstatinfo[7]) {
104 link_two_files
($sfilename, $dfilename);
107 my $err = sprintf("ERROR: File sizes are not the same, cannot relink %s to %s.\n",
108 $sfilename, $dfilename);
109 if ($fail_on_different_sizes) {
116 } elsif ( ($sstatinfo[0] == $dstatinfo[0]) &&
117 ($sstatinfo[1] == $dstatinfo[1])) {
122 sub get_canonical_form
($) {
126 die "$dir is not a directory." unless -d
$dir;
128 $dir .= "/" unless $dir =~ m
#/$#;
129 $dir .= ".git/" unless $dir =~ m
#\.git/$#;
131 die "$original does not have a .git/ subdirectory.\n" unless -d
$dir;
136 sub link_two_files
($$) {
137 my ($sfilename, $dfilename) = @_;
138 my $tmpdname = sprintf("%s.old",$dfilename);
139 rename($dfilename,$tmpdname)
140 or die sprintf("Failure renaming %s to %s: %s",
141 $dfilename, $tmpdname, $!);
143 if (! link($sfilename,$dfilename)) {
145 unless (rename($tmpdname,$dfilename)) {
147 "Git Repository containing %s is probably corrupted, " .
148 "please copy '%s' to '%s' to fix.\n",
149 $tmpdname, $dfilename);
152 die sprintf("Failed to link %s to %s: %s\n%s" .
153 $sfilename, $dfilename,
154 $!, $dfilename, $failtxt);
158 or die sprintf("Unlink of %s failed: %s\n",
166 print("usage: git relink [--safe] <dir>... <master_dir> \n");
167 print("All directories should contain a .git/objects/ subdirectory.\n");
170 "Stops if two objects with the same hash exist but " .
171 "have different sizes. Default is to warn and continue.\n");