-
Notifications
You must be signed in to change notification settings - Fork 19
/
git-store-meta.pl
executable file
·1158 lines (1058 loc) · 39.3 KB
/
git-store-meta.pl
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env perl
# =============================================================================
# Usage: git-store-meta.pl ACTION [OPTION...]
# Store, update, or apply metadata for files revisioned by Git.
#
# ACTION is one of:
# -s|--store Store the metadata for all files revisioned by Git.
# -u|--update Update the metadata for changed files.
# -a|--apply Apply the stored metadata to files in the working tree.
# -i|--install Install hooks in this repo for automated update/apply.
# (pre-commit, post-checkout, and post-merge)
# -h|--help Print this help and exit.
# --version Show current version and exit.
#
# Available OPTIONs are:
# -f|--fields FIELDs Fields to handle (see below). If omitted, fields in the
# current metadata store file are picked when possible;
# otherwise, "mtime" is picked as the default.
# (available for: --store, --apply)
# -d|--directory Also store, update, or apply for directories.
# (available for: --store, --apply)
# --no-directory Do not store, update, or apply for directories.
# (available for: --store, --apply)
# --topdir Also store, update, or apply for the top directory.
# (available for: --store, --apply)
# --no-topdir Do not store, update, or apply for the top directory.
# (available for: --store, --apply)
# --lazy Check whether metadata will be changed before write
# when applying. This can boost performance if only few
# files or directories have metadata actually changed.
# (available for: --store, --apply)
# --no-lazy Do not apply lazily.
# (available for: --store, --apply)
# -n|--dry-run Run a test and print the output, without real action.
# (available for: --store, --update, --apply)
# -v|--verbose Apply with verbose output.
# (available for: --apply)
# --force Force an apply even if the working tree is not clean. Or
# install hooks and overwrite existing ones.
# (available for: --apply, --install)
# -t|--target FILE Specify another filename to store metadata. Defaults to
# ".git_store_meta" in the root of the working tree.
# (available for: --store, --update, --apply, --install)
#
# FIELDs is a comma-separated string consisting of the following values:
# mtime last modified time
# atime last access time
# mode Unix permissions
# user user name
# group group name
# uid user ID (if user is also set, prefer user and fallback to uid)
# gid group ID (if group is also set, prefer group and fallback to gid)
# acl access control lists for POSIX setfacl/getfacl
#
# git-store-meta 2.3.7
# Copyright (c) 2015-2023, Danny Lin
# Released under MIT License
# Project home: https://github.com/danny0838/git-store-meta
# =============================================================================
use utf8;
use strict;
use warnings;
use version; our $VERSION = version->declare("v2.3.7");
use Getopt::Long;
Getopt::Long::Configure qw(gnu_getopt);
use File::Basename;
use File::Path qw(make_path);
use File::Spec::Functions qw(rel2abs abs2rel catfile);
use POSIX qw(strftime);
use Time::Local;
# define constants
my $GIT_STORE_META_PREFIX = "# generated by";
my $GIT_STORE_META_APP = "git-store-meta";
my $GIT_STORE_META_FILENAME = ".git_store_meta";
my $GIT = "git";
my @ACTIONS = ('help', 'version', 'install', 'update', 'store', 'apply');
my @FIELDS = ('file', 'type', 'mtime', 'atime', 'mode', 'uid', 'gid', 'user', 'group', 'acl');
my %CONFIGS = (
directory => undef,
topdir => undef,
lazy => undef,
);
# runtime variables
my $script = rel2abs(__FILE__);
my $action;
my $gitdir;
my $topdir;
my $git_store_meta_filename;
my $git_store_meta_file;
my $git_store_meta_header;
my $temp_file;
my $cache_file_exist = 0;
my $cache_file_accessible = 0;
my $cache_header_valid = 0;
my $cache_app;
my $cache_version;
my %cache_configs;
my @cache_fields;
my $touch;
my $chown;
my $getfacl;
my $setfacl;
my $configs;
# parse arguments
my %argv = (
"store" => 0,
"update" => 0,
"apply" => 0,
"install" => 0,
"help" => 0,
"version" => 0,
"target" => undef,
"fields" => undef,
"directory" => undef,
"topdir" => undef,
"lazy" => undef,
"force" => 0,
"dry-run" => 0,
"verbose" => 0,
);
GetOptions(
"store|s" => \$argv{'store'},
"update|u" => \$argv{'update'},
"apply|a" => \$argv{'apply'},
"install|i" => \$argv{'install'},
"help|h" => \$argv{'help'},
"version" => \$argv{'version'},
"fields|f=s" => \@{$argv{'fields'}},
"directory|d!" => \$argv{'directory'},
"topdir!" => \$argv{'topdir'},
"lazy!" => \$argv{'lazy'},
"force" => \$argv{'force'},
"dry-run|n" => \$argv{'dry-run'},
"verbose|v" => \$argv{'verbose'},
"target|t=s" => \$argv{'target'},
) or die;
# determine action
foreach (@ACTIONS) {
if ($argv{$_}) { $action = $_; last; }
}
# handle action: help, version, and unknown
if (!defined($action)) {
usage();
exit 1;
} elsif ($action eq "help") {
usage();
exit 0;
} elsif ($action eq "version") {
print $VERSION . "\n";
exit 0;
}
# init and validate gitdir
$gitdir = `$GIT rev-parse --git-dir 2>/dev/null`
or die "error: unknown git repository.\n";
chomp($gitdir);
# handle action: install
if ($action eq "install") {
print "installing hooks...\n";
install_hooks();
exit 0;
}
# init and validate topdir
$topdir = `$GIT rev-parse --show-cdup 2>/dev/null`
or die "error: current working directory is not in a git working tree.\n";
chomp($topdir);
# cd to the top level directory of current git repo
chdir($topdir) if $topdir;
# init paths and header info
$git_store_meta_filename = defined($argv{'target'}) ? $argv{'target'} : $GIT_STORE_META_FILENAME;
$git_store_meta_file = rel2abs($git_store_meta_filename);
$temp_file = catfile($gitdir, $GIT_STORE_META_FILENAME . ".tmp");
get_cache_header_info();
# handle action: store, update, apply
# validate
if ($action eq "store") {
print "storing metadata to `$git_store_meta_file' ...\n";
} elsif ($action eq "update") {
print "updating metadata to `$git_store_meta_file' ...\n";
if (!$cache_file_exist) {
die "error: `$git_store_meta_file' doesn't exist.\nRun --store to create new.\n";
}
if (!$cache_file_accessible) {
die "error: `$git_store_meta_file' is not an accessible file.\n";
}
if (!$cache_header_valid) {
die "error: `$git_store_meta_file' is malformatted.\nFix it or run --store to create new.\n";
}
if ($cache_app ne $GIT_STORE_META_APP) {
die "error: `$git_store_meta_file' is using an unknown schema: $cache_app $cache_version\nFix it or run --store to create new.\n";
}
if (!(2.3.0 <= $cache_version && $cache_version < 2.4.0)) {
die "error: `$git_store_meta_file' is using an unsupported version: $cache_version\n";
}
} elsif ($action eq "apply") {
print "applying metadata from `$git_store_meta_file' ...\n";
if (!$cache_file_exist) {
print "`$git_store_meta_file' doesn't exist, skipped.\n";
exit;
}
if (!$argv{'force'} && `$GIT status --porcelain -uno --ignore-submodules=all -z 2>/dev/null` ne "") {
die "error: git working tree is not clean.\nCommit, stash, or revert changes before running this, or add --force.\n";
}
if (!$cache_file_accessible) {
die "error: `$git_store_meta_file' is not an accessible file.\n";
}
if (!$cache_header_valid) {
die "error: `$git_store_meta_file' is malformatted.\n";
}
if ($cache_app ne $GIT_STORE_META_APP) {
die "error: `$git_store_meta_file' is using an unknown schema: $cache_app $cache_version\n";
}
}
# adjust fields, configs and output header
fix_fields: {
# use $argv{'fields'} if provided, or use fields in the cache file
# special handling for --update, which must take fields in the cache file
if (@{$argv{'fields'}} > 0 && $action ne "update") {
@{$argv{'fields'}} = split(/\s*,\s*/, join(",", @{$argv{'fields'}}));
unshift(@{$argv{'fields'}}, "file", "type");
} elsif ($cache_header_valid) {
@{$argv{'fields'}} = @cache_fields;
} else {
@{$argv{'fields'}} = ("file", "type", "mtime");
}
# filter fields
my %FIELDS = map { $_ => 1 } @FIELDS;
my @fields;
foreach (@{$argv{'fields'}}) {
if ($FIELDS{$_}) {
$FIELDS{$_} = 0;
push (@fields, $_);
}
}
@{$argv{'fields'}} = @fields;
}
fix_configs: {
# use value in the cache file for unspecified configs
while (my ($key, $value) = each(%cache_configs)) {
if (!defined($argv{$key}) || $action eq 'update') {
$argv{$key} = $value;
}
}
# Versions < 2 don't record --directory
# Add "--directory" if a directory entry exists.
if (defined($cache_version) && $cache_version < 2) {
if (has_directory_entry()) {
$argv{'directory'} = 1;
}
}
# set default configs and header to write
my @configs;
foreach my $key (sort keys %CONFIGS) {
my $value = $CONFIGS{$key};
$argv{$key} = $value if !defined($argv{$key});
if (!defined($value)) {
# boolean style
push(@configs, "--$key") if $argv{$key};
} else {
push(@configs, "--$key=$argv{$key}");
}
}
my $version = substr($VERSION, 1);
$configs = join(' ', @configs);
if ($configs) {
$git_store_meta_header = join("\t", $GIT_STORE_META_PREFIX, $GIT_STORE_META_APP, $version, $configs) . "\n";
} else {
$git_store_meta_header = join("\t", $GIT_STORE_META_PREFIX, $GIT_STORE_META_APP, $version) . "\n";
}
}
prepare_subroutines: {
if (eval { require File::lchown; }) {
$touch = \&touch_internal;
$chown = \&chown_internal;
} else {
$touch = \&touch_external;
$chown = \&chown_external;
}
if (eval { require Linux::ACL; }) {
$getfacl = \&getfacl_internal;
# Linux::ACL::setfacl (0.05) has an issue of adding "mask" field even
# when not specified, which breaks compatibility with setfacl. Disable
# it temporarily until it's fixed.
# https://github.com/nazarov-yuriy/Linux--ACL/issues/2
#
# $setfacl = \&setfacl_internal;
$setfacl = \&setfacl_external;
} else {
$getfacl = \&getfacl_external;
$setfacl = \&setfacl_external;
}
}
# show settings
print "fields: " . join(", ", @{$argv{'fields'}}) . "\n";
print "flags: " . $configs . "\n" if $configs;
# do the action
if ($action eq "store") {
if (!$argv{'dry-run'}) {
make_path(dirname($git_store_meta_file));
open(GIT_STORE_META_FILE, '>', $git_store_meta_file)
or die "error: failed to write to `$git_store_meta_file': $!\n";
select(GIT_STORE_META_FILE);
store();
close(GIT_STORE_META_FILE);
select(STDOUT);
} else {
store();
}
} elsif ($action eq "update") {
# copy the cache file to the temp file
# to prevent a conflict in further operation
open(GIT_STORE_META_FILE, "<:crlf", $git_store_meta_file)
or die "error: failed to access `$git_store_meta_file': $!\n";
open(TEMP_FILE, ">", $temp_file) or die;
# discard first 2 lines (header)
<GIT_STORE_META_FILE>;
<GIT_STORE_META_FILE>;
while (<GIT_STORE_META_FILE>) {
print TEMP_FILE;
}
close(TEMP_FILE);
close(GIT_STORE_META_FILE);
# update cache
if (!$argv{'dry-run'}) {
open(GIT_STORE_META_FILE, '>', $git_store_meta_file)
or die "error: failed to write to `$git_store_meta_file': $!\n";
select(GIT_STORE_META_FILE);
update();
close(GIT_STORE_META_FILE);
select(STDOUT);
} else {
update();
}
# clean up
unlink($temp_file);
} elsif ($action eq "apply") {
apply();
}
# -----------------------------------------------------------------------------
sub get_file_type {
my ($file) = @_;
if (-l $file) {
return "l";
} elsif (-f $file) {
return "f";
} elsif (-d $file) {
return "d";
}
return undef;
}
sub timestamp_to_gmtime {
my ($timestamp) = @_;
my @t = gmtime($timestamp);
return strftime("%Y-%m-%dT%H:%M:%SZ", @t);
}
sub gmtime_to_timestamp {
my ($gmtime) = @_;
$gmtime =~ m/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z$/;
return timegm($6, $5, $4, $3, $2 - 1, $1);
}
# escape a string to be safe to use as a shell script argument
sub escapeshellarg {
my ($str) = @_;
$str =~ s/'/'\\''/g;
return "'$str'";
}
# escape special chars in a filename to be safe to stay in the data file
sub escape_filename {
my ($str) = @_;
$str =~ s/([\x00-\x1F\x5C\x7F])/'\x'.sprintf("%02X", ord($1))/eg;
return $str;
}
# reverse of escape_filename
# "\\" is left for backward compatibility with versions < 1.1.4
sub unescape_filename {
my ($str) = @_;
$str =~ s/\\(?:x([0-9A-Fa-f]{2})|\\)/$1?chr(hex($1)):"\\"/eg;
return $str;
}
sub touch_internal {
my ($atime, $mtime, $file) = @_;
$atime = $atime ? gmtime_to_timestamp($atime) : (lstat($file))[8];
$mtime = $mtime ? gmtime_to_timestamp($mtime) : (lstat($file))[9];
return File::lchown::lutimes($atime, $mtime, $file);
}
sub touch_external {
my ($atime, $mtime, $file) = @_;
if (-l $file) {
my @cmds;
if ($atime) {
push(@cmds, join(" ", ("touch", "-hcad", escapeshellarg($atime), escapeshellarg("./$file"), "2>&1")));
}
if ($mtime) {
push(@cmds, join(" ", ("touch", "-hcmd", escapeshellarg($mtime), escapeshellarg("./$file"), "2>&1")));
}
my $cmd = join(' && ', @cmds);
`$cmd`;
return ($? == 0);
}
$atime = $atime ? gmtime_to_timestamp($atime) : (lstat($file))[8];
$mtime = $mtime ? gmtime_to_timestamp($mtime) : (lstat($file))[9];
return utime($atime, $mtime, $file);
}
sub chown_internal {
my ($uid, $gid, $file) = @_;
$uid = defined($uid) ? $uid : (lstat($file))[4];
$gid = defined($gid) ? $gid : (lstat($file))[5];
return File::lchown::lchown($uid, $gid, $file);
}
sub chown_external {
my ($uid, $gid, $file) = @_;
if (-l $file) {
my $cmd = join(" ", ("chown", "-h", escapeshellarg($uid).':'.escapeshellarg($gid), escapeshellarg("./$file"), "2>&1"));
`$cmd`;
return ($? == 0);
}
$uid = defined($uid) ? $uid : (lstat($file))[4];
$gid = defined($gid) ? $gid : (lstat($file))[5];
return chown($uid, $gid, $file);
}
# Serialization: join lines with ","
sub getfacl_internal {
my ($file) = @_;
if (-l $file) {
return "";
}
my @results;
my ($acl, $default_acl) = Linux::ACL::getfacl($file);
if (defined $acl) {
my %acl = %{$acl};
if (defined $acl{'uperm'}) {
push(@results, "user::" . getfacl_internal_getperms(\%{$acl{'uperm'}}));
}
if (defined $acl{'user'}) {
foreach my $uid (keys %{$acl{'user'}}) {
my $user = getpwuid($uid);
$user = defined($user) ? $user : $uid;
push(@results, "user:$user:" . getfacl_internal_getperms(\%{$acl{'user'}{$uid}}));
}
}
if (defined $acl{'gperm'}) {
push(@results, "group::" . getfacl_internal_getperms(\%{$acl{'gperm'}}));
}
if (defined $acl{'group'}) {
foreach my $gid (keys %{$acl{'group'}}) {
my $group = getgrgid($gid);
$group = defined($group) ? $group : $gid;
push(@results, "group:$group:" . getfacl_internal_getperms(\%{$acl{'group'}{$gid}}));
}
}
if (defined $acl{'mask'}) {
push(@results, "mask::" . getfacl_internal_getperms(\%{$acl{'mask'}}));
}
if (defined $acl{'other'}) {
push(@results, "other::" . getfacl_internal_getperms(\%{$acl{'other'}}));
}
}
if (defined $default_acl) {
my %acl = %{$default_acl};
if (defined $acl{'uperm'}) {
push(@results, "default:user::" . getfacl_internal_getperms(\%{$acl{'uperm'}}));
}
if (defined $acl{'gperm'}) {
push(@results, "default:group::" . getfacl_internal_getperms(\%{$acl{'gperm'}}));
}
if (defined $acl{'mask'}) {
push(@results, "default:mask::" . getfacl_internal_getperms(\%{$acl{'mask'}}));
}
if (defined $acl{'other'}) {
push(@results, "default:other::" . getfacl_internal_getperms(\%{$acl{'other'}}));
}
}
return join(",", @results);
}
sub getfacl_internal_getperms {
my ($perms) = @_;
my %perms = %{$perms};
return ($perms{'r'} ? 'r' : '-') . ($perms{'w'} ? 'w' : '-') . ($perms{'x'} ? 'x' : '-');
}
sub getfacl_external {
my ($file) = @_;
my $cmd = join(" ", ("getfacl", "-PcE", escapeshellarg("./$file"), "2>/dev/null"));
my $acl = `$cmd`; $acl =~ s/\n+$//; $acl =~ s/\n/,/g;
return $acl;
}
sub setfacl_internal {
my ($acl, $file) = @_;
if (-l $file) {
return 1;
}
my @acl;
foreach my $line (split(",", $acl)) {
my @parts = split(":", $line);
my $index;
my $field;
my $id;
my $perms;
if ($#parts == 2) {
($field, $id, $perms) = @parts;
$index = 0;
} else {
($_, $field, $id, $perms) = @parts;
$index = 1;
}
if ($id eq '') {
if ($field eq 'user') {
$field = 'uperm';
} elsif ($field eq 'group') {
$field = 'gperm';
}
$acl[$index]{$field}{'r'} = substr($perms, 0, 1) ne '-' ? 1 : 0;
$acl[$index]{$field}{'w'} = substr($perms, 1, 1) ne '-' ? 1 : 0;
$acl[$index]{$field}{'x'} = substr($perms, 2, 1) ne '-' ? 1 : 0;
} else {
if ($id =~ m/^\D/) {
$id = (getpwnam($id))[2];
}
$acl[$index]{$field}{$id}{'r'} = substr($perms, 0, 1) ne '-' ? 1 : 0;
$acl[$index]{$field}{$id}{'w'} = substr($perms, 1, 1) ne '-' ? 1 : 0;
$acl[$index]{$field}{$id}{'x'} = substr($perms, 2, 1) ne '-' ? 1 : 0;
}
}
return Linux::ACL::setfacl($file, @acl);
}
sub setfacl_external {
my ($acl, $file) = @_;
my $cmd = join(" ", ("setfacl", "-Pbm", escapeshellarg($acl), escapeshellarg("./$file"), "2>&1"));
`$cmd`;
return ($? == 0);
}
# Print the initial comment block, from first to second "# ==",
# with "# " removed
sub usage {
my $start = 0;
open(GIT_STORE_META, "<:crlf", $script)
or die "error: failed to access `$script': $!\n";
while (<GIT_STORE_META>) {
if (m/^# ={2,}/) {
if (!$start) { $start = 1; next; }
else { last; }
}
next if !$start;
s/^# ?//;
print;
}
close(GIT_STORE_META);
}
sub install_hooks {
# Ensure hook files don't exist unless --force
if (!$argv{'force'}) {
my $err = '';
foreach my $n ("pre-commit", "post-checkout", "post-merge") {
my $f = "$gitdir/hooks/$n";
if (-e "$f") {
$err .= "error: hook file `$f' already exists.\n";
}
}
if ($err) { die $err . "Add --force to overwrite current hook files.\n"; }
}
# Install the hooks
# CWD of a hook is the top level of working tree.
my $mask = umask; if (!defined($mask)) { $mask = 0022; }
my $mode = 0777 & ~$mask;
my $t;
my $s = escapeshellarg($GIT_STORE_META_APP . ".pl");
my $f = defined($argv{'target'}) ? " -t " . escapeshellarg($argv{'target'}) : "";
my $f2 = escapeshellarg(defined($argv{'target'}) ? $argv{'target'} : $GIT_STORE_META_FILENAME);
$t = "$gitdir/hooks/pre-commit";
open(FILE, '>', $t) or die "error: failed to write to `$t': $!\n";
printf FILE <<'EOF', $s, $f, $f2;
#!/bin/sh
git_store_meta() (
script=$(dirname "$0")/%1$s; [ ! -x "$script" ] && script=%1$s
# skip if a merge, revert, etc. is in progress
# ref: https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh
r=""
if [ -d "$(git rev-parse --git-path rebase-merge)" ]; then
r="rebase"
elif [ -d "$(git rev-parse --git-path rebase-apply)" ]; then
if [ -d "$(git rev-parse --git-path rebase-apply/rebasing)" ]; then
r="rebase"
elif [ -d "$(git rev-parse --git-path rebase-apply/applying)" ]; then
r="am"
else
r="am/rebase"
fi
elif [ -f "$(git rev-parse --git-path MERGE_HEAD)" ]; then
r="merge"
elif [ -f "$(git rev-parse --git-path SQUASH_MSG)" ]; then
r="squashing merge"
elif [ -f "$(git rev-parse --git-path REVERT_HEAD)" ]; then
r="revert"
elif [ -f "$(git rev-parse --git-path CHERRY_PICK_HEAD)" ]; then
r="cherry-pick"
elif [ -d "$(git rev-parse --git-path sequencer)" ]; then
f=$(git rev-parse --git-path sequencer/todo)
[ -r "$f" ] && IFS=$'\r\n' read todo <"$f"
case "$todo" in
p[\ \ ]*|pick[\ \ ]*) r="cherry-pick";;
revert[\ \ ]*) r="revert";;
esac
elif [ -f "$(git rev-parse --git-path BISECT_LOG)" ]; then
r="bisect"
fi
[ -n "$r" ] && echo "skipped updating metadata for a commit during a $r..." && return
# update (or store as fallback) only when the cache file exists
[ ! -f %3$s ] && return
"$script" --update%2$s || "$script" --store%2$s || return 1
git add %3$s
)
git_store_meta "$@" || exit 1
EOF
close(FILE);
chmod($mode, $t) == 1 or die "error: failed to set permissions on `$t': $!\n";
print "created `$t'\n";
$t = "$gitdir/hooks/post-checkout";
open(FILE, '>', $t) or die "error: failed to write to `$t': $!\n";
printf FILE <<'EOF', $s, $f;
#!/bin/sh
git_store_meta() (
script=$(dirname "$0")/%1$s; [ ! -x "$script" ] && script=%1$s
sha_old=$1; sha_new=$2; change_br=$3
# apply metadata only when HEAD is changed
[ "$sha_new" = "$sha_old" ] && return
"$script" --apply%2$s
)
git_store_meta "$@" || exit 1
EOF
close(FILE);
chmod($mode, $t) == 1 or die "error: failed to set permissions on `$t': $!\n";
print "created `$t'\n";
$t = "$gitdir/hooks/post-merge";
open(FILE, '>', $t) or die "error: failed to write to `$t': $!\n";
printf FILE <<'EOF', $s, $f;
#!/bin/sh
git_store_meta() (
script=$(dirname "$0")/%1$s; [ ! -x "$script" ] && script=%1$s
is_squash=$1
# apply metadata only after a successful non-squash merge
[ $is_squash -ne 0 ] && return
"$script" --apply%2$s
)
git_store_meta "$@" || exit 1
EOF
close(FILE);
chmod($mode, $t) == 1 or die "error: failed to set permissions on `$t': $!\n";
print "created `$t'\n";
}
sub get_cache_header_info {
-e $git_store_meta_file or return;
$cache_file_exist = 1;
-f $git_store_meta_file and open(GIT_STORE_META_FILE, "<:crlf", $git_store_meta_file) or return;
$cache_file_accessible = 1;
# first line: retrieve the header
my $line = <GIT_STORE_META_FILE>;
$line or return;
chomp($line);
my ($prefix, $app, $version, $configs) = split("\t", $line);
$prefix eq $GIT_STORE_META_PREFIX or return;
$cache_app = $app;
eval { $cache_version = version->parse("v" . $version); } or return;
if (defined($configs)) {
foreach (split(/\s+/, $configs)) {
if (m/^--([^=\s]+)=([^=\s]+)$/) {
$cache_configs{$1} = $2;
} elsif (m/^--([^=\s]+)$/) {
$cache_configs{$1} = 1;
}
}
}
# second line: retrieve the fields
$line = <GIT_STORE_META_FILE>;
$line or return;
chomp($line);
foreach (split("\t", $line)) {
m/^<(.*)>$/ and push(@cache_fields, $1) or return;
}
# check for existence of "file" and "type" fields
grep { $_ eq 'file' } @cache_fields or return;
grep { $_ eq 'type' } @cache_fields or return;
close(GIT_STORE_META_FILE);
$cache_header_valid = 1;
}
sub has_directory_entry {
open(GIT_STORE_META_FILE, "<:crlf", $git_store_meta_file) or die;
# skip first 2 lines (header)
<GIT_STORE_META_FILE>;
<GIT_STORE_META_FILE>;
while (<GIT_STORE_META_FILE>) {
s/^\s+//; s/\s+$//;
next if $_ eq "";
return 1 if (split "\t")[1] eq "d";
}
return 0;
}
sub get_file_metadata {
my ($file, $fields) = @_;
my @fields = @{$fields};
my @rec;
my $type = get_file_type($file);
return @rec if !$type; # skip unsupported "file" types
# evaluate stat, which is almost always used
my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = lstat($file);
# output formatted data
# @TODO: further optimization to prevent a possible long condition checking?
foreach (@fields) {
if ($_ eq "file") {
push(@rec, escape_filename($file));
} elsif ($_ eq "type") {
push(@rec, $type);
} elsif ($_ eq "mtime") {
push(@rec, timestamp_to_gmtime($mtime));
} elsif ($_ eq "atime") {
push(@rec, timestamp_to_gmtime($atime));
} elsif ($_ eq "mode") {
$mode = sprintf("%04o", $mode & 07777);
$mode = "" if $type eq "l"; # do not apply mode for symlinks
push(@rec, $mode);
} elsif ($_ eq "uid") {
push(@rec, $uid);
} elsif ($_ eq "gid") {
push(@rec, $gid);
} elsif ($_ eq "user") {
my $user = getpwuid($uid);
push(@rec, $user || "");
} elsif ($_ eq "group") {
my $group = getgrgid($gid);
push(@rec, $group || "");
} elsif ($_ eq "acl") {
push(@rec, &$getfacl($file));
}
}
return @rec;
}
sub store {
my @fields = @{$argv{'fields'}};
# read the file list and write retrieved metadata to a temp file
open(TEMP_FILE, ">", $temp_file) or die;
list: {
# set input record separator for chomp
local $/ = "\0";
open(CMD, "$GIT ls-files -s -z |") or die;
while(<CMD>) {
chomp;
next if m/^160000 /; # skip submodules
s/^.*?\t//; # remove fields other than filename
next if $_ eq $git_store_meta_filename; # skip data file
$_ = join("\t", get_file_metadata($_, \@fields));
print TEMP_FILE "$_\n" if $_;
}
close(CMD);
if ($argv{'directory'}) {
open(CMD, "$GIT ls-tree -rd -z \$($GIT write-tree) |") or die;
while(<CMD>) {
chomp;
next if m/^160000 /; # skip submodules
s/^.*?\t//; # remove fields other than filename
$_ = join("\t", get_file_metadata($_, \@fields));
print TEMP_FILE "$_\n" if $_;
}
close(CMD);
if ($argv{'topdir'}) {
$_ = join("\t", get_file_metadata(".", \@fields));
print TEMP_FILE "$_\n" if $_;
}
}
}
close(TEMP_FILE);
# output sorted entries
print $git_store_meta_header;
print join("\t", map {"<" . $_ . ">"} @fields) . "\n";
open(CMD, "LC_ALL=C sort <".escapeshellarg($temp_file)." |") or die;
while (<CMD>) { print; }
close(CMD);
# clean up
unlink($temp_file);
}
sub update {
my @fields = @{$argv{'fields'}};
# append new entries to the temp file
open(TEMP_FILE, ">>", $temp_file) or die;
list: {
# set input record separator for chomp
local $/ = "\0";
# go through the diff list and append entries
open(CMD, "$GIT diff --name-status --cached --no-renames --ignore-submodules=all -z |") or die;
while(my $stat = <CMD>) {
chomp($stat);
my $file = <CMD>;
chomp($file);
if ($stat eq "M") {
# a modified file
print TEMP_FILE escape_filename($file)."\0\2M\0\n";
} elsif ($stat eq "A") {
# an added file
print TEMP_FILE escape_filename($file)."\0\2M\0\n";
# mark ancestor directories as modified
if ($argv{'directory'}) {
my @parts = split("/", $file);
pop(@parts);
while ($#parts >= 0) {
$file = join("/", @parts);
print TEMP_FILE escape_filename($file)."\0\2M\0\n";
pop(@parts);
}
}
} elsif ($stat eq "D") {
# a deleted file
print TEMP_FILE escape_filename($file)."\0\0D\0\n";
# mark ancestor directories as deleted (temp and revertable)
# mark parent directory as modified
if ($argv{'directory'}) {
my @parts = split("/", $file);
pop(@parts);
if ($#parts >= 0) {
$file = join("/", @parts);
print TEMP_FILE escape_filename($file)."\0\2M\0\n";
}
while ($#parts >= 0) {
$file = join("/", @parts);
print TEMP_FILE escape_filename($file)."\0\0D\0\n";
pop(@parts);
}
}
}
}
close(CMD);
# add all directories as a placeholder, which prevents deletion
if ($argv{'directory'}) {
open(CMD, "$GIT ls-tree -rd --name-only -z \$($GIT write-tree) |") or die;
while(<CMD>) { chomp; print TEMP_FILE "$_\0\1H\0\n"; }
close(CMD);
# update topdir
if ($argv{'topdir'}) {
print TEMP_FILE ".\0\2M\0\n";
}
}
}
close(TEMP_FILE);
# output sorted entries
print $git_store_meta_header;
print join("\t", map {"<" . $_ . ">"} @fields) . "\n";
my $cur_line = "";
my $cur_file = "";
my $cur_stat = "";
my $last_file = "";
open(CMD, "LC_ALL=C sort <".escapeshellarg($temp_file)." |") or die;
# Since sorted, same paths are grouped together, with the changed entries
# sorted prior.
# We print the first seen entry and skip subsequent entries with a same
# path, so that the original entry is overwritten.
while ($cur_line = <CMD>) {
chomp($cur_line);
if ($cur_line =~ m/\x00[\x00-\x02]+(\w+)\x00/) {
# has mark: a changed entry line
$cur_stat = $1;
$cur_line =~ s/\x00[\x00-\x02]+\w+\x00//;
$cur_file = $cur_line;
if ($cur_stat eq "D") {
# a delete => clear $cur_line so that this path is not printed
$cur_line = "";
} elsif ($cur_stat eq "H") {
# a placeholder => revert previous "delete"
# This is after a delete (optionally) and before a modify or
# no-op line (must). We clear $last_file so the next line will
# see a "path change" and be printed.
$last_file = "";
next;
}
} else {
# a no-op line
$cur_stat = "";
($cur_file) = split("\t", $cur_line);
$cur_line .= "\n";
}
# print for a new file
if ($cur_file ne $last_file) {
if ($cur_stat eq "M") {
# a modify => retrieve file metadata to print
if ($cur_file eq $git_store_meta_filename) {
# skip data file
$cur_line = "";
} else {
$_ = join("\t", get_file_metadata(unescape_filename($cur_file), \@fields));
$cur_line = $_ ? "$_\n" : "";
}
}
print $cur_line;
$last_file = $cur_file;
}
}
close(CMD);
}
sub apply {
my @fields = @{$argv{'fields'}};
my %fields_used = map { $_ => 1 } @fields;
my @fields0;
my %fields0_index;
if ($argv{'lazy'}) {
my %fields0;
foreach my $k (keys %fields_used) {
next if $k eq 'file';
next if $k eq 'type';
$k = 'uid' if $k eq 'user';
$k = 'gid' if $k eq 'group';
$fields0{$k} = 1;
}
@fields0 = keys %fields0;
while (my ($i, $v) = each @fields0) {
$fields0_index{$v} = $i;
}