git.pm 2.9 KB

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