git.pm 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. # TODO support dpkg-buildpackage -i and allow building with
  77. # modified ignored files. This requires a way to get a list
  78. # of files, and git-status's output is too evil to parse.
  79. print $status;
  80. main::error(_g("uncommitted changes in working directory"));
  81. }
  82. # garbage collect the repo
  83. system("cd $srcdir && git-gc --prune");
  84. $? && main::subprocerr("cd $srcdir && git-gc --prune");
  85. # TODO support for creating a shallow clone for those cases where
  86. # uploading the whole repo history is not desired
  87. system("cp -a $srcdir/.git $tardir");
  88. $? && main::subprocerr("cp -a $srcdir/.git $tardir");
  89. # As an optimisation, remove the index. It will be recreated by git
  90. # reset during unpack. It's probably small, but you never know, this
  91. # might save a lot of space.
  92. unlink("$tardir/.git/index"); # error intentionally ignored
  93. }
  94. # Called after a tarball is unpacked, to check out the working copy.
  95. sub post_unpack_tar {
  96. my $srcdir=shift;
  97. sanity_check($srcdir);
  98. # Disable git hooks, as unpacking a source package should not
  99. # involve running code.
  100. foreach my $hook (glob("$srcdir/.git/hooks/*")) {
  101. if (-x $hook) {
  102. main::warning(sprintf(_g("executable bit set on %s; clearing"), $hook));
  103. chmod(0644 &~ umask(), $hook) ||
  104. main::syserr(sprintf(_g("unable to change permission of `%s'"), $hook));
  105. }
  106. }
  107. # This is a paranoia measure, since the index is not normally
  108. # provided by possible-untrusted third parties, remove it if
  109. # present (git-rebase will recreate it).
  110. unlink("$srcdir/.git/index") ||
  111. main::syserr(sprintf(_g("unable to remove `%s'"), "$srcdir/.git/index"));
  112. # Comment out potentially probamatic or annoying stuff in
  113. # .git/config.
  114. my $safe_fields=qr/^(
  115. core\.autocrlf |
  116. branch\..* |
  117. remote\..* |
  118. core\.repositoryformatversion |
  119. core.filemode |
  120. core.bare
  121. )$/x;
  122. my $removed="";
  123. # This needs to be an absolute path, for some reason.
  124. my $configfile=Cwd::abs_path("$srcdir/.git/config");
  125. open(GIT_CONFIG, "git-config --file $configfile -l |") ||
  126. main::subprocerr("git-config");
  127. my @config=<GIT_CONFIG>;
  128. close(GIT_CONFIG) || main::syserr("git-config exited nonzero");
  129. foreach (@config) {
  130. chomp;
  131. my ($field, $value)=split(/=/, $_, 2);
  132. if ($field !~ /$safe_fields/) {
  133. system("git-config", "--file", $configfile,
  134. "--unset-all", $field);
  135. $? && main::subprocerr("git-config --file $configfile --unset-all $field");
  136. $removed.="# $_\n";
  137. }
  138. }
  139. if ($removed) {
  140. open(GIT_CONFIG, ">>", $configfile) ||
  141. main::syserr(sprintf(_g("unstable to append to %s", $configfile)));
  142. print GIT_CONFIG "\n# "._g("The following setting(s) were disabled by dpkg-source").":\n";
  143. print GIT_CONFIG $removed;
  144. close GIT_CONFIG;
  145. main::warning(_g(_g("modifying .git/config to comment out some settings")));
  146. }
  147. # Note that git-reset is used to repopulate the WC with files.
  148. # git-clone isn't used because the repo might be an unclonable
  149. # shallow copy. git-reset also recreates the index.
  150. # XXX git-reset should be made to run in quiet mode here, but
  151. # lacks a good way to do it. Bug filed.
  152. system("cd $srcdir && git-reset --hard");
  153. $? && main::subprocerr("cd $srcdir && git-reset --hard");
  154. }
  155. 1