github.com/hwaf/hwaf@v0.0.0-20140814122253-5465f73b20f1/cmd_git_svn_clone.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"os/exec"
     7  	// "path/filepath"
     8  
     9  	"github.com/gonuts/commander"
    10  	"github.com/gonuts/flag"
    11  	"github.com/sbinet/go-svn2git/svn"
    12  )
    13  
    14  func hwaf_make_cmd_git_svn_clone() *commander.Command {
    15  	cmd := &commander.Command{
    16  		Run:       hwaf_run_cmd_git_svn_clone,
    17  		UsageLine: "svn-clone [options] <URL>",
    18  		Short:     "convert a SVN repository into a GIT one",
    19  		Long: `
    20  svn-clone converts a SVN repository into a GIT one.
    21  
    22  ex:
    23   $ hwaf git svn-clone svn+ssh://svn.cern.ch/atlasoff/Control/AthenaCommon
    24  `,
    25  		Flag: *flag.NewFlagSet("hwaf-git-svn-clone", flag.ExitOnError),
    26  	}
    27  	cmd.Flag.Bool("verbose", false, "")
    28  	cmd.Flag.Bool("metadata", false, "include metadata in git logs (git-svn-id)")
    29  	cmd.Flag.Bool("no-minimize-url", false, "accept URLs as-is without attempting to connect a higher level directory")
    30  	cmd.Flag.Bool("root-is-trunk", false, "use this if the root level of the repo is equivalent to the trunk and there are no tags or branches")
    31  	cmd.Flag.Bool("rebase", false, "instead of cloning a new project, rebase an existing one against SVN")
    32  	cmd.Flag.String("username", "", "username for transports that needs it (http(s), svn)")
    33  	cmd.Flag.String("trunk", "trunk", "subpath to trunk from repository URL")
    34  	cmd.Flag.String("branches", "branches", "subpath to branches from repository URL")
    35  	cmd.Flag.String("tags", "tags", "subpath to tags from repository URL")
    36  	cmd.Flag.String("exclude", "", "regular expression to filter paths when fetching")
    37  	cmd.Flag.String("revision", "", "start importing from SVN revision START_REV; optionally end at END_REV. e.g. -revision START_REV:END_REV")
    38  
    39  	cmd.Flag.Bool("no-trunk", false, "do not import anything from trunk")
    40  	cmd.Flag.Bool("no-branches", false, "do not import anything from branches")
    41  	cmd.Flag.Bool("no-tags", false, "do not import anything from tags")
    42  	cmd.Flag.String("authors", "$HOME/.config/go-svn2git/authors", "path to file containing svn-to-git authors mapping")
    43  	return cmd
    44  }
    45  
    46  func hwaf_run_cmd_git_svn_clone(cmd *commander.Command, args []string) error {
    47  	var err error
    48  	n := "hwaf-" + cmd.Name()
    49  
    50  	ctx := svn.NewContextFrom(
    51  		"no-url",
    52  		cmd.Flag.Lookup("verbose").Value.Get().(bool),
    53  		cmd.Flag.Lookup("metadata").Value.Get().(bool),
    54  		cmd.Flag.Lookup("no-minimize-url").Value.Get().(bool),
    55  		cmd.Flag.Lookup("root-is-trunk").Value.Get().(bool),
    56  		cmd.Flag.Lookup("rebase").Value.Get().(bool),
    57  		cmd.Flag.Lookup("username").Value.Get().(string),
    58  		cmd.Flag.Lookup("trunk").Value.Get().(string),
    59  		cmd.Flag.Lookup("branches").Value.Get().(string),
    60  		cmd.Flag.Lookup("tags").Value.Get().(string),
    61  		cmd.Flag.Lookup("exclude").Value.Get().(string),
    62  		cmd.Flag.Lookup("revision").Value.Get().(string),
    63  		cmd.Flag.Lookup("no-trunk").Value.Get().(bool),
    64  		cmd.Flag.Lookup("no-branches").Value.Get().(bool),
    65  		cmd.Flag.Lookup("no-tags").Value.Get().(bool),
    66  		cmd.Flag.Lookup("authors").Value.Get().(string),
    67  	)
    68  
    69  	if ctx.RootIsTrunk {
    70  		ctx.Trunk = ""
    71  		ctx.Branches = ""
    72  		ctx.Tags = ""
    73  	}
    74  
    75  	if ctx.NoTrunk {
    76  		ctx.Trunk = ""
    77  	}
    78  
    79  	if ctx.NoBranches {
    80  		ctx.Branches = ""
    81  	}
    82  
    83  	if ctx.NoTags {
    84  		ctx.Tags = ""
    85  	}
    86  
    87  	if ctx.Verbose {
    88  		fmt.Printf("==go-svn2git...\n")
    89  		fmt.Printf(" verbose:  %v\n", ctx.Verbose)
    90  		fmt.Printf(" rebase:   %v\n", ctx.Rebase)
    91  		fmt.Printf(" username: %q\n", ctx.UserName)
    92  		fmt.Printf(" trunk:    %q\n", ctx.Trunk)
    93  		fmt.Printf(" branches: %q\n", ctx.Branches)
    94  		fmt.Printf(" tags:     %q\n", ctx.Tags)
    95  		fmt.Printf(" authors:  %q\n", ctx.Authors)
    96  		fmt.Printf(" root-is-trunk: %v\n", ctx.RootIsTrunk)
    97  		fmt.Printf(" exclude:  %q\n", ctx.Exclude)
    98  	}
    99  
   100  	if ctx.Rebase {
   101  		if len(args) > 0 {
   102  			fmt.Printf("%s: too many arguments\n", n)
   103  			fmt.Printf("%s: \"-rebase\" takes no argument\n", n)
   104  			//git_svn_usage()
   105  			git := exec.Command("git", "status", "--porcelain", "--untracked-files=no")
   106  			out, err := git.CombinedOutput()
   107  			if len(out) != 0 {
   108  				fmt.Printf("%s: you have pending changes. The working tree must be clean in order to continue.\n", n)
   109  			}
   110  			if err != nil {
   111  				return err
   112  			}
   113  		}
   114  	} else {
   115  		ok := true
   116  		switch len(args) {
   117  		case 0:
   118  			fmt.Printf("%s: missing SVN_URL parameter\n", n)
   119  			ok = false
   120  		case 1:
   121  			/*noop*/
   122  		default:
   123  			fmt.Printf("%s: too many arguments: %v\n", n, args)
   124  			fmt.Printf("%s: did you pass an option *after* the url ?\n", n)
   125  			ok = false
   126  		}
   127  		if !ok {
   128  			fmt.Printf("%s: \"-help\" for help\n", n)
   129  			os.Exit(1)
   130  		}
   131  		ctx.Url = args[0]
   132  	}
   133  
   134  	err = ctx.Run()
   135  
   136  	return err
   137  }
   138  
   139  // EOF