summaryrefslogtreecommitdiffstats
path: root/vcx
blob: 2c956912c1776f6cf60d5df556cb8c44ca352916 (plain)
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
#!/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 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 = $ARGV[0] // '';
my $path = $ARGV[1] // '';

if ($cmd eq 'init') {
	init_repo();
} elsif ($cmd eq 'status') {
	run_status();
} elsif ($cmd eq 'add') {
	die "Usage: $0 add [path]\n" unless $path;
	run_add($path); 
} 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;

	# 1. Get the staging target (what's in .vcx/obj/)
	my $staged_target = readlink($tmp_link);
	
	# 2. CASE: Work tree entry is a SYMLINK
	if (-l $path) {
		# Compare where the work tree link points vs where the staging link points
		return (readlink($path) eq $staged_target);
	}
	
	# 3. CASE: Work tree entry is a REGULAR FILE
	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);
	}
	
	return 0;
}

sub run_add {
	my ($target) = @_;
	my @targets = ($target eq '.') ? ('.') : bsd_glob($target);

	foreach my $t (@targets) {
		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);
				my $base_link = File::Spec->catfile(BSE_DIR, $rel);
				
				# CASE 1: File (Regular)
				if (-f $File::Find::name && !-l $File::Find::name) {
					my $obj_path = File::Spec->catfile(OBJ_DIR, $rel . ".tmp");
					_sync_file_to_obj($File::Find::name, $obj_path, $tmp_link);
				} 
				# CASE 2: Symlink
				elsif (-l $File::Find::name) {
					_sync_symlink_to_tmp($File::Find::name, $tmp_link);
				}
			},
			no_chdir => 1,
		}, $t) if -e $t;
		
		_handle_deletions($t);
	}
}

# For Regular Files: Copy to OBJ and link to TMP
sub _sync_file_to_obj {
	my ($src, $obj, $tmp) = @_;
	
	# 1. Ensure the directory path to the OBJ file exists
	make_path(dirname($obj));
	# 2. Ensure the directory path to the TMP symlink exists
	make_path(dirname($tmp));
	
	copy($src, $obj);

	# Create relative link from TMP to OBJ
	my $rel_target = File::Spec->abs2rel(File::Spec->rel2abs($obj), dirname($tmp));
	
	unlink($tmp) if -e $tmp || -l $tmp;
	symlink($rel_target, $tmp);
	print "[Add File] $src\n";
}

# 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) = @_;
	my $search = ($target eq '.') ? BSE_DIR : File::Spec->catfile(BSE_DIR, $target);
	return unless -d $search || -e $search;
	find({ wanted => sub { return if -d $_; my $rel = File::Spec->abs2rel($_, BSE_DIR); 
		   if (!-e $rel) { unlink(File::Spec->catfile(OBJ_DIR, $rel), $_); print "[Deleted] $rel\n"; } 
		 }, no_chdir => 1 }, $search);
}