git.pm 7.5 KB

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