github.com/rcowham/go-libgitfastimport@v0.1.6/cmd.go (about)

     1  // Copyright (C) 2017-2018, 2021  Luke Shumaker <lukeshu@lukeshu.com>
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as published by
     5  // the Free Software Foundation, either version 3 of the License, or
     6  // (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    15  
    16  // Package libfastimport implements reading and writing of git
    17  // fast-import streams.
    18  //
    19  // The documentation here focuses on use of the package itself; it
    20  // generally assumes a working understanding of the format.
    21  // Documentation on the format itself can be found in the
    22  // git-fast-import(1) man-page.
    23  //
    24  // A program can write commands to a backend by wrapping the
    25  // appropriate io.Writer with a Backend object.
    26  //
    27  // A program can read commands from a frontend by wrapping the
    28  // appropriate io.Reader with a Frontend object.
    29  //
    30  // This is up-to-date with full syntax supported by git v2.30.0.
    31  package libfastimport
    32  
    33  type fiReader interface {
    34  	PeekLine() (string, error)
    35  	ReadLine() (string, error)
    36  }
    37  
    38  type fiWriter interface {
    39  	WriteData(string) error
    40  	WriteLine(a ...interface{}) error
    41  }
    42  
    43  type cmdClass int
    44  
    45  const (
    46  	cmdClassCommand   cmdClass = 1 << iota // can be a top-level command
    47  	cmdClassInCommit                       // can be used within in a commit
    48  	cmdClassInCommand                      // can be used in-between lines of another multi-line command
    49  
    50  	cmdClassComment = cmdClassCommand | cmdClassInCommit | cmdClassInCommand // "can be used anywhere in the stream that comments are accepted"
    51  )
    52  
    53  // Cmd is a command that may be found in a fast-import stream.
    54  type Cmd interface {
    55  	fiCmdRead(fiReader) (Cmd, error)
    56  	fiCmdWrite(fiWriter) error
    57  	fiCmdClass() cmdClass
    58  }
    59  
    60  func cmdIs(cmd Cmd, class cmdClass) bool {
    61  	return cmd.fiCmdClass()&class != 0
    62  }