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