File: Padre-SVN.pm

package info (click to toggle)
libperl-minimumversion-fast-perl 0.22-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 152 kB
  • sloc: perl: 356; makefile: 7
file content (118 lines) | stat: -rw-r--r-- 2,332 bytes parent folder | download | duplicates (3)
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
package Padre::SVN;

# Utility functions needed for basic SVN introspection

use 5.008;
use strict;
use warnings;
use File::Spec ();
our $VERSION = '0.18';

# Find the mime type for a file
sub file_mimetype {
	my $hash = file_props(shift);
	return $hash->{'svn:mime-type'};
}

# Find and parse the properties file
sub file_props {
	my $file = shift;
	my $base = find_props($file) or return undef;
	return parse_props($base);
}

# Find the props-base for a file
sub find_props {
	my $file = shift;
	my ( $v, $d, $f ) = File::Spec->splitpath($file);
	my $path = File::Spec->catpath(
		$v,
		File::Spec->catdir( $d, '.svn', 'props' ),
		$f . '.svn-work',
	);
	return $path if -f $path;
	$path = File::Spec->catpath(
		$v,
		File::Spec->catdir( $d, '.svn', 'prop-base' ),
		$f . '.svn-base',
	);
	return $path if -f $path;
	return undef;
}

# Parse a property file
sub parse_props {
	my $file = shift;
	open( my $fh, '<', $file ) or die "Failed to open '$file'";

	# Simple state parser
	my %hash   = ();
	my $kbytes = 0;
	my $vbytes = 0;
	my $key    = undef;
	my $value  = undef;
	while ( my $line = <$fh> ) {
		if ($vbytes) {
			my $l = length $line;
			if ( $l == $vbytes + 1 ) {

				# Perfect content length
				chomp($line);
				$hash{$key} = $value . $line;
				$vbytes     = 0;
				$key        = undef;
				$value      = undef;
				next;
			}
			if ( $l > $vbytes ) {
				$value .= $line;
				$vbytes -= $l;
				next;
			}
			die "Found value longer than specified length";
		}

		if ($kbytes) {
			my $l = length $line;
			if ( $l == $kbytes + 1 ) {

				# Perfect content length
				chomp($line);
				$key .= $line;
				$kbytes = 0;
				next;
			}
			if ( $l > $kbytes ) {
				$key .= $line;
				$kbytes -= $l;
				next;
			}
			die "Found key longer than specified length";
		}

		if ( defined $key ) {
			$line =~ /^V\s(\d+)/ or die "Failed to find expected V line";
			$vbytes = $1;
			$value  = '';
			next;
		}

		last if $line =~ /^END/;

		# We should have a K line indicating key size
		$line =~ /^K\s(\d+)/ or die "Failed to find expected K line";
		$kbytes = $1;
		$key    = '';
	}

	close $fh;

	return \%hash;
}

1;

# Copyright 2008-2013 The Padre development team as listed in Padre.pm.
# LICENSE
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl 5 itself.