sg 4.5 KB

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