| 1 | package App::gh::Command::Commit; | ||
| 2 | use warnings; | ||
| 3 | use strict; | ||
| 4 | use utf8; | ||
| 5 | |||
| 6 | use base qw(App::gh::Command); | ||
| 7 | use File::Path qw(mkpath); | ||
| 8 | use App::gh::Utils; | ||
| 9 | use File::Temp; | ||
| 10 | use Term::ReadLine; | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | App::gh::Command::Commit - quick commit process (show status, diff, then commitCloneall) | ||
| 15 | |||
| 16 | =head1 DESCRIPTION | ||
| 17 | |||
| 18 | gh commit command provides a normal commit process. (show status, diff, then commit in few | ||
| 19 | strokes) | ||
| 20 | |||
| 21 | =head1 USAGE | ||
| 22 | |||
| 23 | |||
| 24 | $ gh commit | ||
| 25 | |||
| 26 | =cut | ||
| 27 | |||
| 28 | |||
| 29 | sub options { | ||
| 30 | ( | ||
| 31 | 'e|editor' => 'with_editor', | ||
| 32 | ) | ||
| 33 | } | ||
| 34 | |||
| 35 | sub run { | ||
| 36 | my ($self) = @_; | ||
| 37 | |||
| 38 | die "Nothing to commit" unless qx(git diff); | ||
| 39 | |||
| 40 | print "=========== Current Change Status ===========\n"; | ||
| 41 | system( qq(git status) ); | ||
| 42 | |||
| 43 | print "\n"; | ||
| 44 | print "=========== GH Commit Interface ===========\n"; | ||
| 45 | print "diff(d). status(s). status with untracked files (ss).\n"; | ||
| 46 | print "commit(c). quit(q).\n"; | ||
| 47 | |||
| 48 | my $term = Term::ReadLine->new('Simple'); | ||
| 49 | my $prompt = "Diff(d), Status(s/ss), Commit(c), Quit(q): "; | ||
| 50 | |||
| 51 | my $OUT = $term->OUT || \*STDOUT; | ||
| 52 | my $res; | ||
| 53 | while ( defined ($res = $term->readline($prompt)) ) { | ||
| 54 | chomp($res); | ||
| 55 | |||
| 56 | $res =~ s{^\s*(\S*)\s*$}{$1}; | ||
| 57 | |||
| 58 | if ( $res =~ /^d/ ) { | ||
| 59 | system( qq(git diff --color=auto) ); | ||
| 60 | } | ||
| 61 | elsif( $res =~ /^ss$/ ) { | ||
| 62 | system( qq(git status -unormal) ); | ||
| 63 | } | ||
| 64 | elsif( $res =~ /^s$/ ) { | ||
| 65 | system( qq(git status -uno) ); | ||
| 66 | } | ||
| 67 | elsif( $res =~ /^c$/ ) { | ||
| 68 | # read commit messages | ||
| 69 | print "Please enter commit messages below (empty line to finish):\n"; | ||
| 70 | my @lines = qw(); | ||
| 71 | my $line; | ||
| 72 | my $cnt = 0; | ||
| 73 | while( defined ($line = $term->readline(">> ")) ) { | ||
| 74 | chomp $line; | ||
| 75 | $cnt++ unless $line; | ||
| 76 | last if $cnt > 1; | ||
| 77 | push @lines , $line; | ||
| 78 | } | ||
| 79 | |||
| 80 | last unless grep { $_ } @lines; # skip commit if those lines are empty. | ||
| 81 | |||
| 82 | # create a tempfile and put messages into the temp file. | ||
| 83 | use File::Temp qw(tempfile); | ||
| 84 | my ($fh, $filename) = tempfile( ".gh_commit_XXXX" , SUFFIX => '.msg'); | ||
| 85 | |||
| 86 | # put messages into history. | ||
| 87 | $term->addhistory( join "\n" , @lines ); | ||
| 88 | |||
| 89 | print $fh join "\n", @lines; | ||
| 90 | |||
| 91 | system( "git commit -a -F " . $filename ) == 0 or | ||
| 92 | die "Commit failed. \nCommit message is saved to $filename.\n" | ||
| 93 | ."You can use 'git commit -F $filename' to commit again"; | ||
| 94 | |||
| 95 | unlink( $filename ); | ||
| 96 | last; | ||
| 97 | } | ||
| 98 | elsif( $res =~ /^q/ ) { | ||
| 99 | print "Skipped\n"; | ||
| 100 | last; | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | } | ||
| 105 | |||
| 106 | 1; |