Skip to content

Commit

Permalink
small refactor
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Zamarin <[email protected]>
  • Loading branch information
arthurzam committed Sep 7, 2024
1 parent 86e4771 commit bef23ac
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 26 deletions.
15 changes: 4 additions & 11 deletions pkg/portage/utils/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ func AllFiles() []string {
"ls-tree",
"-r", "master",
"--name-only")

cmd.Dir = config.PortDir()
out, err := cmd.CombinedOutput()
if err != nil {
Expand Down Expand Up @@ -95,24 +94,18 @@ func GetLatestCommit() string {
// GetLatestCommitAndPreceding retrieves the latest
// commit in the database. The hash of the latest commit
// as well as the number of preceding commits is returned
func GetLatestCommitAndPreceding() (string, int) {
latestCommit := EmptyTree()
PrecedingCommitsOffset := 0

func GetLatestCommitAndPreceding() (latestCommit string, precedingCommitsOffset int) {
var commits []*models.Commit
err := database.DBCon.Model(&commits).
Order("preceding_commits DESC").
Limit(1).
Select()
if err == nil && len(commits) == 1 {
latestCommit = commits[0].Id
PrecedingCommitsOffset = commits[0].PrecedingCommits
return commits[0].Id, commits[0].PrecedingCommits
}

return latestCommit, PrecedingCommitsOffset
return EmptyTree, 0
}

// EmptyTree returns the hash of the empty tree
func EmptyTree() string {
return "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
}
const EmptyTree = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
18 changes: 3 additions & 15 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,16 @@
package utils

import (
"sort"
"slices"
"strings"
)

// Deduplicate accepts a slice of strings and returns
// a slice which only contains unique items.
func Deduplicate(items []string) []string {
if len(items) > 1 {
sort.Strings(items)
j := 0
for i := 1; i < len(items); i++ {
if items[j] == items[i] {
continue
}
j++
// preserve the original data
// in[i], in[j] = in[j], in[i]
// only set what is required
items[j] = items[i]
}
result := items[:j+1]
return result
slices.Sort(items)
return slices.Compact(items)
} else {
return items
}
Expand Down

0 comments on commit bef23ac

Please sign in to comment.