Exemple #1
0
func runPostCommit(cmd *cobra.Command, args []string) {
	repo := getRepoOrExit()

	head, err := repo.Head()
	if err != nil {
		log.Fatalf("Could not get repo HEAD")
	}

	commit, err := repo.LookupCommit(head.Target())
	if err != nil {
		log.Fatalf("Could not find commit pointed at by HEAD")
	}

	ident, err := githelp.GetRepoIdent(repo)
	if err != nil {
		log.Fatalln("Failed to retrieve identifier for repository")
	}

	m := new(ingest.Message)
	m.Add(commitToSemanticForm(commit, ident))

	recordHead(m, repo)
	sendMapToPipeviz(m, repo)
}
Exemple #2
0
func syncHistory(repo *git.Repository, all bool) {
	var err error
	var ident string

	msg := new(ingest.Message)

	if all {
		ident, err = githelp.GetRepoIdent(repo)
		if err != nil {
			log.Fatalf("Failed to retrieve a stable identifier for this repository; cannot formulate commits correctly. Aborting.")
		}
	}

	cvisited := make(map[git.Oid]struct{})

	iter, err := repo.NewReferenceIterator()
	if err != nil {
		log.Fatalln("Error while creating reference iterator:", err)
	}

	// For simplicity, create a revwalker now even if we don't use it later
	w, err := repo.Walk()
	if err != nil {
		log.Fatalln("Could not create revwalker iterator:", err)
	}

	w.Sorting(git.SortTopological)
	//defer w.Free()

	for ref, err := iter.Next(); err == nil; ref, err = iter.Next() {
		// in func for easy defer of Free()
		func(r *git.Reference, m *ingest.Message) {
			//defer r.Free()
			oid := r.Target()
			if !r.IsBranch() && !r.IsTag() {
				return
			}
			if r.IsBranch() {
				bn, _ := r.Branch().Name()

				w.Push(oid)
				m.Add(semantic.CommitMeta{
					Sha1Str:  hex.EncodeToString(oid[:]),
					Tags:     make([]string, 0),
					Branches: []string{bn},
				})
			} else if r.IsTag() {
				w.Push(oid)
				m.Add(semantic.CommitMeta{
					Sha1Str: hex.EncodeToString(oid[:]),
					// TODO this still emits the refs/tags/<name> form, ugh
					Tags:     []string{r.Name()},
					Branches: make([]string, 0),
				})
			} else {
				log.Fatalf("Ref %s is neither branch nor tag - wtf\n", r.Name())
			}
		}(ref, msg)
	}
	//iter.Free()

	if err != nil {
		if !git.IsErrorCode(err, git.ErrIterOver) {
			//if gerr, ok := err.(*git.GitError); !ok || gerr.Code != git.ErrIterOver {
			gerr := err.(*git.GitError)
			log.Fatalf("Iteration through repository refs terminated with unexpected error (code: %d, message: %q)\n", gerr.Code, gerr.Message)
		}
	}

	if all {
		w.Iterate(func(c *git.Commit) bool {
			//defer c.Free()
			if _, exists := cvisited[*c.Id()]; exists {
				return false
			}

			cvisited[*c.Id()] = struct{}{}
			msg.Add(commitToSemanticForm(c, ident))
			return true
		})
	}
	//w.Free()
	recordHead(msg, repo)

	sendMapToPipeviz(msg, repo)
}
Exemple #3
0
func (ic *instrumentCmd) run(cmd *cobra.Command, args []string) {
	var err error
	repo := getRepoOrExit(args...)

	// If we can't get an ident, error out
	_, err = githelp.GetRepoIdent(repo)
	if err != nil {
		log.Fatalf("Failed to retrieve a stable identifier for this repository; hooks will not work correctly. Aborting.")
	}

	if ic.target == "" {
		ic.target, err = githelp.GetTargetAddr(repo)
		if err != nil {
			log.Fatalf("No pipeviz server target provided, and one is not already registered in git's config.")
		}
	} else {
		cfg, err := repo.Config()
		if err != nil {
			log.Fatalln("Error attempting to retrieve git config", err)
		}

		err = cfg.SetString("pipeviz.target", ic.target)
		if err != nil {
			log.Fatalln("Error while writing pipeviz target to config", err)
		}
		fmt.Println("Set target pipeviz server to", ic.target)
	}

	// Write the post-commit hook, unless user said no
	if !ic.ncom {
		f, err := os.Create(repo.Path() + "/hooks/post-commit")
		if err != nil {
			log.Fatalln("Error while attempting to open post-commit hook for writing:", err)
		}

		tmpl, err := template.New("post-commit").Funcs(template.FuncMap{
			"binpath": osext.Executable,
		}).Parse(postCommit)

		if err != nil {
			log.Fatalln("Error while parsing script template:", err)
		}

		err = tmpl.Execute(f, nil)
		if err != nil {
			log.Fatalln("Error while writing to post-commit hook file:", err)
		}
		f.Chmod(0755)
		fmt.Println("Wrote post-commit hook.")
	}

	// Write the post-checkout hook, unless user said no
	if !ic.ncheck {
		f, err := os.Create(repo.Path() + "/hooks/post-checkout")
		if err != nil {
			log.Fatalln("Error while attempting to open post-commit hook for writing:", err)
		}

		tmpl, err := template.New("post-checkout").Funcs(template.FuncMap{
			"binpath": osext.Executable,
		}).Parse(postCheckout)

		if err != nil {
			log.Fatalln("Error while parsing script template:", err)
		}

		err = tmpl.Execute(f, nil)
		if err != nil {
			log.Fatalln("Error while writing to post-checkout hook file:", err)
		}
		f.Chmod(0755)
		fmt.Println("Wrote post-checkout hook.")
	}

	if ic.refs || ic.history {
		syncHistory(repo, ic.history)
	}

	//repo.Free()
}