sigs.k8s.io/release-sdk@v0.11.1-0.20240417074027-8061fb5e4952/git/describe.go (about)

     1  /*
     2  Copyright 2020 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package git
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  
    23  	"sigs.k8s.io/release-utils/command"
    24  )
    25  
    26  // DescribeOptions is the type for the argument passed to repo.Describe
    27  type DescribeOptions struct {
    28  	revision string
    29  	abbrev   int16
    30  	always   bool
    31  	dirty    bool
    32  	tags     bool
    33  }
    34  
    35  // NewDescribeOptions creates new repository describe options
    36  func NewDescribeOptions() *DescribeOptions {
    37  	return &DescribeOptions{
    38  		revision: "",
    39  		abbrev:   -1,
    40  		always:   false,
    41  		dirty:    false,
    42  		tags:     false,
    43  	}
    44  }
    45  
    46  // WithRevision sets the revision in the DescribeOptions
    47  func (d *DescribeOptions) WithRevision(rev string) *DescribeOptions {
    48  	d.revision = rev
    49  	return d
    50  }
    51  
    52  // WithAbbrev sets the --abbrev=<parameter> in the DescribeOptions
    53  func (d *DescribeOptions) WithAbbrev(abbrev uint8) *DescribeOptions {
    54  	d.abbrev = int16(abbrev)
    55  	return d
    56  }
    57  
    58  // WithAlways sets always to true in the DescribeOptions
    59  func (d *DescribeOptions) WithAlways() *DescribeOptions {
    60  	d.always = true
    61  	return d
    62  }
    63  
    64  // WithDirty sets dirty to true in the DescribeOptions
    65  func (d *DescribeOptions) WithDirty() *DescribeOptions {
    66  	d.dirty = true
    67  	return d
    68  }
    69  
    70  // WithTags sets tags to true in the DescribeOptions
    71  func (d *DescribeOptions) WithTags() *DescribeOptions {
    72  	d.tags = true
    73  	return d
    74  }
    75  
    76  // toArgs converts DescribeOptions to string arguments
    77  func (d *DescribeOptions) toArgs() (args []string) {
    78  	if d.tags {
    79  		args = append(args, "--tags")
    80  	}
    81  	if d.dirty {
    82  		args = append(args, "--dirty")
    83  	}
    84  	if d.always {
    85  		args = append(args, "--always")
    86  	}
    87  	if d.abbrev >= 0 {
    88  		args = append(args, fmt.Sprintf("--abbrev=%d", d.abbrev))
    89  	}
    90  	if d.revision != "" {
    91  		args = append(args, d.revision)
    92  	}
    93  	return args
    94  }
    95  
    96  // Describe runs `git describe` with the provided arguments
    97  func (r *Repo) Describe(opts *DescribeOptions) (string, error) {
    98  	if opts == nil {
    99  		return "", errors.New("provided describe options are nil")
   100  	}
   101  	output, err := command.NewWithWorkDir(
   102  		r.Dir(), gitExecutable, append([]string{"describe"}, opts.toArgs()...)...,
   103  	).RunSilentSuccessOutput()
   104  	if err != nil {
   105  		return "", err
   106  	}
   107  	return output.OutputTrimNL(), nil
   108  }