Skip to contentSkip to conversation

git-svn: Added 'find-rev' command

PATCH In review Adam Roben 3 participants 5 messages Jun 15, 2016 Documentation

lore
Share & tools
lore

Adam Roben

author
#0 lore raw

PATCH 1/1 git-svn: Added 'find-rev' command

This patch adds a new 'find-rev' command to git-svn that lets you easily translate between SVN revision numbers and git tree-ish.

---
 Documentation/git-svn.txt |    5 +++++
 git-svn.perl              |   24 ++++++++++++++++++++++++
 2 files changed, 29 insertions(+), 0 deletions(-)
Documentation/git-svn.txt +5 -0
index a0d34e0..a35b9de 100644
@@ -159,6 +159,11 @@ New features:
 Any other arguments are passed directly to `git log'
 
 --
+'find-rev'::
+	When given an SVN revision number of the form 'rN', returns the
+	corresponding git commit hash.  When given a tree-ish, returns the
+	corresponding SVN revision number.
+
 'set-tree'::
 	You should consider using 'dcommit' instead of this command.
 	Commit specified commit or tree objects to SVN.  This relies on
git-svn.perl +24 -0
index 7b5f8ab..4be8576 100755
@@ -141,6 +141,8 @@ my %cmd = (
 			  'color' => \$Git::SVN::Log::color,
 			  'pager=s' => \$Git::SVN::Log::pager,
 			} ],
+	'find-rev' => [ \&cmd_find_rev, "Translate between SVN revision numbers and tree-ish",
+			{ } ],
 	'rebase' => [ \&cmd_rebase, "Fetch and rebase your working directory",
 			{ 'merge|m|M' => \$_merge,
 			  'verbose|v' => \$_verbose,
@@ -428,6 +430,28 @@ sub cmd_dcommit {
 	command_noisy(@finish, $gs->refname);
 }
 
+sub cmd_find_rev {
+	my $revision_or_hash = shift;
+	my $result;
+	if ($revision_or_hash =~ /^r\d+$/) {
+		my $desired_revision = substr($revision_or_hash, 1);
+		my ($fh, $ctx) = command_output_pipe('rev-list', 'HEAD');
+		while (my $hash = <$fh>) {
+			chomp($hash);
+			my (undef, $rev, undef) = cmt_metadata($hash);
+			if ($rev && $rev eq $desired_revision) {
+				$result = $hash;
+				last;
+			}
+		}
+		command_close_pipe($fh, $ctx);
+	} else {
+		my (undef, $rev, undef) = cmt_metadata($revision_or_hash);
+		$result = $rev;
+	}
+	print "$result\n" if $result;
+}
+
 sub cmd_rebase {
 	command_noisy(qw/update-index --refresh/);
 	my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
Signature · 1 line
1.5.2.rc0.75.g959b-dirty
Acked-by on v1 1/1
#1 lore raw
Quoted 2 lines · Adam Roben

This patch adds a new 'find-rev' command to git-svn that lets you easily translate between SVN revision numbers and git tree-ish.

Looks useful.

Quoted 67 lines
---
 Documentation/git-svn.txt |    5 +++++
 git-svn.perl              |   24 ++++++++++++++++++++++++
 2 files changed, 29 insertions(+), 0 deletions(-)
Documentation/git-svn.txt +5 -0
index a0d34e0..a35b9de 100644
@@ -159,6 +159,11 @@ New features:
 Any other arguments are passed directly to `git log'
 
 --
+'find-rev'::
+	When given an SVN revision number of the form 'rN', returns the
+	corresponding git commit hash.  When given a tree-ish, returns the
+	corresponding SVN revision number.
+
 'set-tree'::
 	You should consider using 'dcommit' instead of this command.
 	Commit specified commit or tree objects to SVN.  This relies on
git-svn.perl +24 -0
index 7b5f8ab..4be8576 100755
@@ -141,6 +141,8 @@ my %cmd = (
 			  'color' => \$Git::SVN::Log::color,
 			  'pager=s' => \$Git::SVN::Log::pager,
 			} ],
+	'find-rev' => [ \&cmd_find_rev, "Translate between SVN revision numbers and tree-ish",
+			{ } ],
 	'rebase' => [ \&cmd_rebase, "Fetch and rebase your working directory",
 			{ 'merge|m|M' => \$_merge,
 			  'verbose|v' => \$_verbose,
@@ -428,6 +430,28 @@ sub cmd_dcommit {
 	command_noisy(@finish, $gs->refname);
 }
 
+sub cmd_find_rev {
+	my $revision_or_hash = shift;
+	my $result;
+	if ($revision_or_hash =~ /^r\d+$/) {
+		my $desired_revision = substr($revision_or_hash, 1);
+		my ($fh, $ctx) = command_output_pipe('rev-list', 'HEAD');
+		while (my $hash = <$fh>) {
+			chomp($hash);
+			my (undef, $rev, undef) = cmt_metadata($hash);
+			if ($rev && $rev eq $desired_revision) {
+				$result = $hash;
+				last;
+			}
+		}
+		command_close_pipe($fh, $ctx);
+	} else {
+		my (undef, $rev, undef) = cmt_metadata($revision_or_hash);
+		$result = $rev;
+	}
+	print "$result\n" if $result;
+}
+
 sub cmd_rebase {
 	command_noisy(qw/update-index --refresh/);
 	my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
Signature · 1 line
1.5.2.rc0.75.g959b-dirty
Signature · 1 line
Eric Wong
#2 lore raw
Quoted 7 lines · Eric Wong
Quoted 2 lines · Adam Roben

This patch adds a new 'find-rev' command to git-svn that lets you easily translate between SVN revision numbers and git tree-ish.

Looks useful.

But looks quite wasteful. Why not run "rev-list -v" or something instead of running cat-file on revision one-by-one?

#3 lore raw
Quoted 6 lines · Junio C Hamano
Quoted 4 lines · Eric Wong
Quoted 2 lines · Adam Roben

This patch adds a new 'find-rev' command to git-svn that lets you easily translate between SVN revision numbers and git tree-ish.

Quoted 6 lines
Quoted 3 lines

Looks useful.

But looks quite wasteful. Why not run "rev-list -v" or something instead of running cat-file on revision one-by-one?

Didn't know about "rev-list -v", but I just checked and it still has the limited-size buffer that --pretty=raw has.

"git-svn log" only runs cat-file if it can't find a git-svn-id: line at the bottom. (I used log --abbrev-commit --pretty=raw).

Sorry about the premature Ack, I hadn't had my coffee yet at that point (and now I'm half awake :x)

Adam:

However, since we're not fetching ranges...

You can do $gs->rev_db_get($rev_nr) to easily find a commit given a revision. Bonus points if this works independently of the current HEAD so you can look up revision numbers on different branches. (git-svn log should be made to support this, too)

Signature · 1 line
Eric Wong
author
#4 lore raw
Quoted 8 lines · Eric Wong

Adam:

However, since we're not fetching ranges...

You can do $gs->rev_db_get($rev_nr) to easily find a commit given a revision. Bonus points if this works independently of the current HEAD so you can look up revision numbers on different branches. (git-svn log should be made to support this, too)

OK, I'll do that and send out a new patch.

-Adam