sg 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 $homeDir = shellex("echo \$HOME");
  18. chomp $homeDir;
  19. my $path = $homeDir . "/" . $sgDir;
  20. my $logFile = $homeDir . "/" . $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. # Create or append to .gitignore so that we don't push the log
  45. # Maybe this should be an opt?
  46. updateGitIgnore(".","/.sg",$logger);
  47. my %args;
  48. GetOptions(
  49. \%args,
  50. 'push-all',
  51. 'interactive',
  52. 'view',
  53. 'reset-from-master',
  54. 'reset-from-upstream',
  55. 'upstream-url=s',
  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 origin master
  78. --reset-from-upstream [ --upstream-url ]
  79. If upstream is defined will reset local branch to match upstream ( does not push changes to origin )
  80. * Assumes you have an upstream configured
  81. * Pass SSH/HTTPS URL to --upstream-url to add an upstream
  82. --configure-local-user [--user,--email]
  83. Configure local git user
  84. * Can be used with interactive mode
  85. EOF
  86. ;
  87. print "$help\n";
  88. }
  89. if ( scalar keys %args < 1 ) {
  90. printHelp();
  91. }
  92. ###
  93. # TODO: This args processing is better and more predictable than it was, bit there is still
  94. # likely a lot of room for improvement...
  95. ###
  96. sub parseArgs {
  97. if ( defined $args{'view'} && scalar keys %args > 1 ) {
  98. print "Can't pass other args with --view\n";
  99. exit 1;
  100. }
  101. if ( defined $args{'dump-config'} && scalar keys %args > 1 ) {
  102. print "Can't pass other args with --dump-config\n";
  103. exit 1;
  104. }
  105. if ( defined $args{'reset-from-master'} && scalar keys %args > 1 ) {
  106. print "Can't pass other args with --reset-from-master\n";
  107. exit 1;
  108. }
  109. if ( defined $args{'push-all'} ) {
  110. foreach my $arg ( keys %args ) {
  111. if ( $arg eq "interactive" || $arg eq "commit-msg" || $arg eq "push-all" ) {
  112. next;
  113. } else {
  114. print "Can only pass --interactive and --commit-msg with --push-all\n";
  115. exit 1;
  116. }
  117. }
  118. }
  119. if ( defined $args{'configure-local-user'} ) {
  120. if ( scalar keys %args < 2 ) {
  121. print "Must pass either --interactive or --user AND --email to --configure-local-user\n";
  122. exit 1;
  123. }
  124. foreach my $arg ( keys %args ) {
  125. if ( $arg eq "interactive" || $arg eq "user" || $arg eq "email" || $arg eq "configure-local-user" ) {
  126. next;
  127. } else {
  128. print "Must/can only pass --interactive, OR --user AND --email with --configure-local-user\n";
  129. exit 1;
  130. }
  131. }
  132. if ( ! defined $args{'interactive'} && ! defined $args{'user'} || ! defined $args{'interactive'} && ! defined $args{'email'} ) {
  133. print "If not using --interactive with --configure-local-user, --user and --email MUST be defined\n";
  134. exit 1;
  135. }
  136. }
  137. if ( defined $args{'reset-from-upstream'} ) {
  138. if ( scalar keys %args > 2 ) {
  139. print "Can only pass --upstream-url with --reset-from-upstream\n";
  140. exit 1;
  141. }
  142. foreach my $arg ( keys %args ) {
  143. if ( $arg eq "reset-from-upstream" || $arg eq "upstream-url" ) {
  144. next;
  145. } else {
  146. print "Can only pass --upstream-url with --reset-from-upstream\n";
  147. exit 1;
  148. }
  149. }
  150. }
  151. }
  152. parseArgs();
  153. #print "Args parsed successfully\n";
  154. #exit 0;
  155. # TODO: This sub could be more concise with a sub to print array refs
  156. if ( defined $args{'view'} ) {
  157. my ( $untrackedRef, $modifiedRef, $addedRef, $deletedRef ) = returnState($logger);
  158. my $refs = shellex("$gitCmd show-ref",$logger);
  159. my $branch = shellex("$gitCmd show-branch",$logger);
  160. my $name = shellex("$gitCmd config --get user.name",$logger);
  161. chomp $name;
  162. my $email = shellex("$gitCmd config --get user.email",$logger);
  163. chomp $email;
  164. print "-->Username: $name\n-->Email: $email\n";
  165. print "On [branch] @ commit: $branch";
  166. print "$refs\n";
  167. my $swpWarning = "\t# Likely a Vi .swp file";
  168. my $untrackedTotal = scalar @$untrackedRef;
  169. print "* $untrackedTotal untracked file(s):\n";
  170. foreach my $file ( @$untrackedRef ) {
  171. if ( $file =~ m/.swp/ ) {
  172. print "\t$file $swpWarning\n";
  173. } else {
  174. print "\t$file\n";
  175. }
  176. }
  177. my $modifiedTotal = scalar @$modifiedRef;
  178. print "* $modifiedTotal modified file(s):\n";
  179. foreach my $file ( @$modifiedRef ) {
  180. if ( $file =~ m/.swp/ ) {
  181. print "\t$file $swpWarning\n";
  182. } else {
  183. print "\t$file\n";
  184. }
  185. }
  186. my $commitTotal = scalar @$addedRef;
  187. print "* $commitTotal file(s) added to commit:\n";
  188. foreach my $file ( @$addedRef ) {
  189. if ( $file =~ m/.swp/ ) {
  190. print "\t$file $swpWarning\n";
  191. } else {
  192. print "\t$file\n";
  193. }
  194. }
  195. my $deletedTotal = scalar @$deletedRef;
  196. print "* $deletedTotal file(s) to be deleted from commit:\n";
  197. foreach my $file ( @$deletedRef ) {
  198. if ( $file =~ m/.swp/ ) {
  199. print "\t$file $swpWarning\n";
  200. } else {
  201. print "\t$file\n";
  202. }
  203. }
  204. }
  205. if ( defined $args{'push-all'} ) {
  206. my ( $untrackedRef, $modifiedRef ) = returnState($logger);
  207. my @files;
  208. push(@files,@$untrackedRef); push(@files,@$modifiedRef);
  209. my @filesToCommit;
  210. if ( defined $args{'interactive'} ) {
  211. foreach my $file ( @files ) {
  212. print "Add $file to commit (y/n): ";
  213. my $input = <STDIN>;
  214. chomp $input;
  215. if ( $input =~ m/^Y|^y/ ) {
  216. push(@filesToCommit,$file);
  217. } else {
  218. next;
  219. }
  220. }
  221. } else {
  222. @filesToCommit = @files;
  223. }
  224. print "Commiting the following files:\n";
  225. foreach my $file ( @filesToCommit ) {
  226. print "\t$file\n";
  227. }
  228. if ( defined $args{'interactive'} ) {
  229. print "Does this look correct (y/n) : ";
  230. my $input = <STDIN>;
  231. chomp $input;
  232. if ( $input !~ m/^Y|^y/ ) {
  233. print "Canceling...\n";
  234. exit 1;
  235. }
  236. }
  237. addFiles(\@filesToCommit,$logger);
  238. if ( defined $args{'interactive'} && ! defined $args{'commit-msg'}) {
  239. print "Enter a commit message: ";
  240. my $input = <STDIN>;
  241. chomp $input;
  242. commitChanges($input,$logger);
  243. } elsif ( defined $args{'commit-msg'} ) {
  244. my $commitMsg = "$args{'commit-msg'}";
  245. commitChanges($commitMsg,$logger);
  246. } else {
  247. my $epoch = time();
  248. my $commitMsg = "Generic Commit at $epoch";
  249. commitChanges($commitMsg,$logger);
  250. }
  251. if ( defined $args{'interactive'} ) {
  252. print "Push changes? (y/n): ";
  253. my $input = <STDIN>;
  254. chomp $input;
  255. if ( $input !~ m/^Y|^y/ ) {
  256. # TODO: Unstage changes?
  257. print "Canceling...\n";
  258. exit 1;
  259. }
  260. my $gitOutput = pushChanges($logger);
  261. print "Git returned:\n$gitOutput";
  262. } else {
  263. pushChanges($logger);
  264. my $gitOutput = pushChanges($logger);
  265. print "Git returned:\n$gitOutput";
  266. }
  267. }
  268. if ( defined $args{'reset-from-master'} ) {
  269. stashAndReset($logger);
  270. }
  271. if ( defined $args{'reset-from-upstream'} ) {
  272. if ( defined $args{'upstream-url'} ) {
  273. print "Setting upstream to $args{'upstream-url'}\n";
  274. chomp $args{'upstream-url'};
  275. shellex("$gitCmd remote add upstream $args{'upstream-url'}",$logger);
  276. shellex("$gitCmd fetch upstream",$logger);
  277. resetFromUpstream($logger);
  278. } else {
  279. resetFromUpstream($logger);
  280. }
  281. }
  282. if ( defined $args{'dump-config'} ) {
  283. my %configHash = readConfig(".",$logger);
  284. foreach my $key ( keys %configHash ) {
  285. my $hRef = $configHash{$key};
  286. print "[$key]\n";
  287. foreach my $ckey ( keys %$hRef ) {
  288. print "\t$ckey = ${$hRef}{$ckey}\n";
  289. }
  290. }
  291. }
  292. if ( defined $args{'configure-local-user'} ) {
  293. if ( defined $args{'interactive'} ) {
  294. print "Enter user to set: ";
  295. my $desiredName = <STDIN>;
  296. chomp $desiredName;
  297. print "Enter email to set: ";
  298. my $desiredEmail = <STDIN>;
  299. chomp $desiredEmail;
  300. appendRepoUserConfig($desiredName,$desiredEmail,$logger);
  301. } else {
  302. appendRepoUserConfig($args{'user'},$args{'email'},$logger);
  303. }
  304. }