sg 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. 'branch-from-master',
  57. 'commit-msg=s',
  58. 'dump-config',
  59. 'configure-local-user',
  60. 'user=s',
  61. 'email=s',
  62. );
  63. sub printHelp {
  64. my $help = <<EOF
  65. simply-git
  66. Usage:
  67. --view
  68. Display git status of files and other information
  69. --dump-config
  70. Dump .git/config to STDOUT. Not really useful but exposed for testing of reading config into internal data structure
  71. --push-all [--commit-msg]
  72. Push all untracked and modified files
  73. * Can be used with interactive mode
  74. * Can provide a commit msg with --commit-msg (otherwise a generic will be provided)
  75. --interactive
  76. Enable interactive mode with supported opts
  77. --reset-from-master
  78. Reset all current changes so that the file tree matches upstream/master
  79. --branch-from-master
  80. Create a new clean branch from upstream/master
  81. --configure-local-user [--user,--email]
  82. Configure local git user
  83. * Can be used with interactive mode
  84. EOF
  85. ;
  86. print "$help\n";
  87. }
  88. if ( scalar keys %args < 1 ) {
  89. printHelp();
  90. }
  91. ###
  92. # TODO: This args processing is better and more predictable than it was, bit there is still
  93. # likely a lot of room for improvement...
  94. ###
  95. sub parseArgs {
  96. if ( defined $args{'view'} && scalar keys %args > 1 ) {
  97. print "Can't pass other args with --view\n";
  98. exit 1;
  99. }
  100. if ( defined $args{'dump-config'} && scalar keys %args > 1 ) {
  101. print "Can't pass other args with --dump-config\n";
  102. exit 1;
  103. }
  104. if ( defined $args{'reset-from-master'} && scalar keys %args > 1 ) {
  105. print "Can't pass other args with --reset-from-master\n";
  106. exit 1;
  107. }
  108. if ( defined $args{'push-all'} ) {
  109. foreach my $arg ( keys %args ) {
  110. if ( $arg eq "interactive" || $arg eq "commit-msg" || $arg eq "push-all" ) {
  111. next;
  112. } else {
  113. print "Can only pass --interactive and --commit-msg with --push-all\n";
  114. exit 1;
  115. }
  116. }
  117. }
  118. if ( defined $args{'configure-local-user'} ) {
  119. if ( scalar keys %args < 2 ) {
  120. print "Must pass either --interactive or --user AND --email to --configure-local-user\n";
  121. exit 1;
  122. }
  123. foreach my $arg ( keys %args ) {
  124. if ( $arg eq "interactive" || $arg eq "user" || $arg eq "email" || $arg eq "configure-local-user" ) {
  125. next;
  126. } else {
  127. print "Must/can only pass --interactive, OR --user AND --email with --configure-local-user\n";
  128. exit 1;
  129. }
  130. }
  131. if ( ! defined $args{'interactive'} && ! defined $args{'user'} || ! defined $args{'interactive'} && ! defined $args{'email'} ) {
  132. print "If not using --interactive with --configure-local-user, --user and --email MUST be defined\n";
  133. exit 1;
  134. }
  135. }
  136. }
  137. parseArgs();
  138. #print "Args parsed successfully\n";
  139. #exit 0;
  140. # TODO: This sub could be more concise with a sub to print array refs
  141. if ( defined $args{'view'} ) {
  142. my ( $untrackedRef, $modifiedRef, $addedRef, $deletedRef ) = returnState($logger);
  143. my $refs = shellex("$gitCmd show-ref",$logger);
  144. my $branch = shellex("$gitCmd show-branch",$logger);
  145. my $name = shellex("$gitCmd config --get user.name",$logger);
  146. chomp $name;
  147. my $email = shellex("$gitCmd config --get user.email",$logger);
  148. chomp $email;
  149. print "Username: $name\nEmail: $email\n";
  150. print "On [branch] @ commit: $branch";
  151. print "$refs\n";
  152. my $swpWarning = "\t# Likely a Vi .swp file";
  153. my $untrackedTotal = scalar @$untrackedRef;
  154. print "* $untrackedTotal untracked file(s):\n";
  155. foreach my $file ( @$untrackedRef ) {
  156. if ( $file =~ m/.swp/ ) {
  157. print "\t$file $swpWarning\n";
  158. } else {
  159. print "\t$file\n";
  160. }
  161. }
  162. my $modifiedTotal = scalar @$modifiedRef;
  163. print "* $modifiedTotal modified file(s):\n";
  164. foreach my $file ( @$modifiedRef ) {
  165. if ( $file =~ m/.swp/ ) {
  166. print "\t$file $swpWarning\n";
  167. } else {
  168. print "\t$file\n";
  169. }
  170. }
  171. my $commitTotal = scalar @$addedRef;
  172. print "* $commitTotal file(s) added to commit:\n";
  173. foreach my $file ( @$addedRef ) {
  174. if ( $file =~ m/.swp/ ) {
  175. print "\t$file $swpWarning\n";
  176. } else {
  177. print "\t$file\n";
  178. }
  179. }
  180. my $deletedTotal = scalar @$deletedRef;
  181. print "* $deletedTotal file(s) to be deleted from commit:\n";
  182. foreach my $file ( @$deletedRef ) {
  183. if ( $file =~ m/.swp/ ) {
  184. print "\t$file $swpWarning\n";
  185. } else {
  186. print "\t$file\n";
  187. }
  188. }
  189. }
  190. if ( defined $args{'push-all'} ) {
  191. my ( $untrackedRef, $modifiedRef ) = returnState($logger);
  192. my @files;
  193. push(@files,@$untrackedRef); push(@files,@$modifiedRef);
  194. my @filesToCommit;
  195. if ( defined $args{'interactive'} ) {
  196. foreach my $file ( @files ) {
  197. print "Add $file to commit (y/n): ";
  198. my $input = <STDIN>;
  199. chomp $input;
  200. if ( $input =~ m/^Y|^y/ ) {
  201. push(@filesToCommit,$file);
  202. } else {
  203. next;
  204. }
  205. }
  206. } else {
  207. @filesToCommit = @files;
  208. }
  209. print "Commiting the following files:\n";
  210. foreach my $file ( @filesToCommit ) {
  211. print "\t$file\n";
  212. }
  213. if ( defined $args{'interactive'} ) {
  214. print "Does this look correct (y/n) : ";
  215. my $input = <STDIN>;
  216. chomp $input;
  217. if ( $input !~ m/^Y|^y/ ) {
  218. print "Canceling...\n";
  219. exit 1;
  220. }
  221. }
  222. addFiles(\@filesToCommit,$logger);
  223. if ( defined $args{'interactive'} && ! defined $args{'commit-msg'}) {
  224. print "Enter a commit message: ";
  225. my $input = <STDIN>;
  226. chomp $input;
  227. commitChanges($input,$logger);
  228. } elsif ( defined $args{'commit-msg'} ) {
  229. my $commitMsg = "$args{'commit-msg'}";
  230. commitChanges($commitMsg,$logger);
  231. } else {
  232. my $epoch = time();
  233. my $commitMsg = "Generic Commit at $epoch";
  234. commitChanges($commitMsg,$logger);
  235. }
  236. if ( defined $args{'interactive'} ) {
  237. print "Push changes? (y/n): ";
  238. my $input = <STDIN>;
  239. chomp $input;
  240. if ( $input !~ m/^Y|^y/ ) {
  241. # TODO: Unstage changes?
  242. print "Canceling...\n";
  243. exit 1;
  244. }
  245. my $gitOutput = pushChanges($logger);
  246. print "Git returned:\n$gitOutput";
  247. } else {
  248. pushChanges($logger);
  249. my $gitOutput = pushChanges($logger);
  250. print "Git returned:\n$gitOutput";
  251. }
  252. }
  253. if ( defined $args{'reset-from-master'} ) {
  254. stashAndReset($logger);
  255. }
  256. if ( defined $args{'reset-from-upstream'} ) {
  257. print "This does nothing right now\n";
  258. resetFromUpstream($logger);
  259. }
  260. if ( defined $args{'dump-config'} ) {
  261. my %configHash = readConfig(".",$logger);
  262. foreach my $key ( keys %configHash ) {
  263. my $hRef = $configHash{$key};
  264. print "[$key]\n";
  265. foreach my $ckey ( keys %$hRef ) {
  266. print "\t$ckey = ${$hRef}{$ckey}\n";
  267. }
  268. }
  269. }
  270. if ( defined $args{'configure-local-user'} ) {
  271. if ( defined $args{'interactive'} ) {
  272. print "Enter user to set: ";
  273. my $desiredName = <STDIN>;
  274. chomp $desiredName;
  275. print "Enter email to set: ";
  276. my $desiredEmail = <STDIN>;
  277. chomp $desiredEmail;
  278. appendRepoUserConfig($desiredName,$desiredEmail,$logger);
  279. } else {
  280. appendRepoUserConfig($args{'user'},$args{'email'},$logger);
  281. }
  282. }