Other usage examples¶
This page contains some examples of Python scripts using Universum.
Export Perforce repository to Git¶
Here’s an example script that ports certain directory with commit history from Helix Perforce (P4) repository to Git. It sequentially reproduces directory state for each submitted CL, and submits this repository state to Git using the same commit description.
Note
To port the whole repository you just have to set Perforce source directory and Git destination directory to repository root
Command line description¶
--p4-port | |
Source Perforce repository; format equals to Perforce P4PORT
environment variable and usually looks like example.com:1666 | |
--p4-user | |
Perforce account used to download (sync) files from Perforce server | |
--p4-password | |
Corresponding password for –p4-user | |
--p4-client | |
Name of a temporary Perforce client (workspace) to be created automatically; this client will be deleted after the script finishes its work | |
--p4-depot-path | |
Particular folder in source Perforce repository to be ported;
should be provided in Perforce-specific format, e.g. //depot/path/... | |
--p4-client-root | |
Local folder, where the source commits will be downloaded to; should be absolute path; should be anywhere inside destination Git repository | |
--git-repo | |
Destination Git repository; should be absolute path; repository should already exist | |
--git-user | |
Git account used to commit ported changes | |
--git-email | |
Mandatory Git parameter for committer; should correspond to –git-user |
Note
--p4-client-root
should not necessarily equal --git-repo
;
it just has to be somewhere inside repository
Preconditions¶
- Git repository already exists. It can be cloned from remote or created via
git init
- Git account used for porting is authorized to commit
- Perforce account used for porting is authorized to download (sync) source folder
- Perforce client does not exist
Script¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | #!/usr/bin/env python import argparse from P4 import P4 from universum import __main__ as universum def main(): parser = argparse.ArgumentParser() parser.add_argument("--p4-port") parser.add_argument("--p4-user") parser.add_argument("--p4-password") parser.add_argument("--p4-client") parser.add_argument("--p4-depot-path") parser.add_argument("--p4-client-root") parser.add_argument("--git-repo") parser.add_argument("--git-user") parser.add_argument("--git-email") args = parser.parse_args() p4 = P4() p4.port = args.p4_port p4.user = args.p4_user p4.password = args.p4_password client_name = args.p4_client p4.connect() depot_path = args.p4_depot_path client = p4.fetch_client(client_name) client["Root"] = args.p4_client_root client["View"] = [depot_path + " //" + client_name + "/..."] p4.save_client(client) p4.client = client_name changes = p4.run_changes("-s", "submitted", depot_path) cl_list = [] for change in changes: cl_list.append(change["change"]) for cl in reversed(cl_list): line = depot_path + '@' + cl p4.run_sync("-f", line) universum.main(["submit", "-ot", "term", "-vt", "git", "-cm", p4.run_describe(cl)[0]['desc'], "-gu", args.git_user, "-ge", args.git_email, "-pr", args.git_repo, "-gr", "file://" + args.git_repo, "-grs", "master"]) p4.delete_client(client_name) if __name__ == "__main__": main() |
Possible script modifications¶
In this example commit messages are preserved, but all changes are committed to Git from one user.
To port commit users as well use p4.run_describe(cl)[0]['user']
to find P4 user
and replace incoming parameters --git-user
, --git-email
with
mapping of P4 users into Git user parameters (-gu
, -ge
) that are passed to submitter.
See lines 52-53 for the reference.
Also this script only can process the contents of one P4 folder, creating a single mapping for it
in client["View"]
. To use multiple mappings, edit client["View"]
accordingly
instead of parsing --depot-path
parameter. See line 35 for the reference.