Exemplo n.º 1
0
func recordHead(m *ingest.Message, repo *git.Repository) {
	head, err := repo.Head()
	if err != nil {
		log.Fatalf("Could not get repo HEAD")
	}
	oid := head.Target()

	hn, err := os.Hostname()
	if err != nil {
		log.Fatalln("Could not determine hostname for environment")
	}

	// be extra safe about cleaning the path wrt trailing slashes
	p := repo.Path()
	if strings.LastIndex(p, string(os.PathSeparator)) == len(p)-1 {
		p = path.Dir(path.Dir(p))
	} else {
		p = path.Dir(p)
	}

	m.Add(semantic.LogicState{
		Environment: semantic.EnvLink{
			Address: semantic.Address{
				Hostname: hn,
			},
		},
		Path: p,
		ID: semantic.LogicIdentiifer{
			CommitStr: hex.EncodeToString(oid[:]),
		},
	})

	if head.IsBranch() {
		b := head.Branch()
		bn, _ := b.Name()

		m.Add(semantic.CommitMeta{
			Sha1Str:  hex.EncodeToString(oid[:]),
			Tags:     make([]string, 0),
			Branches: []string{bn},
		})
	}
}
Exemplo n.º 2
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 := 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)
}
Exemplo n.º 3
0
func syncHistory(repo *git.Repository, all bool) {
	var err error
	var ident string

	msg := new(ingest.Message)

	if all {
		ident, err = 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)
}