git.pm 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. sub sanity_check {
  36. my $srcdir=shift;
  37. if (! -e "$srcdir/.git") {
  38. main::error(sprintf(_g("%s is not a git repository, but Format git was specified"), $srcdir));
  39. }
  40. if (-s "$srcdir/.gitmodules") {
  41. main::error(sprintf(_g("git repository %s uses submodules. This is not yet supported."), $srcdir));
  42. }
  43. }
  44. # Called before a tarball is created, to prepare the tar directory.
  45. sub prep_tar {
  46. my $srcdir=shift;
  47. my $tardir=shift;
  48. sanity_check($srcdir);
  49. if (! -e "$srcdir/.git") {
  50. main::error(sprintf(_g("%s is not a git repository, but Format git was specified"), $srcdir));
  51. }
  52. if (-e "$srcdir/.gitmodules") {
  53. main::error(sprintf(_g("git repository %s uses submodules. This is not yet supported."), $srcdir));
  54. }
  55. # Check for uncommitted files.
  56. open(GIT_STATUS, "LANG=C cd $srcdir && git status |") ||
  57. main::subprocerr("cd $srcdir && git status");
  58. my $clean=0;
  59. my $status="";
  60. while (<GIT_STATUS>) {
  61. if (/^\Qnothing to commit (working directory clean)\E$/) {
  62. $clean=1;
  63. }
  64. else {
  65. $status.="git-status: $_";
  66. }
  67. }
  68. close GIT_STATUS;
  69. # git-status exits 1 if there are uncommitted changes or if
  70. # the repo is clean, and 0 if there are uncommitted changes
  71. # listed in the index.
  72. if ($? && $? >> 8 != 1) {
  73. main::subprocerr("cd $srcdir && git status");
  74. }
  75. if (! $clean) {
  76. print $status;
  77. main::error(_g("uncommitted changed in working directory"));
  78. }
  79. # garbage collect the repo
  80. system("cd $srcdir && git-gc --prune");
  81. $? && main::subprocerr("cd $srcdir && git-gc --prune");
  82. # TODO support for creating a shallow clone for those cases where
  83. # uploading the whole repo history is not desired
  84. system("cp -a $srcdir/.git $tardir");
  85. $? && main::subprocerr("cp -a $srcdir/.git $tardir");
  86. # As an optimisation, remove the index. It will be recreated by git
  87. # reset during unpack. It's probably small, but you never know, this
  88. # might save a lot of space.
  89. unlink("$tardir/.git/index"); # error intentionally ignored
  90. }
  91. # Called after a tarball is unpacked, to check out the working copy.
  92. sub post_unpack_tar {
  93. my $srcdir=shift;
  94. sanity_check($srcdir);
  95. # Disable git hooks, as unpacking a source package should not
  96. # involve running code.
  97. foreach my $hook (glob("$srcdir/.git/hooks/*")) {
  98. if (-x $hook) {
  99. main::warning(sprintf(_g("executable bit set on %s; clearing"), $hook));
  100. chmod(0644 &~ umask(), $hook) ||
  101. main::syserr(sprintf(_g("unable to change permission of `%s'"), $hook));
  102. }
  103. }
  104. # This is a paranoia measure, since the index is not normally
  105. # provided by possible-untrusted third parties, remove it if
  106. # present (git-rebase will recreate it).
  107. unlink("$srcdir/.git/index") ||
  108. main::syserr(sprintf(_g("unable to remove `%s'"), "$srcdir/.git/index"));
  109. # Comment out potentially probamatic or annoying stuff in
  110. # .git/config.
  111. my $safe_fields=qr/^(
  112. core\.autocrlf |
  113. branch\..* |
  114. remote\..* |
  115. core\.repositoryformatversion |
  116. core.filemode |
  117. core.bare
  118. )$/x;
  119. my $removed="";
  120. # This needs to be an absolute path, for some reason.
  121. my $configfile=Cwd::abs_path("$srcdir/.git/config");
  122. open(GIT_CONFIG, "git-config --file $configfile -l |") ||
  123. main::subprocerr("git-config");
  124. my @config=<GIT_CONFIG>;
  125. close(GIT_CONFIG) || main::syserr("git-config exited nonzero");
  126. foreach (@config) {
  127. chomp;
  128. my ($field, $value)=split(/=/, $_, 2);
  129. if ($field !~ /$safe_fields/) {
  130. system("git-config", "--file", $configfile,
  131. "--unset-all", $field);
  132. $? && main::subprocerr("git-config --file $configfile --unset-all $field");
  133. $removed.="# $_\n";
  134. }
  135. }
  136. if ($removed) {
  137. open(GIT_CONFIG, ">>", $configfile) ||
  138. main::syserr(sprintf(_g("unstable to append to %s", $configfile)));
  139. print GIT_CONFIG "\n# "._g("The following setting(s) were disabled by dpkg-source").":\n";
  140. print GIT_CONFIG $removed;
  141. close GIT_CONFIG;
  142. main::warning(_g(_g("modifying .git/config to comment out some settings")));
  143. }
  144. # Note that git-reset is used to repopulate the WC with files.
  145. # git-clone isn't used because the repo might be an unclonable
  146. # shallow copy. git-reset also recreates the index.
  147. # XXX git-reset should be made to run in quiet mode here, but
  148. # lacks a good way to do it. Bug filed.
  149. system("cd $srcdir && git-reset --hard");
  150. $? && main::subprocerr("cd $srcdir && git-reset --hard");
  151. }
  152. 1