Mercurial > evolve
comparison hgext3rd/topic/__init__.py @ 4302:8e9940f1ae56
topic: simplify _showlasttouched and _getlasttouched (and its data structure)
author | Anton Shestakov <av6@dwimlabs.net> |
---|---|
date | Mon, 03 Dec 2018 14:15:00 +0800 |
parents | 69fb9e41ce2b |
children | 78700a59192a |
comparison
equal
deleted
inserted
replaced
4301:5cbaf5d25443 | 4302:8e9940f1ae56 |
---|---|
1024 fm.plain(')\n') | 1024 fm.plain(')\n') |
1025 fm.end() | 1025 fm.end() |
1026 | 1026 |
1027 def _showlasttouched(repo, fm, opts): | 1027 def _showlasttouched(repo, fm, opts): |
1028 topics = repo.topics | 1028 topics = repo.topics |
1029 timedict = _getlasttouched(repo, topics) | |
1030 times = timedict.keys() | |
1031 times.sort() | |
1032 if topics: | 1029 if topics: |
1033 maxwidth = max(len(t) for t in topics) | 1030 maxwidth = max(len(t) for t in topics) |
1034 namemask = '%%-%is' % maxwidth | 1031 namemask = '%%-%is' % maxwidth |
1035 activetopic = repo.currenttopic | 1032 activetopic = repo.currenttopic |
1036 for timevalue in times: | 1033 topicsdata = sorted(_getlasttouched(repo, topics)) |
1037 curtopics = sorted(timedict[timevalue][1]) | 1034 for age, topic, date, user in topicsdata: |
1038 for topic, user in curtopics: | 1035 fm.startitem() |
1039 fm.startitem() | 1036 marker = ' ' |
1040 marker = ' ' | 1037 label = 'topic' |
1041 label = 'topic' | 1038 active = (topic == activetopic) |
1042 active = (topic == activetopic) | 1039 if active: |
1043 if active: | 1040 marker = '*' |
1044 marker = '*' | 1041 label = 'topic.active' |
1045 label = 'topic.active' | 1042 fm.plain(' %s ' % marker, label=label) |
1046 fm.plain(' %s ' % marker, label=label) | 1043 fm.write('topic', namemask, topic, label=label) |
1047 fm.write('topic', namemask, topic, label=label) | 1044 fm.data(active=active) |
1048 fm.data(active=active) | 1045 fm.plain(' (') |
1049 fm.plain(' (') | 1046 if age == -1: |
1050 if timevalue == -1: | 1047 timestr = 'empty and active' |
1051 timestr = 'empty and active' | 1048 else: |
1052 else: | 1049 timestr = templatefilters.age(date) |
1053 timestr = templatefilters.age(timedict[timevalue][0]) | 1050 fm.write('lasttouched', '%s', timestr, label='topic.list.time') |
1054 fm.write('lasttouched', '%s', timestr, label='topic.list.time') | 1051 if user: |
1055 if user: | 1052 fm.write('usertouched', ' by %s', user, label='topic.list.user') |
1056 fm.write('usertouched', ' by %s', user, label='topic.list.user') | 1053 fm.plain(')') |
1057 fm.plain(')') | 1054 fm.plain('\n') |
1058 fm.plain('\n') | |
1059 fm.end() | 1055 fm.end() |
1060 | 1056 |
1061 def _getlasttouched(repo, topics): | 1057 def _getlasttouched(repo, topics): |
1062 """ | 1058 """ |
1063 Calculates the last time a topic was used. Returns a dictionary of seconds | 1059 Calculates the last time a topic was used. Returns a generator of 4-tuples: |
1064 passed from current time for a topic as keys and topic name as values. | 1060 (age in seconds, topic name, date, and user who last touched the topic). |
1065 """ | 1061 """ |
1066 topicstime = {} | |
1067 curtime = time.time() | 1062 curtime = time.time() |
1068 for t in topics: | 1063 for topic in topics: |
1069 secspassed = -1 | 1064 age = -1 |
1070 user = None | 1065 user = None |
1071 maxtime = (0, 0) | 1066 maxtime = (0, 0) |
1072 trevs = repo.revs("topic(%s)", t) | 1067 trevs = repo.revs("topic(%s)", topic) |
1073 # Need to check for the time of all changesets in the topic, whether | 1068 # Need to check for the time of all changesets in the topic, whether |
1074 # they are obsolete of non-heads | 1069 # they are obsolete of non-heads |
1075 # XXX: can we just rely on the max rev number for this | 1070 # XXX: can we just rely on the max rev number for this |
1076 for revs in trevs: | 1071 for revs in trevs: |
1077 rt = repo[revs].date() | 1072 rt = repo[revs].date() |
1088 if rt[0] > maxtime[0]: | 1083 if rt[0] > maxtime[0]: |
1089 user = marker.metadata().get('user', user) | 1084 user = marker.metadata().get('user', user) |
1090 maxtime = rt | 1085 maxtime = rt |
1091 | 1086 |
1092 username = stack.parseusername(user) | 1087 username = stack.parseusername(user) |
1093 topicuser = (t, username) | |
1094 | |
1095 if trevs: | 1088 if trevs: |
1096 secspassed = (curtime - maxtime[0]) | 1089 age = curtime - maxtime[0] |
1097 try: | 1090 |
1098 topicstime[secspassed][1].append(topicuser) | 1091 yield (age, topic, maxtime, username) |
1099 except KeyError: | |
1100 topicstime[secspassed] = (maxtime, [topicuser]) | |
1101 | |
1102 return topicstime | |
1103 | 1092 |
1104 def summaryhook(ui, repo): | 1093 def summaryhook(ui, repo): |
1105 t = getattr(repo, 'currenttopic', '') | 1094 t = getattr(repo, 'currenttopic', '') |
1106 if not t: | 1095 if not t: |
1107 return | 1096 return |