git.pm 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #!/usr/bin/perl
  2. #
  3. # git support for dpkg-source
  4. #
  5. # Copyright © 2007 Joey Hess <joeyh@debian.org>.
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. package Dpkg::Source::VCS::git;
  21. use strict;
  22. use warnings;
  23. use Cwd;
  24. use Dpkg;
  25. use Dpkg::Gettext;
  26. push (@INC, $dpkglibdir);
  27. require 'controllib.pl';
  28. # Remove variables from the environment that might cause git to do
  29. # something unexpected.
  30. delete $ENV{GIT_DIR};
  31. delete $ENV{GIT_INDEX_FILE};
  32. delete $ENV{GIT_OBJECT_DIRECTORY};
  33. delete $ENV{GIT_ALTERNATE_OBJECT_DIRECTORIES};
  34. delete $ENV{GIT_WORK_TREE};
  35. # Called before a tarball is created, to prepare the tar directory.
  36. sub prep_tar {
  37. my $srcdir=shift;
  38. my $tardir=shift;
  39. if (! -e "$srcdir/.git") {
  40. main::error(sprintf(_g("%s is not a git repository, but Format git was specified"), $srcdir));
  41. }
  42. # Check for uncommitted files.
  43. open(GIT_STATUS, "LANG=C cd $srcdir && git status |") ||
  44. main::subprocerr("cd $srcdir && git status");
  45. my $clean=0;
  46. my $status="";
  47. while (<GIT_STATUS>) {
  48. if (/^\Qnothing to commit (working directory clean)\E$/) {
  49. $clean=1;
  50. }
  51. else {
  52. $status.="git-status: $_";
  53. }
  54. }
  55. close GIT_STATUS;
  56. # git-status exits 1 if there are uncommitted changes or if
  57. # the repo is clean, and 0 if there are uncommitted changes
  58. # listed in the index.
  59. if ($? && $? >> 8 != 1) {
  60. main::subprocerr("cd $srcdir && git status");
  61. }
  62. if (! $clean) {
  63. print $status;
  64. main::error(_g("uncommitted changed in working directory"));
  65. }
  66. # garbage collect the repo
  67. system("cd $srcdir && git-gc --prune");
  68. $? && main::subprocerr("cd $srcdir && git-gc --prune");
  69. # TODO support for creating a shallow clone for those cases where
  70. # uploading the whole repo history is not desired
  71. system("cp -a $srcdir/.git $tardir");
  72. $? && main::subprocerr("cp -a $srcdir/.git $tardir");
  73. }
  74. # Called after a tarball is unpacked, to check out the working copy.
  75. sub post_unpack_tar {
  76. my $srcdir=shift;
  77. if (! -e "$srcdir/.git") {
  78. main::error(sprintf(_g("%s is not a git repository"), $srcdir));
  79. }
  80. # Disable git hooks, as unpacking a source package should not
  81. # involve running code.
  82. foreach my $hook (glob("$srcdir/.git/hooks/*")) {
  83. if (-x $hook) {
  84. main::warning(sprintf(_g("executable bit set on %s; clearing"), $hook));
  85. chmod(0644 &~ umask(), $hook) ||
  86. main::syserr(sprintf(_g("unable to change permission of `%s'"), $hook));
  87. }
  88. }
  89. # Comment out potentially probamatic or annoying stuff in
  90. # .git/config.
  91. my $safe_fields=qr/^(
  92. core\.autocrlf |
  93. branch\..* |
  94. remote\..* |
  95. core\.repositoryformatversion |
  96. core.filemode |
  97. core.bare
  98. )$/x;
  99. my $removed="";
  100. # This needs to be an absolute path, for some reason.
  101. my $configfile=Cwd::abs_path("$srcdir/.git/config");
  102. open(GIT_CONFIG, "git-config --file $configfile -l |") ||
  103. main::subprocerr("git-config");
  104. my @config=<GIT_CONFIG>;
  105. close(GIT_CONFIG) || main::syserr("git-config exited nonzero");
  106. foreach (@config) {
  107. chomp;
  108. my ($field, $value)=split(/=/, $_, 2);
  109. if ($field !~ /$safe_fields/) {
  110. system("git-config", "--file", $configfile,
  111. "--unset-all", $field);
  112. $? && main::subprocerr("git-config --file $configfile --unset-all $field");
  113. $removed.="# $_\n";
  114. }
  115. }
  116. if ($removed) {
  117. open(GIT_CONFIG, ">>", $configfile) ||
  118. main::syserr(sprintf(_g("unstable to append to %s", $configfile)));
  119. print GIT_CONFIG "\n# "._g("The following setting(s) were disabled by dpkg-source").":\n";
  120. print GIT_CONFIG $removed;
  121. close GIT_CONFIG;
  122. main::warning(_g(_g("modifying .git/config to comment out some settings")));
  123. }
  124. # Note that git-reset is used to repopulate the WC with files.
  125. # git-clone isn't used because the repo might be an unclonable
  126. # shallow copy.
  127. # XXX git-reset should be made to run in quiet mode here, but
  128. # lacks a good way to do it. Bug filed.
  129. system("cd $srcdir && git-reset --hard");
  130. $? && main::subprocerr("cd $srcdir && git-reset --hard");
  131. }
  132. 1