<commit_before>// Copyright 2017 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
	"encoding/json"
	"errors"
	"fmt"
	"os"
	"strings"
	"time"

	"github.com/maruel/subcommands"
	"go.chromium.org/luci/common/api/gitiles"
	"go.chromium.org/luci/common/auth"
)

func cmdLog(authOpts auth.Options) *subcommands.Command {
	return &subcommands.Command{
		UsageLine: "log <options> repository treeish",
		ShortDesc: "prints commits based on a repo and treeish",
		LongDesc: `Prints commits based on a repo and treeish.

This should be equivalent of a "git log <treeish>" call in that repository.`,
		CommandRun: func() subcommands.CommandRun {
			c := logRun{}
			c.commonFlags.Init(authOpts)
			c.Flags.IntVar(&c.limit, "limit", 0, "Limit the number of log entries returned.")
			c.Flags.StringVar(&c.jsonOutput, "json-output", "", "Path to write operation results to.")
			return &c
		},
	}
}

type logRun struct {
	commonFlags
	limit      int
	jsonOutput string
}

func (c *logRun) Parse(a subcommands.Application, args []string) error {
	if err := c.commonFlags.Parse(); err != nil {
		return err
	}
	if len(args) < 2 {
		return errors.New("position arguments missing")
	} else if len(args) > 2 {
		return errors.New("position arguments not expected")
	}
	return nil
}

func (c *logRun) main(a subcommands.Application, args []string) error {
	authCl, err := c.createAuthClient()
	if err != nil {
		return err
	}
	ctx := c.defaultFlags.MakeLoggingContext(os.Stderr)

	repo := args[0]
	treeish := args[1]
	opts := []gitiles.LogOption{}
	if c.limit != 0 {
		opts = append(opts, gitiles.Limit(c.limit))
	}

	g := &gitiles.Client{Client: authCl}
	commits, err := g.Log(ctx, repo, treeish, opts...)
	if err != nil {
		return err
	}

	if c.jsonOutput == "" {
		for _, c := range commits {<commit_msg>gitiles: Support -with-tree-diff option<commit_after>// Copyright 2017 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
	"encoding/json"
	"errors"
	"fmt"
	"os"
	"strings"
	"time"

	"github.com/maruel/subcommands"
	"go.chromium.org/luci/common/api/gitiles"
	"go.chromium.org/luci/common/auth"
)

func cmdLog(authOpts auth.Options) *subcommands.Command {
	return &subcommands.Command{
		UsageLine: "log <options> repository treeish",
		ShortDesc: "prints commits based on a repo and treeish",
		LongDesc: `Prints commits based on a repo and treeish.

This should be equivalent of a "git log <treeish>" call in that repository.`,
		CommandRun: func() subcommands.CommandRun {
			c := logRun{}
			c.commonFlags.Init(authOpts)
			c.Flags.IntVar(&c.limit, "limit", 0, "Limit the number of log entries returned.")
			c.Flags.BoolVar(&c.withTreeDiff, "with-tree-diff", false, "Return 'TreeDiff' information in the returned commits.")
			c.Flags.StringVar(&c.jsonOutput, "json-output", "", "Path to write operation results to.")
			return &c
		},
	}
}

type logRun struct {
	commonFlags
	limit        int
	withTreeDiff bool
	jsonOutput   string
}

func (c *logRun) Parse(a subcommands.Application, args []string) error {
	if err := c.commonFlags.Parse(); err != nil {
		return err
	}
	if len(args) < 2 {
		return errors.New("position arguments missing")
	} else if len(args) > 2 {
		return errors.New("position arguments not expected")
	}
	return nil
}

func (c *logRun) main(a subcommands.Application, args []string) error {
	authCl, err := c.createAuthClient()
	if err != nil {
		return err
	}
	ctx := c.defaultFlags.MakeLoggingContext(os.Stderr)

	repo := args[0]
	treeish := args[1]
	opts := []gitiles.LogOption{}
	if c.limit != 0 {
		opts = append(opts, gitiles.Limit(c.limit))
	}
	if c.withTreeDiff {
		opts = append(opts, gitiles.WithTreeDiff)
	}

	g := &gitiles.Client{Client: authCl}
	commits, err := g.Log(ctx, repo, treeish, opts...)
	if err != nil {
		return err
	}

	if c.jsonOutput == "" {
		for _, c := range commits {
{'fraction_non_alphanumeric': 0.1277461350691619, 'fraction_numerical': 0.005695687550854353, 'mean_word_length': 3.7142857142857144, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6506808', 'n_tokens_mistral': 1669, 'n_tokens_neox': 1586, 'n_words': 530}