sg 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 updateGitIgnore appendRepoUserConfig);
  10. # TODO: This should maybe be more robust?
  11. if ( ! -d ".git" ) {
  12. print "Not a git dir, exiting...\n";
  13. exit 1;
  14. }
  15. sub initSG($) {
  16. my $sgDir = shift;
  17. my $pwdCmd = findBin("pwd");
  18. my $pwd = shellex($pwdCmd);
  19. chomp $pwd;
  20. my $path = $pwd . "/" . $sgDir;
  21. my $logFile = $pwd . "/" . $sgDir . "/" . "sgLog.txt";
  22. if ( ! -d $path ) {
  23. print "Creating $path\n";
  24. shellex("mkdir $path");
  25. }
  26. if ( ! -f $logFile ) {
  27. print "Creating $logFile\n";
  28. shellex("touch $logFile");
  29. }
  30. return ( $path, $logFile );
  31. }
  32. my ( $sgPath, $sgLogFile ) = initSG(".sg");
  33. sub getLogName { return $sgLogFile; };
  34. my $log_conf = q(
  35. log4perl.rootLogger = ERROR, LOG1
  36. log4perl.appender.LOG1 = Log::Log4perl::Appender::File
  37. log4perl.appender.LOG1.filename = sub { getLogName(); }
  38. log4perl.appender.LOG1.mode = append
  39. log4perl.appender.LOG1.layout = Log::Log4perl::Layout::PatternLayout
  40. log4perl.appender.LOG1.layout.ConversionPattern = %d %p %m %n
  41. );
  42. Log::Log4perl::init(\$log_conf);
  43. my $logger = get_logger();
  44. my $gitCmd = findBin("git",$logger);
  45. # Create or append to .gitignore so that we don't push the log
  46. # Maybe this should be an opt?
  47. updateGitIgnore(".","/.sg",$logger);
  48. my %args;
  49. GetOptions(
  50. \%args,
  51. 'push-all',
  52. 'interactive',
  53. 'view',
  54. 'reset-from-master',
  55. 'reset-from-upstream',
  56. 'commit-msg=s',
  57. 'dump-config',
  58. 'configure-local-user',
  59. 'user=s',
  60. 'email=s',
  61. );
  62. sub printHelp {
  63. my $help = <<EOF
  64. simply-git
  65. Usage:
  66. --view
  67. Display git status of files and other information
  68. --dump-config
  69. Dump .git/config to STDOUT. Not really useful but exposed for testing of reading config into internal data structure
  70. --push-all [--commit-msg]
  71. Push all untracked and modified files
  72. * Can be used with interactive mode
  73. * Can provide a commit msg with --commit-msg (otherwise a generic will be provided)
  74. --interactive
  75. Enable interactive mode with supported opts
  76. --reset-from-master
  77. Reset all current changes so that the file tree matches upstream/master
  78. --reset-from-upstream
  79. If upstream is defined will reset local branch to match upstream ( does not push changes by default )
  80. --configure-local-user [--user,--email]
  81. Configure local git user
  82. * Can be used with interactive mode
  83. EOF
  84. ;
  85. print "$help\n";
  86. }
  87. if ( scalar keys %args < 1 ) {
  88. printHelp();
  89. }
  90. ###
  91. # TODO: This args processing is better and more predictable than it was, bit there is still
  92. # likely a lot of room for improvement...
  93. ###
  94. sub parseArgs {
  95. if ( defined $args{'view'} && scalar keys %args > 1 ) {
  96. print "Can't pass other args with --view\n";
  97. exit 1;
  98. }
  99. if ( defined $args{'dump-config'} && scalar keys %args > 1 ) {
  100. print "Can't pass other args with --dump-config\n";
  101. exit 1;
  102. }
  103. if ( defined $args{'reset-from-master'} && scalar keys %args > 1 ) {
  104. print "Can't pass other args with --reset-from-master\n";
  105. exit 1;
  106. }
  107. if ( defined $args{'push-all'} ) {
  108. foreach my $arg ( keys %args ) {
  109. if ( $arg eq "interactive" || $arg eq "commit-msg" || $arg eq "push-all" ) {
  110. next;
  111. } else {
  112. print "Can only pass --interactive and --commit-msg with --push-all\n";
  113. exit 1;
  114. }
  115. }
  116. }
  117. if ( defined $args{'configure-local-user'} ) {
  118. if ( scalar keys %args < 2 ) {
  119. print "Must pass either --interactive or --user AND --email to --configure-local-user\n";
  120. exit 1;
  121. }
  122. foreach my $arg ( keys %args ) {
  123. if ( $arg eq "interactive" || $arg eq "user" || $arg eq "email" || $arg eq "configure-local-user" ) {
  124. next;
  125. } else {
  126. print "Must/can only pass --interactive, OR --user AND --email with --configure-local-user\n";
  127. exit 1;
  128. }
  129. }
  130. if ( ! defined $args{'interactive'} && ! defined $args{'user'} || ! defined $args{'interactive'} && ! defined $args{'email'} ) {
  131. print "If not using --interactive with --configure-local-user, --user and --email MUST be defined\n";
  132. exit 1;
  133. }
  134. }
  135. if ( defined $args{'reset-from-upstream'} && scalar keys %args > 1 ) {
  136. print "Cannot call --reset-from-upstream with any other args\n";
  137. exit 1;
  138. }
  139. }
  140. parseArgs();
  141. #print "Args parsed successfully\n";
  142. #exit 0;
  143. # TODO: This sub could be more concise with a sub to print array refs
  144. if ( defined $args{'view'} ) {
  145. my ( $untrackedRef, $modifiedRef, $addedRef, $deletedRef ) = returnState($logger);
  146. my $refs = shellex("$gitCmd show-ref",$logger);
  147. my $branch = shellex("$gitCmd show-branch",$logger);
  148. my $name = shellex("$gitCmd config --get user.name",$logger);
  149. chomp $name;
  150. my $email = shellex("$gitCmd config --get user.email",$logger);
  151. chomp $email;
  152. print "Username: $name Email: $email\n";
  153. print "On [branch] @ commit: $branch";
  154. print "$refs\n";
  155. my $swpWarning = "\t# Likely a Vi .swp file";
  156. my $untrackedTotal = scalar @$untrackedRef;
  157. print "* $untrackedTotal untracked file(s):\n";
  158. foreach my $file ( @$untrackedRef ) {
  159. if ( $file =~ m/.swp/ ) {
  160. print "\t$file $swpWarning\n";
  161. } else {
  162. print "\t$file\n";
  163. }
  164. }
  165. my $modifiedTotal = scalar @$modifiedRef;
  166. print "* $modifiedTotal modified file(s):\n";
  167. foreach my $file ( @$modifiedRef ) {
  168. if ( $file =~ m/.swp/ ) {
  169. print "\t$file $swpWarning\n";
  170. } else {
  171. print "\t$file\n";
  172. }
  173. }
  174. my $commitTotal = scalar @$addedRef;
  175. print "* $commitTotal file(s) added to commit:\n";
  176. foreach my $file ( @$addedRef ) {
  177. if ( $file =~ m/.swp/ ) {
  178. print "\t$file $swpWarning\n";
  179. } else {
  180. print "\t$file\n";
  181. }
  182. }
  183. my $deletedTotal = scalar @$deletedRef;
  184. print "* $deletedTotal file(s) to be deleted from commit:\n";
  185. foreach my $file ( @$deletedRef ) {
  186. if ( $file =~ m/.swp/ ) {
  187. print "\t$file $swpWarning\n";
  188. } else {
  189. print "\t$file\n";
  190. }
  191. }
  192. }
  193. if ( defined $args{'push-all'} ) {
  194. my ( $untrackedRef, $modifiedRef ) = returnState($logger);
  195. my @files;
  196. push(@files,@$untrackedRef); push(@files,@$modifiedRef);
  197. my @filesToCommit;
  198. if ( defined $args{'interactive'} ) {
  199. foreach my $file ( @files ) {
  200. print "Add $file to commit (y/n): ";
  201. my $input = <STDIN>;
  202. chomp $input;
  203. if ( $input =~ m/^Y|^y/ ) {
  204. push(@filesToCommit,$file);
  205. } else {
  206. next;
  207. }
  208. }
  209. } else {
  210. @filesToCommit = @files;
  211. }
  212. print "Commiting the following files:\n";
  213. foreach my $file ( @filesToCommit ) {
  214. print "\t$file\n";
  215. }
  216. if ( defined $args{'interactive'} ) {
  217. print "Does this look correct (y/n) : ";
  218. my $input = <STDIN>;
  219. chomp $input;
  220. if ( $input !~ m/^Y|^y/ ) {
  221. print "Canceling...\n";
  222. exit 1;
  223. }
  224. }
  225. addFiles(\@filesToCommit,$logger);
  226. if ( defined $args{'interactive'} && ! defined $args{'commit-msg'}) {
  227. print "Enter a commit message: ";
  228. my $input = <STDIN>;
  229. chomp $input;
  230. commitChanges($input,$logger);
  231. } elsif ( defined $args{'commit-msg'} ) {
  232. my $commitMsg = "$args{'commit-msg'}";
  233. commitChanges($commitMsg,$logger);
  234. } else {
  235. my $epoch = time();
  236. my $commitMsg = "Generic Commit at $epoch";
  237. commitChanges($commitMsg,$logger);
  238. }
  239. if ( defined $args{'interactive'} ) {
  240. print "Push changes? (y/n): ";
  241. my $input = <STDIN>;
  242. chomp $input;
  243. if ( $input !~ m/^Y|^y/ ) {
  244. # TODO: Unstage changes?
  245. print "Canceling...\n";
  246. exit 1;
  247. }
  248. my $gitOutput = pushChanges($logger);
  249. print "Git returned:\n$gitOutput";
  250. } else {
  251. pushChanges($logger);
  252. my $gitOutput = pushChanges($logger);
  253. print "Git returned:\n$gitOutput";
  254. }
  255. }
  256. if ( defined $args{'reset-from-master'} ) {
  257. stashAndReset($logger);
  258. }
  259. if ( defined $args{'reset-from-upstream'} ) {
  260. resetFromUpstream($logger);
  261. }
  262. if ( defined $args{'dump-config'} ) {
  263. my %configHash = readConfig(".",$logger);
  264. foreach my $key ( keys %configHash ) {
  265. my $hRef = $configHash{$key};
  266. print "[$key]\n";
  267. foreach my $ckey ( keys %$hRef ) {
  268. print "\t$ckey = ${$hRef}{$ckey}\n";
  269. }
  270. }
  271. }
  272. if ( defined $args{'configure-local-user'} ) {
  273. if ( defined $args{'interactive'} ) {
  274. print "Enter user to set: ";
  275. my $desiredName = <STDIN>;
  276. chomp $desiredName;
  277. print "Enter email to set: ";
  278. my $desiredEmail = <STDIN>;
  279. chomp $desiredEmail;
  280. appendRepoUserConfig($desiredName,$desiredEmail,$logger);
  281. } else {
  282. appendRepoUserConfig($args{'user'},$args{'email'},$logger);
  283. }
  284. }