diff options
Diffstat (limited to 'tools/git2svn.pl')
-rwxr-xr-x | tools/git2svn.pl | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/tools/git2svn.pl b/tools/git2svn.pl new file mode 100755 index 00000000..c90ea96e --- /dev/null +++ b/tools/git2svn.pl | |||
@@ -0,0 +1,129 @@ | |||
1 | #!/usr/bin/perl | ||
2 | # | ||
3 | # This script pulls the current branch, then walks the first parents and | ||
4 | # commit each of them into subversion. | ||
5 | # | ||
6 | # Copyright (C) 2008 Thomas Guyot-Sionnest <dermoth@aei.ca> | ||
7 | # | ||
8 | # The subversion repository must not be taking any external commit or this | ||
9 | # script will erase them. This script cannot run off a bare repository. | ||
10 | # | ||
11 | # *** INITIAL SETUP *** | ||
12 | # | ||
13 | # 1. Run this command line to get the repository up and ready for this script: | ||
14 | # | ||
15 | # $ cd /path/to/repo/; git log -1 --pretty=format:%H >.git/git2svn.last_commit_hash | ||
16 | # | ||
17 | # 2. Configure the lines below... $ENV{'GIT_DIR'} must point to the .git | ||
18 | # directory of the git-svn repo. | ||
19 | # | ||
20 | # *** INITIAL SETUP *** | ||
21 | # | ||
22 | # This program is free software: you can redistribute it and/or modify | ||
23 | # it under the terms of the GNU General Public License as published by | ||
24 | # the Free Software Foundation, either version 3 of the License, or | ||
25 | # (at your option) any later version. | ||
26 | # | ||
27 | # This program is distributed in the hope that it will be useful, | ||
28 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
29 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
30 | # GNU General Public License for more details. | ||
31 | # | ||
32 | # You should have received a copy of the GNU General Public License | ||
33 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
34 | |||
35 | use strict; | ||
36 | use warnings; | ||
37 | |||
38 | # This is the git working tree. Must be tied to a SVN repository | ||
39 | $ENV{'GIT_DIR'} = '/path/to/nagiosplug/.git'; | ||
40 | |||
41 | # For some strange reasons this is needed: | ||
42 | $ENV{'GIT_SVN_ID'} = 'trunk'; | ||
43 | |||
44 | # Path to git binary | ||
45 | my $git = '/usr/bin/git'; | ||
46 | |||
47 | # Force commits from the hash stored in git2svn.last_commit_hash regardless | ||
48 | # of the state of the current repository. Use this if the repository was | ||
49 | # updated manually or if you need to set that hash to a specific value. | ||
50 | # NB: Re-committing old hashes will revert then roll again changes to SVN. | ||
51 | my $FORCE = 0; | ||
52 | |||
53 | # Print debug output. Useful if you want to see what's being committed. | ||
54 | my $DEBUG = 0; | ||
55 | |||
56 | for (@ARGV) { | ||
57 | $FORCE = 1 if (m/force/); | ||
58 | $DEBUG = 1 if (m/debug/); | ||
59 | if (m/help/ || m/--help/ || m/-h/) { | ||
60 | print "Usage: $0 [ debug ] [ force ] [ help ]\n"; | ||
61 | exit 0; | ||
62 | } | ||
63 | } | ||
64 | |||
65 | # 1st get the current commit hash - we'll start committing to SVN from this one | ||
66 | print "Reading saved hash from $ENV{'GIT_DIR'}/git2svn.last_commit_hash\n" if ($DEBUG); | ||
67 | open(SAVHASH, "<$ENV{'GIT_DIR'}/git2svn.last_commit_hash") | ||
68 | or die("Can't open $ENV{'GIT_DIR'}/git2svn.last_commit_hash: $!"); | ||
69 | my $saved_commit_hash = <SAVHASH>; | ||
70 | chomp $saved_commit_hash; | ||
71 | print "Saved commit hash: $saved_commit_hash\n" if ($DEBUG); | ||
72 | close(SAVHASH); | ||
73 | |||
74 | my $last_commit_hash; | ||
75 | if ($FORCE) { | ||
76 | $last_commit_hash = $saved_commit_hash; | ||
77 | print "Forcing last commit hash to $last_commit_hash\n" if ($DEBUG); | ||
78 | } else { | ||
79 | print "Running: $git log -1 --pretty=format:%H\n" if ($DEBUG); | ||
80 | $last_commit_hash = `$git log -1 --pretty=format:%H`; | ||
81 | die("Failed to retrieve last commit hash") if ($?); | ||
82 | chomp $last_commit_hash; | ||
83 | print "Last commit hash: $last_commit_hash\n" if ($DEBUG); | ||
84 | |||
85 | # Sanity check | ||
86 | die("Last commit hash and saved commit hash don't match, aborting") | ||
87 | if ($last_commit_hash ne $saved_commit_hash); | ||
88 | } | ||
89 | |||
90 | # 2nd pull the remote tree | ||
91 | print "Running: $git pull\n" if ($DEBUG); | ||
92 | `$git pull`; | ||
93 | die("Failed to pull") if ($?); | ||
94 | |||
95 | # Then list all first parents since the last one and insert them into an array | ||
96 | my @commits; | ||
97 | print "Running: $git rev-list --first-parent $last_commit_hash..HEAD\n" if ($DEBUG); | ||
98 | open(REVLIST, "$git rev-list --first-parent $last_commit_hash..HEAD|") | ||
99 | or die("Failed to retrieve revision list: $!"); | ||
100 | |||
101 | while (<REVLIST>) { | ||
102 | chomp; | ||
103 | unshift @commits, $_; | ||
104 | print "Prepending the list with $_\n" if ($DEBUG); | ||
105 | } | ||
106 | |||
107 | close(REVLIST); | ||
108 | |||
109 | if (@commits == 0) { | ||
110 | print "Nothing to do.\n"; | ||
111 | exit 0; | ||
112 | } | ||
113 | |||
114 | # Finally, commit every revision found into SVN | ||
115 | foreach my $commit (@commits) { | ||
116 | print "Commiting $commit to Subversion\n"; | ||
117 | print "Running: $git svn set-tree $commit\n" if ($DEBUG); | ||
118 | `$git svn set-tree $commit`; | ||
119 | die("Failed to commit hash $commit") if ($?); | ||
120 | } | ||
121 | |||
122 | # Once done, update the last commit hash | ||
123 | $last_commit_hash = pop @commits; | ||
124 | print "Writing last commit hash to $ENV{'GIT_DIR'}/git2svn.last_commit_hash\n" if ($DEBUG); | ||
125 | open(SAVHASH, ">$ENV{'GIT_DIR'}/git2svn.last_commit_hash") | ||
126 | or die("Can't open $ENV{'GIT_DIR'}/git2svn.last_commit_hash for writing: $!"); | ||
127 | print SAVHASH $last_commit_hash; | ||
128 | close(SAVHASH); | ||
129 | |||