git.pm 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 File::Find;
  25. use Dpkg;
  26. use Dpkg::Gettext;
  27. push (@INC, $dpkglibdir);
  28. require 'controllib.pl';
  29. # Remove variables from the environment that might cause git to do
  30. # something unexpected.
  31. delete $ENV{GIT_DIR};
  32. delete $ENV{GIT_INDEX_FILE};
  33. delete $ENV{GIT_OBJECT_DIRECTORY};
  34. delete $ENV{GIT_ALTERNATE_OBJECT_DIRECTORIES};
  35. delete $ENV{GIT_WORK_TREE};
  36. sub sanity_check {
  37. my $srcdir=shift;
  38. if (! -s "$srcdir/.git") {
  39. 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));
  40. }
  41. if (-s "$srcdir/.gitmodules") {
  42. main::error(sprintf(_g("git repository %s uses submodules. This is not yet supported."), $srcdir));
  43. }
  44. # Symlinks from .git to outside could cause unpack failures, or
  45. # point to files they shouldn't, so check for and don't allow.
  46. if (-l "$srcdir/.git") {
  47. main::error(sprintf(_g("%s is a symlink"), "$srcdir/.git"));
  48. }
  49. my $abs_srcdir=Cwd::abs_path($srcdir);
  50. find(sub {
  51. if (-l $_) {
  52. if (Cwd::abs_path(readlink($_)) !~ /^\Q$abs_srcdir\E(\/|$)/) {
  53. main::error(sprintf(_g("%s is a symlink to outside %s"), $File::Find::name, $srcdir));
  54. }
  55. }
  56. }, "$srcdir/.git");
  57. }
  58. # Called before a tarball is created, to prepare the tar directory.
  59. sub prep_tar {
  60. my $srcdir=shift;
  61. my $tardir=shift;
  62. sanity_check($srcdir);
  63. if (! -e "$srcdir/.git") {
  64. main::error(sprintf(_g("%s is not a git repository, but Format git was specified"), $srcdir));
  65. }
  66. if (-e "$srcdir/.gitmodules") {
  67. main::error(sprintf(_g("git repository %s uses submodules. This is not yet supported."), $srcdir));
  68. }
  69. # Check for uncommitted files.
  70. open(GIT_STATUS, "LANG=C cd $srcdir && git-status |") ||
  71. main::subprocerr("cd $srcdir && git-status");
  72. my $clean=0;
  73. my $status="";
  74. while (<GIT_STATUS>) {
  75. if (/^\Qnothing to commit (working directory clean)\E$/) {
  76. $clean=1;
  77. }
  78. else {
  79. $status.="git-status: $_";
  80. }
  81. }
  82. close GIT_STATUS;
  83. # git-status exits 1 if there are uncommitted changes or if
  84. # the repo is clean, and 0 if there are uncommitted changes
  85. # listed in the index.
  86. if ($? && $? >> 8 != 1) {
  87. main::subprocerr("cd $srcdir && git status");
  88. }
  89. if (! $clean) {
  90. # To support dpkg-buildpackage -i, get a list of files
  91. # eqivilant to the ones git-status finds, and remove any
  92. # ignored files from it.
  93. my @ignores="--exclude-per-directory=.gitignore";
  94. my $core_excludesfile=`cd $srcdir && git-config --get core.excludesfile`;
  95. chomp $core_excludesfile;
  96. if (length $core_excludesfile && -e "$srcdir/$core_excludesfile") {
  97. push @ignores, "--exclude-from='$core_excludesfile'";
  98. }
  99. if (-e "$srcdir/.git/info/exclude") {
  100. push @ignores, "--exclude-from=.git/info/exclude";
  101. }
  102. open(GIT_LS_FILES, "cd $srcdir && git-ls-files -m -d -o @ignores |") ||
  103. main::subprocerr("cd $srcdir && git-ls-files");
  104. my @files;
  105. while (<GIT_LS_FILES>) {
  106. chomp;
  107. if (! length $main::diff_ignore_regexp ||
  108. ! m/$main::diff_ignore_regexp/o) {
  109. push @files, $_;
  110. }
  111. }
  112. close(GIT_LS_FILES) || main::syserr("git-ls-files exited nonzero");
  113. if (@files) {
  114. print $status;
  115. main::error(sprintf(_g("uncommitted, not-ignored changes in working directory: %s"),
  116. join(" ", @files)));
  117. }
  118. }
  119. # garbage collect the repo
  120. system("cd $srcdir && git-gc --prune");
  121. $? && main::subprocerr("cd $srcdir && git-gc --prune");
  122. # TODO support for creating a shallow clone for those cases where
  123. # uploading the whole repo history is not desired
  124. mkdir($tardir,0755) ||
  125. &syserr(sprintf(_g("unable to create `%s'"), $tardir));
  126. system("cp -a $srcdir/.git $tardir");
  127. $? && main::subprocerr("cp -a $srcdir/.git $tardir");
  128. # As an optimisation, remove the index. It will be recreated by git
  129. # reset during unpack. It's probably small, but you never know, this
  130. # might save a lot of space.
  131. unlink("$tardir/.git/index"); # error intentionally ignored
  132. }
  133. # Called after a tarball is unpacked, to check out the working copy.
  134. sub post_unpack_tar {
  135. my $srcdir=shift;
  136. sanity_check($srcdir);
  137. # Disable git hooks, as unpacking a source package should not
  138. # involve running code.
  139. foreach my $hook (glob("$srcdir/.git/hooks/*")) {
  140. if (-x $hook) {
  141. main::warning(sprintf(_g("executable bit set on %s; clearing"), $hook));
  142. chmod(0644 &~ umask(), $hook) ||
  143. main::syserr(sprintf(_g("unable to change permission of `%s'"), $hook));
  144. }
  145. }
  146. # This is a paranoia measure, since the index is not normally
  147. # provided by possibly-untrusted third parties, remove it if
  148. # present (git-rebase will recreate it).
  149. if (-e "$srcdir/.git/index" || -l "$srcdir/.git/index") {
  150. unlink("$srcdir/.git/index") ||
  151. main::syserr(sprintf(_g("unable to remove `%s'"), "$srcdir/.git/index"));
  152. }
  153. # Comment out potentially probamatic or annoying stuff in
  154. # .git/config.
  155. my $safe_fields=qr/^(
  156. core\.autocrlf |
  157. branch\..* |
  158. remote\..* |
  159. core\.repositoryformatversion |
  160. core\.filemode |
  161. core\.logallrefupdates |
  162. core\.bare
  163. )$/x;
  164. # This needs to be an absolute path, for some reason.
  165. my $configfile=Cwd::abs_path("$srcdir/.git/config");
  166. open(GIT_CONFIG, "git-config --file $configfile -l |") ||
  167. main::subprocerr("git-config");
  168. my @config=<GIT_CONFIG>;
  169. close(GIT_CONFIG) || main::syserr("git-config exited nonzero");
  170. my %removed_fields;
  171. foreach (@config) {
  172. chomp;
  173. my ($field, $value)=split(/=/, $_, 2);
  174. if ($field !~ /$safe_fields/) {
  175. if (! exists $removed_fields{$field}) {
  176. system("git-config", "--file", $configfile,
  177. "--unset-all", $field);
  178. $? && main::subprocerr("git-config --file $configfile --unset-all $field");
  179. }
  180. push @{$removed_fields{$field}}, $value;
  181. }
  182. }
  183. if (%removed_fields) {
  184. open(GIT_CONFIG, ">>", $configfile) ||
  185. main::syserr(sprintf(_g("unstable to append to %s", $configfile)));
  186. print GIT_CONFIG "\n# "._g("The following setting(s) were disabled by dpkg-source").":\n";
  187. foreach my $field (sort keys %removed_fields) {
  188. foreach my $value (@{$removed_fields{$field}}) {
  189. print GIT_CONFIG "# $field=$value\n";
  190. }
  191. }
  192. close GIT_CONFIG;
  193. main::warning(_g(_g("modifying .git/config to comment out some settings")));
  194. }
  195. # Note that git-reset is used to repopulate the WC with files.
  196. # git-clone isn't used because the repo might be an unclonable
  197. # shallow copy. git-reset also recreates the index.
  198. # XXX git-reset should be made to run in quiet mode here, but
  199. # lacks a good way to do it. Bug filed.
  200. system("cd $srcdir && git-reset --hard");
  201. $? && main::subprocerr("cd $srcdir && git-reset --hard");
  202. }
  203. 1