git.pm 6.4 KB

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