Git.pm 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 <https://www.gnu.org/licenses/>.
  19. package Dpkg::Source::Package::V3::Git;
  20. use strict;
  21. use warnings;
  22. our $VERSION = '0.02';
  23. use parent qw(Dpkg::Source::Package);
  24. use Cwd qw(abs_path getcwd);
  25. use File::Basename;
  26. use File::Temp qw(tempdir);
  27. use Dpkg::Gettext;
  28. use Dpkg::ErrorHandling;
  29. use Dpkg::Exit qw(push_exit_handler pop_exit_handler);
  30. use Dpkg::Source::Functions qw(erasedir);
  31. our $CURRENT_MINOR_VERSION = '0';
  32. # Remove variables from the environment that might cause git to do
  33. # something unexpected.
  34. delete $ENV{GIT_DIR};
  35. delete $ENV{GIT_INDEX_FILE};
  36. delete $ENV{GIT_OBJECT_DIRECTORY};
  37. delete $ENV{GIT_ALTERNATE_OBJECT_DIRECTORIES};
  38. delete $ENV{GIT_WORK_TREE};
  39. sub import {
  40. foreach my $dir (split(/:/, $ENV{PATH})) {
  41. if (-x "$dir/git") {
  42. return 1;
  43. }
  44. }
  45. error(g_('cannot unpack git-format source package because ' .
  46. 'git is not in the PATH'));
  47. }
  48. sub sanity_check {
  49. my $srcdir = shift;
  50. if (! -d "$srcdir/.git") {
  51. error(g_('source directory is not the top directory of a git ' .
  52. 'repository (%s/.git not present), but Format git was ' .
  53. 'specified'), $srcdir);
  54. }
  55. if (-s "$srcdir/.gitmodules") {
  56. error(g_('git repository %s uses submodules; this is not yet supported'),
  57. $srcdir);
  58. }
  59. return 1;
  60. }
  61. sub parse_cmdline_option {
  62. my ($self, $opt) = @_;
  63. return 1 if $self->SUPER::parse_cmdline_option($opt);
  64. if ($opt =~ /^--git-ref=(.*)$/) {
  65. push @{$self->{options}{git_ref}}, $1;
  66. return 1;
  67. } elsif ($opt =~ /^--git-depth=(\d+)$/) {
  68. $self->{options}{git_depth} = $1;
  69. return 1;
  70. }
  71. return 0;
  72. }
  73. sub can_build {
  74. my ($self, $dir) = @_;
  75. return (0, g_("doesn't contain a git repository")) unless -d "$dir/.git";
  76. return 1;
  77. }
  78. sub do_build {
  79. my ($self, $dir) = @_;
  80. my $diff_ignore_regex = $self->{options}{diff_ignore_regex};
  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) or 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) or subprocerr('git ls-files');
  102. my @files;
  103. {
  104. local $_;
  105. local $/ = "\0";
  106. while (<$git_ls_files_fh>) {
  107. chomp;
  108. if (! length $diff_ignore_regex ||
  109. ! m/$diff_ignore_regex/o) {
  110. push @files, $_;
  111. }
  112. }
  113. }
  114. close($git_ls_files_fh) or syserr(g_('git ls-files exited nonzero'));
  115. if (@files) {
  116. error(g_('uncommitted, not-ignored changes in working directory: %s'),
  117. join(' ', @files));
  118. }
  119. # If a depth was specified, need to create a shallow clone and
  120. # bundle that.
  121. my $tmp;
  122. my $shallowfile;
  123. if ($self->{options}{git_depth}) {
  124. chdir($old_cwd) or syserr(g_("unable to chdir to `%s'"), $old_cwd);
  125. $tmp = tempdir("$dirname.git.XXXXXX", DIR => $updir);
  126. push_exit_handler(sub { erasedir($tmp) });
  127. my $clone_dir = "$tmp/repo.git";
  128. # file:// is needed to avoid local cloning, which does not
  129. # create a shallow clone.
  130. info(g_('creating shallow clone with depth %s'),
  131. $self->{options}{git_depth});
  132. system('git', 'clone', '--depth=' . $self->{options}{git_depth},
  133. '--quiet', '--bare', 'file://' . abs_path($dir), $clone_dir);
  134. subprocerr('git clone') if $?;
  135. chdir($clone_dir)
  136. or syserr(g_("unable to chdir to `%s'"), $clone_dir);
  137. $shallowfile = "$basenamerev.gitshallow";
  138. system('cp', '-f', 'shallow', "$old_cwd/$shallowfile");
  139. subprocerr('cp shallow') if $?;
  140. }
  141. # Create the git bundle.
  142. my $bundlefile = "$basenamerev.git";
  143. my @bundle_arg=$self->{options}{git_ref} ?
  144. (@{$self->{options}{git_ref}}) : '--all';
  145. info(g_('bundling: %s'), join(' ', @bundle_arg));
  146. system('git', 'bundle', 'create', "$old_cwd/$bundlefile",
  147. @bundle_arg,
  148. 'HEAD', # ensure HEAD is included no matter what
  149. '--', # avoids ambiguity error when referring to eg, a debian branch
  150. );
  151. subprocerr('git bundle') if $?;
  152. chdir($old_cwd) or syserr(g_("unable to chdir to `%s'"), $old_cwd);
  153. if (defined $tmp) {
  154. erasedir($tmp);
  155. pop_exit_handler();
  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') if $?;
  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') if $?;
  200. }
  201. sanity_check($newdirectory);
  202. }
  203. 1;