sg 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Getopt::Long qw(GetOptions);
  5. use Log::Log4perl qw(:easy);
  6. # TODO: This needs to be scoped properly
  7. use lib "/usr/local/lib";
  8. use SimplyGit::Shellex qw(shellex findBin);
  9. use SimplyGit::Git qw(readConfig getStatus returnState addFiles commitChanges pushChanges stashAndReset);
  10. if ( ! -d ".git" ) {
  11. print "Not a git dir, exiting...\n";
  12. exit 1;
  13. }
  14. sub initSG($) {
  15. my $sgDir = shift;
  16. my $pwdCmd = findBin("pwd");
  17. my $pwd = shellex($pwdCmd);
  18. chomp $pwd;
  19. my $path = $pwd . "/" . $sgDir;
  20. my $logFile = $pwd . "/" . $sgDir . "/" . "sgLog.txt";
  21. if ( ! -d $path ) {
  22. print "Creating $path\n";
  23. shellex("mkdir $path");
  24. }
  25. if ( ! -f $logFile ) {
  26. print "Creating $logFile\n";
  27. shellex("touch $logFile");
  28. }
  29. return ( $path, $logFile );
  30. }
  31. my ( $sgPath, $sgLogFile ) = initSG(".sg");
  32. sub getLogName { return $sgLogFile; };
  33. my $log_conf = q(
  34. log4perl.rootLogger = ERROR, LOG1
  35. log4perl.appender.LOG1 = Log::Log4perl::Appender::File
  36. log4perl.appender.LOG1.filename = sub { getLogName(); }
  37. log4perl.appender.LOG1.mode = append
  38. log4perl.appender.LOG1.layout = Log::Log4perl::Layout::PatternLayout
  39. log4perl.appender.LOG1.layout.ConversionPattern = %d %p %m %n
  40. );
  41. Log::Log4perl::init(\$log_conf);
  42. my $logger = get_logger();
  43. my $gitCmd = findBin("git",$logger);
  44. my %args;
  45. GetOptions(
  46. \%args,
  47. 'push-all',
  48. 'interactive',
  49. 'view',
  50. 'reset-from-master',
  51. 'branch-from-master',
  52. 'commit-msg=s',
  53. );
  54. sub printHelp {
  55. my $help = <<EOF
  56. simply-git
  57. Usage:
  58. --view
  59. Display git status of files and other information
  60. --push-all [--commit-msg]
  61. Push all untracked and modified files
  62. * (can be used with interactive mode)
  63. * can provide a commit msg with --commit-msg (otherwise a generic will be provided)
  64. --interactive
  65. Enable interactive mode with supported opts
  66. --reset-from-master
  67. Reset all current changes so that the file tree matches upstream/master
  68. --branch-from-master
  69. Create a new clean branch from upstream/master
  70. EOF
  71. ;
  72. print "$help\n";
  73. }
  74. if ( scalar keys %args < 1 ) {
  75. printHelp();
  76. }
  77. # This functionality is mainly here to test building out Git.pm, and doesn't really offer anything that regular git doesn't
  78. if ( defined $args{'view'} ) {
  79. my ( $untrackedRef, $modifiedRef, $addedRef, $deletedRef ) = returnState($logger);
  80. my $refs = shellex("$gitCmd show-ref",$logger);
  81. my $branch = shellex("$gitCmd show-branch",$logger);
  82. print "On [branch] @ commit: $branch";
  83. print "$refs\n";
  84. my $swpWarning = "\t# Likely a Vi .swp file";
  85. my $untrackedTotal = scalar @$untrackedRef;
  86. print "* $untrackedTotal untracked file(s):\n";
  87. foreach my $file ( @$untrackedRef ) {
  88. if ( $file =~ m/.swp/ ) {
  89. print "\t$file $swpWarning\n";
  90. } else {
  91. print "\t$file\n";
  92. }
  93. }
  94. my $modifiedTotal = scalar @$modifiedRef;
  95. print "* $modifiedTotal modified file(s):\n";
  96. foreach my $file ( @$modifiedRef ) {
  97. if ( $file =~ m/.swp/ ) {
  98. print "\t$file $swpWarning\n";
  99. } else {
  100. print "\t$file\n";
  101. }
  102. }
  103. my $commitTotal = scalar @$addedRef;
  104. print "* $commitTotal file(s) added to commit:\n";
  105. foreach my $file ( @$addedRef ) {
  106. if ( $file =~ m/.swp/ ) {
  107. print "\t$file $swpWarning\n";
  108. } else {
  109. print "\t$file\n";
  110. }
  111. }
  112. my $deletedTotal = scalar @$deletedRef;
  113. print "* $deletedTotal file(s) to be deleted from commit:\n";
  114. foreach my $file ( @$deletedRef ) {
  115. if ( $file =~ m/.swp/ ) {
  116. print "\t$file $swpWarning\n";
  117. } else {
  118. print "\t$file\n";
  119. }
  120. }
  121. }
  122. # This functionality is mainly here to test building out Git.pm, and doesn't really offer anything that regular git doesn't
  123. if ( defined $args{'push-all'} ) {
  124. my ( $untrackedRef, $modifiedRef ) = returnState($logger);
  125. my @files;
  126. push(@files,@$untrackedRef); push(@files,@$modifiedRef);
  127. my @filesToCommit;
  128. if ( defined $args{'interactive'} ) {
  129. foreach my $file ( @files ) {
  130. print "Add $file to commit (y/n): ";
  131. my $input = <STDIN>;
  132. chomp $input;
  133. if ( $input =~ m/^Y|^y/ ) {
  134. push(@filesToCommit,$file);
  135. } else {
  136. next;
  137. }
  138. }
  139. } else {
  140. @filesToCommit = @files;
  141. }
  142. print "Commiting the following files:\n";
  143. foreach my $file ( @filesToCommit ) {
  144. print "\t$file\n";
  145. }
  146. if ( defined $args{'interactive'} ) {
  147. print "Does this look correct (y/n) : ";
  148. my $input = <STDIN>;
  149. chomp $input;
  150. if ( $input !~ m/^Y|^y/ ) {
  151. print "Canceling...\n";
  152. exit 1;
  153. }
  154. }
  155. addFiles(\@filesToCommit,$logger);
  156. if ( defined $args{'interactive'} && ! defined $args{'commit-msg'}) {
  157. print "Enter a commit message: ";
  158. my $input = <STDIN>;
  159. chomp $input;
  160. commitChanges($input,$logger);
  161. } elsif ( defined $args{'commit-msg'} ) {
  162. my $commitMsg = "$args{'commit-msg'}";
  163. commitChanges($commitMsg,$logger);
  164. } else {
  165. my $epoch = time();
  166. my $commitMsg = "Generic Commit at $epoch";
  167. commitChanges($commitMsg,$logger);
  168. }
  169. if ( defined $args{'interactive'} ) {
  170. print "Push changes? (y/n): ";
  171. my $input = <STDIN>;
  172. chomp $input;
  173. if ( $input !~ m/^Y|^y/ ) {
  174. # TODO: Unstage changes?
  175. print "Canceling...\n";
  176. exit 1;
  177. }
  178. my $gitOutput = pushChanges($logger);
  179. print "Git returned:\n$gitOutput";
  180. } else {
  181. pushChanges($logger);
  182. my $gitOutput = pushChanges($logger);
  183. print "Git returned:\n$gitOutput";
  184. }
  185. }
  186. if ( defined $args{'reset-from-master'} ) {
  187. stashAndReset($logger);
  188. }