git-status.pl 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # Do some git-status checking for the current dir and (optionally)
  2. # the patches dir.
  3. sub check_git_state
  4. {
  5. my($master_branch, $fatal_unless_clean, $check_patches_dir) = @_;
  6. my($cur_branch) = check_git_status($fatal_unless_clean);
  7. if ($cur_branch ne $master_branch) {
  8. print "The checkout is not on the $master_branch branch.\n";
  9. exit 1 if $master_branch ne 'master';
  10. print "Do you want me to continue with --branch=$cur_branch? [n] ";
  11. $_ = <STDIN>;
  12. exit 1 unless /^y/i;
  13. $_[0] = $master_branch = $cur_branch; # Updates caller's $master_branch too.
  14. }
  15. if ($check_patches_dir && -d 'patches/.git') {
  16. ($cur_branch) = check_git_status($fatal_unless_clean, 'patches');
  17. if ($cur_branch ne $master_branch) {
  18. print "The *patches* checkout is on branch $cur_branch, not branch $master_branch.\n";
  19. print "Do you want to change it to branch $master_branch? [n] ";
  20. $_ = <STDIN>;
  21. exit 1 unless /^y/i;
  22. system "cd patches && git checkout '$master_branch'";
  23. }
  24. }
  25. }
  26. sub check_git_status
  27. {
  28. my($fatal_unless_clean, $subdir) = @_;
  29. $subdir = '.' unless defined $subdir;
  30. my $status = `cd '$subdir' && git status`;
  31. my $is_clean = $status =~ /\nnothing to commit \(working directory clean\)/;
  32. my($cur_branch) = $status =~ /^# On branch (.+)\n/;
  33. if ($fatal_unless_clean && !$is_clean) {
  34. if ($subdir eq '.') {
  35. $subdir = '';
  36. } else {
  37. $subdir = " *$subdir*";
  38. }
  39. die "The$subdir checkout is not clean:\n", $status;
  40. }
  41. ($cur_branch, $is_clean, $status);
  42. }
  43. 1;