Beispiel #1
0
func SplitLines(b []byte) []string {
	lines := []string{}
	r := bufio.NewReader(bytes.NewBuffer(b))
	for line := range iochan.ReaderChan(r, "\n") {
		lines = append(lines, line)
	}
	return lines
}
Beispiel #2
0
func (ctx *Context) git_cmd(cmdargs ...string) []string {
	lines := []string{}
	cmd := exec.Command("git", cmdargs...)
	if ctx.Verbose {
		fmt.Printf(":: running %s\n", strings.Join(cmd.Args, " "))
	}
	out, err := cmd.CombinedOutput()
	if err != nil {
		// ignore error
		return lines
	}
	r := bufio.NewReader(bytes.NewBuffer(out))
	for line := range iochan.ReaderChan(r, "\n") {
		lines = append(lines, line)
	}
	return lines
}
Beispiel #3
0
func (ctx *Context) fix_tags() error {
	var err error = nil
	usr := make(map[string]string)

	// we only change git config values if ctx.Repo.tags are available.
	// so it stands to reason we should revert them only in that case.
	defer func() {
		if len(ctx.Repo.tags) == 0 {
			return
		}
		for name, v := range usr {
			vv := strings.Trim(v, " ")
			if vv != "" {
				cmd := exec.Command("git", "config", "--local", name,
					strconv.Quote(vv))
				_ = cmd.Run()
			} else {
				cmd := exec.Command("git", "config", "--local", "--unset", name)
				_ = cmd.Run()
			}
			//fmt.Printf("%s: %q %q\n", name, v, vv)
		}
	}()

	git_cfg := func(k string) (string, error) {
		cmd := exec.Command("git", "config", "--local", "--get", k)
		out, err := cmd.CombinedOutput()
		if err != nil {
			// ignore error!
			return "", nil
		}
		r := bufio.NewReader(bytes.NewBuffer(out))
		lines := []string{}
		for line := range iochan.ReaderChan(r, "\n") {
			lines = append(lines, line)
		}
		if len(lines) != 1 {
			return "", fmt.Errorf("too many lines (%d)", len(lines))
		}
		return lines[0], err
	}
	usr["user.name"], _ = git_cfg("user.name")
	usr["user.email"], _ = git_cfg("user.name")

	for itag, tag := range ctx.Repo.tags {
		tag = strings.Trim(tag, " ")
		id := tag[len("svn/tags/"):]
		if ctx.Verbose {
			hdr := ""
			if itag > 0 {
				hdr = "\n"
			}
			fmt.Printf("%s:: processing svn tag [%s]...\n", hdr, tag)
		}
		subject := ctx.git_cmd("log", "-1", "--pretty=format:%s", tag)[0]
		date := ctx.git_cmd("log", "-1", "--pretty=format:%ci", tag)[0]
		author := ctx.git_cmd("log", "-1", "--pretty=format:%an", tag)[0]
		email := ctx.git_cmd("log", "-1", "--pretty=format:%ae", tag)[0]

		cmd := exec.Command("git", "config", "--local", "user.name",
			"\""+author+"\"")
		ctx.print_cmd(cmd)
		_ = cmd.Run()

		cmd = exec.Command("git", "config", "--local", "user.email",
			"\""+email+"\"")
		ctx.print_cmd(cmd)
		_ = cmd.Run()

		cmd = exec.Command("git", "tag", "-a", "-m",
			fmt.Sprintf("\"%s\"", subject),
			id,
			tag)
		cmd.Env = os.Environ()
		cmd.Env = append(cmd.Env, fmt.Sprintf("GIT_COMMITTER_DATE=%s", date))
		ctx.print_cmd(cmd)
		err = cmd.Run()
		if err != nil {
			fmt.Printf("**error** %v\n", err)
			return err
		}

		cmd = exec.Command("git", "branch", "-d", "-r", tag)
		ctx.print_cmd(cmd)
		err = cmd.Run()
		if err != nil {
			fmt.Printf("**error** %v\n", err)
			return err
		}

		//fmt.Printf("tag: %q - subject: %q\n", tag, subject)
	}
	return err
}
Beispiel #4
0
func (ctx *Context) get_branches() error {
	var err error = nil
	// get the list of local and remote branches
	// ignore console color codes
	cmd := exec.Command("git", "branch", "-l", "--no-color")
	if ctx.Verbose {
		fmt.Printf(":: --> building list of [local branches]...\n")
		fmt.Printf(":: running %s\n", strings.Join(cmd.Args, " "))
	}
	out, err := cmd.CombinedOutput()
	if err != nil {
		return err
	}
	lines := bufio.NewReader(bytes.NewBuffer(out))
	for line := range iochan.ReaderChan(lines, "\n") {
		if ctx.Verbose {
			fmt.Printf("   %q\n", line)
		}
		if strings.HasPrefix(line, "*") {
			line = strings.Replace(line, "*", "", 1)
		}
		line = strings.Trim(line, " \r\n")
		if ctx.Verbose {
			fmt.Printf("-> %q\n", line)
		}
		ctx.Repo.local_branches = append(ctx.Repo.local_branches, line)
	}

	// remote branches...
	cmd = exec.Command("git", "branch", "-r", "--no-color")
	if ctx.Verbose {
		fmt.Printf(":: --> building list of [remote branches]...\n")
		fmt.Printf(":: running %s\n", strings.Join(cmd.Args, " "))
	}
	out, err = cmd.CombinedOutput()
	if err != nil {
		return err
	}
	lines = bufio.NewReader(bytes.NewBuffer(out))
	for {
		line, err1 := lines.ReadString('\n')
		if err1 != nil {
			if err1 != io.EOF {
				err = err1
			}
			break
		}
		if ctx.Verbose {
			fmt.Printf("   %q\n", line)
		}
		if strings.HasPrefix(line, "*") {
			line = strings.Replace(line, "*", "", 1)
		}
		line = strings.Trim(line, " \r\n")
		if ctx.Verbose {
			fmt.Printf("-> %q\n", line)
		}
		ctx.Repo.remote_branches = append(ctx.Repo.remote_branches, line)
	}

	// tags are remote branches that start with "svn/tags/"
	if ctx.Verbose {
		fmt.Printf(":: --> building list of [svn-tags]...\n")
	}
	for _, branch := range ctx.Repo.remote_branches {
		if strings.HasPrefix(branch, "svn/tags/") {
			tag := branch //branch[len("svn/tags/"):]
			if ctx.Verbose {
				fmt.Printf(":: adding tag %q...\n", tag)
			}
			ctx.Repo.tags = append(ctx.Repo.tags, tag)

		}
	}
	return err
}