Mercurial > hg > mercurial
annotate mercurialcommon/src/python/loadsubstatescommand.py @ 715:be86907926ae
new approach: remember hg substate file hash per commit,
dump hash > file, and log with refs
author  eugene.petrenko@jetbrains.com 

date  Mon, 13 Jan 2014 18:51:13 +0100 
parents  78266f6904df 
children  0607a0504129 
rev  line source 

706  1 #!/usr/bin/env python 
711
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

2 ## 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

3 ## Copyright 20002014 JetBrains 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

4 ## 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

5 ## Licensed under the Apache License, Version 2.0 (the "License"); 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

6 ## you may not use this file except in compliance with the License. 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

7 ## You may obtain a copy of the License at 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

8 ## 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

9 ## http://www.apache.org/licenses/LICENSE2.0 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

10 ## 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

11 ## Unless required by applicable law or agreed to in writing, software 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

12 ## distributed under the License is distributed on an "AS IS" BASIS, 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

13 ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

14 ## See the License for the specific language governing permissions and 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

15 ## limitations under the License. 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

16 ## 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

17 ## 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

18 ## http://www.gnu.org/licenses/gplfaq.html#GPLModuleLicense 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

19 ## http://www.gnu.org/licenses/licenselist.html#apache2 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

20 ## http://en.wikipedia.org/wiki/Apache_License#GPL_compatibility 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

21 ## 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

22 ## 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

23 """ 
706  24 loadsubstatescommand 
711
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

25 """ 
706  26 
27 import base64  
711
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

28 from mercurial import util, node 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

29 from threading import Thread 
706  30 
31  
32 def load_substates_command(ui, repo, outputFile, **opts):  
33 """Tons of docs"""  
34  
711
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

35 ui.write("Fetching commits...") 
706  36 
715
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

37 NONE = "=====" 
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

38 
711
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

39 def b64(x): 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

40 if x is None or x == "": 
715
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

41 return NONE 
711
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

42 return base64.b64encode( x ) 
706  43 
711
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

44 def fetch_commits(): 
714  45 ui.write("Iterating over commits...\n") 
711
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

46 with open(outputFile + ".commits", "w", 5 * 1024 * 1024) as result: 
715
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

47 result.write("format: prefix commitID commitHash num_parents parent branch num_tags tag user message date [.hgsub] [.hgsubstate]\n") 
711
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

48 result.flush() 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

49 
715
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

50 commit_to_substates = {} 
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

51 
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

52 def update_sub_states(ctx): 
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

53 def filenode(ctx, filename): 
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

54 if filename in ctx.files(): 
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

55 try: 
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

56 return node.hex(ctx.filenode(filename)) 
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

57 except: 
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

58 # file could have been deleted => so there would be no filenode for it 
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

59 return NONE 
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

60 else: 
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

61 return NONE 
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

62 
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

63 def notnull(a, ctx, i): 
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

64 if a != NONE: 
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

65 return a 
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

66 
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

67 for p in ctx.parents(): 
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

68 if commit_to_substates.has_key(p): 
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

69 v = commit_to_substates[p][i] 
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

70 if v != NONE: 
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

71 return v 
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

72 return NONE 
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

73 
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

74 best_sub = notnull(filenode(ctx, ".hgsub"), ctx, 0) 
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

75 best_state = notnull(filenode(ctx, ".hgsubstate"), ctx, 1) 
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

76 
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

77 commit_to_substates[ctx.node()] = (best_sub, best_state) 
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

78 
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

79 return (best_sub, best_state) 
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

80 
711
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

81 for r in list(repo.changelog): 
715
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

82 ctx = repo[r] 
711
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

83 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

84 result.write("$$@@@@ ") # magic 
715
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

85 result.write( str( ctx.rev() ) ) # commit Num 
711
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

86 result.write(" ") 
715
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

87 result.write( ctx.hex() ) # commit ID 
711
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

88 result.write(" ") 
715
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

89 result.write( str( len( ctx.parents()) ) ) # num parents 
711
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

90 
715
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

91 for p in ctx.parents(): # parents 
711
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

92 result.write(" ") 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

93 result.write(p.hex()) 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

94 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

95 result.write(" ") 
715
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

96 result.write( b64( ctx.branch() ) ) # commit branch 
711
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

97 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

98 result.write(" ") 
715
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

99 result.write( str( len( ctx.tags() ) ) ) # num tags 
711
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

100 
715
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

101 for tag in ctx.tags(): # tags 
711
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

102 result.write(" ") 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

103 result.write( b64 ( tag ) ) 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

104 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

105 result.write(" ") # user 
715
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

106 result.write( b64( ctx.user() ) ) 
711
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

107 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

108 result.write(" ") # message 
715
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

109 result.write( b64( ctx.description() ) ) 
711
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

110 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

111 result.write(" ") # date 
715
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

112 result.write( util.datestr( ctx.date(), "%Y%m%dZ%H:%M:%ST%1%2") ) 
711
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

113 
715
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

114 #resolve subrepo mounts 
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

115 (sub_node, state_node) = update_sub_states(ctx) 
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

116 result.write(" " + sub_node + " " + state_node) 
711
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

117 result.write("\n") 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

118 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

119 ui.write("Commits iteration completed") 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

120 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

121 def fetch_file_revisions(filename): 
714  122 ui.write("Fetching revisions of " + filename + " file\n") 
711
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

123 with open(outputFile + filename, "w", 5 * 1024 * 1024) as result: 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

124 result.write("format: prefix commitID base64(" + filename + ")\n") 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

125 result.flush() 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

126 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

127 log = repo.file(filename) 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

128 for r in log: 
715
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

129 result.write("$$@@@@ " + node.hex(log.node(r)) + " " + b64(log.read(r)) + "\n") 
711
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

130 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

131 ui.write("All revisions of file " + filename + " are fetched\n") 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

132 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

133 tasks = [ 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

134 Thread(target=fetch_commits, args=[], name="Fetch commits graph"), 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

135 Thread(target=fetch_file_revisions, args=[".hgsub"], name="Fetch .hgsub"), 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

136 Thread(target=fetch_file_revisions, args=[".hgsubstate"], name="Fetch .hgsubstate"), 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

137 ] 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

138 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

139 for task in tasks: 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

140 task.start() 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

141 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

142 for task in tasks: 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

143 task.join() 
706  144 
145 ui.write("\n##Completed##\n")  
146  
147  
148 #so here goes command registration and options  
149 cmdtable = {  
711
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

150 "loadsubstates": (load_substates_command, [ ], " [options] OUTPUT_FILE") 
706  151 } 
152  
153 testedwith = '2.2.2'  
154 buglink = "@jonnyzzz"  
155 