Git.pm 7.2 KB

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