5 # Author: Simon Hausmann <hausmann@kde.org>
6 # License: MIT <http://www.opensource.org/licenses/mit-license.php>
9 import os
, string
, shelve
, stat
10 import getopt
, sys
, marshal
13 cmd
= "p4 -G %s" % cmd
14 pipe
= os
.popen(cmd
, "rb")
19 entry
= marshal
.load(pipe
)
35 opts
, args
= getopt
.getopt(sys
.argv
[1:], "", [ "continue", "--git-dir=", "origin=", "reset", "master=",
36 "submit-log-subst=" ])
37 except getopt
.GetoptError
:
38 print "fixme, syntax error"
42 logSubstitutions
["<enter description here>"] = "%log%"
43 logSubstitutions
["\tDetails:"] = "\tDetails: %log%"
44 gitdir
= os
.environ
.get("GIT_DIR", "")
57 elif o
== "--continue":
62 elif o
== "--submit-log-subst":
64 value
= a
.split("%")[1]
65 logSubstitutions
[key
] = value
70 os
.environ
["GIT_DIR"] = gitdir
72 configFile
= gitdir
+ "/p4-git-sync.cfg"
79 sys
.stderr
.write(msg
+ "\n")
83 if os
.system(cmd
) != 0:
84 die("command failed: %s" % cmd
)
88 if len(p4CmdList("opened ...")) > 0:
89 die("You have files opened with perforce! Close them before starting the sync.")
92 if len(config
) > 0 and not reset
:
93 die("Cannot start sync. Previous sync config found at %s" % configFile
)
95 #if len(os.popen("git-update-index --refresh").read()) > 0:
96 # die("Your working tree is not clean. Check with git status!")
99 for line
in os
.popen("git-rev-list --no-merges %s..%s" % (origin
, master
)).readlines():
100 commits
.append(line
[:-1])
103 config
["commits"] = commits
105 # print "Cleaning index..."
106 # system("git checkout -f")
108 def prepareLogMessage(template
, message
):
111 substs
= logSubstitutions
112 for k
in substs
.keys():
113 substs
[k
] = substs
[k
].replace("%log%", message
)
115 for line
in template
.split("\n"):
116 if line
.startswith("#"):
117 result
+= line
+ "\n"
121 for key
in substs
.keys():
122 if line
.find(key
) != -1:
124 if value
!= "@remove@":
125 result
+= line
.replace(key
, value
) + "\n"
130 result
+= line
+ "\n"
135 print "Applying %s" % (os
.popen("git-log --max-count=1 --pretty=oneline %s" % id).read())
136 diff
= os
.popen("git diff-tree -r --name-status \"%s^\" \"%s\"" % (id, id)).readlines()
138 filesToDelete
= set()
141 path
= line
[1:].strip()
143 system("p4 edit %s" % path
)
144 elif modifier
== "A":
146 if path
in filesToDelete
:
147 filesToDelete
.remove(path
)
148 elif modifier
== "D":
149 filesToDelete
.add(path
)
150 if path
in filesToAdd
:
151 filesToAdd
.remove(path
)
153 die("unknown modifier %s for %s" % (modifier
, path
))
155 system("git-diff-files --name-only -z | git-update-index --remove -z --stdin")
156 system("git cherry-pick --no-commit \"%s\"" % id)
159 system("p4 add %s" % f
)
160 for f
in filesToDelete
:
161 system("p4 revert %s" % f
)
162 system("p4 delete %s" % f
)
166 for log
in os
.popen("git-cat-file commit %s" % id).readlines():
173 if len(logMessage
) > 0:
175 logMessage
+= log
+ "\n"
177 template
= os
.popen("p4 change -o").read()
178 fileName
= "submit.txt"
179 file = open(fileName
, "w+")
180 file.write(prepareLogMessage(template
, logMessage
))
182 print "Perforce submit template written as %s. Please review/edit and then use p4 submit -i < %s to submit directly!" % (fileName
, fileName
)
186 config
= shelve
.open(configFile
, writeback
=True)
191 commits
= config
.get("commits", [])
196 commits
= commits
[1:]
197 config
["commits"] = commits
202 if len(commits
) == 0:
204 print "No changes found to apply between %s and current HEAD" % origin
206 print "All changes applied!"
207 os
.remove(configFile
)