1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
#!/usr/bin/perl -w
####################################################################################################
#
# git-qt-grafts
#
# Sets up the proper grafts for a Qt repository
#
# Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
# Contact: Nokia Corporation (qt-info@nokia.com)
#
####################################################################################################
use File::Basename;
use Cwd 'abs_path';
use strict;
my $history_location;
# Syntax: fileContents(filename)
# Returns: String with contents of the file, or empty string if file
# doens't exist.
sub fileContents {
my ($filename) = @_;
my $filecontents = "";
if (-e $filename) {
open(I, "< $filename") || die "Could not open $filename for reading, read block?";
local $/;
binmode I;
$filecontents = <I>;
close I;
}
return $filecontents;
}
sub fileContains {
my ($filename, $text) = @_;
# purposely not using perl grep here
foreach my $line (split /\n/, fileContents("$filename")) {
return 1 if ("$line" eq "$text");
}
return 0;
}
sub showUsage {
my $prg = basename($0);
print "Usage: $prg <path to Qt history>:\n";
}
while ( @ARGV ) {
my $arg = shift @ARGV;
if ($arg eq "-?" || $arg eq "-h" || $arg eq "-help" || $arg eq "?") {
showUsage();
exit 0;
} elsif (!$history_location) {
$history_location = $arg;
} else {
print "Unknown option: $arg\n\n";
showUsage();
exit 1;
}
}
# Get current git-dir
my $git_dir = `git rev-parse --git-dir`;
chomp $git_dir;
if (!$git_dir) {
print "Cannot find any Git dir!\n";
exit 1;
}
# validate path for history repo
if (!$history_location || !-e $history_location) {
print "You need to provide a path to the monolithic Qt repo!\n";
exit 1;
}
my $history_alternates_objects = `cd $history_location && git rev-parse --git-dir`;
chomp $history_alternates_objects ;
if (!$history_alternates_objects || !-e "$history_alternates_objects/objects") {
print "Monolithic Qt repo path is not a valid Git repo!\n";
exit 1;
}
$history_alternates_objects = abs_path("$history_location/$history_alternates_objects/objects");;
# check if we already point to this alternate object store
my $git_alternates_file = "$git_dir/objects/info/alternates";
my $found_alternate = fileContains($git_alternates_file, $history_alternates_objects);
# get first commit SHA1 of this repo which mentions branching from a commit
my $GIT_REVLIST;
my $git_pid = open(GIT_REVLIST, "git rev-list --reverse --grep='Branched from .*at commit' HEAD |");
my $first_commit = <GIT_REVLIST>;
chomp $first_commit;
close(GIT_REVLIST);
# find the graft point which the first commit mentions
my $first_commit_msg = `git show -s --format=%B $first_commit`;
my $graft_to_commit = $first_commit_msg;
$graft_to_commit =~ s/^.+Branched from .+at commit\n([a-f0-9]+)\n.*/$1/s;
if (!$graft_to_commit || $graft_to_commit eq $first_commit_msg) {
$first_commit_msg =~ s/\n([^\n])/\n $1/g; #indent msg
print "This repo does not refer to a graft point, so no grafts added!\n";
print "\nFirst commit message of this repo:".
"\n----------------------------------\n ".
$first_commit_msg;
exit 0;
}
# check that we don't already have this graft already setup
my $graft_file = "$git_dir/info/grafts";
my $graft_line = "$first_commit $graft_to_commit";
my $found_graft = fileContains($graft_file, $graft_line);
if ($found_graft) {
print "This repo already grafts to the proper commit\n";
exit 0;
}
# verify that alternate object store contains the commit we want to graft to
my $validated_object = `cd $history_location && git rev-parse --verify $graft_to_commit`;
chomp $validated_object;
if ("$validated_object" ne "$graft_to_commit") {
print "History repo ($history_location) does not contain commit $graft_to_commit!\n";
exit 1;
}
# if our alternates file didn't contain the "history repo" path already
# add its path now
if (!$found_alternate) {
open(I, ">> $git_alternates_file") || die "Could not open $git_alternates_file for writing, wrong permissions?";
print I "$history_alternates_objects\n";
close(I);
}
# add graft
open(I, ">> $graft_file") || die "Could not open $graft_file for writing, wrong permissions?";
print I "$graft_line\n";
close(I);
print "Grafted $first_commit -> $graft_to_commit\n";
|