Skip to content

Commit

Permalink
Adds Git information to report, i.e. URL of remote, current commit ha…
Browse files Browse the repository at this point in the history
…sh & branch and whether folder currently has changes relative to HEAD
  • Loading branch information
bvobart committed May 28, 2021
1 parent 0f08641 commit addf81b
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 7 deletions.
14 changes: 14 additions & 0 deletions api/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
type Project struct {
// The project's assumed root directory, absolute path.
Dir string
// Information about the project's Git repository.
Git GitInfo
// mllint's configuration for this project
Config config.Config
// Type of mllint configuration
Expand All @@ -23,6 +25,18 @@ type Project struct {
PythonFiles utils.Filenames
}

// GitInfo describes some info about the Git repository that a project is in.
type GitInfo struct {
// the URL of the Git remote, e.g. `[email protected]:bvobart/mllint.git`
RemoteURL string
// the hash of the current commit.
Commit string
// the name of the current branch.
Branch string
// whether the repository is currently in a dirty state (i.e. files added / removed / changed)
Dirty bool
}

// ProjectReport is what you end up with after mllint finishes analysing a project.
type ProjectReport struct {
Project
Expand Down
3 changes: 3 additions & 0 deletions commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/bvobart/mllint/linters"
"github.com/bvobart/mllint/setools/cqlinters"
"github.com/bvobart/mllint/setools/depmanagers"
"github.com/bvobart/mllint/setools/git"
"github.com/bvobart/mllint/utils"
"github.com/bvobart/mllint/utils/markdown"
)
Expand Down Expand Up @@ -46,10 +47,12 @@ type runCommand struct {
}

// Runs pre-analysis checks:
// - Retrieve some info about project's Git state
// - Detect dependency managers used in the project
// - Detect code quality linters used in the project
// - Detect the Python files in the project repository.
func (rc *runCommand) runPreAnalysisChecks() error {
rc.ProjectR.Git = git.MakeGitInfo(rc.ProjectR.Dir)
rc.ProjectR.DepManagers = depmanagers.Detect(rc.ProjectR.Project)
rc.ProjectR.CQLinters = cqlinters.Detect(rc.ProjectR.Project)

Expand Down
33 changes: 33 additions & 0 deletions setools/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strconv"
"strings"

"github.com/bvobart/mllint/api"
"github.com/bvobart/mllint/utils"
"github.com/bvobart/mllint/utils/exec"
)
Expand All @@ -16,6 +17,38 @@ func Detect(dir string) bool {
return err == nil
}

func MakeGitInfo(dir string) api.GitInfo {
if !Detect(dir) {
return api.GitInfo{}
}

remote, _ := GetRemoteURL(dir)
commit, _ := GetCurrentCommit(dir)
branch, _ := GetCurrentBranch(dir)
dirty := IsDirty(dir)
return api.GitInfo{RemoteURL: remote, Commit: commit, Branch: branch, Dirty: dirty}
}

func GetRemoteURL(dir string) (string, error) {
output, err := exec.CommandOutput(dir, "git", "remote", "get-url", "origin")
return strings.TrimSpace(string(output)), err
}

func GetCurrentCommit(dir string) (string, error) {
output, err := exec.CommandOutput(dir, "git", "rev-parse", "HEAD")
return strings.TrimSpace(string(output)), err
}

func GetCurrentBranch(dir string) (string, error) {
output, err := exec.CommandOutput(dir, "git", "branch", "--show-current")
return strings.TrimSpace(string(output)), err
}

func IsDirty(dir string) bool {
_, err := exec.CommandOutput(dir, "git", "diff", "--no-ext-diff", "--quiet")
return err != nil
}

// IsTracking checks whether the Git repository in the given folder is tracking the files specified
// by the given pattern. This can be a literal folder or file name, but can also be a pattern
// containing wildcards, e.g. 'foo.*'
Expand Down
25 changes: 18 additions & 7 deletions utils/markdown/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,30 @@ func FromProject(project api.ProjectReport) string {
return output.String()
}

func writeProjectHeader(output *strings.Builder, project api.ProjectReport) {
isDefault := "No"
if cmp.Equal(project.Config, *config.Default()) {
isDefault = "Yes"
func humanizeBool(b bool) string {
if b {
return "Yes"
}
return "No"
}

func writeProjectHeader(output *strings.Builder, project api.ProjectReport) {
output.WriteString("# ML Project Report\n")
output.WriteString("Project | Details\n")
output.WriteString("**Project** | **Details**\n")
output.WriteString("--------|--------\n")
output.WriteString(fmt.Sprintf("Date | %s \n", time.Now().Format(time.RFC1123Z)))
output.WriteString("Path | `" + project.Dir + "`\n")
output.WriteString("Config | `" + project.ConfigType.String() + "`\n")
output.WriteString("Default | " + isDefault + "\n")
output.WriteString(fmt.Sprintf("Date | %s \n", time.Now().Format(time.RFC1123Z)))
configIsDefault := cmp.Equal(project.Config, *config.Default())
output.WriteString("Default | " + humanizeBool(configIsDefault) + "\n")

if project.Git.RemoteURL != "" {
output.WriteString("Git: Remote URL | `" + project.Git.RemoteURL + "`\n")
output.WriteString("Git: Commit | `" + project.Git.Commit + "`\n")
output.WriteString("Git: Branch | `" + project.Git.Branch + "`\n")
output.WriteString("Git: Dirty Workspace? | " + humanizeBool(project.Git.Dirty) + "\n")
}

output.WriteString(fmt.Sprintf("Number of Python files | %d\n", len(project.PythonFiles)))
output.WriteString(fmt.Sprintf("Lines of Python code | %d\n", project.PythonFiles.CountLoC()))
output.WriteString("\n---\n\n")
Expand Down

0 comments on commit addf81b

Please sign in to comment.