sg 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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 Shellex::Shellex qw(shellex findBin);
  9. use SimplyGit::Git qw(
  10. readConfig getStatus returnState addFiles
  11. commitChanges pushChanges stashAndReset resetFromUpstream
  12. updateGitIgnore appendRepoUserConfig parseSGConfig
  13. warnOnUser basicClone basicPull knocker
  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. my $configFile = $homeDir . "/" . $sgDir . "/" . "sg.config";
  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. if ( ! -f $configFile ) {
  31. print "Creating $configFile\n";
  32. shellex("touch $configFile");
  33. }
  34. return ( $path, $logFile, $configFile );
  35. }
  36. my ( $sgPath, $sgLogFile, $sgConfigFile ) = initSG(".sg");
  37. sub getLogName { return $sgLogFile; };
  38. my $log_conf = q(
  39. log4perl.rootLogger = ERROR, LOG1, screen
  40. log4perl.appender.LOG1 = Log::Log4perl::Appender::File
  41. log4perl.appender.LOG1.filename = sub { getLogName(); }
  42. log4perl.appender.LOG1.mode = append
  43. log4perl.appender.LOG1.layout = Log::Log4perl::Layout::PatternLayout
  44. log4perl.appender.LOG1.layout.ConversionPattern = %d %p >> %m %n
  45. log4perl.appender.screen = Log::Log4perl::Appender::Screen
  46. log4perl.appender.screen.stderr = 0
  47. log4perl.appender.screen.layout = PatternLayout
  48. log4perl.appender.screen.layout.ConversionPattern = %d %p >> %m %n
  49. );
  50. Log::Log4perl::init(\$log_conf);
  51. my $logger = get_logger();
  52. my $gitCmd = findBin("git",$logger);
  53. # Removed .sg from repo dir to home, don't need this sub right now
  54. # updateGitIgnore(".","/.sg",$logger);
  55. my %args;
  56. GetOptions(
  57. \%args,
  58. 'push-all',
  59. 'interactive',
  60. 'view',
  61. 'reset-from-master',
  62. 'reset-from-upstream',
  63. 'upstream-url=s',
  64. 'commit-msg=s',
  65. 'dump-config',
  66. 'configure-local-user',
  67. 'user=s',
  68. 'email=s',
  69. 'config-file=s',
  70. 'knock',
  71. 'knock-clone=s',
  72. 'help',
  73. 'knock-pull',
  74. );
  75. # TODO: This should maybe be more robust?
  76. if ( ! -d ".git" && ( ! defined $args{'knock-clone'} && ! defined $args{'knock'} && ! defined $args{'help'} ) ) {
  77. print "Not a git dir, exiting...\n";
  78. exit 1;
  79. }
  80. sub printHelp {
  81. my $help = <<EOF
  82. simply-git
  83. Usage:
  84. --view
  85. Display git status of files and other information
  86. --dump-config
  87. Dump .git/config to STDOUT. Not really useful but exposed for testing of reading config into internal data structure
  88. --push-all [--commit-msg]
  89. Push all untracked and modified files
  90. * Can be used with interactive mode
  91. * Can provide a commit msg with --commit-msg (otherwise a generic will be provided)
  92. --interactive
  93. Enable interactive mode with supported opts
  94. --reset-from-master
  95. Reset all current changes so that the file tree matches origin master
  96. --reset-from-upstream [ --upstream-url ]
  97. If upstream is defined will reset local branch to match upstream ( does not push changes to origin )
  98. * Assumes you have an upstream configured
  99. * Pass SSH/HTTPS URL to --upstream-url to add an upstream
  100. --configure-local-user [--user,--email]
  101. Configure local git user
  102. * Can be used with interactive mode
  103. --config-file
  104. Default is ~/.sg/sg.config, can use this opt to use another file
  105. * See example.config
  106. --knock
  107. Will try and knock the defined git server at the defined ports before any operation
  108. * See example.config
  109. * Can pass this by itself to perform a knock and exit
  110. --knock-clone
  111. Will try and knock the defined git server and clone the provided repo
  112. * Will not check if you're in a git dir
  113. --knock-pull
  114. Will try and knock the defined git server and git pull
  115. EOF
  116. ;
  117. print "$help\n";
  118. }
  119. if ( scalar keys %args < 1 ) {
  120. printHelp();
  121. }
  122. ###
  123. # TODO: This args processing is better and more predictable than it was, bit there is still
  124. # likely a lot of room for improvement...
  125. ###
  126. sub parseArgs {
  127. if ( defined $args{'help'} ) {
  128. printHelp();
  129. exit 0;
  130. }
  131. if ( defined $args{'view'} && scalar keys %args > 1 ) {
  132. print "Can't pass other args with --view\n";
  133. exit 1;
  134. }
  135. if ( defined $args{'dump-config'} && scalar keys %args > 1 ) {
  136. print "Can't pass other args with --dump-config\n";
  137. exit 1;
  138. }
  139. if ( defined $args{'reset-from-master'} && scalar keys %args > 1 ) {
  140. print "Can't pass other args with --reset-from-master\n";
  141. exit 1;
  142. }
  143. if ( defined $args{'push-all'} ) {
  144. foreach my $arg ( keys %args ) {
  145. if ( $arg eq "interactive" || $arg eq "commit-msg" || $arg eq "push-all" || $arg eq "knock" ) {
  146. next;
  147. } else {
  148. print "Can only pass --interactive and --commit-msg with --push-all\n";
  149. exit 1;
  150. }
  151. }
  152. }
  153. if ( defined $args{'configure-local-user'} ) {
  154. if ( scalar keys %args < 2 ) {
  155. print "Must pass either --interactive or --user AND --email to --configure-local-user\n";
  156. exit 1;
  157. }
  158. foreach my $arg ( keys %args ) {
  159. if ( $arg eq "interactive" || $arg eq "user" || $arg eq "email" || $arg eq "configure-local-user" ) {
  160. next;
  161. } else {
  162. print "Must/can only pass --interactive, OR --user AND --email with --configure-local-user\n";
  163. exit 1;
  164. }
  165. }
  166. if ( ! defined $args{'interactive'} && ! defined $args{'user'} || ! defined $args{'interactive'} && ! defined $args{'email'} ) {
  167. print "If not using --interactive with --configure-local-user, --user and --email MUST be defined\n";
  168. exit 1;
  169. }
  170. }
  171. if ( defined $args{'reset-from-upstream'} ) {
  172. if ( scalar keys %args > 2 ) {
  173. print "Can only pass --upstream-url with --reset-from-upstream\n";
  174. exit 1;
  175. }
  176. foreach my $arg ( keys %args ) {
  177. if ( $arg eq "reset-from-upstream" || $arg eq "upstream-url" ) {
  178. next;
  179. } else {
  180. print "Can only pass --upstream-url with --reset-from-upstream\n";
  181. exit 1;
  182. }
  183. }
  184. }
  185. if ( ! defined $args{'config-file'} ) {
  186. $args{'config-file'} = $sgConfigFile;
  187. }
  188. if ( defined $args{'knock-clone'} ) {
  189. if ( scalar keys %args > 2 ) {
  190. print "--knock-clone accepts no other args\n";
  191. exit 1;
  192. }
  193. }
  194. if ( defined $args{'knock-pull'} ) {
  195. if ( scalar keys %args > 2 ) {
  196. print "--knock-pull accepts no other args\n";
  197. exit 1;
  198. }
  199. }
  200. }
  201. sub color_print($$) {
  202. #echo -e "\033[0mNC (No color)"
  203. #echo -e "\033[1;37mWHITE\t\033[0;30mBLACK"
  204. #echo -e "\033[0;34mBLUE\t\033[1;34mLIGHT_BLUE"
  205. #echo -e "\033[0;32mGREEN\t\033[1;32mLIGHT_GREEN"
  206. #echo -e "\033[0;36mCYAN\t\033[1;36mLIGHT_CYAN"
  207. #echo -e "\033[0;31mRED\t\033[1;31mLIGHT_RED"
  208. #echo -e "\033[0;35mPURPLE\t\033[1;35mLIGHT_PURPLE"
  209. #echo -e "\033[0;33mYELLOW\t\033[1;33mLIGHT_YELLOW"
  210. #echo -e "\033[1;30mGRAY\t\033[0;37mLIGHT_GRAY"
  211. #
  212. # Doing it this way likely hurts portability but
  213. # it's better than nothing which is what I'm currently doing
  214. # and would like to avoid additional module deps, as this
  215. # isn't too hard to implement
  216. my $print_string = shift;
  217. my $color = shift;
  218. if ( $color ne "BLUE" && $color ne "GREEN" && $color ne "RED" ) {
  219. $logger->error("Bad color passed to color_print");
  220. exit 1;
  221. }
  222. my %color_map = (
  223. BLUE => "\033[0;34m",
  224. GREEN => "\033[0;32m",
  225. RED => "\033[0;31m",
  226. RESET => "\033[0m",
  227. );
  228. printf "$color_map{$color}$print_string$color_map{'RESET'}";
  229. }
  230. parseArgs();
  231. my %sgConfig = parseSGConfig($args{'config-file'},$logger);
  232. if ( defined $sgConfig{'UserWarn'} && -d ".git" ) {
  233. warnOnUser($sgConfig{'user.name'},$sgConfig{'user.email'},$logger);
  234. }
  235. sub knock() {
  236. if ( defined $sgConfig{'Knock'} && ( defined $args{'knock'} || defined $args{'knock-clone'} || defined $args{'knock-pull'} ) ) {
  237. knocker($sgConfig{'knock.target'},$sgConfig{'ports'},$logger);
  238. }
  239. }
  240. if ( defined $args{'knock'} && scalar keys %args == 2 ) {
  241. print "Just knocking then exiting...\n";
  242. knock();
  243. exit 1;
  244. }
  245. # TODO: This sub could be more concise with a sub to print array refs
  246. if ( defined $args{'view'} ) {
  247. my ( $untrackedRef, $modifiedRef, $addedRef, $deletedRef ) = returnState($logger);
  248. my $refs = shellex("$gitCmd show-ref",$logger);
  249. my $branch = shellex("$gitCmd show-branch",$logger);
  250. my $name = shellex("$gitCmd config --get user.name",$logger);
  251. chomp $name;
  252. my $email = shellex("$gitCmd config --get user.email",$logger);
  253. chomp $email;
  254. color_print("-->Username: $name\n-->Email: $email\n","BLUE");
  255. color_print("On [branch] @ commit: $branch\n","GREEN");
  256. print "$refs\n";
  257. my $swpWarning = "\t# Likely a Vi .swp file";
  258. my $untrackedTotal = scalar @$untrackedRef;
  259. print "* $untrackedTotal untracked file(s):\n";
  260. foreach my $file ( @$untrackedRef ) {
  261. if ( $file =~ m/.swp/ ) {
  262. color_print("\t$file $swpWarning\n","GREEN");
  263. } else {
  264. color_print("\t$file\n","GREEN");
  265. }
  266. }
  267. my $modifiedTotal = scalar @$modifiedRef;
  268. print "* $modifiedTotal modified file(s):\n";
  269. foreach my $file ( @$modifiedRef ) {
  270. if ( $file =~ m/.swp/ ) {
  271. color_print("\t$file $swpWarning\n","GREEN");
  272. } else {
  273. color_print("\t$file\n","GREEN");
  274. }
  275. }
  276. my $commitTotal = scalar @$addedRef;
  277. print "* $commitTotal file(s) added to commit:\n";
  278. foreach my $file ( @$addedRef ) {
  279. if ( $file =~ m/.swp/ ) {
  280. print "\t$file $swpWarning\n";
  281. } else {
  282. print "\t$file\n";
  283. }
  284. }
  285. my $deletedTotal = scalar @$deletedRef;
  286. print "* $deletedTotal file(s) to be deleted from commit:\n";
  287. foreach my $file ( @$deletedRef ) {
  288. if ( $file =~ m/.swp/ ) {
  289. color_print("\t$file $swpWarning\n","RED");
  290. } else {
  291. color_print("\t$file\n","RED");
  292. }
  293. }
  294. }
  295. if ( defined $args{'push-all'} ) {
  296. my ( $untrackedRef, $modifiedRef ) = returnState($logger);
  297. my @files;
  298. push(@files,@$untrackedRef); push(@files,@$modifiedRef);
  299. my @filesToCommit;
  300. if ( defined $args{'interactive'} ) {
  301. foreach my $file ( @files ) {
  302. print "Add $file to commit (y/n): ";
  303. my $input = <STDIN>;
  304. chomp $input;
  305. if ( $input =~ m/^Y|^y/ ) {
  306. push(@filesToCommit,$file);
  307. } else {
  308. next;
  309. }
  310. }
  311. } else {
  312. @filesToCommit = @files;
  313. }
  314. print "Commiting the following files:\n";
  315. foreach my $file ( @filesToCommit ) {
  316. print "\t$file\n";
  317. }
  318. if ( defined $args{'interactive'} ) {
  319. print "Does this look correct (y/n) : ";
  320. my $input = <STDIN>;
  321. chomp $input;
  322. if ( $input !~ m/^Y|^y/ ) {
  323. print "Canceling...\n";
  324. exit 1;
  325. }
  326. }
  327. addFiles(\@filesToCommit,$logger);
  328. if ( defined $args{'interactive'} && ! defined $args{'commit-msg'}) {
  329. print "Enter a commit message: ";
  330. my $input = <STDIN>;
  331. chomp $input;
  332. commitChanges($input,$logger);
  333. } elsif ( defined $args{'commit-msg'} ) {
  334. my $commitMsg = "$args{'commit-msg'}";
  335. commitChanges($commitMsg,$logger);
  336. } else {
  337. my $epoch = time();
  338. my $commitMsg = "Generic Commit at $epoch";
  339. commitChanges($commitMsg,$logger);
  340. }
  341. if ( defined $args{'interactive'} ) {
  342. print "Push changes? (y/n): ";
  343. my $input = <STDIN>;
  344. chomp $input;
  345. if ( $input !~ m/^Y|^y/ ) {
  346. # TODO: Unstage changes?
  347. print "Canceling...\n";
  348. exit 1;
  349. }
  350. knock();
  351. my $gitOutput = pushChanges($logger);
  352. print "Git returned:\n$gitOutput\n";
  353. }
  354. else {
  355. knock();
  356. pushChanges($logger);
  357. my $gitOutput = pushChanges($logger);
  358. print "Git returned:\n$gitOutput\n";
  359. }
  360. }
  361. if ( defined $args{'reset-from-master'} ) {
  362. knock();
  363. stashAndReset($logger);
  364. }
  365. if ( defined $args{'reset-from-upstream'} ) {
  366. if ( defined $args{'upstream-url'} ) {
  367. print "Setting upstream to $args{'upstream-url'}\n";
  368. chomp $args{'upstream-url'};
  369. shellex("$gitCmd remote add upstream $args{'upstream-url'}",$logger);
  370. shellex("$gitCmd fetch upstream",$logger);
  371. knock();
  372. resetFromUpstream($logger);
  373. } else {
  374. knock();
  375. resetFromUpstream($logger);
  376. }
  377. }
  378. if ( defined $args{'dump-config'} ) {
  379. my %configHash = readConfig(".",$logger);
  380. foreach my $key ( keys %configHash ) {
  381. my $hRef = $configHash{$key};
  382. print "[$key]\n";
  383. foreach my $ckey ( keys %$hRef ) {
  384. print "\t$ckey = ${$hRef}{$ckey}\n";
  385. }
  386. }
  387. }
  388. if ( defined $args{'configure-local-user'} ) {
  389. if ( defined $args{'interactive'} ) {
  390. print "Enter user to set: ";
  391. my $desiredName = <STDIN>;
  392. chomp $desiredName;
  393. print "Enter email to set: ";
  394. my $desiredEmail = <STDIN>;
  395. chomp $desiredEmail;
  396. appendRepoUserConfig($desiredName,$desiredEmail,$logger);
  397. } else {
  398. appendRepoUserConfig($args{'user'},$args{'email'},$logger);
  399. }
  400. }
  401. if ( defined $args{'knock-clone'} ) {
  402. knock();
  403. basicClone($args{'knock-clone'},$logger);
  404. }
  405. if ( defined $args{'knock-pull'} ) {
  406. knock();
  407. basicPull($logger);
  408. }