| 1 |
package git |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"context" |
| 6 |
"crypto" |
| 7 |
"encoding/hex" |
| 8 |
"errors" |
| 9 |
"fmt" |
| 10 |
"io" |
| 11 |
"os" |
| 12 |
"path" |
| 13 |
"path/filepath" |
| 14 |
"strings" |
| 15 |
"time" |
| 16 |
|
| 17 |
"dario.cat/mergo" |
| 18 |
"github.com/ProtonMail/go-crypto/openpgp" |
| 19 |
"github.com/go-git/go-billy/v5" |
| 20 |
"github.com/go-git/go-billy/v5/osfs" |
| 21 |
"github.com/go-git/go-billy/v5/util" |
| 22 |
|
| 23 |
"github.com/go-git/go-git/v5/config" |
| 24 |
"github.com/go-git/go-git/v5/internal/path_util" |
| 25 |
"github.com/go-git/go-git/v5/internal/revision" |
| 26 |
"github.com/go-git/go-git/v5/internal/url" |
| 27 |
"github.com/go-git/go-git/v5/plumbing" |
| 28 |
"github.com/go-git/go-git/v5/plumbing/cache" |
| 29 |
formatcfg "github.com/go-git/go-git/v5/plumbing/format/config" |
| 30 |
"github.com/go-git/go-git/v5/plumbing/format/packfile" |
| 31 |
"github.com/go-git/go-git/v5/plumbing/hash" |
| 32 |
"github.com/go-git/go-git/v5/plumbing/object" |
| 33 |
"github.com/go-git/go-git/v5/plumbing/storer" |
| 34 |
"github.com/go-git/go-git/v5/storage" |
| 35 |
"github.com/go-git/go-git/v5/storage/filesystem" |
| 36 |
"github.com/go-git/go-git/v5/storage/filesystem/dotgit" |
| 37 |
"github.com/go-git/go-git/v5/utils/ioutil" |
| 38 |
) |
| 39 |
|
| 40 |
// GitDirName this is a special folder where all the git stuff is. |
| 41 |
const GitDirName = ".git" |
| 42 |
|
| 43 |
var ( |
| 44 |
// ErrBranchExists an error stating the specified branch already exists |
| 45 |
ErrBranchExists = errors.New("branch already exists") |
| 46 |
// ErrBranchNotFound an error stating the specified branch does not exist |
| 47 |
ErrBranchNotFound = errors.New("branch not found") |
| 48 |
// ErrTagExists an error stating the specified tag already exists |
| 49 |
ErrTagExists = errors.New("tag already exists") |
| 50 |
// ErrTagNotFound an error stating the specified tag does not exist |
| 51 |
ErrTagNotFound = errors.New("tag not found") |
| 52 |
// ErrFetching is returned when the packfile could not be downloaded |
| 53 |
ErrFetching = errors.New("unable to fetch packfile") |
| 54 |
|
| 55 |
ErrInvalidReference = errors.New("invalid reference, should be a tag or a branch") |
| 56 |
ErrRepositoryNotExists = errors.New("repository does not exist") |
| 57 |
ErrRepositoryIncomplete = errors.New("repository's commondir path does not exist") |
| 58 |
ErrRepositoryAlreadyExists = errors.New("repository already exists") |
| 59 |
ErrRemoteNotFound = errors.New("remote not found") |
| 60 |
ErrRemoteExists = errors.New("remote already exists") |
| 61 |
ErrAnonymousRemoteName = errors.New("anonymous remote name must be 'anonymous'") |
| 62 |
ErrWorktreeNotProvided = errors.New("worktree should be provided") |
| 63 |
ErrIsBareRepository = errors.New("worktree not available in a bare repository") |
| 64 |
ErrUnableToResolveCommit = errors.New("unable to resolve commit") |
| 65 |
ErrPackedObjectsNotSupported = errors.New("packed objects not supported") |
| 66 |
ErrSHA256NotSupported = errors.New("go-git was not compiled with SHA256 support") |
| 67 |
ErrAlternatePathNotSupported = errors.New("alternate path must use the file scheme") |
| 68 |
ErrUnsupportedMergeStrategy = errors.New("unsupported merge strategy") |
| 69 |
ErrFastForwardMergeNotPossible = errors.New("not possible to fast-forward merge changes") |
| 70 |
) |
| 71 |
|
| 72 |
// Repository represents a git repository |
| 73 |
type Repository struct { |
| 74 |
Storer storage.Storer |
| 75 |
|
| 76 |
r map[string]*Remote |
| 77 |
wt billy.Filesystem |
| 78 |
} |
| 79 |
|
| 80 |
type InitOptions struct { |
| 81 |
// The default branch (e.g. "refs/heads/master") |
| 82 |
DefaultBranch plumbing.ReferenceName |
| 83 |
} |
| 84 |
|
| 85 |
// Init creates an empty git repository, based on the given Storer and worktree. |
| 86 |
// The worktree Filesystem is optional, if nil a bare repository is created. If |
| 87 |
// the given storer is not empty ErrRepositoryAlreadyExists is returned |
| 88 |
func Init(s storage.Storer, worktree billy.Filesystem) (*Repository, error) { |
| 89 |
options := InitOptions{ |
| 90 |
DefaultBranch: plumbing.Master, |
| 91 |
} |
| 92 |
return InitWithOptions(s, worktree, options) |
| 93 |
} |
| 94 |
|
| 95 |
func InitWithOptions(s storage.Storer, worktree billy.Filesystem, options InitOptions) (*Repository, error) { |
| 96 |
if err := initStorer(s); err != nil { |
| 97 |
return nil, err |
| 98 |
} |
| 99 |
|
| 100 |
if options.DefaultBranch == "" { |
| 101 |
options.DefaultBranch = plumbing.Master |
| 102 |
} |
| 103 |
|
| 104 |
if err := options.DefaultBranch.Validate(); err != nil { |
| 105 |
return nil, err |
| 106 |
} |
| 107 |
|
| 108 |
r := newRepository(s, worktree) |
| 109 |
_, err := r.Reference(plumbing.HEAD, false) |
| 110 |
switch err { |
| 111 |
case plumbing.ErrReferenceNotFound: |
| 112 |
case nil: |
| 113 |
return nil, ErrRepositoryAlreadyExists |
| 114 |
default: |
| 115 |
return nil, err |
| 116 |
} |
| 117 |
|
| 118 |
h := plumbing.NewSymbolicReference(plumbing.HEAD, options.DefaultBranch) |
| 119 |
if err := s.SetReference(h); err != nil { |
| 120 |
return nil, err |
| 121 |
} |
| 122 |
|
| 123 |
if worktree == nil { |
| 124 |
_ = r.setIsBare(true) |
| 125 |
return r, nil |
| 126 |
} |
| 127 |
|
| 128 |
return r, setWorktreeAndStoragePaths(r, worktree) |
| 129 |
} |
| 130 |
|
| 131 |
func initStorer(s storer.Storer) error { |
| 132 |
i, ok := s.(storer.Initializer) |
| 133 |
if !ok { |
| 134 |
return nil |
| 135 |
} |
| 136 |
|
| 137 |
return i.Init() |
| 138 |
} |
| 139 |
|
| 140 |
func setWorktreeAndStoragePaths(r *Repository, worktree billy.Filesystem) error { |
| 141 |
type fsBased interface { |
| 142 |
Filesystem() billy.Filesystem |
| 143 |
} |
| 144 |
|
| 145 |
// .git file is only created if the storage is file based and the file |
| 146 |
// system is osfs.OS |
| 147 |
fs, isFSBased := r.Storer.(fsBased) |
| 148 |
if !isFSBased { |
| 149 |
return nil |
| 150 |
} |
| 151 |
|
| 152 |
if err := createDotGitFile(worktree, fs.Filesystem()); err != nil { |
| 153 |
return err |
| 154 |
} |
| 155 |
|
| 156 |
return setConfigWorktree(r, worktree, fs.Filesystem()) |
| 157 |
} |
| 158 |
|
| 159 |
func createDotGitFile(worktree, storage billy.Filesystem) error { |
| 160 |
path, err := filepath.Rel(worktree.Root(), storage.Root()) |
| 161 |
if err != nil { |
| 162 |
path = storage.Root() |
| 163 |
} |
| 164 |
|
| 165 |
if path == GitDirName { |
| 166 |
// not needed, since the folder is the default place |
| 167 |
return nil |
| 168 |
} |
| 169 |
|
| 170 |
f, err := worktree.Create(GitDirName) |
| 171 |
if err != nil { |
| 172 |
return err |
| 173 |
} |
| 174 |
|
| 175 |
defer f.Close() |
| 176 |
_, err = fmt.Fprintf(f, "gitdir: %s\n", path) |
| 177 |
return err |
| 178 |
} |
| 179 |
|
| 180 |
func setConfigWorktree(r *Repository, worktree, storage billy.Filesystem) error { |
| 181 |
path, err := filepath.Rel(storage.Root(), worktree.Root()) |
| 182 |
if err != nil { |
| 183 |
path = worktree.Root() |
| 184 |
} |
| 185 |
|
| 186 |
if path == ".." { |
| 187 |
// not needed, since the folder is the default place |
| 188 |
return nil |
| 189 |
} |
| 190 |
|
| 191 |
cfg, err := r.Config() |
| 192 |
if err != nil { |
| 193 |
return err |
| 194 |
} |
| 195 |
|
| 196 |
cfg.Core.Worktree = path |
| 197 |
return r.Storer.SetConfig(cfg) |
| 198 |
} |
| 199 |
|
| 200 |
// Open opens a git repository using the given Storer and worktree filesystem, |
| 201 |
// if the given storer is complete empty ErrRepositoryNotExists is returned. |
| 202 |
// The worktree can be nil when the repository being opened is bare, if the |
| 203 |
// repository is a normal one (not bare) and worktree is nil the err |
| 204 |
// ErrWorktreeNotProvided is returned |
| 205 |
func Open(s storage.Storer, worktree billy.Filesystem) (*Repository, error) { |
| 206 |
_, err := s.Reference(plumbing.HEAD) |
| 207 |
if err == plumbing.ErrReferenceNotFound { |
| 208 |
return nil, ErrRepositoryNotExists |
| 209 |
} |
| 210 |
|
| 211 |
cfg, err := s.Config() |
| 212 |
if err != nil { |
| 213 |
return nil, err |
| 214 |
} |
| 215 |
|
| 216 |
err = verifyExtensions(s, cfg) |
| 217 |
if err != nil { |
| 218 |
return nil, err |
| 219 |
} |
| 220 |
|
| 221 |
return newRepository(s, worktree), nil |
| 222 |
} |
| 223 |
|
| 224 |
// Clone a repository into the given Storer and worktree Filesystem with the |
| 225 |
// given options, if worktree is nil a bare repository is created. If the given |
| 226 |
// storer is not empty ErrRepositoryAlreadyExists is returned. |
| 227 |
func Clone(s storage.Storer, worktree billy.Filesystem, o *CloneOptions) (*Repository, error) { |
| 228 |
return CloneContext(context.Background(), s, worktree, o) |
| 229 |
} |
| 230 |
|
| 231 |
// CloneContext a repository into the given Storer and worktree Filesystem with |
| 232 |
// the given options, if worktree is nil a bare repository is created. If the |
| 233 |
// given storer is not empty ErrRepositoryAlreadyExists is returned. |
| 234 |
// |
| 235 |
// The provided Context must be non-nil. If the context expires before the |
| 236 |
// operation is complete, an error is returned. The context only affects the |
| 237 |
// transport operations. |
| 238 |
func CloneContext( |
| 239 |
ctx context.Context, s storage.Storer, worktree billy.Filesystem, o *CloneOptions, |
| 240 |
) (*Repository, error) { |
| 241 |
r, err := Init(s, worktree) |
| 242 |
if err != nil { |
| 243 |
return nil, err |
| 244 |
} |
| 245 |
|
| 246 |
return r, r.clone(ctx, o) |
| 247 |
} |
| 248 |
|
| 249 |
// PlainInit create an empty git repository at the given path. isBare defines |
| 250 |
// if the repository will have worktree (non-bare) or not (bare), if the path |
| 251 |
// is not empty ErrRepositoryAlreadyExists is returned. |
| 252 |
func PlainInit(path string, isBare bool) (*Repository, error) { |
| 253 |
return PlainInitWithOptions(path, &PlainInitOptions{ |
| 254 |
Bare: isBare, |
| 255 |
}) |
| 256 |
} |
| 257 |
|
| 258 |
func PlainInitWithOptions(path string, opts *PlainInitOptions) (*Repository, error) { |
| 259 |
if opts == nil { |
| 260 |
opts = &PlainInitOptions{} |
| 261 |
} |
| 262 |
|
| 263 |
var wt, dot billy.Filesystem |
| 264 |
|
| 265 |
if opts.Bare { |
| 266 |
dot = osfs.New(path) |
| 267 |
} else { |
| 268 |
wt = osfs.New(path) |
| 269 |
dot, _ = wt.Chroot(GitDirName) |
| 270 |
} |
| 271 |
|
| 272 |
s := filesystem.NewStorage(dot, cache.NewObjectLRUDefault()) |
| 273 |
|
| 274 |
r, err := InitWithOptions(s, wt, opts.InitOptions) |
| 275 |
if err != nil { |
| 276 |
return nil, err |
| 277 |
} |
| 278 |
|
| 279 |
cfg, err := r.Config() |
| 280 |
if err != nil { |
| 281 |
return nil, err |
| 282 |
} |
| 283 |
|
| 284 |
if opts.ObjectFormat != "" { |
| 285 |
if opts.ObjectFormat == formatcfg.SHA256 && hash.CryptoType != crypto.SHA256 { |
| 286 |
return nil, ErrSHA256NotSupported |
| 287 |
} |
| 288 |
|
| 289 |
cfg.Core.RepositoryFormatVersion = formatcfg.Version_1 |
| 290 |
cfg.Extensions.ObjectFormat = opts.ObjectFormat |
| 291 |
} |
| 292 |
|
| 293 |
err = r.Storer.SetConfig(cfg) |
| 294 |
if err != nil { |
| 295 |
return nil, err |
| 296 |
} |
| 297 |
|
| 298 |
return r, err |
| 299 |
} |
| 300 |
|
| 301 |
// PlainOpen opens a git repository from the given path. It detects if the |
| 302 |
// repository is bare or a normal one. If the path doesn't contain a valid |
| 303 |
// repository ErrRepositoryNotExists is returned |
| 304 |
func PlainOpen(path string) (*Repository, error) { |
| 305 |
return PlainOpenWithOptions(path, &PlainOpenOptions{}) |
| 306 |
} |
| 307 |
|
| 308 |
// PlainOpenWithOptions opens a git repository from the given path with specific |
| 309 |
// options. See PlainOpen for more info. |
| 310 |
func PlainOpenWithOptions(path string, o *PlainOpenOptions) (*Repository, error) { |
| 311 |
dot, wt, err := dotGitToOSFilesystems(path, o.DetectDotGit) |
| 312 |
if err != nil { |
| 313 |
return nil, err |
| 314 |
} |
| 315 |
|
| 316 |
if _, err := dot.Stat(""); err != nil { |
| 317 |
if os.IsNotExist(err) { |
| 318 |
return nil, ErrRepositoryNotExists |
| 319 |
} |
| 320 |
|
| 321 |
return nil, err |
| 322 |
} |
| 323 |
|
| 324 |
var repositoryFs billy.Filesystem |
| 325 |
|
| 326 |
if o.EnableDotGitCommonDir { |
| 327 |
dotGitCommon, err := dotGitCommonDirectory(dot) |
| 328 |
if err != nil { |
| 329 |
return nil, err |
| 330 |
} |
| 331 |
repositoryFs = dotgit.NewRepositoryFilesystem(dot, dotGitCommon) |
| 332 |
} else { |
| 333 |
repositoryFs = dot |
| 334 |
} |
| 335 |
|
| 336 |
s := filesystem.NewStorage(repositoryFs, cache.NewObjectLRUDefault()) |
| 337 |
|
| 338 |
return Open(s, wt) |
| 339 |
} |
| 340 |
|
| 341 |
func dotGitToOSFilesystems(path string, detect bool) (dot, wt billy.Filesystem, err error) { |
| 342 |
path, err = path_util.ReplaceTildeWithHome(path) |
| 343 |
if err != nil { |
| 344 |
return nil, nil, err |
| 345 |
} |
| 346 |
|
| 347 |
if path, err = filepath.Abs(path); err != nil { |
| 348 |
return nil, nil, err |
| 349 |
} |
| 350 |
|
| 351 |
var fs billy.Filesystem |
| 352 |
var fi os.FileInfo |
| 353 |
for { |
| 354 |
fs = osfs.New(path) |
| 355 |
|
| 356 |
pathinfo, err := fs.Stat("/") |
| 357 |
if !os.IsNotExist(err) { |
| 358 |
if pathinfo == nil { |
| 359 |
return nil, nil, err |
| 360 |
} |
| 361 |
if !pathinfo.IsDir() && detect { |
| 362 |
fs = osfs.New(filepath.Dir(path)) |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
fi, err = fs.Stat(GitDirName) |
| 367 |
if err == nil { |
| 368 |
// no error; stop |
| 369 |
break |
| 370 |
} |
| 371 |
if !os.IsNotExist(err) { |
| 372 |
// unknown error; stop |
| 373 |
return nil, nil, err |
| 374 |
} |
| 375 |
if detect { |
| 376 |
// try its parent as long as we haven't reached |
| 377 |
// the root dir |
| 378 |
if dir := filepath.Dir(path); dir != path { |
| 379 |
path = dir |
| 380 |
continue |
| 381 |
} |
| 382 |
} |
| 383 |
// not detecting via parent dirs and the dir does not exist; |
| 384 |
// stop |
| 385 |
return fs, nil, nil |
| 386 |
} |
| 387 |
|
| 388 |
if fi.IsDir() { |
| 389 |
dot, err = fs.Chroot(GitDirName) |
| 390 |
return dot, fs, err |
| 391 |
} |
| 392 |
|
| 393 |
dot, err = dotGitFileToOSFilesystem(path, fs) |
| 394 |
if err != nil { |
| 395 |
return nil, nil, err |
| 396 |
} |
| 397 |
|
| 398 |
return dot, fs, nil |
| 399 |
} |
| 400 |
|
| 401 |
func dotGitFileToOSFilesystem(path string, fs billy.Filesystem) (bfs billy.Filesystem, err error) { |
| 402 |
f, err := fs.Open(GitDirName) |
| 403 |
if err != nil { |
| 404 |
return nil, err |
| 405 |
} |
| 406 |
defer ioutil.CheckClose(f, &err) |
| 407 |
|
| 408 |
b, err := io.ReadAll(f) |
| 409 |
if err != nil { |
| 410 |
return nil, err |
| 411 |
} |
| 412 |
|
| 413 |
line := string(b) |
| 414 |
const prefix = "gitdir: " |
| 415 |
if !strings.HasPrefix(line, prefix) { |
| 416 |
return nil, fmt.Errorf(".git file has no %s prefix", prefix) |
| 417 |
} |
| 418 |
|
| 419 |
gitdir := strings.Split(line[len(prefix):], "\n")[0] |
| 420 |
gitdir = strings.TrimSpace(gitdir) |
| 421 |
if filepath.IsAbs(gitdir) { |
| 422 |
return osfs.New(gitdir), nil |
| 423 |
} |
| 424 |
|
| 425 |
return osfs.New(fs.Join(path, gitdir)), nil |
| 426 |
} |
| 427 |
|
| 428 |
func dotGitCommonDirectory(fs billy.Filesystem) (commonDir billy.Filesystem, err error) { |
| 429 |
f, err := fs.Open("commondir") |
| 430 |
if os.IsNotExist(err) { |
| 431 |
return nil, nil |
| 432 |
} |
| 433 |
if err != nil { |
| 434 |
return nil, err |
| 435 |
} |
| 436 |
|
| 437 |
b, err := io.ReadAll(f) |
| 438 |
if err != nil { |
| 439 |
return nil, err |
| 440 |
} |
| 441 |
if len(b) > 0 { |
| 442 |
path := strings.TrimSpace(string(b)) |
| 443 |
if filepath.IsAbs(path) { |
| 444 |
commonDir = osfs.New(path) |
| 445 |
} else { |
| 446 |
commonDir = osfs.New(filepath.Join(fs.Root(), path)) |
| 447 |
} |
| 448 |
if _, err := commonDir.Stat(""); err != nil { |
| 449 |
if os.IsNotExist(err) { |
| 450 |
return nil, ErrRepositoryIncomplete |
| 451 |
} |
| 452 |
|
| 453 |
return nil, err |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
return commonDir, nil |
| 458 |
} |
| 459 |
|
| 460 |
// PlainClone a repository into the path with the given options, isBare defines |
| 461 |
// if the new repository will be bare or normal. If the path is not empty |
| 462 |
// ErrRepositoryAlreadyExists is returned. |
| 463 |
// |
| 464 |
// TODO(mcuadros): move isBare to CloneOptions in v5 |
| 465 |
func PlainClone(path string, isBare bool, o *CloneOptions) (*Repository, error) { |
| 466 |
return PlainCloneContext(context.Background(), path, isBare, o) |
| 467 |
} |
| 468 |
|
| 469 |
// PlainCloneContext a repository into the path with the given options, isBare |
| 470 |
// defines if the new repository will be bare or normal. If the path is not empty |
| 471 |
// ErrRepositoryAlreadyExists is returned. |
| 472 |
// |
| 473 |
// The provided Context must be non-nil. If the context expires before the |
| 474 |
// operation is complete, an error is returned. The context only affects the |
| 475 |
// transport operations. |
| 476 |
// |
| 477 |
// TODO(mcuadros): move isBare to CloneOptions in v5 |
| 478 |
// TODO(smola): refuse upfront to clone on a non-empty directory in v5, see #1027 |
| 479 |
func PlainCloneContext(ctx context.Context, path string, isBare bool, o *CloneOptions) (*Repository, error) { |
| 480 |
cleanup, cleanupParent, err := checkIfCleanupIsNeeded(path) |
| 481 |
if err != nil { |
| 482 |
return nil, err |
| 483 |
} |
| 484 |
|
| 485 |
if o.Mirror { |
| 486 |
isBare = true |
| 487 |
} |
| 488 |
r, err := PlainInit(path, isBare) |
| 489 |
if err != nil { |
| 490 |
return nil, err |
| 491 |
} |
| 492 |
|
| 493 |
err = r.clone(ctx, o) |
| 494 |
if err != nil && err != ErrRepositoryAlreadyExists { |
| 495 |
if cleanup { |
| 496 |
_ = cleanUpDir(path, cleanupParent) |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
return r, err |
| 501 |
} |
| 502 |
|
| 503 |
func newRepository(s storage.Storer, worktree billy.Filesystem) *Repository { |
| 504 |
return &Repository{ |
| 505 |
Storer: s, |
| 506 |
wt: worktree, |
| 507 |
r: make(map[string]*Remote), |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
func checkIfCleanupIsNeeded(path string) (cleanup bool, cleanParent bool, err error) { |
| 512 |
fi, err := osfs.Default.Stat(path) |
| 513 |
if err != nil { |
| 514 |
if os.IsNotExist(err) { |
| 515 |
return true, true, nil |
| 516 |
} |
| 517 |
|
| 518 |
return false, false, err |
| 519 |
} |
| 520 |
|
| 521 |
if !fi.IsDir() { |
| 522 |
return false, false, fmt.Errorf("path is not a directory: %s", path) |
| 523 |
} |
| 524 |
|
| 525 |
files, err := osfs.Default.ReadDir(path) |
| 526 |
if err != nil { |
| 527 |
return false, false, err |
| 528 |
} |
| 529 |
|
| 530 |
if len(files) == 0 { |
| 531 |
return true, false, nil |
| 532 |
} |
| 533 |
|
| 534 |
return false, false, nil |
| 535 |
} |
| 536 |
|
| 537 |
func cleanUpDir(path string, all bool) error { |
| 538 |
if all { |
| 539 |
return util.RemoveAll(osfs.Default, path) |
| 540 |
} |
| 541 |
|
| 542 |
files, err := osfs.Default.ReadDir(path) |
| 543 |
if err != nil { |
| 544 |
return err |
| 545 |
} |
| 546 |
|
| 547 |
for _, fi := range files { |
| 548 |
if err := util.RemoveAll(osfs.Default, osfs.Default.Join(path, fi.Name())); err != nil { |
| 549 |
return err |
| 550 |
} |
| 551 |
} |
| 552 |
|
| 553 |
return err |
| 554 |
} |
| 555 |
|
| 556 |
// Config return the repository config. In a filesystem backed repository this |
| 557 |
// means read the `.git/config`. |
| 558 |
func (r *Repository) Config() (*config.Config, error) { |
| 559 |
return r.Storer.Config() |
| 560 |
} |
| 561 |
|
| 562 |
// SetConfig marshall and writes the repository config. In a filesystem backed |
| 563 |
// repository this means write the `.git/config`. This function should be called |
| 564 |
// with the result of `Repository.Config` and never with the output of |
| 565 |
// `Repository.ConfigScoped`. |
| 566 |
func (r *Repository) SetConfig(cfg *config.Config) error { |
| 567 |
return r.Storer.SetConfig(cfg) |
| 568 |
} |
| 569 |
|
| 570 |
// ConfigScoped returns the repository config, merged with requested scope and |
| 571 |
// lower. For example if, config.GlobalScope is given the local and global config |
| 572 |
// are returned merged in one config value. |
| 573 |
func (r *Repository) ConfigScoped(scope config.Scope) (*config.Config, error) { |
| 574 |
// TODO(mcuadros): v6, add this as ConfigOptions.Scoped |
| 575 |
|
| 576 |
var err error |
| 577 |
system := config.NewConfig() |
| 578 |
if scope >= config.SystemScope { |
| 579 |
system, err = config.LoadConfig(config.SystemScope) |
| 580 |
if err != nil { |
| 581 |
return nil, err |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
global := config.NewConfig() |
| 586 |
if scope >= config.GlobalScope { |
| 587 |
global, err = config.LoadConfig(config.GlobalScope) |
| 588 |
if err != nil { |
| 589 |
return nil, err |
| 590 |
} |
| 591 |
} |
| 592 |
|
| 593 |
local, err := r.Storer.Config() |
| 594 |
if err != nil { |
| 595 |
return nil, err |
| 596 |
} |
| 597 |
|
| 598 |
_ = mergo.Merge(global, system) |
| 599 |
_ = mergo.Merge(local, global) |
| 600 |
return local, nil |
| 601 |
} |
| 602 |
|
| 603 |
// Remote return a remote if exists |
| 604 |
func (r *Repository) Remote(name string) (*Remote, error) { |
| 605 |
cfg, err := r.Config() |
| 606 |
if err != nil { |
| 607 |
return nil, err |
| 608 |
} |
| 609 |
|
| 610 |
c, ok := cfg.Remotes[name] |
| 611 |
if !ok { |
| 612 |
return nil, ErrRemoteNotFound |
| 613 |
} |
| 614 |
|
| 615 |
return NewRemote(r.Storer, c), nil |
| 616 |
} |
| 617 |
|
| 618 |
// Remotes returns a list with all the remotes |
| 619 |
func (r *Repository) Remotes() ([]*Remote, error) { |
| 620 |
cfg, err := r.Config() |
| 621 |
if err != nil { |
| 622 |
return nil, err |
| 623 |
} |
| 624 |
|
| 625 |
remotes := make([]*Remote, len(cfg.Remotes)) |
| 626 |
|
| 627 |
var i int |
| 628 |
for _, c := range cfg.Remotes { |
| 629 |
remotes[i] = NewRemote(r.Storer, c) |
| 630 |
i++ |
| 631 |
} |
| 632 |
|
| 633 |
return remotes, nil |
| 634 |
} |
| 635 |
|
| 636 |
// CreateRemote creates a new remote |
| 637 |
func (r *Repository) CreateRemote(c *config.RemoteConfig) (*Remote, error) { |
| 638 |
if err := c.Validate(); err != nil { |
| 639 |
return nil, err |
| 640 |
} |
| 641 |
|
| 642 |
remote := NewRemote(r.Storer, c) |
| 643 |
|
| 644 |
cfg, err := r.Config() |
| 645 |
if err != nil { |
| 646 |
return nil, err |
| 647 |
} |
| 648 |
|
| 649 |
if _, ok := cfg.Remotes[c.Name]; ok { |
| 650 |
return nil, ErrRemoteExists |
| 651 |
} |
| 652 |
|
| 653 |
cfg.Remotes[c.Name] = c |
| 654 |
return remote, r.Storer.SetConfig(cfg) |
| 655 |
} |
| 656 |
|
| 657 |
// CreateRemoteAnonymous creates a new anonymous remote. c.Name must be "anonymous". |
| 658 |
// It's used like 'git fetch [email protected]:src-d/go-git.git master:master'. |
| 659 |
func (r *Repository) CreateRemoteAnonymous(c *config.RemoteConfig) (*Remote, error) { |
| 660 |
if err := c.Validate(); err != nil { |
| 661 |
return nil, err |
| 662 |
} |
| 663 |
|
| 664 |
if c.Name != "anonymous" { |
| 665 |
return nil, ErrAnonymousRemoteName |
| 666 |
} |
| 667 |
|
| 668 |
remote := NewRemote(r.Storer, c) |
| 669 |
|
| 670 |
return remote, nil |
| 671 |
} |
| 672 |
|
| 673 |
// DeleteRemote delete a remote from the repository and delete the config |
| 674 |
func (r *Repository) DeleteRemote(name string) error { |
| 675 |
cfg, err := r.Config() |
| 676 |
if err != nil { |
| 677 |
return err |
| 678 |
} |
| 679 |
|
| 680 |
if _, ok := cfg.Remotes[name]; !ok { |
| 681 |
return ErrRemoteNotFound |
| 682 |
} |
| 683 |
|
| 684 |
delete(cfg.Remotes, name) |
| 685 |
return r.Storer.SetConfig(cfg) |
| 686 |
} |
| 687 |
|
| 688 |
// Branch return a Branch if exists |
| 689 |
func (r *Repository) Branch(name string) (*config.Branch, error) { |
| 690 |
cfg, err := r.Config() |
| 691 |
if err != nil { |
| 692 |
return nil, err |
| 693 |
} |
| 694 |
|
| 695 |
b, ok := cfg.Branches[name] |
| 696 |
if !ok { |
| 697 |
return nil, ErrBranchNotFound |
| 698 |
} |
| 699 |
|
| 700 |
return b, nil |
| 701 |
} |
| 702 |
|
| 703 |
// CreateBranch creates a new Branch |
| 704 |
func (r *Repository) CreateBranch(c *config.Branch) error { |
| 705 |
if err := c.Validate(); err != nil { |
| 706 |
return err |
| 707 |
} |
| 708 |
|
| 709 |
cfg, err := r.Config() |
| 710 |
if err != nil { |
| 711 |
return err |
| 712 |
} |
| 713 |
|
| 714 |
if _, ok := cfg.Branches[c.Name]; ok { |
| 715 |
return ErrBranchExists |
| 716 |
} |
| 717 |
|
| 718 |
cfg.Branches[c.Name] = c |
| 719 |
return r.Storer.SetConfig(cfg) |
| 720 |
} |
| 721 |
|
| 722 |
// DeleteBranch delete a Branch from the repository and delete the config |
| 723 |
func (r *Repository) DeleteBranch(name string) error { |
| 724 |
cfg, err := r.Config() |
| 725 |
if err != nil { |
| 726 |
return err |
| 727 |
} |
| 728 |
|
| 729 |
if _, ok := cfg.Branches[name]; !ok { |
| 730 |
return ErrBranchNotFound |
| 731 |
} |
| 732 |
|
| 733 |
delete(cfg.Branches, name) |
| 734 |
return r.Storer.SetConfig(cfg) |
| 735 |
} |
| 736 |
|
| 737 |
// CreateTag creates a tag. If opts is included, the tag is an annotated tag, |
| 738 |
// otherwise a lightweight tag is created. |
| 739 |
func (r *Repository) CreateTag(name string, hash plumbing.Hash, opts *CreateTagOptions) (*plumbing.Reference, error) { |
| 740 |
rname := plumbing.NewTagReferenceName(name) |
| 741 |
if err := rname.Validate(); err != nil { |
| 742 |
return nil, err |
| 743 |
} |
| 744 |
|
| 745 |
_, err := r.Storer.Reference(rname) |
| 746 |
switch err { |
| 747 |
case nil: |
| 748 |
// Tag exists, this is an error |
| 749 |
return nil, ErrTagExists |
| 750 |
case plumbing.ErrReferenceNotFound: |
| 751 |
// Tag missing, available for creation, pass this |
| 752 |
default: |
| 753 |
// Some other error |
| 754 |
return nil, err |
| 755 |
} |
| 756 |
|
| 757 |
var target plumbing.Hash |
| 758 |
if opts != nil { |
| 759 |
target, err = r.createTagObject(name, hash, opts) |
| 760 |
if err != nil { |
| 761 |
return nil, err |
| 762 |
} |
| 763 |
} else { |
| 764 |
target = hash |
| 765 |
} |
| 766 |
|
| 767 |
ref := plumbing.NewHashReference(rname, target) |
| 768 |
if err = r.Storer.SetReference(ref); err != nil { |
| 769 |
return nil, err |
| 770 |
} |
| 771 |
|
| 772 |
return ref, nil |
| 773 |
} |
| 774 |
|
| 775 |
func (r *Repository) createTagObject(name string, hash plumbing.Hash, opts *CreateTagOptions) (plumbing.Hash, error) { |
| 776 |
if err := opts.Validate(r, hash); err != nil { |
| 777 |
return plumbing.ZeroHash, err |
| 778 |
} |
| 779 |
|
| 780 |
rawobj, err := object.GetObject(r.Storer, hash) |
| 781 |
if err != nil { |
| 782 |
return plumbing.ZeroHash, err |
| 783 |
} |
| 784 |
|
| 785 |
tag := &object.Tag{ |
| 786 |
Name: name, |
| 787 |
Tagger: *opts.Tagger, |
| 788 |
Message: opts.Message, |
| 789 |
TargetType: rawobj.Type(), |
| 790 |
Target: hash, |
| 791 |
} |
| 792 |
|
| 793 |
if opts.SignKey != nil { |
| 794 |
sig, err := r.buildTagSignature(tag, opts.SignKey) |
| 795 |
if err != nil { |
| 796 |
return plumbing.ZeroHash, err |
| 797 |
} |
| 798 |
|
| 799 |
tag.PGPSignature = sig |
| 800 |
} |
| 801 |
|
| 802 |
obj := r.Storer.NewEncodedObject() |
| 803 |
if err := tag.Encode(obj); err != nil { |
| 804 |
return plumbing.ZeroHash, err |
| 805 |
} |
| 806 |
|
| 807 |
return r.Storer.SetEncodedObject(obj) |
| 808 |
} |
| 809 |
|
| 810 |
func (r *Repository) buildTagSignature(tag *object.Tag, signKey *openpgp.Entity) (string, error) { |
| 811 |
encoded := &plumbing.MemoryObject{} |
| 812 |
if err := tag.Encode(encoded); err != nil { |
| 813 |
return "", err |
| 814 |
} |
| 815 |
|
| 816 |
rdr, err := encoded.Reader() |
| 817 |
if err != nil { |
| 818 |
return "", err |
| 819 |
} |
| 820 |
|
| 821 |
var b bytes.Buffer |
| 822 |
if err := openpgp.ArmoredDetachSign(&b, signKey, rdr, nil); err != nil { |
| 823 |
return "", err |
| 824 |
} |
| 825 |
|
| 826 |
return b.String(), nil |
| 827 |
} |
| 828 |
|
| 829 |
// Tag returns a tag from the repository. |
| 830 |
// |
| 831 |
// If you want to check to see if the tag is an annotated tag, you can call |
| 832 |
// TagObject on the hash of the reference in ForEach: |
| 833 |
// |
| 834 |
// ref, err := r.Tag("v0.1.0") |
| 835 |
// if err != nil { |
| 836 |
// // Handle error |
| 837 |
// } |
| 838 |
// |
| 839 |
// obj, err := r.TagObject(ref.Hash()) |
| 840 |
// switch err { |
| 841 |
// case nil: |
| 842 |
// // Tag object present |
| 843 |
// case plumbing.ErrObjectNotFound: |
| 844 |
// // Not a tag object |
| 845 |
// default: |
| 846 |
// // Some other error |
| 847 |
// } |
| 848 |
func (r *Repository) Tag(name string) (*plumbing.Reference, error) { |
| 849 |
ref, err := r.Reference(plumbing.ReferenceName(path.Join("refs", "tags", name)), false) |
| 850 |
if err != nil { |
| 851 |
if err == plumbing.ErrReferenceNotFound { |
| 852 |
// Return a friendly error for this one, versus just ReferenceNotFound. |
| 853 |
return nil, ErrTagNotFound |
| 854 |
} |
| 855 |
|
| 856 |
return nil, err |
| 857 |
} |
| 858 |
|
| 859 |
return ref, nil |
| 860 |
} |
| 861 |
|
| 862 |
// DeleteTag deletes a tag from the repository. |
| 863 |
func (r *Repository) DeleteTag(name string) error { |
| 864 |
_, err := r.Tag(name) |
| 865 |
if err != nil { |
| 866 |
return err |
| 867 |
} |
| 868 |
|
| 869 |
return r.Storer.RemoveReference(plumbing.ReferenceName(path.Join("refs", "tags", name))) |
| 870 |
} |
| 871 |
|
| 872 |
func (r *Repository) resolveToCommitHash(h plumbing.Hash) (plumbing.Hash, error) { |
| 873 |
obj, err := r.Storer.EncodedObject(plumbing.AnyObject, h) |
| 874 |
if err != nil { |
| 875 |
return plumbing.ZeroHash, err |
| 876 |
} |
| 877 |
switch obj.Type() { |
| 878 |
case plumbing.TagObject: |
| 879 |
t, err := object.DecodeTag(r.Storer, obj) |
| 880 |
if err != nil { |
| 881 |
return plumbing.ZeroHash, err |
| 882 |
} |
| 883 |
return r.resolveToCommitHash(t.Target) |
| 884 |
case plumbing.CommitObject: |
| 885 |
return h, nil |
| 886 |
default: |
| 887 |
return plumbing.ZeroHash, ErrUnableToResolveCommit |
| 888 |
} |
| 889 |
} |
| 890 |
|
| 891 |
// Clone clones a remote repository |
| 892 |
func (r *Repository) clone(ctx context.Context, o *CloneOptions) error { |
| 893 |
if err := o.Validate(); err != nil { |
| 894 |
return err |
| 895 |
} |
| 896 |
|
| 897 |
c := &config.RemoteConfig{ |
| 898 |
Name: o.RemoteName, |
| 899 |
URLs: []string{o.URL}, |
| 900 |
Fetch: r.cloneRefSpec(o), |
| 901 |
Mirror: o.Mirror, |
| 902 |
} |
| 903 |
|
| 904 |
if _, err := r.CreateRemote(c); err != nil { |
| 905 |
return err |
| 906 |
} |
| 907 |
|
| 908 |
// When the repository to clone is on the local machine, |
| 909 |
// instead of using hard links, automatically setup .git/objects/info/alternates |
| 910 |
// to share the objects with the source repository |
| 911 |
if o.Shared { |
| 912 |
if !url.IsLocalEndpoint(o.URL) { |
| 913 |
return ErrAlternatePathNotSupported |
| 914 |
} |
| 915 |
altpath := o.URL |
| 916 |
remoteRepo, err := PlainOpen(o.URL) |
| 917 |
if err != nil { |
| 918 |
return fmt.Errorf("failed to open remote repository: %w", err) |
| 919 |
} |
| 920 |
conf, err := remoteRepo.Config() |
| 921 |
if err != nil { |
| 922 |
return fmt.Errorf("failed to read remote repository configuration: %w", err) |
| 923 |
} |
| 924 |
if !conf.Core.IsBare { |
| 925 |
altpath = path.Join(altpath, GitDirName) |
| 926 |
} |
| 927 |
if err := r.Storer.AddAlternate(altpath); err != nil { |
| 928 |
return fmt.Errorf("failed to add alternate file to git objects dir: %w", err) |
| 929 |
} |
| 930 |
} |
| 931 |
|
| 932 |
ref, err := r.fetchAndUpdateReferences(ctx, &FetchOptions{ |
| 933 |
RefSpecs: c.Fetch, |
| 934 |
Depth: o.Depth, |
| 935 |
Auth: o.Auth, |
| 936 |
Progress: o.Progress, |
| 937 |
Tags: o.Tags, |
| 938 |
RemoteName: o.RemoteName, |
| 939 |
InsecureSkipTLS: o.InsecureSkipTLS, |
| 940 |
ClientCert: o.ClientCert, |
| 941 |
ClientKey: o.ClientKey, |
| 942 |
CABundle: o.CABundle, |
| 943 |
ProxyOptions: o.ProxyOptions, |
| 944 |
}, o.ReferenceName) |
| 945 |
if err != nil { |
| 946 |
return err |
| 947 |
} |
| 948 |
|
| 949 |
if r.wt != nil && !o.NoCheckout { |
| 950 |
w, err := r.Worktree() |
| 951 |
if err != nil { |
| 952 |
return err |
| 953 |
} |
| 954 |
|
| 955 |
head, err := r.Head() |
| 956 |
if err != nil { |
| 957 |
return err |
| 958 |
} |
| 959 |
|
| 960 |
if err := w.Reset(&ResetOptions{ |
| 961 |
Mode: MergeReset, |
| 962 |
Commit: head.Hash(), |
| 963 |
}); err != nil { |
| 964 |
return err |
| 965 |
} |
| 966 |
|
| 967 |
if o.RecurseSubmodules != NoRecurseSubmodules { |
| 968 |
if err := w.updateSubmodules(ctx, &SubmoduleUpdateOptions{ |
| 969 |
RecurseSubmodules: o.RecurseSubmodules, |
| 970 |
Depth: func() int { |
| 971 |
if o.ShallowSubmodules { |
| 972 |
return 1 |
| 973 |
} |
| 974 |
return 0 |
| 975 |
}(), |
| 976 |
Auth: o.Auth, |
| 977 |
}); err != nil { |
| 978 |
return err |
| 979 |
} |
| 980 |
} |
| 981 |
} |
| 982 |
|
| 983 |
if err := r.updateRemoteConfigIfNeeded(o, c, ref); err != nil { |
| 984 |
return err |
| 985 |
} |
| 986 |
|
| 987 |
if !o.Mirror && ref.Name().IsBranch() { |
| 988 |
branchRef := ref.Name() |
| 989 |
branchName := strings.Split(string(branchRef), "refs/heads/")[1] |
| 990 |
|
| 991 |
b := &config.Branch{ |
| 992 |
Name: branchName, |
| 993 |
Merge: branchRef, |
| 994 |
} |
| 995 |
|
| 996 |
if o.RemoteName == "" { |
| 997 |
b.Remote = "origin" |
| 998 |
} else { |
| 999 |
b.Remote = o.RemoteName |
| 1000 |
} |
| 1001 |
|
| 1002 |
if err := r.CreateBranch(b); err != nil { |
| 1003 |
return err |
| 1004 |
} |
| 1005 |
} |
| 1006 |
|
| 1007 |
return nil |
| 1008 |
} |
| 1009 |
|
| 1010 |
const ( |
| 1011 |
refspecTag = "+refs/tags/%s:refs/tags/%[1]s" |
| 1012 |
refspecSingleBranch = "+refs/heads/%s:refs/remotes/%s/%[1]s" |
| 1013 |
refspecSingleBranchHEAD = "+HEAD:refs/remotes/%s/HEAD" |
| 1014 |
) |
| 1015 |
|
| 1016 |
func (r *Repository) cloneRefSpec(o *CloneOptions) []config.RefSpec { |
| 1017 |
switch { |
| 1018 |
case o.Mirror: |
| 1019 |
return []config.RefSpec{"+refs/*:refs/*"} |
| 1020 |
case o.ReferenceName.IsTag(): |
| 1021 |
return []config.RefSpec{ |
| 1022 |
config.RefSpec(fmt.Sprintf(refspecTag, o.ReferenceName.Short())), |
| 1023 |
} |
| 1024 |
case o.SingleBranch && o.ReferenceName == plumbing.HEAD: |
| 1025 |
return []config.RefSpec{ |
| 1026 |
config.RefSpec(fmt.Sprintf(refspecSingleBranchHEAD, o.RemoteName)), |
| 1027 |
} |
| 1028 |
case o.SingleBranch: |
| 1029 |
return []config.RefSpec{ |
| 1030 |
config.RefSpec(fmt.Sprintf(refspecSingleBranch, o.ReferenceName.Short(), o.RemoteName)), |
| 1031 |
} |
| 1032 |
default: |
| 1033 |
return []config.RefSpec{ |
| 1034 |
config.RefSpec(fmt.Sprintf(config.DefaultFetchRefSpec, o.RemoteName)), |
| 1035 |
} |
| 1036 |
} |
| 1037 |
} |
| 1038 |
|
| 1039 |
func (r *Repository) setIsBare(isBare bool) error { |
| 1040 |
cfg, err := r.Config() |
| 1041 |
if err != nil { |
| 1042 |
return err |
| 1043 |
} |
| 1044 |
|
| 1045 |
cfg.Core.IsBare = isBare |
| 1046 |
return r.Storer.SetConfig(cfg) |
| 1047 |
} |
| 1048 |
|
| 1049 |
func (r *Repository) updateRemoteConfigIfNeeded(o *CloneOptions, c *config.RemoteConfig, _ *plumbing.Reference) error { |
| 1050 |
if !o.SingleBranch { |
| 1051 |
return nil |
| 1052 |
} |
| 1053 |
|
| 1054 |
c.Fetch = r.cloneRefSpec(o) |
| 1055 |
|
| 1056 |
cfg, err := r.Config() |
| 1057 |
if err != nil { |
| 1058 |
return err |
| 1059 |
} |
| 1060 |
|
| 1061 |
cfg.Remotes[c.Name] = c |
| 1062 |
return r.Storer.SetConfig(cfg) |
| 1063 |
} |
| 1064 |
|
| 1065 |
func (r *Repository) fetchAndUpdateReferences( |
| 1066 |
ctx context.Context, o *FetchOptions, ref plumbing.ReferenceName, |
| 1067 |
) (*plumbing.Reference, error) { |
| 1068 |
|
| 1069 |
if err := o.Validate(); err != nil { |
| 1070 |
return nil, err |
| 1071 |
} |
| 1072 |
|
| 1073 |
remote, err := r.Remote(o.RemoteName) |
| 1074 |
if err != nil { |
| 1075 |
return nil, err |
| 1076 |
} |
| 1077 |
|
| 1078 |
objsUpdated := true |
| 1079 |
remoteRefs, err := remote.fetch(ctx, o) |
| 1080 |
if err == NoErrAlreadyUpToDate { |
| 1081 |
objsUpdated = false |
| 1082 |
} else if err == packfile.ErrEmptyPackfile { |
| 1083 |
return nil, ErrFetching |
| 1084 |
} else if err != nil { |
| 1085 |
return nil, err |
| 1086 |
} |
| 1087 |
|
| 1088 |
resolvedRef, err := expand_ref(remoteRefs, ref) |
| 1089 |
if err != nil { |
| 1090 |
return nil, err |
| 1091 |
} |
| 1092 |
|
| 1093 |
refsUpdated, err := r.updateReferences(remote.c.Fetch, resolvedRef) |
| 1094 |
if err != nil { |
| 1095 |
return nil, err |
| 1096 |
} |
| 1097 |
|
| 1098 |
if !objsUpdated && !refsUpdated { |
| 1099 |
return nil, NoErrAlreadyUpToDate |
| 1100 |
} |
| 1101 |
|
| 1102 |
return resolvedRef, nil |
| 1103 |
} |
| 1104 |
|
| 1105 |
func (r *Repository) updateReferences(spec []config.RefSpec, |
| 1106 |
resolvedRef *plumbing.Reference) (updated bool, err error) { |
| 1107 |
|
| 1108 |
if !resolvedRef.Name().IsBranch() { |
| 1109 |
// Detached HEAD mode |
| 1110 |
h, err := r.resolveToCommitHash(resolvedRef.Hash()) |
| 1111 |
if err != nil { |
| 1112 |
return false, err |
| 1113 |
} |
| 1114 |
head := plumbing.NewHashReference(plumbing.HEAD, h) |
| 1115 |
return updateReferenceStorerIfNeeded(r.Storer, head) |
| 1116 |
} |
| 1117 |
|
| 1118 |
refs := []*plumbing.Reference{ |
| 1119 |
// Create local reference for the resolved ref |
| 1120 |
resolvedRef, |
| 1121 |
// Create local symbolic HEAD |
| 1122 |
plumbing.NewSymbolicReference(plumbing.HEAD, resolvedRef.Name()), |
| 1123 |
} |
| 1124 |
|
| 1125 |
refs = append(refs, r.calculateRemoteHeadReference(spec, resolvedRef)...) |
| 1126 |
|
| 1127 |
for _, ref := range refs { |
| 1128 |
u, err := updateReferenceStorerIfNeeded(r.Storer, ref) |
| 1129 |
if err != nil { |
| 1130 |
return updated, err |
| 1131 |
} |
| 1132 |
|
| 1133 |
if u { |
| 1134 |
updated = true |
| 1135 |
} |
| 1136 |
} |
| 1137 |
|
| 1138 |
return |
| 1139 |
} |
| 1140 |
|
| 1141 |
func (r *Repository) calculateRemoteHeadReference(spec []config.RefSpec, |
| 1142 |
resolvedHead *plumbing.Reference) []*plumbing.Reference { |
| 1143 |
|
| 1144 |
var refs []*plumbing.Reference |
| 1145 |
|
| 1146 |
// Create resolved HEAD reference with remote prefix if it does not |
| 1147 |
// exist. This is needed when using single branch and HEAD. |
| 1148 |
for _, rs := range spec { |
| 1149 |
name := resolvedHead.Name() |
| 1150 |
if !rs.Match(name) { |
| 1151 |
continue |
| 1152 |
} |
| 1153 |
|
| 1154 |
name = rs.Dst(name) |
| 1155 |
_, err := r.Storer.Reference(name) |
| 1156 |
if err == plumbing.ErrReferenceNotFound { |
| 1157 |
refs = append(refs, plumbing.NewHashReference(name, resolvedHead.Hash())) |
| 1158 |
} |
| 1159 |
} |
| 1160 |
|
| 1161 |
return refs |
| 1162 |
} |
| 1163 |
|
| 1164 |
func checkAndUpdateReferenceStorerIfNeeded( |
| 1165 |
s storer.ReferenceStorer, r, old *plumbing.Reference) ( |
| 1166 |
updated bool, err error) { |
| 1167 |
p, err := s.Reference(r.Name()) |
| 1168 |
if err != nil && err != plumbing.ErrReferenceNotFound { |
| 1169 |
return false, err |
| 1170 |
} |
| 1171 |
|
| 1172 |
// we use the string method to compare references, is the easiest way |
| 1173 |
if err == plumbing.ErrReferenceNotFound || r.String() != p.String() { |
| 1174 |
if err := s.CheckAndSetReference(r, old); err != nil { |
| 1175 |
return false, err |
| 1176 |
} |
| 1177 |
|
| 1178 |
return true, nil |
| 1179 |
} |
| 1180 |
|
| 1181 |
return false, nil |
| 1182 |
} |
| 1183 |
|
| 1184 |
func updateReferenceStorerIfNeeded( |
| 1185 |
s storer.ReferenceStorer, r *plumbing.Reference) (updated bool, err error) { |
| 1186 |
return checkAndUpdateReferenceStorerIfNeeded(s, r, nil) |
| 1187 |
} |
| 1188 |
|
| 1189 |
// Fetch fetches references along with the objects necessary to complete |
| 1190 |
// their histories, from the remote named as FetchOptions.RemoteName. |
| 1191 |
// |
| 1192 |
// Returns nil if the operation is successful, NoErrAlreadyUpToDate if there are |
| 1193 |
// no changes to be fetched, or an error. |
| 1194 |
func (r *Repository) Fetch(o *FetchOptions) error { |
| 1195 |
return r.FetchContext(context.Background(), o) |
| 1196 |
} |
| 1197 |
|
| 1198 |
// FetchContext fetches references along with the objects necessary to complete |
| 1199 |
// their histories, from the remote named as FetchOptions.RemoteName. |
| 1200 |
// |
| 1201 |
// Returns nil if the operation is successful, NoErrAlreadyUpToDate if there are |
| 1202 |
// no changes to be fetched, or an error. |
| 1203 |
// |
| 1204 |
// The provided Context must be non-nil. If the context expires before the |
| 1205 |
// operation is complete, an error is returned. The context only affects the |
| 1206 |
// transport operations. |
| 1207 |
func (r *Repository) FetchContext(ctx context.Context, o *FetchOptions) error { |
| 1208 |
if err := o.Validate(); err != nil { |
| 1209 |
return err |
| 1210 |
} |
| 1211 |
|
| 1212 |
remote, err := r.Remote(o.RemoteName) |
| 1213 |
if err != nil { |
| 1214 |
return err |
| 1215 |
} |
| 1216 |
|
| 1217 |
return remote.FetchContext(ctx, o) |
| 1218 |
} |
| 1219 |
|
| 1220 |
// Push performs a push to the remote. Returns NoErrAlreadyUpToDate if |
| 1221 |
// the remote was already up-to-date, from the remote named as |
| 1222 |
// FetchOptions.RemoteName. |
| 1223 |
func (r *Repository) Push(o *PushOptions) error { |
| 1224 |
return r.PushContext(context.Background(), o) |
| 1225 |
} |
| 1226 |
|
| 1227 |
// PushContext performs a push to the remote. Returns NoErrAlreadyUpToDate if |
| 1228 |
// the remote was already up-to-date, from the remote named as |
| 1229 |
// FetchOptions.RemoteName. |
| 1230 |
// |
| 1231 |
// The provided Context must be non-nil. If the context expires before the |
| 1232 |
// operation is complete, an error is returned. The context only affects the |
| 1233 |
// transport operations. |
| 1234 |
func (r *Repository) PushContext(ctx context.Context, o *PushOptions) error { |
| 1235 |
if err := o.Validate(); err != nil { |
| 1236 |
return err |
| 1237 |
} |
| 1238 |
|
| 1239 |
remote, err := r.Remote(o.RemoteName) |
| 1240 |
if err != nil { |
| 1241 |
return err |
| 1242 |
} |
| 1243 |
|
| 1244 |
return remote.PushContext(ctx, o) |
| 1245 |
} |
| 1246 |
|
| 1247 |
// Log returns the commit history from the given LogOptions. |
| 1248 |
func (r *Repository) Log(o *LogOptions) (object.CommitIter, error) { |
| 1249 |
fn := commitIterFunc(o.Order) |
| 1250 |
if fn == nil { |
| 1251 |
return nil, fmt.Errorf("invalid Order=%v", o.Order) |
| 1252 |
} |
| 1253 |
|
| 1254 |
var ( |
| 1255 |
it object.CommitIter |
| 1256 |
err error |
| 1257 |
) |
| 1258 |
if o.All { |
| 1259 |
it, err = r.logAll(fn) |
| 1260 |
} else { |
| 1261 |
it, err = r.log(o.From, fn) |
| 1262 |
} |
| 1263 |
|
| 1264 |
if err != nil { |
| 1265 |
return nil, err |
| 1266 |
} |
| 1267 |
|
| 1268 |
if o.FileName != nil { |
| 1269 |
// for `git log --all` also check parent (if the next commit comes from the real parent) |
| 1270 |
it = r.logWithFile(*o.FileName, it, o.All) |
| 1271 |
} |
| 1272 |
if o.PathFilter != nil { |
| 1273 |
it = r.logWithPathFilter(o.PathFilter, it, o.All) |
| 1274 |
} |
| 1275 |
|
| 1276 |
if o.Since != nil || o.Until != nil { |
| 1277 |
limitOptions := object.LogLimitOptions{Since: o.Since, Until: o.Until} |
| 1278 |
it = r.logWithLimit(it, limitOptions) |
| 1279 |
} |
| 1280 |
|
| 1281 |
return it, nil |
| 1282 |
} |
| 1283 |
|
| 1284 |
func (r *Repository) log(from plumbing.Hash, commitIterFunc func(*object.Commit) object.CommitIter) (object.CommitIter, error) { |
| 1285 |
h := from |
| 1286 |
if from == plumbing.ZeroHash { |
| 1287 |
head, err := r.Head() |
| 1288 |
if err != nil { |
| 1289 |
return nil, err |
| 1290 |
} |
| 1291 |
|
| 1292 |
h = head.Hash() |
| 1293 |
} |
| 1294 |
|
| 1295 |
commit, err := r.CommitObject(h) |
| 1296 |
if err != nil { |
| 1297 |
return nil, err |
| 1298 |
} |
| 1299 |
return commitIterFunc(commit), nil |
| 1300 |
} |
| 1301 |
|
| 1302 |
func (r *Repository) logAll(commitIterFunc func(*object.Commit) object.CommitIter) (object.CommitIter, error) { |
| 1303 |
return object.NewCommitAllIter(r.Storer, commitIterFunc) |
| 1304 |
} |
| 1305 |
|
| 1306 |
func (*Repository) logWithFile(fileName string, commitIter object.CommitIter, checkParent bool) object.CommitIter { |
| 1307 |
return object.NewCommitPathIterFromIter( |
| 1308 |
func(path string) bool { |
| 1309 |
return path == fileName |
| 1310 |
}, |
| 1311 |
commitIter, |
| 1312 |
checkParent, |
| 1313 |
) |
| 1314 |
} |
| 1315 |
|
| 1316 |
func (*Repository) logWithPathFilter(pathFilter func(string) bool, commitIter object.CommitIter, checkParent bool) object.CommitIter { |
| 1317 |
return object.NewCommitPathIterFromIter( |
| 1318 |
pathFilter, |
| 1319 |
commitIter, |
| 1320 |
checkParent, |
| 1321 |
) |
| 1322 |
} |
| 1323 |
|
| 1324 |
func (*Repository) logWithLimit(commitIter object.CommitIter, limitOptions object.LogLimitOptions) object.CommitIter { |
| 1325 |
return object.NewCommitLimitIterFromIter(commitIter, limitOptions) |
| 1326 |
} |
| 1327 |
|
| 1328 |
func commitIterFunc(order LogOrder) func(c *object.Commit) object.CommitIter { |
| 1329 |
switch order { |
| 1330 |
case LogOrderDefault: |
| 1331 |
return func(c *object.Commit) object.CommitIter { |
| 1332 |
return object.NewCommitPreorderIter(c, nil, nil) |
| 1333 |
} |
| 1334 |
case LogOrderDFS: |
| 1335 |
return func(c *object.Commit) object.CommitIter { |
| 1336 |
return object.NewCommitPreorderIter(c, nil, nil) |
| 1337 |
} |
| 1338 |
case LogOrderDFSPost: |
| 1339 |
return func(c *object.Commit) object.CommitIter { |
| 1340 |
return object.NewCommitPostorderIter(c, nil) |
| 1341 |
} |
| 1342 |
case LogOrderBSF: |
| 1343 |
return func(c *object.Commit) object.CommitIter { |
| 1344 |
return object.NewCommitIterBSF(c, nil, nil) |
| 1345 |
} |
| 1346 |
case LogOrderCommitterTime: |
| 1347 |
return func(c *object.Commit) object.CommitIter { |
| 1348 |
return object.NewCommitIterCTime(c, nil, nil) |
| 1349 |
} |
| 1350 |
} |
| 1351 |
return nil |
| 1352 |
} |
| 1353 |
|
| 1354 |
// Tags returns all the tag References in a repository. |
| 1355 |
// |
| 1356 |
// If you want to check to see if the tag is an annotated tag, you can call |
| 1357 |
// TagObject on the hash Reference passed in through ForEach: |
| 1358 |
// |
| 1359 |
// iter, err := r.Tags() |
| 1360 |
// if err != nil { |
| 1361 |
// // Handle error |
| 1362 |
// } |
| 1363 |
// |
| 1364 |
// if err := iter.ForEach(func (ref *plumbing.Reference) error { |
| 1365 |
// obj, err := r.TagObject(ref.Hash()) |
| 1366 |
// switch err { |
| 1367 |
// case nil: |
| 1368 |
// // Tag object present |
| 1369 |
// case plumbing.ErrObjectNotFound: |
| 1370 |
// // Not a tag object |
| 1371 |
// default: |
| 1372 |
// // Some other error |
| 1373 |
// return err |
| 1374 |
// } |
| 1375 |
// }); err != nil { |
| 1376 |
// // Handle outer iterator error |
| 1377 |
// } |
| 1378 |
func (r *Repository) Tags() (storer.ReferenceIter, error) { |
| 1379 |
refIter, err := r.Storer.IterReferences() |
| 1380 |
if err != nil { |
| 1381 |
return nil, err |
| 1382 |
} |
| 1383 |
|
| 1384 |
return storer.NewReferenceFilteredIter( |
| 1385 |
func(r *plumbing.Reference) bool { |
| 1386 |
return r.Name().IsTag() |
| 1387 |
}, refIter), nil |
| 1388 |
} |
| 1389 |
|
| 1390 |
// Branches returns all the References that are Branches. |
| 1391 |
func (r *Repository) Branches() (storer.ReferenceIter, error) { |
| 1392 |
refIter, err := r.Storer.IterReferences() |
| 1393 |
if err != nil { |
| 1394 |
return nil, err |
| 1395 |
} |
| 1396 |
|
| 1397 |
return storer.NewReferenceFilteredIter( |
| 1398 |
func(r *plumbing.Reference) bool { |
| 1399 |
return r.Name().IsBranch() |
| 1400 |
}, refIter), nil |
| 1401 |
} |
| 1402 |
|
| 1403 |
// Notes returns all the References that are notes. For more information: |
| 1404 |
// https://git-scm.com/docs/git-notes |
| 1405 |
func (r *Repository) Notes() (storer.ReferenceIter, error) { |
| 1406 |
refIter, err := r.Storer.IterReferences() |
| 1407 |
if err != nil { |
| 1408 |
return nil, err |
| 1409 |
} |
| 1410 |
|
| 1411 |
return storer.NewReferenceFilteredIter( |
| 1412 |
func(r *plumbing.Reference) bool { |
| 1413 |
return r.Name().IsNote() |
| 1414 |
}, refIter), nil |
| 1415 |
} |
| 1416 |
|
| 1417 |
// TreeObject return a Tree with the given hash. If not found |
| 1418 |
// plumbing.ErrObjectNotFound is returned |
| 1419 |
func (r *Repository) TreeObject(h plumbing.Hash) (*object.Tree, error) { |
| 1420 |
return object.GetTree(r.Storer, h) |
| 1421 |
} |
| 1422 |
|
| 1423 |
// TreeObjects returns an unsorted TreeIter with all the trees in the repository |
| 1424 |
func (r *Repository) TreeObjects() (*object.TreeIter, error) { |
| 1425 |
iter, err := r.Storer.IterEncodedObjects(plumbing.TreeObject) |
| 1426 |
if err != nil { |
| 1427 |
return nil, err |
| 1428 |
} |
| 1429 |
|
| 1430 |
return object.NewTreeIter(r.Storer, iter), nil |
| 1431 |
} |
| 1432 |
|
| 1433 |
// CommitObject return a Commit with the given hash. If not found |
| 1434 |
// plumbing.ErrObjectNotFound is returned. |
| 1435 |
func (r *Repository) CommitObject(h plumbing.Hash) (*object.Commit, error) { |
| 1436 |
return object.GetCommit(r.Storer, h) |
| 1437 |
} |
| 1438 |
|
| 1439 |
// CommitObjects returns an unsorted CommitIter with all the commits in the repository. |
| 1440 |
func (r *Repository) CommitObjects() (object.CommitIter, error) { |
| 1441 |
iter, err := r.Storer.IterEncodedObjects(plumbing.CommitObject) |
| 1442 |
if err != nil { |
| 1443 |
return nil, err |
| 1444 |
} |
| 1445 |
|
| 1446 |
return object.NewCommitIter(r.Storer, iter), nil |
| 1447 |
} |
| 1448 |
|
| 1449 |
// BlobObject returns a Blob with the given hash. If not found |
| 1450 |
// plumbing.ErrObjectNotFound is returned. |
| 1451 |
func (r *Repository) BlobObject(h plumbing.Hash) (*object.Blob, error) { |
| 1452 |
return object.GetBlob(r.Storer, h) |
| 1453 |
} |
| 1454 |
|
| 1455 |
// BlobObjects returns an unsorted BlobIter with all the blobs in the repository. |
| 1456 |
func (r *Repository) BlobObjects() (*object.BlobIter, error) { |
| 1457 |
iter, err := r.Storer.IterEncodedObjects(plumbing.BlobObject) |
| 1458 |
if err != nil { |
| 1459 |
return nil, err |
| 1460 |
} |
| 1461 |
|
| 1462 |
return object.NewBlobIter(r.Storer, iter), nil |
| 1463 |
} |
| 1464 |
|
| 1465 |
// TagObject returns a Tag with the given hash. If not found |
| 1466 |
// plumbing.ErrObjectNotFound is returned. This method only returns |
| 1467 |
// annotated Tags, no lightweight Tags. |
| 1468 |
func (r *Repository) TagObject(h plumbing.Hash) (*object.Tag, error) { |
| 1469 |
return object.GetTag(r.Storer, h) |
| 1470 |
} |
| 1471 |
|
| 1472 |
// TagObjects returns a unsorted TagIter that can step through all of the annotated |
| 1473 |
// tags in the repository. |
| 1474 |
func (r *Repository) TagObjects() (*object.TagIter, error) { |
| 1475 |
iter, err := r.Storer.IterEncodedObjects(plumbing.TagObject) |
| 1476 |
if err != nil { |
| 1477 |
return nil, err |
| 1478 |
} |
| 1479 |
|
| 1480 |
return object.NewTagIter(r.Storer, iter), nil |
| 1481 |
} |
| 1482 |
|
| 1483 |
// Object returns an Object with the given hash. If not found |
| 1484 |
// plumbing.ErrObjectNotFound is returned. |
| 1485 |
func (r *Repository) Object(t plumbing.ObjectType, h plumbing.Hash) (object.Object, error) { |
| 1486 |
obj, err := r.Storer.EncodedObject(t, h) |
| 1487 |
if err != nil { |
| 1488 |
return nil, err |
| 1489 |
} |
| 1490 |
|
| 1491 |
return object.DecodeObject(r.Storer, obj) |
| 1492 |
} |
| 1493 |
|
| 1494 |
// Objects returns an unsorted ObjectIter with all the objects in the repository. |
| 1495 |
func (r *Repository) Objects() (*object.ObjectIter, error) { |
| 1496 |
iter, err := r.Storer.IterEncodedObjects(plumbing.AnyObject) |
| 1497 |
if err != nil { |
| 1498 |
return nil, err |
| 1499 |
} |
| 1500 |
|
| 1501 |
return object.NewObjectIter(r.Storer, iter), nil |
| 1502 |
} |
| 1503 |
|
| 1504 |
// Head returns the reference where HEAD is pointing to. |
| 1505 |
func (r *Repository) Head() (*plumbing.Reference, error) { |
| 1506 |
return storer.ResolveReference(r.Storer, plumbing.HEAD) |
| 1507 |
} |
| 1508 |
|
| 1509 |
// Reference returns the reference for a given reference name. If resolved is |
| 1510 |
// true, any symbolic reference will be resolved. |
| 1511 |
func (r *Repository) Reference(name plumbing.ReferenceName, resolved bool) ( |
| 1512 |
*plumbing.Reference, error) { |
| 1513 |
|
| 1514 |
if resolved { |
| 1515 |
return storer.ResolveReference(r.Storer, name) |
| 1516 |
} |
| 1517 |
|
| 1518 |
return r.Storer.Reference(name) |
| 1519 |
} |
| 1520 |
|
| 1521 |
// References returns an unsorted ReferenceIter for all references. |
| 1522 |
func (r *Repository) References() (storer.ReferenceIter, error) { |
| 1523 |
return r.Storer.IterReferences() |
| 1524 |
} |
| 1525 |
|
| 1526 |
// Worktree returns a worktree based on the given fs, if nil the default |
| 1527 |
// worktree will be used. |
| 1528 |
func (r *Repository) Worktree() (*Worktree, error) { |
| 1529 |
if r.wt == nil { |
| 1530 |
return nil, ErrIsBareRepository |
| 1531 |
} |
| 1532 |
|
| 1533 |
protectNTFS := defaultProtectNTFS() |
| 1534 |
protectHFS := defaultProtectHFS() |
| 1535 |
if cfg, err := r.Config(); err == nil { |
| 1536 |
if cfg.Core.ProtectNTFS.IsSet() { |
| 1537 |
protectNTFS = cfg.Core.ProtectNTFS.IsTrue() |
| 1538 |
} |
| 1539 |
if cfg.Core.ProtectHFS.IsSet() { |
| 1540 |
protectHFS = cfg.Core.ProtectHFS.IsTrue() |
| 1541 |
} |
| 1542 |
} |
| 1543 |
|
| 1544 |
return &Worktree{r: r, Filesystem: newWorktreeFilesystem(r.wt, protectNTFS, protectHFS)}, nil |
| 1545 |
} |
| 1546 |
|
| 1547 |
func expand_ref(s storer.ReferenceStorer, ref plumbing.ReferenceName) (*plumbing.Reference, error) { |
| 1548 |
// For improving troubleshooting, this preserves the error for the provided `ref`, |
| 1549 |
// and returns the error for that specific ref in case all parse rules fails. |
| 1550 |
var ret error |
| 1551 |
for _, rule := range plumbing.RefRevParseRules { |
| 1552 |
resolvedRef, err := storer.ResolveReference(s, plumbing.ReferenceName(fmt.Sprintf(rule, ref))) |
| 1553 |
|
| 1554 |
if err == nil { |
| 1555 |
return resolvedRef, nil |
| 1556 |
} else if ret == nil { |
| 1557 |
ret = err |
| 1558 |
} |
| 1559 |
} |
| 1560 |
|
| 1561 |
return nil, ret |
| 1562 |
} |
| 1563 |
|
| 1564 |
// ResolveRevision resolves revision to corresponding hash. It will always |
| 1565 |
// resolve to a commit hash, not a tree or annotated tag. |
| 1566 |
// |
| 1567 |
// Implemented resolvers : HEAD, branch, tag, heads/branch, refs/heads/branch, |
| 1568 |
// refs/tags/tag, refs/remotes/origin/branch, refs/remotes/origin/HEAD, tilde and caret (HEAD~1, master~^, tag~2, ref/heads/master~1, ...), selection by text (HEAD^{/fix nasty bug}), hash (prefix and full) |
| 1569 |
func (r *Repository) ResolveRevision(in plumbing.Revision) (*plumbing.Hash, error) { |
| 1570 |
rev := in.String() |
| 1571 |
if rev == "" { |
| 1572 |
return &plumbing.ZeroHash, plumbing.ErrReferenceNotFound |
| 1573 |
} |
| 1574 |
|
| 1575 |
p := revision.NewParserFromString(rev) |
| 1576 |
items, err := p.Parse() |
| 1577 |
|
| 1578 |
if err != nil { |
| 1579 |
return nil, err |
| 1580 |
} |
| 1581 |
|
| 1582 |
var commit *object.Commit |
| 1583 |
|
| 1584 |
for _, item := range items { |
| 1585 |
switch item := item.(type) { |
| 1586 |
case revision.Ref: |
| 1587 |
revisionRef := item |
| 1588 |
|
| 1589 |
var tryHashes []plumbing.Hash |
| 1590 |
|
| 1591 |
tryHashes = append(tryHashes, r.resolveHashPrefix(string(revisionRef))...) |
| 1592 |
|
| 1593 |
ref, err := expand_ref(r.Storer, plumbing.ReferenceName(revisionRef)) |
| 1594 |
if err == nil { |
| 1595 |
tryHashes = append(tryHashes, ref.Hash()) |
| 1596 |
} |
| 1597 |
|
| 1598 |
// in ambiguous cases, `git rev-parse` will emit a warning, but |
| 1599 |
// will always return the oid in preference to a ref; we don't have |
| 1600 |
// the ability to emit a warning here, so (for speed purposes) |
| 1601 |
// don't bother to detect the ambiguity either, just return in the |
| 1602 |
// priority that git would. |
| 1603 |
gotOne := false |
| 1604 |
for _, hash := range tryHashes { |
| 1605 |
commitObj, err := r.CommitObject(hash) |
| 1606 |
if err == nil { |
| 1607 |
commit = commitObj |
| 1608 |
gotOne = true |
| 1609 |
break |
| 1610 |
} |
| 1611 |
|
| 1612 |
tagObj, err := r.TagObject(hash) |
| 1613 |
if err == nil { |
| 1614 |
// If the tag target lookup fails here, this most likely |
| 1615 |
// represents some sort of repo corruption, so let the |
| 1616 |
// error bubble up. |
| 1617 |
tagCommit, err := tagObj.Commit() |
| 1618 |
if err != nil { |
| 1619 |
return &plumbing.ZeroHash, err |
| 1620 |
} |
| 1621 |
commit = tagCommit |
| 1622 |
gotOne = true |
| 1623 |
break |
| 1624 |
} |
| 1625 |
} |
| 1626 |
|
| 1627 |
if !gotOne { |
| 1628 |
return &plumbing.ZeroHash, plumbing.ErrReferenceNotFound |
| 1629 |
} |
| 1630 |
|
| 1631 |
case revision.CaretPath: |
| 1632 |
depth := item.Depth |
| 1633 |
|
| 1634 |
if depth == 0 { |
| 1635 |
break |
| 1636 |
} |
| 1637 |
|
| 1638 |
iter := commit.Parents() |
| 1639 |
|
| 1640 |
c, err := iter.Next() |
| 1641 |
|
| 1642 |
if err != nil { |
| 1643 |
return &plumbing.ZeroHash, err |
| 1644 |
} |
| 1645 |
|
| 1646 |
if depth == 1 { |
| 1647 |
commit = c |
| 1648 |
|
| 1649 |
break |
| 1650 |
} |
| 1651 |
|
| 1652 |
c, err = iter.Next() |
| 1653 |
|
| 1654 |
if err != nil { |
| 1655 |
return &plumbing.ZeroHash, err |
| 1656 |
} |
| 1657 |
|
| 1658 |
commit = c |
| 1659 |
case revision.TildePath: |
| 1660 |
for i := 0; i < item.Depth; i++ { |
| 1661 |
c, err := commit.Parents().Next() |
| 1662 |
|
| 1663 |
if err != nil { |
| 1664 |
return &plumbing.ZeroHash, err |
| 1665 |
} |
| 1666 |
|
| 1667 |
commit = c |
| 1668 |
} |
| 1669 |
case revision.CaretReg: |
| 1670 |
history := object.NewCommitPreorderIter(commit, nil, nil) |
| 1671 |
|
| 1672 |
re := item.Regexp |
| 1673 |
negate := item.Negate |
| 1674 |
|
| 1675 |
var c *object.Commit |
| 1676 |
|
| 1677 |
err := history.ForEach(func(hc *object.Commit) error { |
| 1678 |
if !negate && re.MatchString(hc.Message) { |
| 1679 |
c = hc |
| 1680 |
return storer.ErrStop |
| 1681 |
} |
| 1682 |
|
| 1683 |
if negate && !re.MatchString(hc.Message) { |
| 1684 |
c = hc |
| 1685 |
return storer.ErrStop |
| 1686 |
} |
| 1687 |
|
| 1688 |
return nil |
| 1689 |
}) |
| 1690 |
if err != nil { |
| 1691 |
return &plumbing.ZeroHash, err |
| 1692 |
} |
| 1693 |
|
| 1694 |
if c == nil { |
| 1695 |
return &plumbing.ZeroHash, fmt.Errorf("no commit message match regexp: %q", re.String()) |
| 1696 |
} |
| 1697 |
|
| 1698 |
commit = c |
| 1699 |
} |
| 1700 |
} |
| 1701 |
|
| 1702 |
if commit == nil { |
| 1703 |
return &plumbing.ZeroHash, plumbing.ErrReferenceNotFound |
| 1704 |
} |
| 1705 |
|
| 1706 |
return &commit.Hash, nil |
| 1707 |
} |
| 1708 |
|
| 1709 |
// resolveHashPrefix returns a list of potential hashes that the given string |
| 1710 |
// is a prefix of. It quietly swallows errors, returning nil. |
| 1711 |
func (r *Repository) resolveHashPrefix(hashStr string) []plumbing.Hash { |
| 1712 |
// Handle complete and partial hashes. |
| 1713 |
// plumbing.NewHash forces args into a full 20 byte hash, which isn't suitable |
| 1714 |
// for partial hashes since they will become zero-filled. |
| 1715 |
|
| 1716 |
if hashStr == "" { |
| 1717 |
return nil |
| 1718 |
} |
| 1719 |
if len(hashStr) == len(plumbing.ZeroHash)*2 { |
| 1720 |
// Only a full hash is possible. |
| 1721 |
hexb, err := hex.DecodeString(hashStr) |
| 1722 |
if err != nil { |
| 1723 |
return nil |
| 1724 |
} |
| 1725 |
var h plumbing.Hash |
| 1726 |
copy(h[:], hexb) |
| 1727 |
return []plumbing.Hash{h} |
| 1728 |
} |
| 1729 |
|
| 1730 |
// Partial hash. |
| 1731 |
// hex.DecodeString only decodes to complete bytes, so only works with pairs of hex digits. |
| 1732 |
evenHex := hashStr[:len(hashStr)&^1] |
| 1733 |
hexb, err := hex.DecodeString(evenHex) |
| 1734 |
if err != nil { |
| 1735 |
return nil |
| 1736 |
} |
| 1737 |
candidates := expandPartialHash(r.Storer, hexb) |
| 1738 |
if len(evenHex) == len(hashStr) { |
| 1739 |
// The prefix was an exact number of bytes. |
| 1740 |
return candidates |
| 1741 |
} |
| 1742 |
// Do another prefix check to ensure the dangling nybble is correct. |
| 1743 |
var hashes []plumbing.Hash |
| 1744 |
for _, h := range candidates { |
| 1745 |
if strings.HasPrefix(h.String(), hashStr) { |
| 1746 |
hashes = append(hashes, h) |
| 1747 |
} |
| 1748 |
} |
| 1749 |
return hashes |
| 1750 |
} |
| 1751 |
|
| 1752 |
type RepackConfig struct { |
| 1753 |
// UseRefDeltas configures whether packfile encoder will use reference deltas. |
| 1754 |
// By default OFSDeltaObject is used. |
| 1755 |
UseRefDeltas bool |
| 1756 |
// OnlyDeletePacksOlderThan if set to non-zero value |
| 1757 |
// selects only objects older than the time provided. |
| 1758 |
OnlyDeletePacksOlderThan time.Time |
| 1759 |
} |
| 1760 |
|
| 1761 |
func (r *Repository) RepackObjects(cfg *RepackConfig) (err error) { |
| 1762 |
pos, ok := r.Storer.(storer.PackedObjectStorer) |
| 1763 |
if !ok { |
| 1764 |
return ErrPackedObjectsNotSupported |
| 1765 |
} |
| 1766 |
|
| 1767 |
// Get the existing object packs. |
| 1768 |
hs, err := pos.ObjectPacks() |
| 1769 |
if err != nil { |
| 1770 |
return err |
| 1771 |
} |
| 1772 |
|
| 1773 |
// Create a new pack. |
| 1774 |
nh, err := r.createNewObjectPack(cfg) |
| 1775 |
if err != nil { |
| 1776 |
return err |
| 1777 |
} |
| 1778 |
|
| 1779 |
// Delete old packs. |
| 1780 |
for _, h := range hs { |
| 1781 |
// Skip if new hash is the same as an old one. |
| 1782 |
if h == nh { |
| 1783 |
continue |
| 1784 |
} |
| 1785 |
err = pos.DeleteOldObjectPackAndIndex(h, cfg.OnlyDeletePacksOlderThan) |
| 1786 |
if err != nil { |
| 1787 |
return err |
| 1788 |
} |
| 1789 |
} |
| 1790 |
|
| 1791 |
return nil |
| 1792 |
} |
| 1793 |
|
| 1794 |
// Merge merges the reference branch into the current branch. |
| 1795 |
// |
| 1796 |
// If the merge is not possible (or supported) returns an error without changing |
| 1797 |
// the HEAD for the current branch. Possible errors include: |
| 1798 |
// - The merge strategy is not supported. |
| 1799 |
// - The specific strategy cannot be used (e.g. using FastForwardMerge when one is not possible). |
| 1800 |
func (r *Repository) Merge(ref plumbing.Reference, opts MergeOptions) error { |
| 1801 |
if opts.Strategy != FastForwardMerge { |
| 1802 |
return ErrUnsupportedMergeStrategy |
| 1803 |
} |
| 1804 |
|
| 1805 |
// Ignore error as not having a shallow list is optional here. |
| 1806 |
shallowList, _ := r.Storer.Shallow() |
| 1807 |
var earliestShallow *plumbing.Hash |
| 1808 |
if len(shallowList) > 0 { |
| 1809 |
earliestShallow = &shallowList[0] |
| 1810 |
} |
| 1811 |
|
| 1812 |
head, err := r.Head() |
| 1813 |
if err != nil { |
| 1814 |
return err |
| 1815 |
} |
| 1816 |
|
| 1817 |
ff, err := isFastForward(r.Storer, head.Hash(), ref.Hash(), earliestShallow) |
| 1818 |
if err != nil { |
| 1819 |
return err |
| 1820 |
} |
| 1821 |
|
| 1822 |
if !ff { |
| 1823 |
return ErrFastForwardMergeNotPossible |
| 1824 |
} |
| 1825 |
|
| 1826 |
return r.Storer.SetReference(plumbing.NewHashReference(head.Name(), ref.Hash())) |
| 1827 |
} |
| 1828 |
|
| 1829 |
// createNewObjectPack is a helper for RepackObjects taking care |
| 1830 |
// of creating a new pack. It is used so the PackfileWriter |
| 1831 |
// deferred close has the right scope. |
| 1832 |
func (r *Repository) createNewObjectPack(cfg *RepackConfig) (h plumbing.Hash, err error) { |
| 1833 |
ow := newObjectWalker(r.Storer) |
| 1834 |
err = ow.walkAllRefs() |
| 1835 |
if err != nil { |
| 1836 |
return h, err |
| 1837 |
} |
| 1838 |
objs := make([]plumbing.Hash, 0, len(ow.seen)) |
| 1839 |
for h := range ow.seen { |
| 1840 |
objs = append(objs, h) |
| 1841 |
} |
| 1842 |
pfw, ok := r.Storer.(storer.PackfileWriter) |
| 1843 |
if !ok { |
| 1844 |
return h, fmt.Errorf("Repository storer is not a storer.PackfileWriter") |
| 1845 |
} |
| 1846 |
wc, err := pfw.PackfileWriter() |
| 1847 |
if err != nil { |
| 1848 |
return h, err |
| 1849 |
} |
| 1850 |
defer ioutil.CheckClose(wc, &err) |
| 1851 |
scfg, err := r.Config() |
| 1852 |
if err != nil { |
| 1853 |
return h, err |
| 1854 |
} |
| 1855 |
enc := packfile.NewEncoder(wc, r.Storer, cfg.UseRefDeltas) |
| 1856 |
h, err = enc.Encode(objs, scfg.Pack.Window) |
| 1857 |
if err != nil { |
| 1858 |
return h, err |
| 1859 |
} |
| 1860 |
|
| 1861 |
// Delete the packed, loose objects. |
| 1862 |
if los, ok := r.Storer.(storer.LooseObjectStorer); ok { |
| 1863 |
err = los.ForEachObjectHash(func(hash plumbing.Hash) error { |
| 1864 |
if ow.isSeen(hash) { |
| 1865 |
err = los.DeleteLooseObject(hash) |
| 1866 |
if err != nil { |
| 1867 |
return err |
| 1868 |
} |
| 1869 |
} |
| 1870 |
return nil |
| 1871 |
}) |
| 1872 |
if err != nil { |
| 1873 |
return h, err |
| 1874 |
} |
| 1875 |
} |
| 1876 |
|
| 1877 |
return h, err |
| 1878 |
} |
| 1879 |
|
| 1880 |
func expandPartialHash(st storer.EncodedObjectStorer, prefix []byte) (hashes []plumbing.Hash) { |
| 1881 |
// The fast version is implemented by storage/filesystem.ObjectStorage. |
| 1882 |
type fastIter interface { |
| 1883 |
HashesWithPrefix(prefix []byte) ([]plumbing.Hash, error) |
| 1884 |
} |
| 1885 |
if fi, ok := st.(fastIter); ok { |
| 1886 |
h, err := fi.HashesWithPrefix(prefix) |
| 1887 |
if err != nil { |
| 1888 |
return nil |
| 1889 |
} |
| 1890 |
return h |
| 1891 |
} |
| 1892 |
|
| 1893 |
// Slow path. |
| 1894 |
iter, err := st.IterEncodedObjects(plumbing.AnyObject) |
| 1895 |
if err != nil { |
| 1896 |
return nil |
| 1897 |
} |
| 1898 |
iter.ForEach(func(obj plumbing.EncodedObject) error { |
| 1899 |
h := obj.Hash() |
| 1900 |
if bytes.HasPrefix(h[:], prefix) { |
| 1901 |
hashes = append(hashes, h) |
| 1902 |
} |
| 1903 |
return nil |
| 1904 |
}) |
| 1905 |
return |
| 1906 |
} |