git.pm 2.9 KB

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