sg 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 resetFromUpstream);
  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. 'reset-from-upstream',
  52. 'branch-from-master',
  53. 'commit-msg=s',
  54. 'dump-config',
  55. );
  56. sub printHelp {
  57. my $help = <<EOF
  58. simply-git
  59. Usage:
  60. --view
  61. Display git status of files and other information
  62. --dump-config
  63. Dump .git/config to STDOUT. Not really useful but exposed for testing of reading config into internal data structure
  64. --push-all [--commit-msg]
  65. Push all untracked and modified files
  66. * (can be used with interactive mode)
  67. * can provide a commit msg with --commit-msg (otherwise a generic will be provided)
  68. --interactive
  69. Enable interactive mode with supported opts
  70. --reset-from-master
  71. Reset all current changes so that the file tree matches upstream/master
  72. --branch-from-master
  73. Create a new clean branch from upstream/master
  74. EOF
  75. ;
  76. print "$help\n";
  77. }
  78. if ( scalar keys %args < 1 ) {
  79. printHelp();
  80. }
  81. # This functionality is mainly here to test building out Git.pm, and doesn't really offer anything that regular git doesn't
  82. if ( defined $args{'view'} ) {
  83. my ( $untrackedRef, $modifiedRef, $addedRef, $deletedRef ) = returnState($logger);
  84. my $refs = shellex("$gitCmd show-ref",$logger);
  85. my $branch = shellex("$gitCmd show-branch",$logger);
  86. print "On [branch] @ commit: $branch";
  87. print "$refs\n";
  88. my $swpWarning = "\t# Likely a Vi .swp file";
  89. my $untrackedTotal = scalar @$untrackedRef;
  90. print "* $untrackedTotal untracked file(s):\n";
  91. foreach my $file ( @$untrackedRef ) {
  92. if ( $file =~ m/.swp/ ) {
  93. print "\t$file $swpWarning\n";
  94. } else {
  95. print "\t$file\n";
  96. }
  97. }
  98. my $modifiedTotal = scalar @$modifiedRef;
  99. print "* $modifiedTotal modified file(s):\n";
  100. foreach my $file ( @$modifiedRef ) {
  101. if ( $file =~ m/.swp/ ) {
  102. print "\t$file $swpWarning\n";
  103. } else {
  104. print "\t$file\n";
  105. }
  106. }
  107. my $commitTotal = scalar @$addedRef;
  108. print "* $commitTotal file(s) added to commit:\n";
  109. foreach my $file ( @$addedRef ) {
  110. if ( $file =~ m/.swp/ ) {
  111. print "\t$file $swpWarning\n";
  112. } else {
  113. print "\t$file\n";
  114. }
  115. }
  116. my $deletedTotal = scalar @$deletedRef;
  117. print "* $deletedTotal file(s) to be deleted from commit:\n";
  118. foreach my $file ( @$deletedRef ) {
  119. if ( $file =~ m/.swp/ ) {
  120. print "\t$file $swpWarning\n";
  121. } else {
  122. print "\t$file\n";
  123. }
  124. }
  125. }
  126. ###
  127. # TODO: This args processing is dangerous and incomplete, need to write an args processor function
  128. ###
  129. # This functionality is mainly here to test building out Git.pm, and doesn't really offer anything that regular git doesn't
  130. if ( defined $args{'push-all'} ) {
  131. my ( $untrackedRef, $modifiedRef ) = returnState($logger);
  132. my @files;
  133. push(@files,@$untrackedRef); push(@files,@$modifiedRef);
  134. my @filesToCommit;
  135. if ( defined $args{'interactive'} ) {
  136. foreach my $file ( @files ) {
  137. print "Add $file to commit (y/n): ";
  138. my $input = <STDIN>;
  139. chomp $input;
  140. if ( $input =~ m/^Y|^y/ ) {
  141. push(@filesToCommit,$file);
  142. } else {
  143. next;
  144. }
  145. }
  146. } else {
  147. @filesToCommit = @files;
  148. }
  149. print "Commiting the following files:\n";
  150. foreach my $file ( @filesToCommit ) {
  151. print "\t$file\n";
  152. }
  153. if ( defined $args{'interactive'} ) {
  154. print "Does this look correct (y/n) : ";
  155. my $input = <STDIN>;
  156. chomp $input;
  157. if ( $input !~ m/^Y|^y/ ) {
  158. print "Canceling...\n";
  159. exit 1;
  160. }
  161. }
  162. addFiles(\@filesToCommit,$logger);
  163. if ( defined $args{'interactive'} && ! defined $args{'commit-msg'}) {
  164. print "Enter a commit message: ";
  165. my $input = <STDIN>;
  166. chomp $input;
  167. commitChanges($input,$logger);
  168. } elsif ( defined $args{'commit-msg'} ) {
  169. my $commitMsg = "$args{'commit-msg'}";
  170. commitChanges($commitMsg,$logger);
  171. } else {
  172. my $epoch = time();
  173. my $commitMsg = "Generic Commit at $epoch";
  174. commitChanges($commitMsg,$logger);
  175. }
  176. if ( defined $args{'interactive'} ) {
  177. print "Push changes? (y/n): ";
  178. my $input = <STDIN>;
  179. chomp $input;
  180. if ( $input !~ m/^Y|^y/ ) {
  181. # TODO: Unstage changes?
  182. print "Canceling...\n";
  183. exit 1;
  184. }
  185. my $gitOutput = pushChanges($logger);
  186. print "Git returned:\n$gitOutput";
  187. } else {
  188. pushChanges($logger);
  189. my $gitOutput = pushChanges($logger);
  190. print "Git returned:\n$gitOutput";
  191. }
  192. }
  193. if ( defined $args{'reset-from-master'} ) {
  194. stashAndReset($logger);
  195. }
  196. if ( defined $args{'reset-from-upstream'} ) {
  197. print "This does nothing right now\n";
  198. resetFromUpstream($logger);
  199. }
  200. if ( defined $args{'dump-config'} ) {
  201. my %configHash = readConfig(".",$logger);
  202. foreach my $key ( keys %configHash ) {
  203. my $hRef = $configHash{$key};
  204. print "[$key]\n";
  205. foreach my $ckey ( keys %$hRef ) {
  206. print "\t$ckey = ${$hRef}{$ckey}\n";
  207. }
  208. }
  209. }