Mercurial > public > mercurial-scm > hg
comparison mercurial/wireprotov2server.py @ 40177:41e2633bcd00
wireprotov2: extract file object emission to own function
An upcoming commit will introduce another caller.
Differential Revision: https://phab.mercurial-scm.org/D4980
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Tue, 02 Oct 2018 10:31:36 -0700 |
parents | 41263df08109 |
children | 46a40bce3ae0 |
comparison
equal
deleted
inserted
replaced
40176:41263df08109 | 40177:41e2633bcd00 |
---|---|
973 if not len(fl): | 973 if not len(fl): |
974 raise FileAccessError(path, 'unknown file: %s', (path,)) | 974 raise FileAccessError(path, 'unknown file: %s', (path,)) |
975 | 975 |
976 return fl | 976 return fl |
977 | 977 |
978 def emitfilerevisions(revisions, fields): | |
979 for revision in revisions: | |
980 d = { | |
981 b'node': revision.node, | |
982 } | |
983 | |
984 if b'parents' in fields: | |
985 d[b'parents'] = [revision.p1node, revision.p2node] | |
986 | |
987 followingmeta = [] | |
988 followingdata = [] | |
989 | |
990 if b'revision' in fields: | |
991 if revision.revision is not None: | |
992 followingmeta.append((b'revision', len(revision.revision))) | |
993 followingdata.append(revision.revision) | |
994 else: | |
995 d[b'deltabasenode'] = revision.basenode | |
996 followingmeta.append((b'delta', len(revision.delta))) | |
997 followingdata.append(revision.delta) | |
998 | |
999 if followingmeta: | |
1000 d[b'fieldsfollowing'] = followingmeta | |
1001 | |
1002 yield d | |
1003 | |
1004 for extra in followingdata: | |
1005 yield extra | |
1006 | |
978 @wireprotocommand( | 1007 @wireprotocommand( |
979 'filedata', | 1008 'filedata', |
980 args={ | 1009 args={ |
981 'haveparents': { | 1010 'haveparents': { |
982 'type': 'bool', | 1011 'type': 'bool', |
1024 | 1053 |
1025 yield { | 1054 yield { |
1026 b'totalitems': len(nodes), | 1055 b'totalitems': len(nodes), |
1027 } | 1056 } |
1028 | 1057 |
1058 for o in emitfilerevisions(revisions, fields): | |
1059 yield o | |
1060 | |
1061 @wireprotocommand( | |
1062 'heads', | |
1063 args={ | |
1064 'publiconly': { | |
1065 'type': 'bool', | |
1066 'default': lambda: False, | |
1067 'example': False, | |
1068 }, | |
1069 }, | |
1070 permission='pull') | |
1071 def headsv2(repo, proto, publiconly): | |
1072 if publiconly: | |
1073 repo = repo.filtered('immutable') | |
1074 | |
1075 yield repo.heads() | |
1076 | |
1077 @wireprotocommand( | |
1078 'known', | |
1079 args={ | |
1080 'nodes': { | |
1081 'type': 'list', | |
1082 'default': list, | |
1083 'example': [b'deadbeef'], | |
1084 }, | |
1085 }, | |
1086 permission='pull') | |
1087 def knownv2(repo, proto, nodes): | |
1088 result = b''.join(b'1' if n else b'0' for n in repo.known(nodes)) | |
1089 yield result | |
1090 | |
1091 @wireprotocommand( | |
1092 'listkeys', | |
1093 args={ | |
1094 'namespace': { | |
1095 'type': 'bytes', | |
1096 'example': b'ns', | |
1097 }, | |
1098 }, | |
1099 permission='pull') | |
1100 def listkeysv2(repo, proto, namespace): | |
1101 keys = repo.listkeys(encoding.tolocal(namespace)) | |
1102 keys = {encoding.fromlocal(k): encoding.fromlocal(v) | |
1103 for k, v in keys.iteritems()} | |
1104 | |
1105 yield keys | |
1106 | |
1107 @wireprotocommand( | |
1108 'lookup', | |
1109 args={ | |
1110 'key': { | |
1111 'type': 'bytes', | |
1112 'example': b'foo', | |
1113 }, | |
1114 }, | |
1115 permission='pull') | |
1116 def lookupv2(repo, proto, key): | |
1117 key = encoding.tolocal(key) | |
1118 | |
1119 # TODO handle exception. | |
1120 node = repo.lookup(key) | |
1121 | |
1122 yield node | |
1123 | |
1124 def manifestdatacapabilities(repo, proto): | |
1125 batchsize = repo.ui.configint( | |
1126 b'experimental', b'server.manifestdata.recommended-batch-size') | |
1127 | |
1128 return { | |
1129 b'recommendedbatchsize': batchsize, | |
1130 } | |
1131 | |
1132 @wireprotocommand( | |
1133 'manifestdata', | |
1134 args={ | |
1135 'nodes': { | |
1136 'type': 'list', | |
1137 'example': [b'0123456...'], | |
1138 }, | |
1139 'haveparents': { | |
1140 'type': 'bool', | |
1141 'default': lambda: False, | |
1142 'example': True, | |
1143 }, | |
1144 'fields': { | |
1145 'type': 'set', | |
1146 'default': set, | |
1147 'example': {b'parents', b'revision'}, | |
1148 'validvalues': {b'parents', b'revision'}, | |
1149 }, | |
1150 'tree': { | |
1151 'type': 'bytes', | |
1152 'example': b'', | |
1153 }, | |
1154 }, | |
1155 permission='pull', | |
1156 cachekeyfn=makecommandcachekeyfn('manifestdata', 1, allargs=True), | |
1157 extracapabilitiesfn=manifestdatacapabilities) | |
1158 def manifestdata(repo, proto, haveparents, nodes, fields, tree): | |
1159 store = repo.manifestlog.getstorage(tree) | |
1160 | |
1161 # Validate the node is known and abort on unknown revisions. | |
1162 for node in nodes: | |
1163 try: | |
1164 store.rev(node) | |
1165 except error.LookupError: | |
1166 raise error.WireprotoCommandError( | |
1167 'unknown node: %s', (node,)) | |
1168 | |
1169 revisions = store.emitrevisions(nodes, | |
1170 revisiondata=b'revision' in fields, | |
1171 assumehaveparentrevisions=haveparents) | |
1172 | |
1173 yield { | |
1174 b'totalitems': len(nodes), | |
1175 } | |
1176 | |
1029 for revision in revisions: | 1177 for revision in revisions: |
1030 d = { | 1178 d = { |
1031 b'node': revision.node, | 1179 b'node': revision.node, |
1032 } | 1180 } |
1033 | 1181 |
1053 | 1201 |
1054 for extra in followingdata: | 1202 for extra in followingdata: |
1055 yield extra | 1203 yield extra |
1056 | 1204 |
1057 @wireprotocommand( | 1205 @wireprotocommand( |
1058 'heads', | |
1059 args={ | |
1060 'publiconly': { | |
1061 'type': 'bool', | |
1062 'default': lambda: False, | |
1063 'example': False, | |
1064 }, | |
1065 }, | |
1066 permission='pull') | |
1067 def headsv2(repo, proto, publiconly): | |
1068 if publiconly: | |
1069 repo = repo.filtered('immutable') | |
1070 | |
1071 yield repo.heads() | |
1072 | |
1073 @wireprotocommand( | |
1074 'known', | |
1075 args={ | |
1076 'nodes': { | |
1077 'type': 'list', | |
1078 'default': list, | |
1079 'example': [b'deadbeef'], | |
1080 }, | |
1081 }, | |
1082 permission='pull') | |
1083 def knownv2(repo, proto, nodes): | |
1084 result = b''.join(b'1' if n else b'0' for n in repo.known(nodes)) | |
1085 yield result | |
1086 | |
1087 @wireprotocommand( | |
1088 'listkeys', | |
1089 args={ | |
1090 'namespace': { | |
1091 'type': 'bytes', | |
1092 'example': b'ns', | |
1093 }, | |
1094 }, | |
1095 permission='pull') | |
1096 def listkeysv2(repo, proto, namespace): | |
1097 keys = repo.listkeys(encoding.tolocal(namespace)) | |
1098 keys = {encoding.fromlocal(k): encoding.fromlocal(v) | |
1099 for k, v in keys.iteritems()} | |
1100 | |
1101 yield keys | |
1102 | |
1103 @wireprotocommand( | |
1104 'lookup', | |
1105 args={ | |
1106 'key': { | |
1107 'type': 'bytes', | |
1108 'example': b'foo', | |
1109 }, | |
1110 }, | |
1111 permission='pull') | |
1112 def lookupv2(repo, proto, key): | |
1113 key = encoding.tolocal(key) | |
1114 | |
1115 # TODO handle exception. | |
1116 node = repo.lookup(key) | |
1117 | |
1118 yield node | |
1119 | |
1120 def manifestdatacapabilities(repo, proto): | |
1121 batchsize = repo.ui.configint( | |
1122 b'experimental', b'server.manifestdata.recommended-batch-size') | |
1123 | |
1124 return { | |
1125 b'recommendedbatchsize': batchsize, | |
1126 } | |
1127 | |
1128 @wireprotocommand( | |
1129 'manifestdata', | |
1130 args={ | |
1131 'nodes': { | |
1132 'type': 'list', | |
1133 'example': [b'0123456...'], | |
1134 }, | |
1135 'haveparents': { | |
1136 'type': 'bool', | |
1137 'default': lambda: False, | |
1138 'example': True, | |
1139 }, | |
1140 'fields': { | |
1141 'type': 'set', | |
1142 'default': set, | |
1143 'example': {b'parents', b'revision'}, | |
1144 'validvalues': {b'parents', b'revision'}, | |
1145 }, | |
1146 'tree': { | |
1147 'type': 'bytes', | |
1148 'example': b'', | |
1149 }, | |
1150 }, | |
1151 permission='pull', | |
1152 cachekeyfn=makecommandcachekeyfn('manifestdata', 1, allargs=True), | |
1153 extracapabilitiesfn=manifestdatacapabilities) | |
1154 def manifestdata(repo, proto, haveparents, nodes, fields, tree): | |
1155 store = repo.manifestlog.getstorage(tree) | |
1156 | |
1157 # Validate the node is known and abort on unknown revisions. | |
1158 for node in nodes: | |
1159 try: | |
1160 store.rev(node) | |
1161 except error.LookupError: | |
1162 raise error.WireprotoCommandError( | |
1163 'unknown node: %s', (node,)) | |
1164 | |
1165 revisions = store.emitrevisions(nodes, | |
1166 revisiondata=b'revision' in fields, | |
1167 assumehaveparentrevisions=haveparents) | |
1168 | |
1169 yield { | |
1170 b'totalitems': len(nodes), | |
1171 } | |
1172 | |
1173 for revision in revisions: | |
1174 d = { | |
1175 b'node': revision.node, | |
1176 } | |
1177 | |
1178 if b'parents' in fields: | |
1179 d[b'parents'] = [revision.p1node, revision.p2node] | |
1180 | |
1181 followingmeta = [] | |
1182 followingdata = [] | |
1183 | |
1184 if b'revision' in fields: | |
1185 if revision.revision is not None: | |
1186 followingmeta.append((b'revision', len(revision.revision))) | |
1187 followingdata.append(revision.revision) | |
1188 else: | |
1189 d[b'deltabasenode'] = revision.basenode | |
1190 followingmeta.append((b'delta', len(revision.delta))) | |
1191 followingdata.append(revision.delta) | |
1192 | |
1193 if followingmeta: | |
1194 d[b'fieldsfollowing'] = followingmeta | |
1195 | |
1196 yield d | |
1197 | |
1198 for extra in followingdata: | |
1199 yield extra | |
1200 | |
1201 @wireprotocommand( | |
1202 'pushkey', | 1206 'pushkey', |
1203 args={ | 1207 args={ |
1204 'namespace': { | 1208 'namespace': { |
1205 'type': 'bytes', | 1209 'type': 'bytes', |
1206 'example': b'ns', | 1210 'example': b'ns', |