fork of go-git with some jj specific features
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v4.9.0 484 lines 15 kB view raw
1package git 2 3import ( 4 "errors" 5 "regexp" 6 "strings" 7 8 "golang.org/x/crypto/openpgp" 9 "gopkg.in/src-d/go-git.v4/config" 10 "gopkg.in/src-d/go-git.v4/plumbing" 11 "gopkg.in/src-d/go-git.v4/plumbing/object" 12 "gopkg.in/src-d/go-git.v4/plumbing/protocol/packp/sideband" 13 "gopkg.in/src-d/go-git.v4/plumbing/transport" 14) 15 16// SubmoduleRescursivity defines how depth will affect any submodule recursive 17// operation. 18type SubmoduleRescursivity uint 19 20const ( 21 // DefaultRemoteName name of the default Remote, just like git command. 22 DefaultRemoteName = "origin" 23 24 // NoRecurseSubmodules disables the recursion for a submodule operation. 25 NoRecurseSubmodules SubmoduleRescursivity = 0 26 // DefaultSubmoduleRecursionDepth allow recursion in a submodule operation. 27 DefaultSubmoduleRecursionDepth SubmoduleRescursivity = 10 28) 29 30var ( 31 ErrMissingURL = errors.New("URL field is required") 32) 33 34// CloneOptions describes how a clone should be performed. 35type CloneOptions struct { 36 // The (possibly remote) repository URL to clone from. 37 URL string 38 // Auth credentials, if required, to use with the remote repository. 39 Auth transport.AuthMethod 40 // Name of the remote to be added, by default `origin`. 41 RemoteName string 42 // Remote branch to clone. 43 ReferenceName plumbing.ReferenceName 44 // Fetch only ReferenceName if true. 45 SingleBranch bool 46 // No checkout of HEAD after clone if true. 47 NoCheckout bool 48 // Limit fetching to the specified number of commits. 49 Depth int 50 // RecurseSubmodules after the clone is created, initialize all submodules 51 // within, using their default settings. This option is ignored if the 52 // cloned repository does not have a worktree. 53 RecurseSubmodules SubmoduleRescursivity 54 // Progress is where the human readable information sent by the server is 55 // stored, if nil nothing is stored and the capability (if supported) 56 // no-progress, is sent to the server to avoid send this information. 57 Progress sideband.Progress 58 // Tags describe how the tags will be fetched from the remote repository, 59 // by default is AllTags. 60 Tags TagMode 61} 62 63// Validate validates the fields and sets the default values. 64func (o *CloneOptions) Validate() error { 65 if o.URL == "" { 66 return ErrMissingURL 67 } 68 69 if o.RemoteName == "" { 70 o.RemoteName = DefaultRemoteName 71 } 72 73 if o.ReferenceName == "" { 74 o.ReferenceName = plumbing.HEAD 75 } 76 77 if o.Tags == InvalidTagMode { 78 o.Tags = AllTags 79 } 80 81 return nil 82} 83 84// PullOptions describes how a pull should be performed. 85type PullOptions struct { 86 // Name of the remote to be pulled. If empty, uses the default. 87 RemoteName string 88 // Remote branch to clone. If empty, uses HEAD. 89 ReferenceName plumbing.ReferenceName 90 // Fetch only ReferenceName if true. 91 SingleBranch bool 92 // Limit fetching to the specified number of commits. 93 Depth int 94 // Auth credentials, if required, to use with the remote repository. 95 Auth transport.AuthMethod 96 // RecurseSubmodules controls if new commits of all populated submodules 97 // should be fetched too. 98 RecurseSubmodules SubmoduleRescursivity 99 // Progress is where the human readable information sent by the server is 100 // stored, if nil nothing is stored and the capability (if supported) 101 // no-progress, is sent to the server to avoid send this information. 102 Progress sideband.Progress 103 // Force allows the pull to update a local branch even when the remote 104 // branch does not descend from it. 105 Force bool 106} 107 108// Validate validates the fields and sets the default values. 109func (o *PullOptions) Validate() error { 110 if o.RemoteName == "" { 111 o.RemoteName = DefaultRemoteName 112 } 113 114 if o.ReferenceName == "" { 115 o.ReferenceName = plumbing.HEAD 116 } 117 118 return nil 119} 120 121type TagMode int 122 123const ( 124 InvalidTagMode TagMode = iota 125 // TagFollowing any tag that points into the histories being fetched is also 126 // fetched. TagFollowing requires a server with `include-tag` capability 127 // in order to fetch the annotated tags objects. 128 TagFollowing 129 // AllTags fetch all tags from the remote (i.e., fetch remote tags 130 // refs/tags/* into local tags with the same name) 131 AllTags 132 //NoTags fetch no tags from the remote at all 133 NoTags 134) 135 136// FetchOptions describes how a fetch should be performed 137type FetchOptions struct { 138 // Name of the remote to fetch from. Defaults to origin. 139 RemoteName string 140 RefSpecs []config.RefSpec 141 // Depth limit fetching to the specified number of commits from the tip of 142 // each remote branch history. 143 Depth int 144 // Auth credentials, if required, to use with the remote repository. 145 Auth transport.AuthMethod 146 // Progress is where the human readable information sent by the server is 147 // stored, if nil nothing is stored and the capability (if supported) 148 // no-progress, is sent to the server to avoid send this information. 149 Progress sideband.Progress 150 // Tags describe how the tags will be fetched from the remote repository, 151 // by default is TagFollowing. 152 Tags TagMode 153 // Force allows the fetch to update a local branch even when the remote 154 // branch does not descend from it. 155 Force bool 156} 157 158// Validate validates the fields and sets the default values. 159func (o *FetchOptions) Validate() error { 160 if o.RemoteName == "" { 161 o.RemoteName = DefaultRemoteName 162 } 163 164 if o.Tags == InvalidTagMode { 165 o.Tags = TagFollowing 166 } 167 168 for _, r := range o.RefSpecs { 169 if err := r.Validate(); err != nil { 170 return err 171 } 172 } 173 174 return nil 175} 176 177// PushOptions describes how a push should be performed. 178type PushOptions struct { 179 // RemoteName is the name of the remote to be pushed to. 180 RemoteName string 181 // RefSpecs specify what destination ref to update with what source 182 // object. A refspec with empty src can be used to delete a reference. 183 RefSpecs []config.RefSpec 184 // Auth credentials, if required, to use with the remote repository. 185 Auth transport.AuthMethod 186 // Progress is where the human readable information sent by the server is 187 // stored, if nil nothing is stored. 188 Progress sideband.Progress 189} 190 191// Validate validates the fields and sets the default values. 192func (o *PushOptions) Validate() error { 193 if o.RemoteName == "" { 194 o.RemoteName = DefaultRemoteName 195 } 196 197 if len(o.RefSpecs) == 0 { 198 o.RefSpecs = []config.RefSpec{ 199 config.RefSpec(config.DefaultPushRefSpec), 200 } 201 } 202 203 for _, r := range o.RefSpecs { 204 if err := r.Validate(); err != nil { 205 return err 206 } 207 } 208 209 return nil 210} 211 212// SubmoduleUpdateOptions describes how a submodule update should be performed. 213type SubmoduleUpdateOptions struct { 214 // Init, if true initializes the submodules recorded in the index. 215 Init bool 216 // NoFetch tell to the update command to not fetch new objects from the 217 // remote site. 218 NoFetch bool 219 // RecurseSubmodules the update is performed not only in the submodules of 220 // the current repository but also in any nested submodules inside those 221 // submodules (and so on). Until the SubmoduleRescursivity is reached. 222 RecurseSubmodules SubmoduleRescursivity 223 // Auth credentials, if required, to use with the remote repository. 224 Auth transport.AuthMethod 225} 226 227var ( 228 ErrBranchHashExclusive = errors.New("Branch and Hash are mutually exclusive") 229 ErrCreateRequiresBranch = errors.New("Branch is mandatory when Create is used") 230) 231 232// CheckoutOptions describes how a checkout 31operation should be performed. 233type CheckoutOptions struct { 234 // Hash is the hash of the commit to be checked out. If used, HEAD will be 235 // in detached mode. If Create is not used, Branch and Hash are mutually 236 // exclusive. 237 Hash plumbing.Hash 238 // Branch to be checked out, if Branch and Hash are empty is set to `master`. 239 Branch plumbing.ReferenceName 240 // Create a new branch named Branch and start it at Hash. 241 Create bool 242 // Force, if true when switching branches, proceed even if the index or the 243 // working tree differs from HEAD. This is used to throw away local changes 244 Force bool 245} 246 247// Validate validates the fields and sets the default values. 248func (o *CheckoutOptions) Validate() error { 249 if !o.Create && !o.Hash.IsZero() && o.Branch != "" { 250 return ErrBranchHashExclusive 251 } 252 253 if o.Create && o.Branch == "" { 254 return ErrCreateRequiresBranch 255 } 256 257 if o.Branch == "" { 258 o.Branch = plumbing.Master 259 } 260 261 return nil 262} 263 264// ResetMode defines the mode of a reset operation. 265type ResetMode int8 266 267const ( 268 // MixedReset resets the index but not the working tree (i.e., the changed 269 // files are preserved but not marked for commit) and reports what has not 270 // been updated. This is the default action. 271 MixedReset ResetMode = iota 272 // HardReset resets the index and working tree. Any changes to tracked files 273 // in the working tree are discarded. 274 HardReset 275 // MergeReset resets the index and updates the files in the working tree 276 // that are different between Commit and HEAD, but keeps those which are 277 // different between the index and working tree (i.e. which have changes 278 // which have not been added). 279 // 280 // If a file that is different between Commit and the index has unstaged 281 // changes, reset is aborted. 282 MergeReset 283 // SoftReset does not touch the index file or the working tree at all (but 284 // resets the head to <commit>, just like all modes do). This leaves all 285 // your changed files "Changes to be committed", as git status would put it. 286 SoftReset 287) 288 289// ResetOptions describes how a reset operation should be performed. 290type ResetOptions struct { 291 // Commit, if commit is pressent set the current branch head (HEAD) to it. 292 Commit plumbing.Hash 293 // Mode, form resets the current branch head to Commit and possibly updates 294 // the index (resetting it to the tree of Commit) and the working tree 295 // depending on Mode. If empty MixedReset is used. 296 Mode ResetMode 297} 298 299// Validate validates the fields and sets the default values. 300func (o *ResetOptions) Validate(r *Repository) error { 301 if o.Commit == plumbing.ZeroHash { 302 ref, err := r.Head() 303 if err != nil { 304 return err 305 } 306 307 o.Commit = ref.Hash() 308 } 309 310 return nil 311} 312 313type LogOrder int8 314 315const ( 316 LogOrderDefault LogOrder = iota 317 LogOrderDFS 318 LogOrderDFSPost 319 LogOrderBSF 320 LogOrderCommitterTime 321) 322 323// LogOptions describes how a log action should be performed. 324type LogOptions struct { 325 // When the From option is set the log will only contain commits 326 // reachable from it. If this option is not set, HEAD will be used as 327 // the default From. 328 From plumbing.Hash 329 330 // The default traversal algorithm is Depth-first search 331 // set Order=LogOrderCommitterTime for ordering by committer time (more compatible with `git log`) 332 // set Order=LogOrderBSF for Breadth-first search 333 Order LogOrder 334 335 // Show only those commits in which the specified file was inserted/updated. 336 // It is equivalent to running `git log -- <file-name>`. 337 FileName *string 338 339 // Pretend as if all the refs in refs/, along with HEAD, are listed on the command line as <commit>. 340 // It is equivalent to running `git log --all`. 341 // If set on true, the From option will be ignored. 342 All bool 343} 344 345var ( 346 ErrMissingAuthor = errors.New("author field is required") 347) 348 349// CommitOptions describes how a commit operation should be performed. 350type CommitOptions struct { 351 // All automatically stage files that have been modified and deleted, but 352 // new files you have not told Git about are not affected. 353 All bool 354 // Author is the author's signature of the commit. 355 Author *object.Signature 356 // Committer is the committer's signature of the commit. If Committer is 357 // nil the Author signature is used. 358 Committer *object.Signature 359 // Parents are the parents commits for the new commit, by default when 360 // len(Parents) is zero, the hash of HEAD reference is used. 361 Parents []plumbing.Hash 362 // SignKey denotes a key to sign the commit with. A nil value here means the 363 // commit will not be signed. The private key must be present and already 364 // decrypted. 365 SignKey *openpgp.Entity 366} 367 368// Validate validates the fields and sets the default values. 369func (o *CommitOptions) Validate(r *Repository) error { 370 if o.Author == nil { 371 return ErrMissingAuthor 372 } 373 374 if o.Committer == nil { 375 o.Committer = o.Author 376 } 377 378 if len(o.Parents) == 0 { 379 head, err := r.Head() 380 if err != nil && err != plumbing.ErrReferenceNotFound { 381 return err 382 } 383 384 if head != nil { 385 o.Parents = []plumbing.Hash{head.Hash()} 386 } 387 } 388 389 return nil 390} 391 392var ( 393 ErrMissingName = errors.New("name field is required") 394 ErrMissingTagger = errors.New("tagger field is required") 395 ErrMissingMessage = errors.New("message field is required") 396) 397 398// CreateTagOptions describes how a tag object should be created. 399type CreateTagOptions struct { 400 // Tagger defines the signature of the tag creator. 401 Tagger *object.Signature 402 // Message defines the annotation of the tag. It is canonicalized during 403 // validation into the format expected by git - no leading whitespace and 404 // ending in a newline. 405 Message string 406 // SignKey denotes a key to sign the tag with. A nil value here means the tag 407 // will not be signed. The private key must be present and already decrypted. 408 SignKey *openpgp.Entity 409} 410 411// Validate validates the fields and sets the default values. 412func (o *CreateTagOptions) Validate(r *Repository, hash plumbing.Hash) error { 413 if o.Tagger == nil { 414 return ErrMissingTagger 415 } 416 417 if o.Message == "" { 418 return ErrMissingMessage 419 } 420 421 // Canonicalize the message into the expected message format. 422 o.Message = strings.TrimSpace(o.Message) + "\n" 423 424 return nil 425} 426 427// ListOptions describes how a remote list should be performed. 428type ListOptions struct { 429 // Auth credentials, if required, to use with the remote repository. 430 Auth transport.AuthMethod 431} 432 433// CleanOptions describes how a clean should be performed. 434type CleanOptions struct { 435 Dir bool 436} 437 438// GrepOptions describes how a grep should be performed. 439type GrepOptions struct { 440 // Patterns are compiled Regexp objects to be matched. 441 Patterns []*regexp.Regexp 442 // InvertMatch selects non-matching lines. 443 InvertMatch bool 444 // CommitHash is the hash of the commit from which worktree should be derived. 445 CommitHash plumbing.Hash 446 // ReferenceName is the branch or tag name from which worktree should be derived. 447 ReferenceName plumbing.ReferenceName 448 // PathSpecs are compiled Regexp objects of pathspec to use in the matching. 449 PathSpecs []*regexp.Regexp 450} 451 452var ( 453 ErrHashOrReference = errors.New("ambiguous options, only one of CommitHash or ReferenceName can be passed") 454) 455 456// Validate validates the fields and sets the default values. 457func (o *GrepOptions) Validate(w *Worktree) error { 458 if !o.CommitHash.IsZero() && o.ReferenceName != "" { 459 return ErrHashOrReference 460 } 461 462 // If none of CommitHash and ReferenceName are provided, set commit hash of 463 // the repository's head. 464 if o.CommitHash.IsZero() && o.ReferenceName == "" { 465 ref, err := w.r.Head() 466 if err != nil { 467 return err 468 } 469 o.CommitHash = ref.Hash() 470 } 471 472 return nil 473} 474 475// PlainOpenOptions describes how opening a plain repository should be 476// performed. 477type PlainOpenOptions struct { 478 // DetectDotGit defines whether parent directories should be 479 // walked until a .git directory or file is found. 480 DetectDotGit bool 481} 482 483// Validate validates the fields and sets the default values. 484func (o *PlainOpenOptions) Validate() error { return nil }