|
4 | 4 | from cinnabar.cmd.util import CLI
|
5 | 5 | from cinnabar.git import NULL_NODE_ID
|
6 | 6 | from cinnabar.helper import GitHgHelper
|
7 |
| -from cinnabar.util import bytes_stdout |
| 7 | +from cinnabar.util import ( |
| 8 | + bytes_stdin, |
| 9 | + bytes_stdout, |
| 10 | +) |
8 | 11 |
|
9 | 12 |
|
10 | 13 | class AbbrevAction(argparse.Action):
|
@@ -53,30 +56,51 @@ def sha1_value(value):
|
53 | 56 | return value.encode('ascii')
|
54 | 57 |
|
55 | 58 |
|
| 59 | +def do_all(args, callback): |
| 60 | + for arg in args.sha1: |
| 61 | + callback(arg) |
| 62 | + |
| 63 | + if args.batch: |
| 64 | + bytes_stdout.flush() |
| 65 | + while True: |
| 66 | + line = bytes_stdin.readline() |
| 67 | + if not line: |
| 68 | + break |
| 69 | + for arg in line.split(): |
| 70 | + callback(arg) |
| 71 | + bytes_stdout.flush() |
| 72 | + |
| 73 | + |
56 | 74 | @CLI.subcommand
|
57 | 75 | @CLI.argument('--abbrev', action=AbbrevAction)
|
| 76 | +@CLI.argument('--batch', action='store_true', help='read sha1s on stdin') |
58 | 77 | @CLI.argument('sha1', action=SHA1Action, nargs='*', type=sha1_value,
|
59 | 78 | help='mercurial sha1')
|
60 | 79 | def hg2git(args):
|
61 | 80 | '''convert mercurial sha1 to corresponding git sha1'''
|
62 | 81 |
|
63 |
| - for arg in args.sha1: |
| 82 | + def do_one(arg): |
64 | 83 | bytes_stdout.write(
|
65 | 84 | GitHgHelper.hg2git(arg)[:args.abbrev])
|
66 | 85 | bytes_stdout.write(b'\n')
|
67 | 86 |
|
| 87 | + do_all(args, do_one) |
| 88 | + |
68 | 89 |
|
69 | 90 | @CLI.subcommand
|
70 | 91 | @CLI.argument('--abbrev', action=AbbrevAction)
|
| 92 | +@CLI.argument('--batch', action='store_true', help='read sha1s on stdin') |
71 | 93 | @CLI.argument('sha1', action=SHA1Action, nargs='*', help='git sha1')
|
72 | 94 | def git2hg(args):
|
73 | 95 | '''convert git sha1 to corresponding mercurial sha1'''
|
74 | 96 |
|
75 |
| - for arg in args.sha1: |
| 97 | + def do_one(arg): |
76 | 98 | data = GitHgHelper.git2hg(arg.encode('ascii'))
|
77 | 99 | if data:
|
78 | 100 | assert data.startswith(b'changeset ')
|
79 | 101 | bytes_stdout.write(data[10:10 + args.abbrev])
|
80 | 102 | else:
|
81 | 103 | bytes_stdout.write(NULL_NODE_ID[:args.abbrev])
|
82 | 104 | bytes_stdout.write(b'\n')
|
| 105 | + |
| 106 | + do_all(args, do_one) |
0 commit comments