git.pm 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 Dpkg;
  24. use Dpkg::Gettext;
  25. push (@INC, $dpkglibdir);
  26. require 'controllib.pl';
  27. # Remove variables from the environment that might cause git to do
  28. # something unexpected.
  29. delete $ENV{GIT_DIR};
  30. delete $ENV{GIT_INDEX_FILE};
  31. delete $ENV{GIT_OBJECT_DIRECTORY};
  32. delete $ENV{GIT_ALTERNATE_OBJECT_DIRECTORIES};
  33. delete $ENV{GIT_WORK_TREE};
  34. # Called before a tarball is created, to prepare the tar directory.
  35. sub prep_tar {
  36. my $srcdir=shift;
  37. my $tardir=shift;
  38. if (! -e "$srcdir/.git") {
  39. main::error(sprintf(_g("%s is not a git repository, but Format git was specified"), $srcdir));
  40. }
  41. # check for uncommitted files
  42. open(GIT_STATUS, "LANG=C cd $srcdir && git status |") ||
  43. main::subprocerr("cd $srcdir && git status");
  44. my $clean=0;
  45. my $status="";
  46. while (<GIT_STATUS>) {
  47. if (/^\Qnothing to commit (working directory clean)\E$/) {
  48. $clean=1;
  49. }
  50. else {
  51. $status.="git-status: $_";
  52. }
  53. }
  54. close GIT_STATUS;
  55. # git-status exits 1 if there are uncommitted changes or if
  56. # the repo is clean, and 0 if there are uncommitted changes
  57. # listed in the index.
  58. if ($? && $? >> 8 != 1) {
  59. main::subprocerr("cd $srcdir && git status");
  60. }
  61. if (! $clean) {
  62. print $status;
  63. main::error(_g("uncommitted changed in working directory"));
  64. }
  65. # garbage collect the repo
  66. system("cd $srcdir && git-gc --prune");
  67. $? && main::subprocerr("cd $srcdir && git-gc --prune");
  68. # TODO support for creating a shallow clone for those cases where
  69. # uploading the whole repo history is not desired
  70. system("cp -a $srcdir/.git $tardir");
  71. $? && main::subprocerr("cp -a $srcdir/.git $tardir");
  72. }
  73. # Called after a tarball is unpacked, to check out the working copy.
  74. sub post_unpack_tar {
  75. my $srcdir=shift;
  76. if (! -e "$srcdir/.git") {
  77. main::error(sprintf(_g("%s is not a git repository"), $srcdir));
  78. }
  79. # disable git hooks, as unpacking a source package should not
  80. # involve running code
  81. foreach my $hook (glob("$srcdir/.git/hooks/*")) {
  82. if (-x $hook) {
  83. main::warning(sprintf(_g("executable bit set on %s; clearing"), $hook));
  84. chmod(0644 &~ umask(), $hook) ||
  85. &syserr(sprintf(_g("unable to change permission of `%s'"), $hook));
  86. }
  87. }
  88. # XXX git-reset should be made to run in quiet mode here, but
  89. # lacks a good way to do it. Bug filed.
  90. system("cd $srcdir && git-reset --hard");
  91. $? && main::subprocerr("cd $srcdir && git-reset --hard");
  92. }
  93. 1