Git.pm 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 Cwd qw(abs_path getcwd);
  24. use File::Basename;
  25. use File::Temp qw(tempdir);
  26. use Dpkg::Gettext;
  27. use Dpkg::ErrorHandling;
  28. use Dpkg::Exit qw(push_exit_handler pop_exit_handler);
  29. use Dpkg::Source::Functions qw(erasedir);
  30. use parent qw(Dpkg::Source::Package);
  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. my @module_cmdline = (
  62. {
  63. name => '--git-ref=<ref>',
  64. help => N_('specify a git <ref> to include in the git bundle'),
  65. when => 'build',
  66. }, {
  67. name => '--git-depth=<number>',
  68. help => N_('create a shallow clone with <number> depth'),
  69. when => 'build',
  70. }
  71. );
  72. sub describe_cmdline_options {
  73. my $self = shift;
  74. my @cmdline = ( $self->SUPER::describe_cmdline_options(), @module_cmdline );
  75. return @cmdline;
  76. }
  77. sub parse_cmdline_option {
  78. my ($self, $opt) = @_;
  79. return 1 if $self->SUPER::parse_cmdline_option($opt);
  80. if ($opt =~ /^--git-ref=(.*)$/) {
  81. push @{$self->{options}{git_ref}}, $1;
  82. return 1;
  83. } elsif ($opt =~ /^--git-depth=(\d+)$/) {
  84. $self->{options}{git_depth} = $1;
  85. return 1;
  86. }
  87. return 0;
  88. }
  89. sub can_build {
  90. my ($self, $dir) = @_;
  91. return (0, g_("doesn't contain a git repository")) unless -d "$dir/.git";
  92. return 1;
  93. }
  94. sub do_build {
  95. my ($self, $dir) = @_;
  96. my $diff_ignore_regex = $self->{options}{diff_ignore_regex};
  97. $dir =~ s{/+$}{}; # Strip trailing /
  98. my ($dirname, $updir) = fileparse($dir);
  99. my $basenamerev = $self->get_basename(1);
  100. _sanity_check($dir);
  101. my $old_cwd = getcwd();
  102. chdir $dir or syserr(g_("unable to chdir to '%s'"), $dir);
  103. # Check for uncommitted files.
  104. # To support dpkg-source -i, get a list of files
  105. # equivalent to the ones git status finds, and remove any
  106. # ignored files from it.
  107. my @ignores = '--exclude-per-directory=.gitignore';
  108. my $core_excludesfile = qx(git config --get core.excludesfile);
  109. chomp $core_excludesfile;
  110. if (length $core_excludesfile && -e $core_excludesfile) {
  111. push @ignores, "--exclude-from=$core_excludesfile";
  112. }
  113. if (-e '.git/info/exclude') {
  114. push @ignores, '--exclude-from=.git/info/exclude';
  115. }
  116. open(my $git_ls_files_fh, '-|', 'git', 'ls-files', '--modified', '--deleted',
  117. '-z', '--others', @ignores) or subprocerr('git ls-files');
  118. my @files;
  119. {
  120. local $_;
  121. local $/ = "\0";
  122. while (<$git_ls_files_fh>) {
  123. chomp;
  124. if (! length $diff_ignore_regex ||
  125. ! m/$diff_ignore_regex/o) {
  126. push @files, $_;
  127. }
  128. }
  129. }
  130. close($git_ls_files_fh) or syserr(g_('git ls-files exited nonzero'));
  131. if (@files) {
  132. error(g_('uncommitted, not-ignored changes in working directory: %s'),
  133. join(' ', @files));
  134. }
  135. # If a depth was specified, need to create a shallow clone and
  136. # bundle that.
  137. my $tmp;
  138. my $shallowfile;
  139. if ($self->{options}{git_depth}) {
  140. chdir $old_cwd or syserr(g_("unable to chdir to '%s'"), $old_cwd);
  141. $tmp = tempdir("$dirname.git.XXXXXX", DIR => $updir);
  142. push_exit_handler(sub { erasedir($tmp) });
  143. my $clone_dir = "$tmp/repo.git";
  144. # file:// is needed to avoid local cloning, which does not
  145. # create a shallow clone.
  146. info(g_('creating shallow clone with depth %s'),
  147. $self->{options}{git_depth});
  148. system('git', 'clone', '--depth=' . $self->{options}{git_depth},
  149. '--quiet', '--bare', 'file://' . abs_path($dir), $clone_dir);
  150. subprocerr('git clone') if $?;
  151. chdir($clone_dir)
  152. or syserr(g_("unable to chdir to '%s'"), $clone_dir);
  153. $shallowfile = "$basenamerev.gitshallow";
  154. system('cp', '-f', 'shallow', "$old_cwd/$shallowfile");
  155. subprocerr('cp shallow') if $?;
  156. }
  157. # Create the git bundle.
  158. my $bundlefile = "$basenamerev.git";
  159. my @bundle_arg=$self->{options}{git_ref} ?
  160. (@{$self->{options}{git_ref}}) : '--all';
  161. info(g_('bundling: %s'), join(' ', @bundle_arg));
  162. system('git', 'bundle', 'create', "$old_cwd/$bundlefile",
  163. @bundle_arg,
  164. 'HEAD', # ensure HEAD is included no matter what
  165. '--', # avoids ambiguity error when referring to eg, a debian branch
  166. );
  167. subprocerr('git bundle') if $?;
  168. chdir $old_cwd or syserr(g_("unable to chdir to '%s'"), $old_cwd);
  169. if (defined $tmp) {
  170. erasedir($tmp);
  171. pop_exit_handler();
  172. }
  173. $self->add_file($bundlefile);
  174. if (defined $shallowfile) {
  175. $self->add_file($shallowfile);
  176. }
  177. }
  178. sub do_extract {
  179. my ($self, $newdirectory) = @_;
  180. my $fields = $self->{fields};
  181. my $dscdir = $self->{basedir};
  182. my $basenamerev = $self->get_basename(1);
  183. my @files = $self->get_files();
  184. my ($bundle, $shallow);
  185. foreach my $file (@files) {
  186. if ($file =~ /^\Q$basenamerev\E\.git$/) {
  187. if (! defined $bundle) {
  188. $bundle = $file;
  189. } else {
  190. error(g_('format v3.0 (git) uses only one .git file'));
  191. }
  192. } elsif ($file =~ /^\Q$basenamerev\E\.gitshallow$/) {
  193. if (! defined $shallow) {
  194. $shallow = $file;
  195. } else {
  196. error(g_('format v3.0 (git) uses only one .gitshallow file'));
  197. }
  198. } else {
  199. error(g_('format v3.0 (git) unknown file: %s', $file));
  200. }
  201. }
  202. if (! defined $bundle) {
  203. error(g_('format v3.0 (git) expected %s'), "$basenamerev.git");
  204. }
  205. if ($self->{options}{no_overwrite_dir} and -e $newdirectory) {
  206. error(g_('unpack target exists: %s'), $newdirectory);
  207. } else {
  208. erasedir($newdirectory);
  209. }
  210. # Extract git bundle.
  211. info(g_('cloning %s'), $bundle);
  212. system('git', 'clone', '--quiet', $dscdir . $bundle, $newdirectory);
  213. subprocerr('git bundle') if $?;
  214. if (defined $shallow) {
  215. # Move shallow info file into place, so git does not
  216. # try to follow parents of shallow refs.
  217. info(g_('setting up shallow clone'));
  218. system('cp', '-f', $dscdir . $shallow, "$newdirectory/.git/shallow");
  219. subprocerr('cp') if $?;
  220. }
  221. _sanity_check($newdirectory);
  222. }
  223. 1;