sg 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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);
  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. );
  53. sub printHelp {
  54. my $help = <<EOF
  55. simply-git
  56. Usage:
  57. --view
  58. Display git status of files and other information
  59. --push-all
  60. Push all untracked and modified files (can be used with interactive mode)
  61. --interactive
  62. Enable interactive mode with supported opts
  63. --reset-from-master
  64. Reset all current changes so that the file tree matches upstream/master
  65. --branch-from-master
  66. Create a new clean branch from upstream/master
  67. EOF
  68. ;
  69. print "$help\n";
  70. }
  71. if ( scalar keys %args < 1 ) {
  72. printHelp();
  73. }
  74. # This functionality is mainly here to test building out Git.pm, and doesn't really offer anything that regular git doesn't
  75. if ( defined $args{'view'} ) {
  76. my ( $untrackedRef, $modifiedRef, $addedRef, $deletedRef ) = returnState($logger);
  77. my $refs = shellex("$gitCmd show-ref",$logger);
  78. my $branch = shellex("$gitCmd show-branch",$logger);
  79. print "On [branch] @ commit: $branch";
  80. print "$refs\n";
  81. my $swpWarning = "\t# Likely a Vi .swp file";
  82. my $untrackedTotal = scalar @$untrackedRef;
  83. print "* $untrackedTotal untracked file(s):\n";
  84. foreach my $file ( @$untrackedRef ) {
  85. if ( $file =~ m/.swp/ ) {
  86. print "\t$file $swpWarning\n";
  87. } else {
  88. print "\t$file\n";
  89. }
  90. }
  91. my $modifiedTotal = scalar @$modifiedRef;
  92. print "* $modifiedTotal modified file(s):\n";
  93. foreach my $file ( @$modifiedRef ) {
  94. if ( $file =~ m/.swp/ ) {
  95. print "\t$file $swpWarning\n";
  96. } else {
  97. print "\t$file\n";
  98. }
  99. }
  100. my $commitTotal = scalar @$addedRef;
  101. print "* $commitTotal file(s) added to commit:\n";
  102. foreach my $file ( @$addedRef ) {
  103. if ( $file =~ m/.swp/ ) {
  104. print "\t$file $swpWarning\n";
  105. } else {
  106. print "\t$file\n";
  107. }
  108. }
  109. my $deletedTotal = scalar @$deletedRef;
  110. print "* $deletedTotal file(s) to be deleted from commit:\n";
  111. foreach my $file ( @$deletedRef ) {
  112. if ( $file =~ m/.swp/ ) {
  113. print "\t$file $swpWarning\n";
  114. } else {
  115. print "\t$file\n";
  116. }
  117. }
  118. }
  119. # This functionality is mainly here to test building out Git.pm, and doesn't really offer anything that regular git doesn't
  120. if ( defined $args{'push-all'} ) {
  121. my ( $untrackedRef, $modifiedRef ) = returnState($logger);
  122. my @files;
  123. push(@files,@$untrackedRef); push(@files,@$modifiedRef);
  124. my @filesToCommit;
  125. if ( defined $args{'interactive'} ) {
  126. foreach my $file ( @files ) {
  127. print "Add $file to commit (y/n): ";
  128. my $input = <STDIN>;
  129. chomp $input;
  130. if ( $input =~ m/^Y|^y/ ) {
  131. push(@filesToCommit,$file);
  132. } else {
  133. next;
  134. }
  135. }
  136. } else {
  137. @filesToCommit = @files;
  138. }
  139. print "Commiting the following files:\n";
  140. foreach my $file ( @filesToCommit ) {
  141. print "\t$file\n";
  142. }
  143. if ( defined $args{'interactive'} ) {
  144. print "Does this look correct (y/n) : ";
  145. my $input = <STDIN>;
  146. chomp $input;
  147. if ( $input !~ m/^Y|^y/ ) {
  148. print "Canceling...\n";
  149. exit 1;
  150. }
  151. }
  152. addFiles(\@filesToCommit,$logger);
  153. if ( defined $args{'interactive'} ) {
  154. print "Enter a commit message: ";
  155. my $input = <STDIN>;
  156. chomp $input;
  157. commitChanges($input,$logger);
  158. } else {
  159. my $epoch = time();
  160. my $commitMsg = "Generic Commit at $epoch";
  161. commitChanges($commitMsg,$logger);
  162. }
  163. if ( defined $args{'interactive'} ) {
  164. print "Push changes? (y/n): ";
  165. my $input = <STDIN>;
  166. chomp $input;
  167. if ( $input !~ m/^Y|^y/ ) {
  168. # TODO: Unstage changes?
  169. print "Canceling...\n";
  170. exit 1;
  171. }
  172. my $gitOutput = pushChanges($logger);
  173. print "Git returned:\n$gitOutput";
  174. } else {
  175. pushChanges($logger);
  176. my $gitOutput = pushChanges($logger);
  177. print "Git returned:\n$gitOutput";
  178. }
  179. }