Ignore stderr messages unless return code is non-zero or there is no output
[monitoring-plugins.git] / tools / git2svn.pl
blobafe2b3b3feaeaf138ff94768ec9eee8d849f46d6
1 #!/usr/bin/perl
3 # This script pulls the current branch, then walks the first parents and
4 # commit each of them into subversion.
6 # Copyright (C) 2008 Thomas Guyot-Sionnest <dermoth@aei.ca>
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.
11 # *** INITIAL SETUP ***
13 # 1. Run this command line to get the repository up and ready for this script:
15 # $ cd /path/to/repo/; git log -1 --pretty=format:%H >.git/git2svn.last_commit_hash
17 # 2. Configure the lines below... $ENV{'GIT_DIR'} must point to the .git
18 # directory of the git-svn repo.
20 # *** INITIAL SETUP ***
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.
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.
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/>.
35 use strict;
36 use warnings;
38 # This is the git working tree. Must be tied to a SVN repository
39 $ENV{'GIT_DIR'} = '/path/to/nagiosplug/.git';
41 # For some strange reasons this is needed:
42 $ENV{'GIT_SVN_ID'} = 'trunk';
44 # Path to git binary
45 my $git = '/usr/bin/git';
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;
53 # Print debug output. Useful if you want to see what's being committed.
54 my $DEBUG = 0;
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;
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);
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);
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);
90 # 2nd pull the remote tree
91 print "Running: $git pull\n" if ($DEBUG);
92 `$git pull`;
93 die("Failed to pull") if ($?);
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: $!");
101 while (<REVLIST>) {
102 chomp;
103 unshift @commits, $_;
104 print "Prepending the list with $_\n" if ($DEBUG);
107 close(REVLIST);
109 if (@commits == 0) {
110 print "Nothing to do.\n";
111 exit 0;
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 --add-author-from $commit\n" if ($DEBUG);
118 `$git svn set-tree --add-author-from $commit`;
119 die("Failed to commit hash $commit") if ($?);
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);