git2svn.pl 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. use strict;
  35. use warnings;
  36. # This is the git working tree. Must be tied to a SVN repository
  37. $ENV{'GIT_DIR'} = '/path/to/nagiosplug/.git';
  38. # For some strange reasons this is needed:
  39. $ENV{'GIT_SVN_ID'} = 'trunk';
  40. # Path to git binary
  41. my $git = '/usr/bin/git';
  42. # Force commits from the hash stored in git2svn.last_commit_hash regardless
  43. # of the state of the current repository. Use this if the repository was
  44. # updated manually or if you need to set that hash to a specific value.
  45. # NB: Re-committing old hashes will revert then roll again changes to SVN.
  46. my $FORCE = 0;
  47. # Print debug output. Useful if you want to see what's being committed.
  48. my $DEBUG = 0;
  49. for (@ARGV) {
  50. $FORCE = 1 if (m/force/);
  51. $DEBUG = 1 if (m/debug/);
  52. if (m/help/ || m/--help/ || m/-h/) {
  53. print "Usage: $0 [ debug ] [ force ] [ help ]\n";
  54. exit 0;
  55. }
  56. }
  57. # 1st get the current commit hash - we'll start committing to SVN from this one
  58. print "Reading saved hash from $ENV{'GIT_DIR'}/git2svn.last_commit_hash\n" if ($DEBUG);
  59. open(SAVHASH, "<$ENV{'GIT_DIR'}/git2svn.last_commit_hash")
  60. or die("Can't open $ENV{'GIT_DIR'}/git2svn.last_commit_hash: $!");
  61. my $saved_commit_hash = <SAVHASH>;
  62. chomp $saved_commit_hash;
  63. print "Saved commit hash: $saved_commit_hash\n" if ($DEBUG);
  64. close(SAVHASH);
  65. my $last_commit_hash;
  66. if ($FORCE) {
  67. $last_commit_hash = $saved_commit_hash;
  68. print "Forcing last commit hash to $last_commit_hash\n" if ($DEBUG);
  69. } else {
  70. print "Running: $git log -1 --pretty=format:%H\n" if ($DEBUG);
  71. $last_commit_hash = `$git log -1 --pretty=format:%H`;
  72. die("Failed to retrieve last commit hash") if ($?);
  73. chomp $last_commit_hash;
  74. print "Last commit hash: $last_commit_hash\n" if ($DEBUG);
  75. # Sanity check
  76. die("Last commit hash and saved commit hash don't match, aborting")
  77. if ($last_commit_hash ne $saved_commit_hash);
  78. }
  79. # 2nd pull the remote tree
  80. print "Running: $git pull\n" if ($DEBUG);
  81. `$git pull`;
  82. die("Failed to pull") if ($?);
  83. # Then list all first parents since the last one and insert them into an array
  84. my @commits;
  85. print "Running: $git rev-list --first-parent $last_commit_hash..HEAD\n" if ($DEBUG);
  86. open(REVLIST, "$git rev-list --first-parent $last_commit_hash..HEAD|")
  87. or die("Failed to retrieve revision list: $!");
  88. while (<REVLIST>) {
  89. chomp;
  90. unshift @commits, $_;
  91. print "Prepending the list with $_\n" if ($DEBUG);
  92. }
  93. close(REVLIST);
  94. if (@commits == 0) {
  95. print "Nothing to do.\n";
  96. exit 0;
  97. }
  98. # Finally, commit every revision found into SVN
  99. foreach my $commit (@commits) {
  100. print "Commiting $commit to Subversion\n";
  101. print "Running: $git svn set-tree $commit\n" if ($DEBUG);
  102. `$git svn set-tree $commit`;
  103. die("Failed to commit hash $commit") if ($?);
  104. }
  105. # Once done, update the last commit hash
  106. $last_commit_hash = pop @commits;
  107. print "Writing last commit hash to $ENV{'GIT_DIR'}/git2svn.last_commit_hash\n" if ($DEBUG);
  108. open(SAVHASH, ">$ENV{'GIT_DIR'}/git2svn.last_commit_hash")
  109. or die("Can't open $ENV{'GIT_DIR'}/git2svn.last_commit_hash for writing: $!");
  110. print SAVHASH $last_commit_hash;
  111. close(SAVHASH);