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