@matheusbsbs / matheus-bagliano / commits / e6fefa926d
Fork 0

git-svn info: implement info command

Implement "git-svn info" for files and directories based on the "svn info" command. Note that the -r/--revision argument is not supported yet. Added 18 tests in t/t9119-git-svn-info.sh. [ew: small fix to work without arguments on all working directories] Signed-off-by: David D. Kilzer <ddkilzer@kilzer.net> Acked-by: Eric Wong <normalperson@yhbt.net>

David D. Kilzer committed Nov 21, 2007 at 11:57 UTC e6fefa926de3fcc1f0424e1b901347379ed51935
3 files changed +413
Documentation/git-svn.txt
+5
@@ -193,6 +193,11 @@ Any other arguments are passed directly to `git log'
193 repository (that has been init-ed with git-svn).
194 The -r<revision> option is required for this.
195
196 +'info'::
197 + Shows information about a file or directory similar to what
198 + `svn info' provides. Does not currently support a -r/--revision
199 + argument.
200 +
201 --
202
203 OPTIONS
git-svn.perl
+133
@@ -178,6 +178,10 @@ my %cmd = (
178 'file|F=s' => \$_file,
179 'revision|r=s' => \$_revision,
180 %cmt_opts } ],
181 + 'info' => [ \&cmd_info,
182 + "Show info about the latest SVN revision
183 + on the current branch",
184 + { } ],
185 );
186
187 my $cmd;
@@ -586,12 +590,18 @@ sub cmd_create_ignore {
590
591 sub canonicalize_path {
592 my ($path) = @_;
593 + my $dot_slash_added = 0;
594 + if (substr($path, 0, 1) ne "/") {
595 + $path = "./" . $path;
596 + $dot_slash_added = 1;
597 + }
598 # File::Spec->canonpath doesn't collapse x/../y into y (for a
599 # good reason), so let's do this manually.
600 $path =~ s#/+#/#g;
601 $path =~ s#/\.(?:/|$)#/#g;
602 $path =~ s#/[^/]+/\.\.##g;
603 $path =~ s#/$##g;
604 + $path =~ s#^\./## if $dot_slash_added;
605 return $path;
606 }
607
@@ -743,6 +753,104 @@ sub cmd_commit_diff {
753 }
754 }
755
756 +sub cmd_info {
757 + my $path = canonicalize_path(shift or ".");
758 + unless (scalar(@_) == 0) {
759 + die "Too many arguments specified\n";
760 + }
761 +
762 + my ($file_type, $diff_status) = find_file_type_and_diff_status($path);
763 +
764 + if (!$file_type && !$diff_status) {
765 + print STDERR "$path: (Not a versioned resource)\n\n";
766 + return;
767 + }
768 +
769 + my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
770 + unless ($gs) {
771 + die "Unable to determine upstream SVN information from ",
772 + "working tree history\n";
773 + }
774 + my $full_url = $url . ($path eq "." ? "" : "/$path");
775 +
776 + my $result = "Path: $path\n";
777 + $result .= "Name: " . basename($path) . "\n" if $file_type ne "dir";
778 + $result .= "URL: " . $full_url . "\n";
779 +
780 + my $repos_root = $gs->ra->{repos_root};
781 + Git::SVN::remove_username($repos_root);
782 + $result .= "Repository Root: $repos_root\n";
783 + $result .= "Repository UUID: $uuid\n" unless $diff_status eq "A";
784 + $result .= "Revision: " . ($diff_status eq "A" ? 0 : $rev) . "\n";
785 +
786 + $result .= "Node Kind: " .
787 + ($file_type eq "dir" ? "directory" : "file") . "\n";
788 +
789 + my $schedule = $diff_status eq "A"
790 + ? "add"
791 + : ($diff_status eq "D" ? "delete" : "normal");
792 + $result .= "Schedule: $schedule\n";
793 +
794 + if ($diff_status eq "A") {
795 + print $result, "\n";
796 + return;
797 + }
798 +
799 + my ($lc_author, $lc_rev, $lc_date_utc);
800 + my @args = Git::SVN::Log::git_svn_log_cmd($rev, $rev, "--", $path);
801 + my $log = command_output_pipe(@args);
802 + my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
803 + while (<$log>) {
804 + if (/^${esc_color}author (.+) <[^>]+> (\d+) ([\-\+]?\d+)$/o) {
805 + $lc_author = $1;
806 + $lc_date_utc = Git::SVN::Log::parse_git_date($2, $3);
807 + } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
808 + (undef, $lc_rev, undef) = ::extract_metadata($1);
809 + }
810 + }
811 + close $log;
812 +
813 + Git::SVN::Log::set_local_timezone();
814 +
815 + $result .= "Last Changed Author: $lc_author\n";
816 + $result .= "Last Changed Rev: $lc_rev\n";
817 + $result .= "Last Changed Date: " .
818 + Git::SVN::Log::format_svn_date($lc_date_utc) . "\n";
819 +
820 + if ($file_type ne "dir") {
821 + my $text_last_updated_date =
822 + ($diff_status eq "D" ? $lc_date_utc : (stat $path)[9]);
823 + $result .=
824 + "Text Last Updated: " .
825 + Git::SVN::Log::format_svn_date($text_last_updated_date) .
826 + "\n";
827 + my $checksum;
828 + if ($diff_status eq "D") {
829 + my ($fh, $ctx) =
830 + command_output_pipe(qw(cat-file blob), "HEAD:$path");
831 + if ($file_type eq "link") {
832 + my $file_name = <$fh>;
833 + $checksum = Git::SVN::Util::md5sum("link $file_name");
834 + } else {
835 + $checksum = Git::SVN::Util::md5sum($fh);
836 + }
837 + command_close_pipe($fh, $ctx);
838 + } elsif ($file_type eq "link") {
839 + my $file_name =
840 + command(qw(cat-file blob), "HEAD:$path");
841 + $checksum =
842 + Git::SVN::Util::md5sum("link " . $file_name);
843 + } else {
844 + open FILE, "<", $path or die $!;
845 + $checksum = Git::SVN::Util::md5sum(\*FILE);
846 + close FILE or die $!;
847 + }
848 + $result .= "Checksum: " . $checksum . "\n";
849 + }
850 +
851 + print $result, "\n";
852 +}
853 +
854 ########################### utility functions #########################
855
856 sub rebase_cmd {
@@ -1050,6 +1158,31 @@ sub linearize_history {
1158 (\@linear_refs, \%parents);
1159 }
1160
1161 +sub find_file_type_and_diff_status {
1162 + my ($path) = @_;
1163 + return ('dir', '') if $path eq '.';
1164 +
1165 + my $diff_output =
1166 + command_oneline(qw(diff --cached --name-status --), $path) || "";
1167 + my $diff_status = (split(' ', $diff_output))[0] || "";
1168 +
1169 + my $ls_tree = command_oneline(qw(ls-tree HEAD), $path) || "";
1170 +
1171 + return (undef, undef) if !$diff_status && !$ls_tree;
1172 +
1173 + if ($diff_status eq "A") {
1174 + return ("link", $diff_status) if -l $path;
1175 + return ("dir", $diff_status) if -d $path;
1176 + return ("file", $diff_status);
1177 + }
1178 +
1179 + my $mode = (split(' ', $ls_tree))[0] || "";
1180 +
1181 + return ("link", $diff_status) if $mode eq "120000";
1182 + return ("dir", $diff_status) if $mode eq "040000";
1183 + return ("file", $diff_status);
1184 +}
1185 +
1186 package Git::SVN::Util;
1187 use strict;
1188 use warnings;
t/t9119-git-svn-info.sh new
+275
@@ -0,0 +1,275 @@
1 +#!/bin/sh
2 +#
3 +# Copyright (c) 2007 David D. Kilzer
4 +
5 +test_description='git-svn info'
6 +
7 +. ./lib-git-svn.sh
8 +
9 +ptouch() {
10 + perl -w -e '
11 + use strict;
12 + die "ptouch requires exactly 2 arguments" if @ARGV != 2;
13 + die "$ARGV[0] does not exist" if ! -e $ARGV[0];
14 + my @s = stat $ARGV[0];
15 + utime $s[8], $s[9], $ARGV[1];
16 + ' "$1" "$2"
17 +}
18 +
19 +test_expect_success 'setup repository and import' "
20 + mkdir info &&
21 + cd info &&
22 + echo FIRST > A &&
23 + echo one > file &&
24 + ln -s file symlink-file &&
25 + mkdir directory &&
26 + touch directory/.placeholder &&
27 + ln -s directory symlink-directory &&
28 + svn import -m 'initial' . $svnrepo &&
29 + cd .. &&
30 + mkdir gitwc &&
31 + cd gitwc &&
32 + git-svn init $svnrepo &&
33 + git-svn fetch &&
34 + cd .. &&
35 + svn co $svnrepo svnwc &&
36 + ptouch svnwc/file gitwc/file &&
37 + ptouch svnwc/directory gitwc/directory &&
38 + ptouch svnwc/symlink-file gitwc/symlink-file &&
39 + ptouch svnwc/symlink-directory gitwc/symlink-directory
40 + "
41 +
42 +test_expect_success 'info' "
43 + (cd svnwc; svn info) > expected.info &&
44 + (cd gitwc; git-svn info) > actual.info &&
45 + git-diff expected.info actual.info
46 + "
47 +
48 +test_expect_success 'info .' "
49 + (cd svnwc; svn info .) > expected.info-dot &&
50 + (cd gitwc; git-svn info .) > actual.info-dot &&
51 + git-diff expected.info-dot actual.info-dot
52 + "
53 +
54 +test_expect_success 'info file' "
55 + (cd svnwc; svn info file) > expected.info-file &&
56 + (cd gitwc; git-svn info file) > actual.info-file &&
57 + git-diff expected.info-file actual.info-file
58 + "
59 +
60 +test_expect_success 'info directory' "
61 + (cd svnwc; svn info directory) > expected.info-directory &&
62 + (cd gitwc; git-svn info directory) > actual.info-directory &&
63 + git-diff expected.info-directory actual.info-directory
64 + "
65 +
66 +test_expect_success 'info symlink-file' "
67 + (cd svnwc; svn info symlink-file) > expected.info-symlink-file &&
68 + (cd gitwc; git-svn info symlink-file) > actual.info-symlink-file &&
69 + git-diff expected.info-symlink-file actual.info-symlink-file
70 + "
71 +
72 +test_expect_success 'info symlink-directory' "
73 + (cd svnwc; svn info symlink-directory) \
74 + > expected.info-symlink-directory &&
75 + (cd gitwc; git-svn info symlink-directory) \
76 + > actual.info-symlink-directory &&
77 + git-diff expected.info-symlink-directory actual.info-symlink-directory
78 + "
79 +
80 +test_expect_success 'info added-file' "
81 + echo two > gitwc/added-file &&
82 + cd gitwc &&
83 + git add added-file &&
84 + cd .. &&
85 + cp gitwc/added-file svnwc/added-file &&
86 + ptouch gitwc/added-file svnwc/added-file &&
87 + cd svnwc &&
88 + svn add added-file > /dev/null &&
89 + cd .. &&
90 + (cd svnwc; svn info added-file) > expected.info-added-file &&
91 + (cd gitwc; git-svn info added-file) > actual.info-added-file &&
92 + git-diff expected.info-added-file actual.info-added-file
93 + "
94 +
95 +test_expect_success 'info added-directory' "
96 + mkdir gitwc/added-directory svnwc/added-directory &&
97 + ptouch gitwc/added-directory svnwc/added-directory &&
98 + touch gitwc/added-directory/.placeholder &&
99 + cd svnwc &&
100 + svn add added-directory > /dev/null &&
101 + cd .. &&
102 + cd gitwc &&
103 + git add added-directory &&
104 + cd .. &&
105 + (cd svnwc; svn info added-directory) \
106 + > expected.info-added-directory &&
107 + (cd gitwc; git-svn info added-directory) \
108 + > actual.info-added-directory &&
109 + git-diff expected.info-added-directory actual.info-added-directory
110 + "
111 +
112 +test_expect_success 'info added-symlink-file' "
113 + cd gitwc &&
114 + ln -s added-file added-symlink-file &&
115 + git add added-symlink-file &&
116 + cd .. &&
117 + cd svnwc &&
118 + ln -s added-file added-symlink-file &&
119 + svn add added-symlink-file > /dev/null &&
120 + cd .. &&
121 + ptouch gitwc/added-symlink-file svnwc/added-symlink-file &&
122 + (cd svnwc; svn info added-symlink-file) \
123 + > expected.info-added-symlink-file &&
124 + (cd gitwc; git-svn info added-symlink-file) \
125 + > actual.info-added-symlink-file &&
126 + git-diff expected.info-added-symlink-file \
127 + actual.info-added-symlink-file
128 + "
129 +
130 +test_expect_success 'info added-symlink-directory' "
131 + cd gitwc &&
132 + ln -s added-directory added-symlink-directory &&
133 + git add added-symlink-directory &&
134 + cd .. &&
135 + cd svnwc &&
136 + ln -s added-directory added-symlink-directory &&
137 + svn add added-symlink-directory > /dev/null &&
138 + cd .. &&
139 + ptouch gitwc/added-symlink-directory svnwc/added-symlink-directory &&
140 + (cd svnwc; svn info added-symlink-directory) \
141 + > expected.info-added-symlink-directory &&
142 + (cd gitwc; git-svn info added-symlink-directory) \
143 + > actual.info-added-symlink-directory &&
144 + git-diff expected.info-added-symlink-directory \
145 + actual.info-added-symlink-directory
146 + "
147 +
148 +# The next few tests replace the "Text Last Updated" value with a
149 +# placeholder since git doesn't have a way to know the date that a
150 +# now-deleted file was last checked out locally. Internally it
151 +# simply reuses the Last Changed Date.
152 +
153 +test_expect_success 'info deleted-file' "
154 + cd gitwc &&
155 + git rm -f file > /dev/null &&
156 + cd .. &&
157 + cd svnwc &&
158 + svn rm --force file > /dev/null &&
159 + cd .. &&
160 + (cd svnwc; svn info file) |
161 + sed -e 's/^\(Text Last Updated:\).*/\1 TEXT-LAST-UPDATED-STRING/' \
162 + > expected.info-deleted-file &&
163 + (cd gitwc; git-svn info file) |
164 + sed -e 's/^\(Text Last Updated:\).*/\1 TEXT-LAST-UPDATED-STRING/' \
165 + > actual.info-deleted-file &&
166 + git-diff expected.info-deleted-file actual.info-deleted-file
167 + "
168 +
169 +test_expect_success 'info deleted-directory' "
170 + cd gitwc &&
171 + git rm -r -f directory > /dev/null &&
172 + cd .. &&
173 + cd svnwc &&
174 + svn rm --force directory > /dev/null &&
175 + cd .. &&
176 + (cd svnwc; svn info directory) |
177 + sed -e 's/^\(Text Last Updated:\).*/\1 TEXT-LAST-UPDATED-STRING/' \
178 + > expected.info-deleted-directory &&
179 + (cd gitwc; git-svn info directory) |
180 + sed -e 's/^\(Text Last Updated:\).*/\1 TEXT-LAST-UPDATED-STRING/' \
181 + > actual.info-deleted-directory &&
182 + git-diff expected.info-deleted-directory actual.info-deleted-directory
183 + "
184 +
185 +test_expect_success 'info deleted-symlink-file' "
186 + cd gitwc &&
187 + git rm -f symlink-file > /dev/null &&
188 + cd .. &&
189 + cd svnwc &&
190 + svn rm --force symlink-file > /dev/null &&
191 + cd .. &&
192 + (cd svnwc; svn info symlink-file) |
193 + sed -e 's/^\(Text Last Updated:\).*/\1 TEXT-LAST-UPDATED-STRING/' \
194 + > expected.info-deleted-symlink-file &&
195 + (cd gitwc; git-svn info symlink-file) |
196 + sed -e 's/^\(Text Last Updated:\).*/\1 TEXT-LAST-UPDATED-STRING/' \
197 + > actual.info-deleted-symlink-file &&
198 + git-diff expected.info-deleted-symlink-file \
199 + actual.info-deleted-symlink-file
200 + "
201 +
202 +test_expect_success 'info deleted-symlink-directory' "
203 + cd gitwc &&
204 + git rm -f symlink-directory > /dev/null &&
205 + cd .. &&
206 + cd svnwc &&
207 + svn rm --force symlink-directory > /dev/null &&
208 + cd .. &&
209 + (cd svnwc; svn info symlink-directory) |
210 + sed -e 's/^\(Text Last Updated:\).*/\1 TEXT-LAST-UPDATED-STRING/' \
211 + > expected.info-deleted-symlink-directory &&
212 + (cd gitwc; git-svn info symlink-directory) |
213 + sed -e 's/^\(Text Last Updated:\).*/\1 TEXT-LAST-UPDATED-STRING/' \
214 + > actual.info-deleted-symlink-directory &&
215 + git-diff expected.info-deleted-symlink-directory \
216 + actual.info-deleted-symlink-directory
217 + "
218 +
219 +# NOTE: git does not have the concept of replaced objects,
220 +# so we can't test for files in that state.
221 +
222 +test_expect_success 'info unknown-file' "
223 + echo two > gitwc/unknown-file &&
224 + cp gitwc/unknown-file svnwc/unknown-file &&
225 + ptouch gitwc/unknown-file svnwc/unknown-file &&
226 + (cd svnwc; svn info unknown-file) 2> expected.info-unknown-file &&
227 + (cd gitwc; git-svn info unknown-file) 2> actual.info-unknown-file &&
228 + git-diff expected.info-unknown-file actual.info-unknown-file
229 + "
230 +
231 +test_expect_success 'info unknown-directory' "
232 + mkdir gitwc/unknown-directory svnwc/unknown-directory &&
233 + ptouch gitwc/unknown-directory svnwc/unknown-directory &&
234 + touch gitwc/unknown-directory/.placeholder &&
235 + (cd svnwc; svn info unknown-directory) \
236 + 2> expected.info-unknown-directory &&
237 + (cd gitwc; git-svn info unknown-directory) \
238 + 2> actual.info-unknown-directory &&
239 + git-diff expected.info-unknown-directory actual.info-unknown-directory
240 + "
241 +
242 +test_expect_success 'info unknown-symlink-file' "
243 + cd gitwc &&
244 + ln -s unknown-file unknown-symlink-file &&
245 + cd .. &&
246 + cd svnwc &&
247 + ln -s unknown-file unknown-symlink-file &&
248 + cd .. &&
249 + ptouch gitwc/unknown-symlink-file svnwc/unknown-symlink-file &&
250 + (cd svnwc; svn info unknown-symlink-file) \
251 + 2> expected.info-unknown-symlink-file &&
252 + (cd gitwc; git-svn info unknown-symlink-file) \
253 + 2> actual.info-unknown-symlink-file &&
254 + git-diff expected.info-unknown-symlink-file \
255 + actual.info-unknown-symlink-file
256 + "
257 +
258 +test_expect_success 'info unknown-symlink-directory' "
259 + cd gitwc &&
260 + ln -s unknown-directory unknown-symlink-directory &&
261 + cd .. &&
262 + cd svnwc &&
263 + ln -s unknown-directory unknown-symlink-directory &&
264 + cd .. &&
265 + ptouch gitwc/unknown-symlink-directory \
266 + svnwc/unknown-symlink-directory &&
267 + (cd svnwc; svn info unknown-symlink-directory) \
268 + 2> expected.info-unknown-symlink-directory &&
269 + (cd gitwc; git-svn info unknown-symlink-directory) \
270 + 2> actual.info-unknown-symlink-directory &&
271 + git-diff expected.info-unknown-symlink-directory \
272 + actual.info-unknown-symlink-directory
273 + "
274 +
275 +test_done