Skip to content

Commit 7fd7713

Browse files
committed
Add a --batch flag to git cinnabar {git2hg,hg2git}
1 parent 52ceb15 commit 7fd7713

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

cinnabar/cmd/convert.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
from cinnabar.cmd.util import CLI
55
from cinnabar.git import NULL_NODE_ID
66
from cinnabar.helper import GitHgHelper
7-
from cinnabar.util import bytes_stdout
7+
from cinnabar.util import (
8+
bytes_stdin,
9+
bytes_stdout,
10+
)
811

912

1013
class AbbrevAction(argparse.Action):
@@ -53,30 +56,51 @@ def sha1_value(value):
5356
return value.encode('ascii')
5457

5558

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+
5674
@CLI.subcommand
5775
@CLI.argument('--abbrev', action=AbbrevAction)
76+
@CLI.argument('--batch', action='store_true', help='read sha1s on stdin')
5877
@CLI.argument('sha1', action=SHA1Action, nargs='*', type=sha1_value,
5978
help='mercurial sha1')
6079
def hg2git(args):
6180
'''convert mercurial sha1 to corresponding git sha1'''
6281

63-
for arg in args.sha1:
82+
def do_one(arg):
6483
bytes_stdout.write(
6584
GitHgHelper.hg2git(arg)[:args.abbrev])
6685
bytes_stdout.write(b'\n')
6786

87+
do_all(args, do_one)
88+
6889

6990
@CLI.subcommand
7091
@CLI.argument('--abbrev', action=AbbrevAction)
92+
@CLI.argument('--batch', action='store_true', help='read sha1s on stdin')
7193
@CLI.argument('sha1', action=SHA1Action, nargs='*', help='git sha1')
7294
def git2hg(args):
7395
'''convert git sha1 to corresponding mercurial sha1'''
7496

75-
for arg in args.sha1:
97+
def do_one(arg):
7698
data = GitHgHelper.git2hg(arg.encode('ascii'))
7799
if data:
78100
assert data.startswith(b'changeset ')
79101
bytes_stdout.write(data[10:10 + args.abbrev])
80102
else:
81103
bytes_stdout.write(NULL_NODE_ID[:args.abbrev])
82104
bytes_stdout.write(b'\n')
105+
106+
do_all(args, do_one)

git-core

Submodule git-core updated from de49261 to af6b65d

0 commit comments

Comments
 (0)