Mercurial > hg > mercurial
annotate mercurialcommon/src/python/loadsubstatescommand.py @ 971:c5dc2c802e4b Indore2017.1.x
UP8618
author  Dmitry Neverov <dmitry.neverov@gmail.com> 

date  Thu, 22 Jun 2017 12:54:00 +0200 
parents  0607a0504129 
children  3da1f4168906 
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): 
716  53 def filenode(ctx, filename, i): 
715
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 
716  59 # this also means we should avoid parents as file source 
715
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

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

61 else: 
716  62 for p in ctx.parents(): 
63 if commit_to_substates.has_key(p.hex()):  
64 v = commit_to_substates[p.hex()][i]  
65 if v != NONE:  
66 return v  
715
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

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

68 
716  69 best_sub = filenode(ctx, ".hgsub", 0) 
70 best_state = filenode(ctx, ".hgsubstate", 1)  
71 commit_to_substates[ctx.hex()] = (best_sub, best_state)  
72 return best_sub, best_state  
715
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

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

74 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

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

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

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

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

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

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

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

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

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

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

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

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

87 
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( b64( ctx.branch() ) ) # commit branch 
711
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

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

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

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

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

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

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

96 result.write( b64 ( tag ) ) 
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(" ") # user 
715
be86907926ae
new approach: remember hg substate file hash per commit,
eugene.petrenko@jetbrains.com
parents:
714
diff
changeset

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

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

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

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

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

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

105 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

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

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

108 (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

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

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

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

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

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

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

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

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

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

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

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

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

122 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

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

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

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

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

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

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

129 Thread(target=fetch_file_revisions, args=[".hgsubstate"], name="Fetch .hgsubstate"), 
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 
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

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

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

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

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

136 task.join() 
706  137 
138 ui.write("\n##Completed##\n")  
139  
140  
141 #so here goes command registration and options  
142 cmdtable = {  
711
7d9e79de81e6
updated logsubstates command that logs:
eugene.petrenko@jetbrains.com
parents:
706
diff
changeset

143 "loadsubstates": (load_substates_command, [ ], " [options] OUTPUT_FILE") 
706  144 } 
145  
971  146 load_substates_command.norepo = False 
147 load_substates_command.optionalrepo = False  
148 load_substates_command.inferrepo = False  
706  149 
971  150 testedwith = '4.2.1' 
151 buglink = "upsourcesupport@jetbrains.com"  
152 