/
initCmd.go
60 lines (51 loc) · 1.32 KB
/
initCmd.go
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
package main
import (
"github.com/usovalx/git-bzr-bridge/bzr"
"github.com/usovalx/git-bzr-bridge/git"
"flag"
"fmt"
"io/ioutil"
"os"
)
func initCmd(args []string) {
// command-line flags
fs := flag.NewFlagSet("init", flag.ExitOnError)
help := fs.Bool("h", false, "show usage message")
fs.Usage = func() { initUsage(fs) }
fs.Parse(args)
if *help {
fs.Usage()
os.Exit(0)
}
if fs.NArg() != 1 {
fs.Usage()
os.Exit(2)
}
repoPath := fs.Arg(0)
log.Debug("Creating ", repoPath)
// it's ok if given folder already exists
// example: init .
os.Mkdir(repoPath, 0777)
must(os.Chdir(repoPath))
// first try to initialize bzr repo, so that it will fail early in case of any issues
log.Debug("Initializing bzr repo")
must(bzr.InitRepo(bzrRepo))
log.Debug("Initializing git repo")
must(git.InitRepo("."))
log.Debug("Creating branch config")
must(ioutil.WriteFile(branchConfigName, []byte("[]"), 0666))
log.Debug("Creating marks files")
must(ioutil.WriteFile(bzrMarks, []byte{}, 0666))
must(ioutil.WriteFile(gitMarks, []byte{}, 0666))
log.Debug("Creating temp dir")
must(os.Mkdir(tmpDir, 0777))
}
func initUsage(fs *flag.FlagSet) {
fmt.Println("usage: git-bzr-bridge init [-h] <path>")
fmt.Println("\nflags:")
fs.SetOutput(os.Stdout)
fs.PrintDefaults()
fmt.Print(`
init will initialize a new repository at <path>
`)
}