Authentication-Results: mail-b.sr.ht; dkim=pass header.d=protonmail.com header.i=@protonmail.com Received: from mail-40133.protonmail.ch (mail-40133.protonmail.ch [185.70.40.133]) by mail-b.sr.ht (Postfix) with ESMTPS id 8578211EF1E for <~emersion/hut-dev@lists.sr.ht>; Sat, 22 Jan 2022 23:31:14 +0000 (UTC) Date: Sat, 22 Jan 2022 23:31:04 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail2; t=1642894272; bh=EaoLn1ZhVCE+pL5zEMvuqFfB4EDo8ZlvtsIA16ndztc=; h=Date:To:From:Cc:Reply-To:Subject:Message-ID:In-Reply-To: References:From:To:Cc; b=gWIhO+V44VSDYZwrZvcQt0R1tTRFFcB9Y5qh05sM/CDpqcBdTpoBTexAa6yMHTqNj Jeq2N3xOSIBgD17to393oC1vM7NArTw64IDeauheTN7CYHPai9xlu5gqkP7ZC8baOz tYdne8fnqwD2t/Xn7RXIgcyUzmtfh5pgMjJlMaOqFF2zpdtAv3UMlI9xdP4CO+33Vs wN8ApWmE3J/KUVWJUz1JClzItJbUNvfgBLKW0gEex1tpkVIzkNf10ogI7zNIIObxeO 4lUVMTguXNdQ7r+xJ/eWyA0vjfhZm9ljiNxXbIZLG0lQRcl8gBH1c36wKWidI/gZRt GEVfO3Xo30QIA== To: ~emersion/hut-dev@lists.sr.ht From: Renato Torres Cc: Renato Torres Reply-To: Renato Torres Subject: [PATCH v1 1/1] git: show license Message-ID: <20220122233022.1256016-2-renato.torres@protonmail.com> In-Reply-To: <20220122233022.1256016-1-renato.torres@protonmail.com> References: <20220122233022.1256016-1-renato.torres@protonmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-1.2 required=10.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM shortcircuit=no autolearn=disabled version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on mailout.protonmail.ch --- git.go | 121 ++++++++++++++++++++------------ srht/gitsrht/gql.go | 20 ++++-- srht/gitsrht/operations.graphql | 14 ++++ 3 files changed, 105 insertions(+), 50 deletions(-) diff --git a/git.go b/git.go index cc5f18c..379da20 100644 --- a/git.go +++ b/git.go @@ -411,6 +411,7 @@ func newGitACLDeleteCommand() *cobra.Command { } =20 func newGitShowCommand() *cobra.Command { +=09var showLicense bool =09run :=3D func(cmd *cobra.Command, args []string) { =09=09ctx :=3D cmd.Context() =20 @@ -424,53 +425,12 @@ func newGitShowCommand() *cobra.Command { =20 =09=09c :=3D createClientWithInstance("git", cmd, instance) =20 -=09=09repo, err :=3D gitsrht.RepositoryByName(c.Client, ctx, name) -=09=09if err !=3D nil { -=09=09=09log.Fatal(err) -=09=09} - -=09=09// prints basic information -=09=09fmt.Printf("%s (%s)\n", termfmt.Bold.String(repo.Name), repo.Visibil= ity.TermString()) -=09=09if repo.Description !=3D nil && *repo.Description !=3D "" { -=09=09=09fmt.Printf(" %s\n", *repo.Description) -=09=09} -=09=09if repo.UpstreamUrl !=3D nil && *repo.UpstreamUrl !=3D "" { -=09=09=09fmt.Printf(" Upstream URL: %s\n", *repo.UpstreamUrl) -=09=09} - -=09=09// prints latest tag -=09=09tags :=3D repo.References.Tags() -=09=09if len(tags) > 0 { -=09=09=09fmt.Println() -=09=09=09fmt.Printf(" Latest tag: %s\n", tags[0]) -=09=09} - -=09=09// prints branches -=09=09branches :=3D repo.References.Heads() -=09=09if len(branches) > 0 { -=09=09=09fmt.Println() -=09=09=09fmt.Printf(" Branches:\n") -=09=09=09for i :=3D 0; i < len(branches); i++ { -=09=09=09=09fmt.Printf(" %s\n", branches[i]) -=09=09=09} +=09=09if showLicense { +=09=09=09showRepositoryLicense(c, ctx, name) +=09=09=09return =09=09} =20 -=09=09// prints the three most recent commits -=09=09if len(repo.Log.Results) >=3D 3 { -=09=09=09fmt.Println() -=09=09=09fmt.Printf(" Recent log:\n") - -=09=09=09for _, commit :=3D range repo.Log.Results[:3] { -=09=09=09=09fmt.Printf(" %s %s <%s> (%s ago)\n", -=09=09=09=09=09termfmt.Yellow.Sprintf("%s", commit.ShortId), -=09=09=09=09=09commit.Author.Name, -=09=09=09=09=09commit.Author.Email, -=09=09=09=09=09timeDelta(commit.Author.Time)) - -=09=09=09=09commitLines :=3D strings.Split(commit.Message, "\n") -=09=09=09=09fmt.Printf(" %s\n", commitLines[0]) -=09=09=09} -=09=09} +=09=09showRepository(c, ctx, name) =09} =20 =09cmd :=3D &cobra.Command{ @@ -481,9 +441,80 @@ func newGitShowCommand() *cobra.Command { =09=09Run: run, =09} =20 +=09cmd.Flags().BoolVarP(&showLicense, "license", "l", false, "shows only t= he license") + =09return cmd } =20 +func showRepositoryLicense(c *Client, ctx context.Context, name string) { +=09repo, err :=3D gitsrht.LicenseByRepositoryName(c.Client, ctx, name) +=09if err !=3D nil { +=09=09log.Fatal(err) +=09} +=09if repo =3D=3D nil { +=09=09log.Fatalf("repository %s does not exist", name) +=09} + +=09if repo.Path =3D=3D nil { +=09=09log.Fatalf("LICENSE file not found for %s", name) +=09} +=09license :=3D repo.Path.Object["text"] + +=09fmt.Println(license) +} + +func showRepository(c *Client, ctx context.Context, name string) { +=09repo, err :=3D gitsrht.RepositoryByName(c.Client, ctx, name) +=09if err !=3D nil { +=09=09log.Fatal(err) +=09} +=09if repo =3D=3D nil { +=09=09log.Fatalf("repository %s does not exist", name) +=09} + +=09// prints basic information +=09if repo.Description !=3D nil && *repo.Description !=3D "" { +=09=09fmt.Printf(" %s\n", *repo.Description) +=09} +=09if repo.UpstreamUrl !=3D nil && *repo.UpstreamUrl !=3D "" { +=09=09fmt.Printf(" Upstream URL: %s\n", *repo.UpstreamUrl) +=09} + +=09// prints latest tag +=09tags :=3D repo.References.Tags() +=09if len(tags) > 0 { +=09=09fmt.Println() +=09=09fmt.Printf(" Latest tag: %s\n", tags[0]) +=09} + +=09// prints branches +=09branches :=3D repo.References.Heads() +=09if len(branches) > 0 { +=09=09fmt.Println() +=09=09fmt.Printf(" Branches:\n") +=09=09for i :=3D 0; i < len(branches); i++ { +=09=09=09fmt.Printf(" %s\n", branches[i]) +=09=09} +=09} + +=09// prints the three most recent commits +=09if len(repo.Log.Results) >=3D 3 { +=09=09fmt.Println() +=09=09fmt.Printf(" Recent log:\n") + +=09=09for _, commit :=3D range repo.Log.Results[:3] { +=09=09=09fmt.Printf(" %s %s <%s> (%s ago)\n", +=09=09=09=09termfmt.Yellow.Sprintf("%s", commit.ShortId), +=09=09=09=09commit.Author.Name, +=09=09=09=09commit.Author.Email, +=09=09=09=09timeDelta(commit.Author.Time)) + +=09=09=09commitLines :=3D strings.Split(commit.Message, "\n") +=09=09=09fmt.Printf(" %s\n", commitLines[0]) +=09=09} +=09} +} + func getRepoName(ctx context.Context, cmd *cobra.Command) (repoName, insta= nce string) { =09if repoName, err :=3D cmd.Flags().GetString("repo"); err !=3D nil { =09=09log.Fatal(err) diff --git a/srht/gitsrht/gql.go b/srht/gitsrht/gql.go index 55d59ea..ac225a4 100644 --- a/srht/gitsrht/gql.go +++ b/srht/gitsrht/gql.go @@ -205,10 +205,10 @@ type Tree struct { } =20 type TreeEntry struct { -=09Id string `json:"id"` -=09Name string `json:"name"` -=09Object *Object `json:"object"` -=09Mode int32 `json:"mode"` +=09Id string `json:"id"` +=09Name string `json:"name"` +=09Object map[string]interface {} `json:"object"` +=09Mode int32 `json:"mode"` } =20 type TreeEntryCursor struct { @@ -267,7 +267,17 @@ func ListArtifacts(client *gqlclient.Client, ctx conte= xt.Context, name string) ( } =20 func RepositoryByName(client *gqlclient.Client, ctx context.Context, name = string) (repositoryByName *Repository, err error) { -=09op :=3D gqlclient.NewOperation("query repositoryByName ($name: String!)= {\n\trepositoryByName(name: $name) {\n\t\tname\n\t\tdescription\n\t\tvisib= ility\n\t\tupstreamUrl\n\t\treferences {\n\t\t\tresults {\n\t\t\t\tname\n\t= \t\t}\n\t\t}\n\t\tlog {\n\t\t\tresults {\n\t\t\t\tshortId\n\t\t\t\tauthor {= \n\t\t\t\t\tname\n\t\t\t\t\temail\n\t\t\t\t\ttime\n\t\t\t\t}\n\t\t\t\tmessa= ge\n\t\t\t}\n\t\t}\n\t}\n}\n") +=09op :=3D gqlclient.NewOperation("query repositoryByName ($name: String!)= {\n\trepositoryByName(name: $name) {\n\t\tname\n\t\tdescription\n\t\tvisib= ility\n\t\tupstreamUrl\n\t\tpath(path: \"LICENSE\") {\n\t\t\tname\n\t\t\tob= ject {\n\t\t\t\t... on TextBlob {\n\t\t\t\t\ttext\n\t\t\t\t}\n\t\t\t}\n\t\t= }\n\t\treferences {\n\t\t\tresults {\n\t\t\t\tname\n\t\t\t}\n\t\t}\n\t\tlog= {\n\t\t\tresults {\n\t\t\t\tshortId\n\t\t\t\tauthor {\n\t\t\t\t\tname\n\t\= t\t\t\temail\n\t\t\t\t\ttime\n\t\t\t\t}\n\t\t\t\tmessage\n\t\t\t}\n\t\t}\n\= t}\n}\n") +=09op.Var("name", name) +=09var respData struct { +=09=09RepositoryByName *Repository +=09} +=09err =3D client.Execute(ctx, op, &respData) +=09return respData.RepositoryByName, err +} + +func LicenseByRepositoryName(client *gqlclient.Client, ctx context.Context= , name string) (repositoryByName *Repository, err error) { +=09op :=3D gqlclient.NewOperation("query licenseByRepositoryByName ($name:= String!) {\n\trepositoryByName(name: $name) {\n\t\tpath(path: \"LICENSE\")= {\n\t\t\tname\n\t\t\tobject {\n\t\t\t\t... on TextBlob {\n\t\t\t\t\ttext\n= \t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n") =09op.Var("name", name) =09var respData struct { =09=09RepositoryByName *Repository diff --git a/srht/gitsrht/operations.graphql b/srht/gitsrht/operations.grap= hql index 57d2cfd..3ec5ad1 100644 --- a/srht/gitsrht/operations.graphql +++ b/srht/gitsrht/operations.graphql @@ -45,6 +45,20 @@ query repositoryByName($name: String!) { } } =20 + +query licenseByRepositoryName($name: String!) { + repositoryByName(name: $name) { + path(path: "LICENSE") { + name + object { + ... on TextBlob { + text + } + } + } + } +} + query repositories { repositories { ...repos --=20 2.34.1