Skip to content

Commit 20d25e2

Browse files
author
Robin Luckey
committed
OTWO-415 Rake passes using HglibAdapter
1 parent 29c311d commit 20d25e2

File tree

7 files changed

+61
-22
lines changed

7 files changed

+61
-22
lines changed

lib/scm/adapters/hglib/cat_file.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
module Scm::Adapters
22
class HglibAdapter < HgAdapter
33

4-
def cat(revision, path)
5-
hg_client.cat_file(revision, path)
4+
def cat_file(commit, diff)
5+
hg_client.cat_file(commit.token, diff.path)
66
end
77

8+
def cat_file_parent(commit, diff)
9+
tokens = parent_tokens(commit)
10+
hg_client.cat_file(tokens.first, diff.path) if tokens.first
11+
end
12+
813
end
914
end

lib/scm/adapters/hglib/client.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@ def open_repository
1717
end
1818

1919
def cat_file(revision, file)
20-
send_command("CAT_FILE\t#{revision}\t#{file}")
20+
begin
21+
send_command("CAT_FILE\t#{revision}\t#{file}")
22+
rescue RuntimeError => e
23+
if e.message =~ /not found in manifest/
24+
return nil # File does not exist.
25+
else
26+
raise
27+
end
28+
end
2129
end
2230

2331
def parent_tokens(revision)

lib/scm/adapters/hglib/head.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module Scm::Adapters
2+
class HglibAdapter < HgAdapter
3+
4+
def parent_tokens(commit)
5+
hg_client.parent_tokens(commit.token)
6+
end
7+
8+
end
9+
end

lib/scm/adapters/hglib/server.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ def get_file_content(self, filename, revision):
1616
return contents
1717

1818
def get_parent_tokens(self, revision):
19-
return None
19+
c = self.repository.changectx(revision)
20+
parents = [p.hex() for p in c.parents() if p.hex() != '0000000000000000000000000000000000000000']
21+
return parents
2022

2123
class Command:
2224
def __init__(self, line):
@@ -45,42 +47,38 @@ def send_data(result):
4547
sys.stdout.write(result)
4648
sys.stdout.flush()
4749

48-
def exit_delayed(status, delay=1):
49-
time.sleep(delay)
50-
sys.exit(status)
51-
5250
def command_loop():
5351
while True:
54-
cmd = Command(sys.stdin.readline())
55-
if cmd.get_action() == 'REPO_OPEN':
52+
s = sys.stdin.readline()
53+
cmd = Command(s)
54+
if s == '' or cmd.get_action() == 'QUIT':
55+
sys.exit(0)
56+
elif cmd.get_action() == 'REPO_OPEN':
5657
commander = HglibPipeServer(cmd.get_arg(1))
5758
send_success()
5859
elif cmd.get_action() == 'CAT_FILE':
59-
content = commander.get_file_content(cmd.get_arg(2), cmd.get_arg(1))
60-
if content == None:
61-
send_failure()
62-
else:
60+
try:
61+
content = commander.get_file_content(cmd.get_arg(2), cmd.get_arg(1))
6362
send_success(len(content))
6463
send_data(content)
64+
except Exception:
65+
send_failure() # Assume file not found
6566
elif cmd.get_action() == 'PARENT_TOKENS':
6667
tokens = commander.get_parent_tokens(cmd.get_arg(1))
67-
tokens = '|'.join(tokens)
68+
tokens = '\n'.join(tokens)
6869
send_success(len(tokens))
6970
send_data(tokens)
70-
elif cmd.get_action() == 'QUIT':
71-
send_success()
72-
exit_delayed(status=0)
7371
else:
7472
error = "Invalid Command - %s" % cmd.get_action()
7573
send_error(len(error))
7674
send_data(error)
77-
exit_delayed(status=1)
75+
sys.exit(1)
7876

7977
if __name__ == "__main__":
8078
try:
8179
command_loop()
82-
except:
80+
except Exception:
8381
exc_trace = traceback.format_exc()
8482
send_error(len(exc_trace))
8583
send_data(exc_trace)
86-
exit_delayed(status=1)
84+
sys.exit(1)

lib/scm/adapters/hglib_adapter.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ def cleanup
2222
end
2323

2424
require 'lib/scm/adapters/hglib/cat_file'
25+
require 'lib/scm/adapters/hglib/head'

test/unit/hglib_cat_file_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def test_cat_file
2121
EXPECTED
2222

2323
# The file was deleted in revision 468336c6671c. Check that it does not exist now, but existed in parent.
24-
# assert_equal nil, hg.cat_file(Scm::Commit.new(:token => '75532c1e1f1d'), Scm::Diff.new(:path => 'helloworld.c'))
24+
assert_equal nil, hg.cat_file(Scm::Commit.new(:token => '75532c1e1f1d'), Scm::Diff.new(:path => 'helloworld.c'))
2525
assert_equal expected, hg.cat_file_parent(Scm::Commit.new(:token => '75532c1e1f1d'), Scm::Diff.new(:path => 'helloworld.c'))
2626
assert_equal expected, hg.cat_file(Scm::Commit.new(:token => '468336c6671c'), Scm::Diff.new(:path => 'helloworld.c'))
2727
end

test/unit/hglib_head_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require File.dirname(__FILE__) + '/../test_helper'
2+
3+
module Scm::Adapters
4+
class HgHeadTest < Scm::Test
5+
6+
def test_head_and_parents
7+
with_hglib_repository('hg') do |hg|
8+
assert_equal '75532c1e1f1d', hg.head_token
9+
assert_equal '75532c1e1f1de55c2271f6fd29d98efbe35397c4', hg.head.token
10+
assert hg.head.diffs.any? # diffs should be populated
11+
12+
assert_equal '468336c6671cbc58237a259d1b7326866afc2817', hg.parents(hg.head).first.token
13+
assert hg.parents(hg.head).first.diffs.any?
14+
end
15+
end
16+
17+
end
18+
end

0 commit comments

Comments
 (0)