git.pm 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 (! -d "$srcdir/.git") {
  39. main::error(sprintf(_g("source directory is not the top directory of a git repository (%s/.git not present), but Format git was specified"), $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. my $old_cwd=getcwd();
  64. chdir($srcdir) ||
  65. main::syserr(sprintf(_g("unable to chdir to `%s'"), $srcdir));
  66. # Check for uncommitted files.
  67. # To support dpkg-source -i, get a list of files
  68. # equivalent to the ones git-status finds, and remove any
  69. # ignored files from it.
  70. my @ignores="--exclude-per-directory=.gitignore";
  71. my $core_excludesfile=`git-config --get core.excludesfile`;
  72. chomp $core_excludesfile;
  73. if (length $core_excludesfile && -e $core_excludesfile) {
  74. push @ignores, "--exclude-from='$core_excludesfile'";
  75. }
  76. if (-e ".git/info/exclude") {
  77. push @ignores, "--exclude-from=.git/info/exclude";
  78. }
  79. open(GIT_LS_FILES, '-|', "git-ls-files", "--modified", "--deleted",
  80. "--others", @ignores) ||
  81. main::subprocerr("git-ls-files");
  82. my @files;
  83. while (<GIT_LS_FILES>) {
  84. chomp;
  85. if (! length $main::diff_ignore_regexp ||
  86. ! m/$main::diff_ignore_regexp/o) {
  87. push @files, $_;
  88. }
  89. }
  90. close(GIT_LS_FILES) || main::syserr("git-ls-files exited nonzero");
  91. if (@files) {
  92. main::error(sprintf(_g("uncommitted, not-ignored changes in working directory: %s"),
  93. join(" ", @files)));
  94. }
  95. # git-clone isn't used to copy the repo because the it might be an
  96. # unclonable shallow copy.
  97. chdir($old_cwd) ||
  98. main::syserr(sprintf(_g("unable to chdir to `%s'"), $old_cwd));
  99. mkdir($tardir,0755) ||
  100. main::syserr(sprintf(_g("unable to create `%s'"), $tardir));
  101. system("cp", "-a", "$srcdir/.git", $tardir);
  102. $? && main::subprocerr("cp -a $srcdir/.git $tardir");
  103. chdir($tardir) ||
  104. main::syserr(sprintf(_g("unable to chdir to `%s'"), $tardir));
  105. # TODO support for creating a shallow clone for those cases where
  106. # uploading the whole repo history is not desired
  107. # Clean up the new repo to save space.
  108. # First, delete the whole reflog, which is not needed in a
  109. # distributed source package.
  110. system("rm", "-rf", ".git/logs");
  111. $? && main::subprocerr("rm -rf .git/logs");
  112. system("git-gc", "--prune");
  113. $? && main::subprocerr("git-gc --prune");
  114. # As an optimisation, remove the index. It will be recreated by git
  115. # reset during unpack. It's probably small, but you never know, this
  116. # might save a lot of space.
  117. unlink(".git/index"); # error intentionally ignored
  118. chdir($old_cwd) ||
  119. main::syserr(sprintf(_g("unable to chdir to `%s'"), $old_cwd));
  120. return 1;
  121. }
  122. # Called after a tarball is unpacked, to check out the working copy.
  123. sub post_unpack_tar {
  124. my $srcdir=shift;
  125. sanity_check($srcdir);
  126. my $old_cwd=getcwd();
  127. chdir($srcdir) ||
  128. main::syserr(sprintf(_g("unable to chdir to `%s'"), $srcdir));
  129. # Disable git hooks, as unpacking a source package should not
  130. # involve running code.
  131. foreach my $hook (glob("./.git/hooks/*")) {
  132. if (-x $hook) {
  133. main::warning(sprintf(_g("executable bit set on %s; clearing"), $hook));
  134. chmod(0666 &~ umask(), $hook) ||
  135. main::syserr(sprintf(_g("unable to change permission of `%s'"), $hook));
  136. }
  137. }
  138. # This is a paranoia measure, since the index is not normally
  139. # provided by possibly-untrusted third parties, remove it if
  140. # present (git-rebase will recreate it).
  141. if (-e ".git/index" || -l ".git/index") {
  142. unlink(".git/index") ||
  143. main::syserr(sprintf(_g("unable to remove `%s'"), ".git/index"));
  144. }
  145. # Comment out potentially probamatic or annoying stuff in
  146. # .git/config.
  147. my $safe_fields=qr/^(
  148. core\.autocrlf |
  149. branch\..* |
  150. remote\..* |
  151. core\.repositoryformatversion |
  152. core\.filemode |
  153. core\.logallrefupdates |
  154. core\.bare
  155. )$/x;
  156. # This needs to be an absolute path, for some reason.
  157. my $configfile=Cwd::abs_path(".git/config");
  158. open(GIT_CONFIG, '-|', "git-config", "--file", $configfile, "-l") ||
  159. main::subprocerr("git-config");
  160. my @config=<GIT_CONFIG>;
  161. close(GIT_CONFIG) || main::syserr("git-config exited nonzero");
  162. my %removed_fields;
  163. foreach (@config) {
  164. chomp;
  165. my ($field, $value)=split(/=/, $_, 2);
  166. if ($field !~ /$safe_fields/) {
  167. if (! exists $removed_fields{$field}) {
  168. system("git-config", "--file", $configfile,
  169. "--unset-all", $field);
  170. $? && main::subprocerr("git-config --file $configfile --unset-all $field");
  171. }
  172. push @{$removed_fields{$field}}, $value;
  173. }
  174. }
  175. if (%removed_fields) {
  176. open(GIT_CONFIG, ">>", $configfile) ||
  177. main::syserr(sprintf(_g("unstable to append to %s", $configfile)));
  178. print GIT_CONFIG "\n# "._g("The following setting(s) were disabled by dpkg-source").":\n";
  179. foreach my $field (sort keys %removed_fields) {
  180. foreach my $value (@{$removed_fields{$field}}) {
  181. print GIT_CONFIG "# $field=$value\n";
  182. }
  183. }
  184. close GIT_CONFIG;
  185. main::warning(_g("modifying .git/config to comment out some settings"));
  186. }
  187. # git-checkout is used to repopulate the WC with files
  188. # and recreate the index.
  189. system("git-checkout", "-f");
  190. $? && main::subprocerr("git-clone -f");
  191. chdir($old_cwd) ||
  192. main::syserr(sprintf(_g("unable to chdir to `%s'"), $old_cwd));
  193. return 1;
  194. }
  195. 1