git.pm 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #
  2. # git support for dpkg-source
  3. #
  4. # Copyright © 2007,2010 Joey Hess <joeyh@debian.org>.
  5. # Copyright © 2008 Frank Lichtenheld <djpig@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, see <http://www.gnu.org/licenses/>.
  19. package Dpkg::Source::Package::V3::git;
  20. use strict;
  21. use warnings;
  22. our $VERSION = '0.02';
  23. use base 'Dpkg::Source::Package';
  24. use Cwd qw(abs_path getcwd);
  25. use File::Basename;
  26. use File::Temp qw(tempdir);
  27. use Dpkg;
  28. use Dpkg::Gettext;
  29. use Dpkg::ErrorHandling;
  30. use Dpkg::Exit;
  31. use Dpkg::Source::Functions qw(erasedir);
  32. our $CURRENT_MINOR_VERSION = '0';
  33. # Remove variables from the environment that might cause git to do
  34. # something unexpected.
  35. delete $ENV{GIT_DIR};
  36. delete $ENV{GIT_INDEX_FILE};
  37. delete $ENV{GIT_OBJECT_DIRECTORY};
  38. delete $ENV{GIT_ALTERNATE_OBJECT_DIRECTORIES};
  39. delete $ENV{GIT_WORK_TREE};
  40. sub import {
  41. foreach my $dir (split(/:/, $ENV{PATH})) {
  42. if (-x "$dir/git") {
  43. return 1;
  44. }
  45. }
  46. error(_g('cannot unpack git-format source package because ' .
  47. 'git is not in the PATH'));
  48. }
  49. sub sanity_check {
  50. my $srcdir = shift;
  51. if (! -d "$srcdir/.git") {
  52. error(_g('source directory is not the top directory of a git ' .
  53. 'repository (%s/.git not present), but Format git was ' .
  54. 'specified'), $srcdir);
  55. }
  56. if (-s "$srcdir/.gitmodules") {
  57. error(_g('git repository %s uses submodules; this is not yet supported'),
  58. $srcdir);
  59. }
  60. return 1;
  61. }
  62. sub parse_cmdline_option {
  63. my ($self, $opt) = @_;
  64. return 1 if $self->SUPER::parse_cmdline_option($opt);
  65. if ($opt =~ /^--git-ref=(.*)$/) {
  66. push @{$self->{options}{git_ref}}, $1;
  67. return 1;
  68. } elsif ($opt =~ /^--git-depth=(\d+)$/) {
  69. $self->{options}{git_depth} = $1;
  70. return 1;
  71. }
  72. return 0;
  73. }
  74. sub can_build {
  75. my ($self, $dir) = @_;
  76. return (-d "$dir/.git", _g("doesn't contain a git repository"));
  77. }
  78. sub do_build {
  79. my ($self, $dir) = @_;
  80. my $diff_ignore_regexp = $self->{options}{diff_ignore_regexp};
  81. $dir =~ s{/+$}{}; # Strip trailing /
  82. my ($dirname, $updir) = fileparse($dir);
  83. my $basenamerev = $self->get_basename(1);
  84. sanity_check($dir);
  85. my $old_cwd = getcwd();
  86. chdir($dir) || syserr(_g("unable to chdir to `%s'"), $dir);
  87. # Check for uncommitted files.
  88. # To support dpkg-source -i, get a list of files
  89. # equivalent to the ones git status finds, and remove any
  90. # ignored files from it.
  91. my @ignores = '--exclude-per-directory=.gitignore';
  92. my $core_excludesfile = `git config --get core.excludesfile`;
  93. chomp $core_excludesfile;
  94. if (length $core_excludesfile && -e $core_excludesfile) {
  95. push @ignores, "--exclude-from=$core_excludesfile";
  96. }
  97. if (-e '.git/info/exclude') {
  98. push @ignores, '--exclude-from=.git/info/exclude';
  99. }
  100. open(my $git_ls_files_fh, '-|', 'git', 'ls-files', '--modified', '--deleted',
  101. '-z', '--others', @ignores) || subprocerr('git ls-files');
  102. my @files;
  103. { local $/ = "\0";
  104. while (<$git_ls_files_fh>) {
  105. chomp;
  106. if (! length $diff_ignore_regexp ||
  107. ! m/$diff_ignore_regexp/o) {
  108. push @files, $_;
  109. }
  110. }
  111. }
  112. close($git_ls_files_fh) || syserr(_g('git ls-files exited nonzero'));
  113. if (@files) {
  114. error(_g('uncommitted, not-ignored changes in working directory: %s'),
  115. join(' ', @files));
  116. }
  117. # If a depth was specified, need to create a shallow clone and
  118. # bundle that.
  119. my $tmp;
  120. my $shallowfile;
  121. if ($self->{options}{git_depth}) {
  122. chdir($old_cwd) ||
  123. syserr(_g("unable to chdir to `%s'"), $old_cwd);
  124. $tmp = tempdir("$dirname.git.XXXXXX", DIR => $updir);
  125. push @Dpkg::Exit::handlers, sub { erasedir($tmp) };
  126. my $clone_dir = "$tmp/repo.git";
  127. # file:// is needed to avoid local cloning, which does not
  128. # create a shallow clone.
  129. info(_g('creating shallow clone with depth %s'),
  130. $self->{options}{git_depth});
  131. system('git', 'clone', '--depth=' . $self->{options}{git_depth},
  132. '--quiet', '--bare', 'file://' . abs_path($dir), $clone_dir);
  133. $? && subprocerr('git clone');
  134. chdir($clone_dir) ||
  135. syserr(_g("unable to chdir to `%s'"), $clone_dir);
  136. $shallowfile = "$basenamerev.gitshallow";
  137. system('cp', '-f', 'shallow', "$old_cwd/$shallowfile");
  138. $? && subprocerr('cp shallow');
  139. }
  140. # Create the git bundle.
  141. my $bundlefile = "$basenamerev.git";
  142. my @bundle_arg=$self->{options}{git_ref} ?
  143. (@{$self->{options}{git_ref}}) : '--all';
  144. info(_g('bundling: %s'), join(' ', @bundle_arg));
  145. system('git', 'bundle', 'create', "$old_cwd/$bundlefile",
  146. @bundle_arg,
  147. 'HEAD', # ensure HEAD is included no matter what
  148. '--', # avoids ambiguity error when referring to eg, a debian branch
  149. );
  150. $? && subprocerr('git bundle');
  151. chdir($old_cwd) ||
  152. syserr(_g("unable to chdir to `%s'"), $old_cwd);
  153. if (defined $tmp) {
  154. erasedir($tmp);
  155. pop @Dpkg::Exit::handlers;
  156. }
  157. $self->add_file($bundlefile);
  158. if (defined $shallowfile) {
  159. $self->add_file($shallowfile);
  160. }
  161. }
  162. sub do_extract {
  163. my ($self, $newdirectory) = @_;
  164. my $fields = $self->{fields};
  165. my $dscdir = $self->{basedir};
  166. my $basenamerev = $self->get_basename(1);
  167. my @files = $self->get_files();
  168. my ($bundle, $shallow);
  169. foreach my $file (@files) {
  170. if ($file =~ /^\Q$basenamerev\E\.git$/) {
  171. if (! defined $bundle) {
  172. $bundle = $file;
  173. } else {
  174. error(_g('format v3.0 (git) uses only one .git file'));
  175. }
  176. } elsif ($file =~ /^\Q$basenamerev\E\.gitshallow$/) {
  177. if (! defined $shallow) {
  178. $shallow = $file;
  179. } else {
  180. error(_g('format v3.0 (git) uses only one .gitshallow file'));
  181. }
  182. } else {
  183. error(_g('format v3.0 (git) unknown file: %s', $file));
  184. }
  185. }
  186. if (! defined $bundle) {
  187. error(_g('format v3.0 (git) expected %s'), "$basenamerev.git");
  188. }
  189. erasedir($newdirectory);
  190. # Extract git bundle.
  191. info(_g('cloning %s'), $bundle);
  192. system('git', 'clone', '--quiet', $dscdir . $bundle, $newdirectory);
  193. $? && subprocerr('git bundle');
  194. if (defined $shallow) {
  195. # Move shallow info file into place, so git does not
  196. # try to follow parents of shallow refs.
  197. info(_g('setting up shallow clone'));
  198. system('cp', '-f', $dscdir . $shallow, "$newdirectory/.git/shallow");
  199. $? && subprocerr('cp');
  200. }
  201. sanity_check($newdirectory);
  202. }
  203. 1;