git.pm 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 import {
  37. foreach my $dir (split(/:/, $ENV{PATH})) {
  38. if (-x "$dir/git") {
  39. return 1;
  40. }
  41. }
  42. main::error(sprintf(_g("This source package can only be manipulated using git, which is not in the PATH.")));
  43. }
  44. sub sanity_check {
  45. my $srcdir=shift;
  46. if (! -d "$srcdir/.git") {
  47. 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));
  48. }
  49. if (-s "$srcdir/.gitmodules") {
  50. main::error(sprintf(_g("git repository %s uses submodules. This is not yet supported."), $srcdir));
  51. }
  52. # Symlinks from .git to outside could cause unpack failures, or
  53. # point to files they shouldn't, so check for and don't allow.
  54. if (-l "$srcdir/.git") {
  55. main::error(sprintf(_g("%s is a symlink"), "$srcdir/.git"));
  56. }
  57. my $abs_srcdir=Cwd::abs_path($srcdir);
  58. find(sub {
  59. if (-l $_) {
  60. if (Cwd::abs_path(readlink($_)) !~ /^\Q$abs_srcdir\E(\/|$)/) {
  61. main::error(sprintf(_g("%s is a symlink to outside %s"), $File::Find::name, $srcdir));
  62. }
  63. }
  64. }, "$srcdir/.git");
  65. return 1;
  66. }
  67. # Returns a hash of arrays of git config values.
  68. sub read_git_config {
  69. my $file=shift;
  70. my %ret;
  71. open(GIT_CONFIG, '-|', "git-config", "--file", $file, "--null", "-l") ||
  72. main::subprocerr("git-config");
  73. my ($key, $value);
  74. while (<GIT_CONFIG>) {
  75. if (! defined $key) {
  76. $key=$_;
  77. chomp $key;
  78. $value="";
  79. }
  80. elsif (/(.*)\0(.*)/) {
  81. $value.=$1;
  82. push @{$ret{$key}}, $value;
  83. $key=$2;
  84. chomp $key;
  85. $value="";
  86. }
  87. else {
  88. $value.=$1;
  89. }
  90. }
  91. if (defined $key && length $key) {
  92. push @{$ret{$key}}, $value;
  93. }
  94. close(GIT_CONFIG) || main::syserr("git-config exited nonzero");
  95. return \%ret;
  96. }
  97. # Called before a tarball is created, to prepare the tar directory.
  98. sub prep_tar {
  99. my $srcdir=shift;
  100. my $tardir=shift;
  101. sanity_check($srcdir);
  102. my $old_cwd=getcwd();
  103. chdir($srcdir) ||
  104. main::syserr(sprintf(_g("unable to chdir to `%s'"), $srcdir));
  105. # Check for uncommitted files.
  106. # To support dpkg-source -i, get a list of files
  107. # equivalent to the ones git-status finds, and remove any
  108. # ignored files from it.
  109. my @ignores="--exclude-per-directory=.gitignore";
  110. my $core_excludesfile=`git-config --get core.excludesfile`;
  111. chomp $core_excludesfile;
  112. if (length $core_excludesfile && -e $core_excludesfile) {
  113. push @ignores, "--exclude-from='$core_excludesfile'";
  114. }
  115. if (-e ".git/info/exclude") {
  116. push @ignores, "--exclude-from=.git/info/exclude";
  117. }
  118. open(GIT_LS_FILES, '-|', "git-ls-files", "--modified", "--deleted",
  119. "--others", @ignores) ||
  120. main::subprocerr("git-ls-files");
  121. my @files;
  122. while (<GIT_LS_FILES>) {
  123. chomp;
  124. if (! length $main::diff_ignore_regexp ||
  125. ! m/$main::diff_ignore_regexp/o) {
  126. push @files, $_;
  127. }
  128. }
  129. close(GIT_LS_FILES) || main::syserr("git-ls-files exited nonzero");
  130. if (@files) {
  131. main::error(sprintf(_g("uncommitted, not-ignored changes in working directory: %s"),
  132. join(" ", @files)));
  133. }
  134. # git-clone isn't used to copy the repo because the it might be an
  135. # unclonable shallow copy.
  136. chdir($old_cwd) ||
  137. main::syserr(sprintf(_g("unable to chdir to `%s'"), $old_cwd));
  138. mkdir($tardir,0755) ||
  139. main::syserr(sprintf(_g("unable to create `%s'"), $tardir));
  140. system("cp", "-a", "$srcdir/.git", $tardir);
  141. $? && main::subprocerr("cp -a $srcdir/.git $tardir");
  142. chdir($tardir) ||
  143. main::syserr(sprintf(_g("unable to chdir to `%s'"), $tardir));
  144. # TODO support for creating a shallow clone for those cases where
  145. # uploading the whole repo history is not desired
  146. # Clean up the new repo to save space.
  147. # First, delete the whole reflog, which is not needed in a
  148. # distributed source package.
  149. system("rm", "-rf", ".git/logs");
  150. $? && main::subprocerr("rm -rf .git/logs");
  151. system("git-gc", "--prune");
  152. $? && main::subprocerr("git-gc --prune");
  153. # As an optimisation, remove the index. It will be recreated by git
  154. # reset during unpack. It's probably small, but you never know, this
  155. # might save a lot of space.
  156. unlink(".git/index"); # error intentionally ignored
  157. chdir($old_cwd) ||
  158. main::syserr(sprintf(_g("unable to chdir to `%s'"), $old_cwd));
  159. return 1;
  160. }
  161. # Called after a tarball is unpacked, to check out the working copy.
  162. sub post_unpack_tar {
  163. my $srcdir=shift;
  164. sanity_check($srcdir);
  165. my $old_cwd=getcwd();
  166. chdir($srcdir) ||
  167. main::syserr(sprintf(_g("unable to chdir to `%s'"), $srcdir));
  168. # Disable git hooks, as unpacking a source package should not
  169. # involve running code.
  170. foreach my $hook (glob("./.git/hooks/*")) {
  171. if (-x $hook) {
  172. main::warning(sprintf(_g("executable bit set on %s; clearing"), $hook));
  173. chmod(0666 &~ umask(), $hook) ||
  174. main::syserr(sprintf(_g("unable to change permission of `%s'"), $hook));
  175. }
  176. }
  177. # This is a paranoia measure, since the index is not normally
  178. # provided by possibly-untrusted third parties, remove it if
  179. # present (git will recreate it as needed).
  180. if (-e ".git/index" || -l ".git/index") {
  181. unlink(".git/index") ||
  182. main::syserr(sprintf(_g("unable to remove `%s'"), ".git/index"));
  183. }
  184. # Comment out potentially probamatic or annoying stuff in
  185. # .git/config.
  186. my $safe_fields=qr/^(
  187. core\.autocrlf |
  188. branch\..* |
  189. remote\..* |
  190. core\.repositoryformatversion |
  191. core\.filemode |
  192. core\.logallrefupdates |
  193. core\.bare
  194. )$/x;
  195. my %config=%{read_git_config(".git/config")};
  196. foreach my $field (keys %config) {
  197. if ($field =~ /$safe_fields/) {
  198. delete $config{$field};
  199. }
  200. else {
  201. system("git-config", "--file", ".git/config",
  202. "--unset-all", $field);
  203. $? && main::subprocerr("git-config --file .git/config --unset-all $field");
  204. }
  205. }
  206. if (%config) {
  207. main::warning(_g("modifying .git/config to comment out some settings"));
  208. open(GIT_CONFIG, ">>", ".git/config") ||
  209. main::syserr(sprintf(_g("unstable to append to %s", ".git/config")));
  210. print GIT_CONFIG "\n# "._g("The following setting(s) were disabled by dpkg-source").":\n";
  211. foreach my $field (sort keys %config) {
  212. foreach my $value (@{$config{$field}}) {
  213. print GIT_CONFIG "# $field=$value\n";
  214. }
  215. }
  216. close GIT_CONFIG;
  217. }
  218. # git-checkout is used to repopulate the WC with files
  219. # and recreate the index.
  220. system("git-checkout", "-f");
  221. $? && main::subprocerr("git-checkout -f");
  222. chdir($old_cwd) ||
  223. main::syserr(sprintf(_g("unable to chdir to `%s'"), $old_cwd));
  224. return 1;
  225. }
  226. 1