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
|
#!/usr/bin/perl
use strict;
use warnings;
use File::Path qw(make_path);
use File::Copy qw(copy);
use File::Find;
use File::Basename;
use File::Glob qw(:bsd_glob);
use File::Spec;
use Digest::SHA qw(sha1_hex);
use constant VCX_DIR => '.vcx';
use constant BSE_DIR => VCX_DIR . '/bse';
use constant OBJ_DIR => VCX_DIR . '/obj';
use constant TMP_DIR => VCX_DIR . '/tmp';
use constant IGNORE_FILE => '.vcxignore';
my $cmd = shift @ARGV // '';
my @paths = @ARGV;
if ($cmd eq 'init') {
init_repo();
} elsif ($cmd eq 'status') {
run_status();
} elsif ($cmd eq 'add') {
die "Usage: $0 add [path1] [path2] ...\n" unless @paths;
run_add(@paths);
} else {
print "Usage: $0 [init|status|add]\n";
exit 1;
}
sub init_repo {
make_path(OBJ_DIR, BSE_DIR, TMP_DIR);
print "Repository ready\n";
}
sub run_status {
my $compare_file = sub {
return if $File::Find::name =~ /^\.\/\Q${\VCX_DIR}\E/;
return if -d $File::Find::name;
my $path = File::Spec->abs2rel($File::Find::name, '.');
$path =~ s|^\./||;
my $base_path = File::Spec->catfile(BSE_DIR, $path);
if (-e $base_path) {
if (system("diff -q '$File::Find::name' '$base_path' > /dev/null") != 0) {
print "[M] $path" . (check_staged_status($path, 'M') ? " (staged)" : "") . "\n";
}
} else {
print "[N] $path" . (check_staged_status($path, 'N') ? " (staged)" : "") . "\n";
}
};
find({ wanted => $compare_file, no_chdir => 1 }, '.');
find({
wanted => sub {
return if -d $_;
my $rel = File::Spec->abs2rel($_, BSE_DIR);
if (!-e $rel) {
print "[D] $rel" . (check_staged_status($rel, 'D') ? " (staged)" : "") . "\n";
}
},
no_chdir => 1
}, BSE_DIR);
}
sub check_staged_status {
my ($path, $type) = @_;
my $tmp_link = File::Spec->catfile(TMP_DIR, $path);
return 0 unless -l $tmp_link;
my $staged_target = readlink($tmp_link);
if (-f $path) {
# The staged target (e.g., ../obj/path.tmp) must exist to diff
my $abs_target = File::Spec->rel2abs($staged_target, dirname($tmp_link));
return 0 unless -e $abs_target;
return (system("diff -q '$path' '$abs_target' > /dev/null") == 0);
}
if (-l $path) {
# Compare where the work tree link points vs where the staging link points
return (readlink($path) eq $staged_target);
}
return 0;
}
sub run_add {
my (@targets) = @_;
foreach my $target (@targets) {
# Expand globs (e.g., *.txt) for each target
my @expanded = ($target eq '.') ? ('.') : bsd_glob($target);
foreach my $t (@expanded) {
next unless -e $t; # Skip if file doesn't exist
find({
wanted => sub {
return if $File::Find::name =~ /^\.\/\Q${\VCX_DIR}\E/;
my $rel = File::Spec->abs2rel($File::Find::name, '.');
$rel =~ s|^\./||;
my $tmp_link = File::Spec->catfile(TMP_DIR, $rel);
if (-f $File::Find::name && !-l $File::Find::name) {
_sync_file_to_obj($File::Find::name, undef, $tmp_link);
}
elsif (-l $File::Find::name) {
_sync_symlink_to_tmp($File::Find::name, $tmp_link);
}
},
no_chdir => 1,
}, $t);
_handle_deletions($t);
}
}
}
# For Regular Files: Copy to OBJ and link to TMP
sub _sync_file_to_obj {
my ($src, $obj_path_not_used, $tmp) = @_;
my $rel_path = File::Spec->abs2rel($src, '.');
$rel_path =~ s|^\./||;
my $filename = sha1_hex($rel_path) . ".tmp";
my $obj = File::Spec->catfile(OBJ_DIR, $filename);
make_path(dirname($tmp));
copy($src, $obj) or die "Copy failed: $!";
my $target = File::Spec->abs2rel($obj, dirname($tmp));
unlink($tmp) if -e $tmp || -l $tmp;
symlink($target, $tmp) or die "Symlink failed: $!";
}
# For Symlinks: Mirror the symlink into TMP
sub _sync_symlink_to_tmp {
my ($src, $tmp) = @_;
my $target = readlink($src); # Read where the original points
make_path(dirname($tmp));
unlink($tmp) if -e $tmp || -l $tmp;
symlink($target, $tmp); # Create exact same link in TMP
print "[Add Link] $src -> $target\n";
}
sub _handle_deletions {
my ($target) = @_;
# If target is '.', search the whole BSE_DIR. Otherwise, search the specific path in BSE_DIR.
my $search = ($target eq '.') ? BSE_DIR : File::Spec->catfile(BSE_DIR, $target);
return unless -d $search || -e $search;
find({
wanted => sub {
return if -d $_; # Skip directories
my $rel = File::Spec->abs2rel($_, BSE_DIR);
if (!-e $rel) {
my $tmp_link = File::Spec->catfile(TMP_DIR, $rel);
if (-l $tmp_link || -e $tmp_link) {
unlink($tmp_link);
}
}
},
no_chdir => 1
}, $search);
}
|