def main()

in contrib/client-side/svnmerge/svnmerge.py [0:0]


def main(args):
    global opts

    # Initialize default options
    opts = default_opts.copy()
    logs.clear()

    optsparser = CommandOpts(global_opts, common_opts, command_table,
                             version="%%prog r%s\n  modified: %s\n\n"
                                     "Copyright (C) 2004,2005 Awarix Inc.\n"
                                     "Copyright (C) 2005, Giovanni Bajo"
                                     % (__revision__, __date__))

    cmd, args, state = optsparser.parse(args)
    opts.update(state)

    source = opts.get("source", None)
    branch_dir = "."

    if str(cmd) == "init":
        if len(args) == 1:
            source = args[0]
        elif len(args) > 1:
            optsparser.error("wrong number of parameters", cmd)
    elif str(cmd) in command_table.keys():
        if len(args) == 1:
            branch_dir = args[0]
        elif len(args) > 1:
            optsparser.error("wrong number of parameters", cmd)
    else:
        assert False, "command not handled: %s" % cmd

    # Validate branch_dir
    if not is_wc(branch_dir):
        if str(cmd) == "avail":
            info = None
            # it should be noted here that svn info does not error exit
            # if an invalid target is specified to it (as is
            # intuitive). so the try, except code is not absolutely
            # necessary. but, I retain it to indicate the intuitive
            # handling.
            try:
                info = get_svninfo(branch_dir)
            except LaunchError:
                pass
            # test that we definitely targeted a subversion directory,
            # mirroring the purpose of the earlier is_wc() call
            if info is None or not info.has_key("Node Kind") or info["Node Kind"] != "directory":
                error('"%s" is neither a valid URL, nor a working directory' % branch_dir)
        else:
            error('"%s" is not a subversion working directory' % branch_dir)

    # give out some hints as to potential pathids
    PathIdentifier.hint(branch_dir)
    if source: PathIdentifier.hint(source)

    # Extract the integration info for the branch_dir
    branch_props = get_merge_props(branch_dir)

    # Calculate source_url and source_path
    report("calculate source path for the branch")
    if not source:
        if str(cmd) == "init":
            cf_source, cf_rev, copy_committed_in_rev = get_copyfrom(branch_dir)
            if not cf_source:
                error('no copyfrom info available. '
                      'Explicit source argument (-S/--source) required.')
            opts["source-url"] = get_repo_root(branch_dir) + cf_source
            opts["source-pathid"] = PathIdentifier.from_target(opts["source-url"])

            if not opts["revision"]:
                opts["revision"] = "1-" + cf_rev
        else:
            opts["source-pathid"] = get_default_source(branch_dir, branch_props)
            opts["source-url"] = opts["source-pathid"].get_url()

        assert is_pathid(opts["source-pathid"])
        assert is_url(opts["source-url"])
    else:
        # The source was given as a command line argument and is stored in
        # SOURCE.  Ensure that the specified source does not end in a /,
        # otherwise it's easy to have the same source path listed more
        # than once in the integrated version properties, with and without
        # trailing /'s.
        source = rstrip(source, "/")
        if not is_wc(source) and not is_url(source):
            # Check if it is a substring of a pathid recorded
            # within the branch properties.
            found = []
            for pathid in branch_props.keys():
                if pathid.match_substring(source):
                    found.append(pathid)
            if len(found) == 1:
                # (assumes pathid is a repository-relative-path)
                source_pathid = found[0]
                source = source_pathid.get_url()
            else:
                error('"%s" is neither a valid URL, nor an unambiguous '
                      'substring of a repository path, nor a working directory'
                      % source)
        else:
            source_pathid = PathIdentifier.from_target(source)

        source_pathid = PathIdentifier.from_target(source)
        if str(cmd) == "init" and \
               source_pathid == PathIdentifier.from_target("."):
            error("cannot init integration source path '%s'\n"
                  "Its repository-relative path must differ from the "
                  "repository-relative path of the current directory."
                  % source_pathid)
        opts["source-pathid"] = source_pathid
        opts["source-url"] = target_to_url(source)

    # Sanity check source_url
    assert is_url(opts["source-url"])
    # SVN does not support non-normalized URL (and we should not
    # have created them)
    assert opts["source-url"].find("/..") < 0

    report('source is "%s"' % opts["source-url"])

    # Get previously merged revisions (except when command is init)
    if str(cmd) != "init":
        opts["merged-revs"] = merge_props_to_revision_set(branch_props,
                                                          opts["source-pathid"])

    # Perform the action
    cmd(branch_dir, branch_props)