Example #1
0
func newRepository(config *Config, path string, hooks map[string]string, self *url.URL, origin *url.URL) ([]byte, error) {
	var out []byte
	repo := git.NewRepositoryForBinary(config.GitBinary)

	if origin != nil {
		if err := repo.CloneMirror(path, origin.String()); err != nil {
			return out, err
		}
	} else {
		if err := repo.Init(path, true); err != nil {
			return out, err
		}
	}

	if self != nil {
		if err := repo.AddLocalConfig(path, "gitserver.self.url", self.String()); err != nil {
			return out, err
		}
	}

	// remove all sample hooks, ignore errors here
	if files, err := ioutil.ReadDir(filepath.Join(path, "hooks")); err == nil {
		for _, file := range files {
			os.Remove(filepath.Join(path, "hooks", file.Name()))
		}
	}

	for name, hook := range hooks {
		dest := filepath.Join(path, "hooks", name)
		if err := os.Remove(dest); err != nil && !os.IsNotExist(err) {
			return out, err
		}
		if err := os.Symlink(hook, dest); err != nil {
			return out, err
		}
	}

	if initHook, ok := hooks["init"]; ok {
		cmd := exec.Command(initHook)
		cmd.Dir = path
		result, err := cmd.CombinedOutput()
		if err != nil {
			return out, fmt.Errorf("init hook failed: %v\n%s", err, string(result))
		}
		out = result
	}

	return out, nil
}
Example #2
0
func newRepository(config *Config, path string, hooks map[string]string, self *url.URL, origin *url.URL) ([]byte, error) {
	var out []byte
	repo := git.NewRepositoryForBinary(config.GitBinary)

	barePath := path
	if !strings.HasSuffix(barePath, ".git") {
		barePath += ".git"
	}
	aliasPath := strings.TrimSuffix(barePath, ".git")

	if origin != nil {
		if err := repo.CloneMirror(barePath, origin.String()); err != nil {
			return out, err
		}
	} else {
		if err := repo.Init(barePath, true); err != nil {
			return out, err
		}
	}

	if self != nil {
		if err := repo.AddLocalConfig(barePath, "gitserver.self.url", self.String()); err != nil {
			return out, err
		}
	}

	// remove all sample hooks, ignore errors here
	if files, err := ioutil.ReadDir(filepath.Join(barePath, "hooks")); err == nil {
		for _, file := range files {
			os.Remove(filepath.Join(barePath, "hooks", file.Name()))
		}
	}

	for name, hook := range hooks {
		dest := filepath.Join(barePath, "hooks", name)
		if err := os.Remove(dest); err != nil && !os.IsNotExist(err) {
			return out, err
		}
		glog.V(5).Infof("Creating hook symlink %s -> %s", dest, hook)
		if err := os.Symlink(hook, dest); err != nil {
			return out, err
		}
	}

	if initHook, ok := hooks["init"]; ok {
		glog.V(5).Infof("Init hook exists, invoking it")
		cmd := exec.Command(initHook)
		cmd.Dir = barePath
		result, err := cmd.CombinedOutput()
		glog.V(5).Infof("Init output:\n%s", result)
		if err != nil {
			return out, fmt.Errorf("init hook failed: %v\n%s", err, string(result))
		}
		out = result
	}

	if err := os.Symlink(barePath, aliasPath); err != nil {
		return out, fmt.Errorf("cannot create alias path %s: %v", aliasPath, err)
	}

	return out, nil
}