@@ -2878,88 +2878,72 @@ def self_update(args):
28782878 logger .info ("%s updated" % SELF_FILE )
28792879
28802880
2881- def order_revisions_by_id (phids_by_id , revs , ids ):
2882- return [revs [phids_by_id [str (id )]] for id in ids if phids_by_id [str (id )]]
2883-
2884-
2885- def order_revisions_by_phid (revs , phids ):
2886- return [revs [phid ] for phid in phids ]
2887-
2888-
28892881def get_revisions (cwd , ids = None , phids = None ):
28902882 """Get revisions info from Phabricator.
28912883
28922884 Args:
28932885 cwd - current working directory
2894- ids - list of ids
2886+ ids - list of revision ids
28952887 phids - list of revision phids
28962888
28972889 Returns a list of revisions ordered by ids or phids
2898-
2899- Raises Exception if both ids and phids are provided
29002890 """
2901- if ids and phids :
2902- # Ordering would be too complicated
2903- raise ValueError ("ids and phids are mutually exclusive " )
2891+
2892+ if ( ids and phids ) or ( ids is None and phids is None ):
2893+ raise ValueError ("Internal Error: Invalid args to get_revisions " )
29042894
29052895 if not ids and not phids :
29062896 return []
29072897
2908- collected_ids = (
2909- dict (
2898+ # Initialise depending on if we're passed revision IDs or PHIDs.
2899+ if ids :
2900+ ids = [str (rev_id ) for rev_id in ids ]
2901+ phids_by_id = dict (
29102902 [
2911- (str ( id ) , cache .get ("rev-id-%s" % id ))
2912- for id in ids
2913- if "rev-id-%s" % id in cache
2903+ (rev_id , cache .get ("rev-id-%s" % rev_id ))
2904+ for rev_id in ids
2905+ if "rev-id-%s" % rev_id in cache
29142906 ]
29152907 )
2916- if ids
2917- else {}
2918- )
2908+ found_phids = phids_by_id . values ()
2909+ query_field = "ids"
2910+ query_values = [ int ( rev_id ) for rev_id in set ( ids ) - set ( phids_by_id . keys ())]
29192911
2920- found_phids = collected_ids .values () if ids else phids [:]
2921-
2922- collected_revs = (
2923- dict (
2924- [
2925- (phid , cache .get ("rev-%s" % phid ))
2926- for phid in found_phids
2927- if "rev-%s" % phid in cache
2928- ]
2929- )
2930- if found_phids
2931- else {}
2912+ else :
2913+ phids_by_id = {}
2914+ found_phids = phids [:]
2915+ query_field = "phids"
2916+ query_values = list (set (phids ) - set (phids_by_id .values ()))
2917+
2918+ # Revisions metadata keyed by PHID.
2919+ revisions = dict (
2920+ [
2921+ (phid , cache .get ("rev-%s" % phid ))
2922+ for phid in found_phids
2923+ if "rev-%s" % phid in cache
2924+ ]
29322925 )
29332926
2934- if ids and len (ids ) == len (collected_ids ):
2935- return order_revisions_by_id (collected_ids , collected_revs , ids )
2936-
2937- if phids and len (phids ) == len (collected_revs ):
2938- return order_revisions_by_phid (collected_revs , phids )
2939-
2940- api_call_args = {"constraints" : {}, "attachments" : {"reviewers" : True }}
2927+ # Query Phabricator if we don't have cached values for revisions.
2928+ if query_values :
2929+ api_call_args = {
2930+ "constraints" : {query_field : query_values },
2931+ "attachments" : {"reviewers" : True },
2932+ }
2933+ response = arc_call_conduit ("differential.revision.search" , api_call_args , cwd )
2934+ rev_list = response .get ("data" )
2935+
2936+ for r in rev_list :
2937+ phids_by_id [str (r ["id" ])] = r ["phid" ]
2938+ revisions [r ["phid" ]] = r
2939+ cache .set ("rev-id-%s" % r ["id" ], r ["phid" ])
2940+ cache .set ("rev-%s" % r ["phid" ], r )
2941+
2942+ # Return revisions in the same order requested.
29412943 if ids :
2942- to_collect = list (set (ids ) - set ([int (key ) for key in collected_ids .keys ()]))
2943- api_call_args ["constraints" ]["ids" ] = to_collect
2944+ return [revisions [phids_by_id [rev_id ]] for rev_id in ids if phids_by_id [rev_id ]]
29442945 else :
2945- to_collect = list (set (phids ) - set (collected_ids .values ()))
2946- api_call_args ["constraints" ]["phids" ] = to_collect
2947-
2948- response = arc_call_conduit ("differential.revision.search" , api_call_args , cwd )
2949- rev_list = response .get ("data" )
2950- if not rev_list :
2951- return
2952-
2953- for r in rev_list :
2954- collected_ids [str (r ["id" ])] = r ["phid" ]
2955- collected_revs [r ["phid" ]] = r
2956- cache .set ("rev-id-%s" % r ["id" ], r ["phid" ])
2957- cache .set ("rev-%s" % r ["phid" ], r )
2958-
2959- if ids :
2960- return order_revisions_by_id (collected_ids , collected_revs , ids )
2961-
2962- return order_revisions_by_phid (collected_revs , phids )
2946+ return [revisions [phid ] for phid in phids ]
29632947
29642948
29652949def get_diffs (cwd , phids ):
0 commit comments