def __replace_file(self, node, replacement_path): """ Replace the contents of a node. @type node: SvnDumpNode @param node: The node to replace the contents of. @type replacement_path: string @param replacement_path: The path to the replacement file. @rtype: SvnDumpNode @return: The converted node. """ if node.get_text_length() == -1: # no text self.__print(1, " no content to replace.") return node self.__print(1, " replace content with '%s'" % replacement_path) if self.dry_run: # do not replace with --dry-run return node # do the replacement newnode = SvnDumpNode(node.get_path(), node.get_action(), node.get_kind()) if node.has_copy_from(): newnode.set_copy_from(node.get_copy_from_path(), node.get_copy_from_rev()) if node.has_properties(): newnode.set_properties(node.get_properties()) newnode.set_text_file(replacement_path) return newnode
def copy_adding_git_ignore(srcfile, dstfile): """ Copy a dump file adding .gitignore files in all directories that have an svn:ignore property. @type srcfile: string @param srcfile: Source filename. @type dstfile: string @param dstfile: Destination filename. """ # SvnDumpFile classes for reading/writing dumps srcdmp = SvnDumpFile() dstdmp = SvnDumpFile() # open source file srcdmp.open(srcfile) gitignores = {} # used to ensure that copyfrom-revs are correct after the copy. If # there are any empty revision in the source dump file, the copyfrom-revs # could be affected. revmap = {} hasrev = srcdmp.read_next_rev() if hasrev: # create the dump file dstdmp.create_like(dstfile, srcdmp) # now copy all the revisions while hasrev: dstdmp.add_rev(srcdmp.get_rev_props()) for node in srcdmp.get_nodes_iter(): if node.has_copy_from(): node.set_copy_from(node.get_copy_from_path(), revmap[node.get_copy_from_rev()]) # add the original node dstdmp.add_node(node) if node.get_property("svn:ignore") is not None: # find out what the change is and act on it appropriately path = node.get_path() + "/.gitignore" action = node.get_action() if action == "change" or action == "add": if path in gitignores: # already saw this one - it is a change to the .gitignore file newnode = SvnDumpNode(path, "change", "file") else: # haven't seen this one yet newnode = SvnDumpNode(path, "add", "file") gitignores[path] = True f = open("gitignore", "wb") f.write(node.get_property("svn:ignore")) f.close() newnode.set_text_file("gitignore") dstdmp.add_node(newnode) os.remove("gitignore") elif action == "delete": newnode = SvnDumpNode(path, "delete", "file") dstdmp.add_node(newnode) del gitignores[path] else: print("Unhandled action: '%s'" % action) revmap[srcdmp.get_rev_nr()] = dstdmp.get_rev_nr() hasrev = srcdmp.read_next_rev() else: print("no revisions in the source dump '%s' ???" % srcfile) # cleanup srcdmp.close() dstdmp.close()
def __convert_eol(self, node, revnr): """ Convert EOL of a node. @type node: SvnDumpNode @param node: The node to convert. @type revnr: integer @param revnr: The current revision number. @rtype: SvnDumpNode @return: The converted node. """ if not node.has_text(): # no text print(" selected file, no text changes") return node # search for CR need_conv = False handle = node.text_open() data = node.text_read(handle) while len(data) > 0: if data.find("\r") != -1: # found one, need to convert the file need_conv = True break data = node.text_read(handle) fix = self.__fix if need_conv: # special fix option for rev/file ? key = (revnr, node.get_path()) if self.__fix_rev_path.has_key(key): fix = self.__fix_rev_path[key] print(" selected file, convert (fix option %d)" % fix) if self.__dry_run or fix == 0: # do not convert with --dry-run or when there's nothing to fix need_conv = False else: print(" selected file, no conversion required") if need_conv: # do the conversion node.text_reopen(handle) outfilename = self.__temp_file_name() outfile = open(outfilename, "wb") outlen = 0 md = sdt_md5() data = node.text_read(handle) carry = "" warning_printed = False while len(data) > 0: if len(carry) != 0: data = carry + data n = len(data) - 1 carry = data[n] if carry == "\r": data = data[:n] else: carry = "" if fix & 1 != 0: data = data.replace("\r\n", "\n") if fix & 2 != 0: data = data.replace("\r", "\n") if fix & 4 != 0: data = data.replace("\r", "") if not warning_printed and data.find("\r") >= 0: warning_printed = True print(" WARNING: file still contains CR") print(" file: '%s'" % node.get_path()) if self.__warning_file is not None: self.__warning_file.write( "# WARNING: file still contains CR\n") file = node.get_path() while file[0] == "/": file = file[1:] tmpfile = file.replace("/", "__") cmd = '$SVN cat -r %d "$REPOS/%s" > "%s"\n' % \ (revnr, node.get_path(), tmpfile) self.__warning_file.write(cmd) self.__warning_count += 1 md.update(data) outfile.write(data) outlen = outlen + len(data) data = node.text_read(handle) if len(carry) != 0: if fix & 2 != 0: carry = "\n" elif fix & 4 != 0: carry = "" outfile.write(carry) md.update(carry) outlen += len(carry) outfile.close() newnode = SvnDumpNode(node.get_path(), node.get_action(), node.get_kind()) if node.has_copy_from(): newnode.set_copy_from(node.get_copy_from_path(), node.get_copy_from_rev()) if node.has_properties(): newnode.set_properties(node.get_properties()) newnode.set_text_file(outfilename, outlen, md.hexdigest()) else: newnode = node node.text_close(handle) return newnode