-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
git-rec
executable file
·149 lines (114 loc) · 3.37 KB
/
git-rec
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/perl
# abstract: perform git actions recursively
use strict;
my $APP = 'git-rec';
our $VERSION = '0.200';
use Getopt::Long;
use Pod::Usage;
usage() if !@ARGV;
our($cmd, $dir, $branch) = (undef) x 3;
GetOptions(
'c|md:s' => \$cmd,
'd|ir:s' => \$dir,
'b|ranch:s' => \$branch,
'h|help' => sub { pod2usage(verbose => 1) and exit; },
'm|man' => sub { pod2usage(verbose => 3) and exit; },
'v|version' => sub { print "$APP v$VERSION\n" and exit; },
);
($cmd) = $cmd ? $cmd : 'pull';
($dir) = $dir ? $dir : "$ENV{HOME}/devel";
($branch) = $branch ? $branch : 'origin master';
cmd($dir, $cmd);
sub cmd {
my($start, $operation) = @_;
$start =~ s;/$;;;
my @content = grep{! /(?:\.\.?)$/ }<$start/.* $start/*>;
for(@content) {
if(-d $_) {
if($_ =~ /\.git$/) {
my ($basename) = $_ =~ m;(.+)/\.git;;
my ($project) = $_ =~ m;.+/(.+)/\.git;;
my $status = undef;
if($operation eq 'pull') {
chomp($status = `cd $basename && git $cmd $branch 2> /dev/null`);
if($status =~ /Already up-to-date\./) {
printf("\e[1m::\e[38;5;178m %s\e[0m\n\e[38;5;34m Up to date\e[0m\n", $project);
next;
}
else {
printf("\e[1m::\e[38;5;178m %s\e[0m - \e[38;5;162mPulling\e[0m\n", $project);
print "$status\n";
next;
}
}
elsif($operation eq 'status') {
my @records;
chomp(@records = `cd $basename && git status -s`);
@records = git_status(@records);
printf("\e[1m::\e[38;5;178m %s\e[0m\n", $project);
printf(" %s\n", $_) for @records;
}
elsif($operation eq 'log') {
my @records;
chomp(@records = `cd $basename && git --no-pager log`);
@records = git_log(@records);
print "$_\n" for @records;
}
else {
print $operation;
}
}
else {
cmd($_, $operation);
}
}
}
return 0;
}
sub git_status {
for(@_) {
s/^\?\? (.+)$/\e[38;5;137m\e[1mUntracked: \e[0m$1/gms;
s/^ M (.+)$/\e[38;5;34m\e[1m Modified: \e[0m$1\e[0m/gms;
s/^ D (.+)$/\e[38;5;244m\e[1m Deleted: \e[1m$1\e[0m/gms;
}
return(@_);
}
sub git_log {
for(@_) {
s/^(commit) (.+)$/\e[1m$1 \e[0m\e[3m\e[38;5;197m$2\e[0m/;
s/^(Author:) (.+) <(.+)>$/$1 \e[1m\e[38;5;65m$2\e[0m < \e[38;5;208m$3\e[0m >\e[0m/;
s/^(Date) (.+)$/\e[1m$1 \e[0m\e[3m$2\e[0m/;
s/^\s+ (.+)/\e[38;5;249m\t$1\e[0m/;
}
return(@_);
}
sub usage {
print "$APP $VERSION\n";
pod2usage(verbose => 1);
exit(0);
}
=pod
=head1 NAME
git-rec - perform git stuff recursively
=head1 SYNOPSIS
git-rec -d [DIRECTORY] -c [COMMAND] -b [BRANCH]
=head1 OPTIONS
-d, --dir root directory; default: $HOME/devel
-c, --cmd git command; default: pull
-b, --branch branch; default: master
-h, --help show the help and exit
-v, --version show version info and exit
-m, --man show the manpage and exit
=head1 AUTHOR
Magnus Woldrich
CPAN ID: WOLDRICH
http://japh.se
=head1 CONTRIBUTORS
None required yet.
=head1 COPYRIGHT
Copyright 2011 B<THIS PROGRAM>s L</AUTHOR> and L</CONTRIBUTORS> as listed above.
=head1 LICENSE
This program is free software; you may redistribute it and/or modify it under
the same terms as Perl itself.
=cut