-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgit-config-sort
executable file
·60 lines (46 loc) · 1001 Bytes
/
git-config-sort
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
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
if (!$ARGV[0]) {
print <<EOF;
This will sort the keys of a git config file in place.
ie keep your submodules in a nice order to make them simple to read through.
git config-sort [filename]
EOF
exit;
}
my $filename = $ARGV[0];
my %keys;
my @lines = (<>);
my $key = '';
my $data = '';
foreach my $line (@lines) {
# Ignore blank lines
if ($line =~ /^\s+$/) {
next;
}
# Normalise indenting
$line =~ s/^\s+/\t/g;
if ($line =~ /^\[/) {
$keys{$key} = $data;
$key = $line;
$data = $line;
} else {
$data .= $line;
}
}
$keys{$key} = $data;
open(my $fh, '>', $filename) or die "Could not open file '$filename' $!";
foreach my $key (sort keys %keys){
my @lines = split /\n/, $keys{$key};
if (!$key) {
next;
}
print $fh $key;
shift @lines;
@lines = sort @lines;
print $fh join "\n", @lines;
print $fh "\n";
}
close $fh;