sg 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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 knocker);
  9. use SimplyGit::Git qw(
  10. readConfig getStatus returnState addFiles
  11. commitChanges pushChanges stashAndReset resetFromUpstream
  12. updateGitIgnore appendRepoUserConfig parseSGConfig
  13. warnOnUser basicClone basicPull
  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. parseArgs();
  202. my %sgConfig = parseSGConfig($args{'config-file'},$logger);
  203. if ( defined $sgConfig{'UserWarn'} && -d ".git" ) {
  204. warnOnUser($sgConfig{'user.name'},$sgConfig{'user.email'},$logger);
  205. }
  206. sub knock() {
  207. if ( defined $sgConfig{'Knock'} && ( defined $args{'knock'} || defined $args{'knock-clone'} || defined $args{'knock-pull'} ) ) {
  208. print "Knocking...\n";
  209. knocker($sgConfig{'knock.target'},$sgConfig{'ports'},$logger);
  210. }
  211. }
  212. if ( defined $args{'knock'} && scalar keys %args == 2 ) {
  213. print "Just knocking then exiting...\n";
  214. knock();
  215. exit 1;
  216. }
  217. # TODO: This sub could be more concise with a sub to print array refs
  218. if ( defined $args{'view'} ) {
  219. my ( $untrackedRef, $modifiedRef, $addedRef, $deletedRef ) = returnState($logger);
  220. my $refs = shellex("$gitCmd show-ref",$logger);
  221. my $branch = shellex("$gitCmd show-branch",$logger);
  222. my $name = shellex("$gitCmd config --get user.name",$logger);
  223. chomp $name;
  224. my $email = shellex("$gitCmd config --get user.email",$logger);
  225. chomp $email;
  226. print "-->Username: $name\n-->Email: $email\n";
  227. print "On [branch] @ commit: $branch\n";
  228. print "$refs\n";
  229. my $swpWarning = "\t# Likely a Vi .swp file";
  230. my $untrackedTotal = scalar @$untrackedRef;
  231. print "* $untrackedTotal untracked file(s):\n";
  232. foreach my $file ( @$untrackedRef ) {
  233. if ( $file =~ m/.swp/ ) {
  234. print "\t$file $swpWarning\n";
  235. } else {
  236. print "\t$file\n";
  237. }
  238. }
  239. my $modifiedTotal = scalar @$modifiedRef;
  240. print "* $modifiedTotal modified file(s):\n";
  241. foreach my $file ( @$modifiedRef ) {
  242. if ( $file =~ m/.swp/ ) {
  243. print "\t$file $swpWarning\n";
  244. } else {
  245. print "\t$file\n";
  246. }
  247. }
  248. my $commitTotal = scalar @$addedRef;
  249. print "* $commitTotal file(s) added to commit:\n";
  250. foreach my $file ( @$addedRef ) {
  251. if ( $file =~ m/.swp/ ) {
  252. print "\t$file $swpWarning\n";
  253. } else {
  254. print "\t$file\n";
  255. }
  256. }
  257. my $deletedTotal = scalar @$deletedRef;
  258. print "* $deletedTotal file(s) to be deleted from commit:\n";
  259. foreach my $file ( @$deletedRef ) {
  260. if ( $file =~ m/.swp/ ) {
  261. print "\t$file $swpWarning\n";
  262. } else {
  263. print "\t$file\n";
  264. }
  265. }
  266. }
  267. if ( defined $args{'push-all'} ) {
  268. my ( $untrackedRef, $modifiedRef ) = returnState($logger);
  269. my @files;
  270. push(@files,@$untrackedRef); push(@files,@$modifiedRef);
  271. my @filesToCommit;
  272. if ( defined $args{'interactive'} ) {
  273. foreach my $file ( @files ) {
  274. print "Add $file to commit (y/n): ";
  275. my $input = <STDIN>;
  276. chomp $input;
  277. if ( $input =~ m/^Y|^y/ ) {
  278. push(@filesToCommit,$file);
  279. } else {
  280. next;
  281. }
  282. }
  283. } else {
  284. @filesToCommit = @files;
  285. }
  286. print "Commiting the following files:\n";
  287. foreach my $file ( @filesToCommit ) {
  288. print "\t$file\n";
  289. }
  290. if ( defined $args{'interactive'} ) {
  291. print "Does this look correct (y/n) : ";
  292. my $input = <STDIN>;
  293. chomp $input;
  294. if ( $input !~ m/^Y|^y/ ) {
  295. print "Canceling...\n";
  296. exit 1;
  297. }
  298. }
  299. addFiles(\@filesToCommit,$logger);
  300. if ( defined $args{'interactive'} && ! defined $args{'commit-msg'}) {
  301. print "Enter a commit message: ";
  302. my $input = <STDIN>;
  303. chomp $input;
  304. commitChanges($input,$logger);
  305. } elsif ( defined $args{'commit-msg'} ) {
  306. my $commitMsg = "$args{'commit-msg'}";
  307. commitChanges($commitMsg,$logger);
  308. } else {
  309. my $epoch = time();
  310. my $commitMsg = "Generic Commit at $epoch";
  311. commitChanges($commitMsg,$logger);
  312. }
  313. if ( defined $args{'interactive'} ) {
  314. print "Push changes? (y/n): ";
  315. my $input = <STDIN>;
  316. chomp $input;
  317. if ( $input !~ m/^Y|^y/ ) {
  318. # TODO: Unstage changes?
  319. print "Canceling...\n";
  320. exit 1;
  321. }
  322. knock();
  323. my $gitOutput = pushChanges($logger);
  324. print "Git returned:\n$gitOutput\n";
  325. }
  326. else {
  327. knock();
  328. pushChanges($logger);
  329. my $gitOutput = pushChanges($logger);
  330. print "Git returned:\n$gitOutput\n";
  331. }
  332. }
  333. if ( defined $args{'reset-from-master'} ) {
  334. knock();
  335. stashAndReset($logger);
  336. }
  337. if ( defined $args{'reset-from-upstream'} ) {
  338. if ( defined $args{'upstream-url'} ) {
  339. print "Setting upstream to $args{'upstream-url'}\n";
  340. chomp $args{'upstream-url'};
  341. shellex("$gitCmd remote add upstream $args{'upstream-url'}",$logger);
  342. shellex("$gitCmd fetch upstream",$logger);
  343. knock();
  344. resetFromUpstream($logger);
  345. } else {
  346. knock();
  347. resetFromUpstream($logger);
  348. }
  349. }
  350. if ( defined $args{'dump-config'} ) {
  351. my %configHash = readConfig(".",$logger);
  352. foreach my $key ( keys %configHash ) {
  353. my $hRef = $configHash{$key};
  354. print "[$key]\n";
  355. foreach my $ckey ( keys %$hRef ) {
  356. print "\t$ckey = ${$hRef}{$ckey}\n";
  357. }
  358. }
  359. }
  360. if ( defined $args{'configure-local-user'} ) {
  361. if ( defined $args{'interactive'} ) {
  362. print "Enter user to set: ";
  363. my $desiredName = <STDIN>;
  364. chomp $desiredName;
  365. print "Enter email to set: ";
  366. my $desiredEmail = <STDIN>;
  367. chomp $desiredEmail;
  368. appendRepoUserConfig($desiredName,$desiredEmail,$logger);
  369. } else {
  370. appendRepoUserConfig($args{'user'},$args{'email'},$logger);
  371. }
  372. }
  373. if ( defined $args{'knock-clone'} ) {
  374. knock();
  375. basicClone($args{'knock-clone'},$logger);
  376. }
  377. if ( defined $args{'knock-pull'} ) {
  378. knock();
  379. basicPull($logger);
  380. }