sg 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. print "Branches:\n";
  256. color_print("$branch\n","GREEN");
  257. #print "$refs\n";
  258. print "Files:\n";
  259. my $swpWarning = "\t# Likely a Vi .swp file";
  260. my $untrackedTotal = scalar @$untrackedRef;
  261. print "* $untrackedTotal untracked file(s):\n";
  262. foreach my $file ( @$untrackedRef ) {
  263. if ( $file =~ m/.swp/ ) {
  264. color_print("\t$file $swpWarning\n","GREEN");
  265. } else {
  266. color_print("\t$file\n","GREEN");
  267. }
  268. }
  269. my $modifiedTotal = scalar @$modifiedRef;
  270. print "* $modifiedTotal modified file(s):\n";
  271. foreach my $file ( @$modifiedRef ) {
  272. if ( $file =~ m/.swp/ ) {
  273. color_print("\t$file $swpWarning\n","GREEN");
  274. } else {
  275. color_print("\t$file\n","GREEN");
  276. }
  277. }
  278. my $commitTotal = scalar @$addedRef;
  279. print "* $commitTotal file(s) added to commit:\n";
  280. foreach my $file ( @$addedRef ) {
  281. if ( $file =~ m/.swp/ ) {
  282. print "\t$file $swpWarning\n";
  283. } else {
  284. print "\t$file\n";
  285. }
  286. }
  287. my $deletedTotal = scalar @$deletedRef;
  288. print "* $deletedTotal file(s) to be deleted from commit:\n";
  289. foreach my $file ( @$deletedRef ) {
  290. if ( $file =~ m/.swp/ ) {
  291. color_print("\t$file $swpWarning\n","RED");
  292. } else {
  293. color_print("\t$file\n","RED");
  294. }
  295. }
  296. }
  297. if ( defined $args{'push-all'} ) {
  298. my ( $untrackedRef, $modifiedRef ) = returnState($logger);
  299. my @files;
  300. push(@files,@$untrackedRef); push(@files,@$modifiedRef);
  301. my @filesToCommit;
  302. if ( defined $args{'interactive'} ) {
  303. foreach my $file ( @files ) {
  304. print "Add $file to commit (y/n): ";
  305. my $input = <STDIN>;
  306. chomp $input;
  307. if ( $input =~ m/^Y|^y/ ) {
  308. push(@filesToCommit,$file);
  309. } else {
  310. next;
  311. }
  312. }
  313. } else {
  314. @filesToCommit = @files;
  315. }
  316. print "Commiting the following files:\n";
  317. foreach my $file ( @filesToCommit ) {
  318. print "\t$file\n";
  319. }
  320. if ( defined $args{'interactive'} ) {
  321. print "Does this look correct (y/n) : ";
  322. my $input = <STDIN>;
  323. chomp $input;
  324. if ( $input !~ m/^Y|^y/ ) {
  325. print "Canceling...\n";
  326. exit 1;
  327. }
  328. }
  329. addFiles(\@filesToCommit,$logger);
  330. if ( defined $args{'interactive'} && ! defined $args{'commit-msg'}) {
  331. print "Enter a commit message: ";
  332. my $input = <STDIN>;
  333. chomp $input;
  334. commitChanges($input,$logger);
  335. } elsif ( defined $args{'commit-msg'} ) {
  336. my $commitMsg = "$args{'commit-msg'}";
  337. commitChanges($commitMsg,$logger);
  338. } else {
  339. my $epoch = time();
  340. my $commitMsg = "Generic Commit at $epoch";
  341. commitChanges($commitMsg,$logger);
  342. }
  343. if ( defined $args{'interactive'} ) {
  344. print "Push changes? (y/n): ";
  345. my $input = <STDIN>;
  346. chomp $input;
  347. if ( $input !~ m/^Y|^y/ ) {
  348. # TODO: Unstage changes?
  349. print "Canceling...\n";
  350. exit 1;
  351. }
  352. knock();
  353. my $gitOutput = pushChanges($logger);
  354. print "Git returned:\n$gitOutput\n";
  355. }
  356. else {
  357. knock();
  358. pushChanges($logger);
  359. my $gitOutput = pushChanges($logger);
  360. print "Git returned:\n$gitOutput\n";
  361. }
  362. }
  363. if ( defined $args{'reset-from-master'} ) {
  364. knock();
  365. stashAndReset($logger);
  366. }
  367. if ( defined $args{'reset-from-upstream'} ) {
  368. if ( defined $args{'upstream-url'} ) {
  369. print "Setting upstream to $args{'upstream-url'}\n";
  370. chomp $args{'upstream-url'};
  371. shellex("$gitCmd remote add upstream $args{'upstream-url'}",$logger);
  372. shellex("$gitCmd fetch upstream",$logger);
  373. knock();
  374. resetFromUpstream($logger);
  375. } else {
  376. knock();
  377. resetFromUpstream($logger);
  378. }
  379. }
  380. if ( defined $args{'dump-config'} ) {
  381. my %configHash = readConfig(".",$logger);
  382. foreach my $key ( keys %configHash ) {
  383. my $hRef = $configHash{$key};
  384. print "[$key]\n";
  385. foreach my $ckey ( keys %$hRef ) {
  386. print "\t$ckey = ${$hRef}{$ckey}\n";
  387. }
  388. }
  389. }
  390. if ( defined $args{'configure-local-user'} ) {
  391. if ( defined $args{'interactive'} ) {
  392. print "Enter user to set: ";
  393. my $desiredName = <STDIN>;
  394. chomp $desiredName;
  395. print "Enter email to set: ";
  396. my $desiredEmail = <STDIN>;
  397. chomp $desiredEmail;
  398. appendRepoUserConfig($desiredName,$desiredEmail,$logger);
  399. } else {
  400. appendRepoUserConfig($args{'user'},$args{'email'},$logger);
  401. }
  402. }
  403. if ( defined $args{'knock-clone'} ) {
  404. knock();
  405. basicClone($args{'knock-clone'},$logger);
  406. }
  407. if ( defined $args{'knock-pull'} ) {
  408. knock();
  409. basicPull($logger);
  410. }