How to use fetchRemote method of vcs Package

Best Syzkaller code snippet using vcs.fetchRemote

repo.go

Source:repo.go Github

copy

Full Screen

...536 }537 if headDir == r.Dir {538 return r.Diff(base, head, opt)539 }540 if err := r.fetchRemote(headDir); err != nil {541 return nil, err542 }543 return r.Diff(base, head, opt)544}545func (r *Repository) fetchRemote(repoDir string) error {546 r.editLock.Lock()547 defer r.editLock.Unlock()548 name := base64.URLEncoding.EncodeToString([]byte(repoDir))549 // Fetch remote commit data.550 cmd := exec.Command("git", "fetch", "-v", filepath.ToSlash(repoDir), "+refs/heads/*:refs/remotes/"+name+"/*")551 cmd.Dir = r.Dir552 out, err := cmd.CombinedOutput()553 if err != nil {554 return fmt.Errorf("exec %v in %s failed: %s. Output was:\n\n%s", cmd.Args, cmd.Dir, err, out)555 }556 return nil557}558func (r *Repository) UpdateEverything(opt vcs.RemoteOpts) (*vcs.UpdateResult, error) {559 r.editLock.Lock()560 defer r.editLock.Unlock()561 cmd := exec.Command("git", "remote", "update", "--prune")562 cmd.Dir = r.Dir563 if opt.SSH != nil {564 gitSSHWrapper, gitSSHWrapperDir, keyFile, err := makeGitSSHWrapper(opt.SSH.PrivateKey)565 defer func() {566 if keyFile != "" {567 if err := os.Remove(keyFile); err != nil {568 log.Fatalf("Error removing SSH key file %s: %s.", keyFile, err)569 }570 }571 }()572 if err != nil {573 return nil, err574 }575 defer os.Remove(gitSSHWrapper)576 if gitSSHWrapperDir != "" {577 defer os.RemoveAll(gitSSHWrapperDir)578 }579 cmd.Env = []string{"GIT_SSH=" + gitSSHWrapper}580 }581 if opt.HTTPS != nil {582 env := environ(os.Environ())583 env.Unset("GIT_TERMINAL_PROMPT")584 gitPassHelper, gitPassHelperDir, err := makeGitPassHelper(opt.HTTPS.Pass)585 if err != nil {586 return nil, err587 }588 defer os.Remove(gitPassHelper)589 if gitPassHelperDir != "" {590 defer os.RemoveAll(gitPassHelperDir)591 }592 env = append(env, "GIT_ASKPASS="+gitPassHelper)593 cmd.Env = env594 }595 var stderr bytes.Buffer596 cmd.Stderr = &stderr597 err := cmd.Run()598 if err != nil {599 return nil, fmt.Errorf("exec `git remote update` failed: %v. Stderr was:\n\n%s", err, stderr.String())600 }601 result, err := parseRemoteUpdate(stderr.Bytes())602 if err != nil {603 return nil, fmt.Errorf("parsing output of `git remote update` failed: %v", err)604 }605 return &result, nil606}607func (r *Repository) BlameFile(path string, opt *vcs.BlameOptions) ([]*vcs.Hunk, error) {608 r.editLock.RLock()609 defer r.editLock.RUnlock()610 if opt == nil {611 opt = &vcs.BlameOptions{}612 }613 if opt.OldestCommit != "" {614 return nil, fmt.Errorf("OldestCommit not implemented")615 }616 if err := checkSpecArgSafety(string(opt.NewestCommit)); err != nil {617 return nil, err618 }619 if err := checkSpecArgSafety(string(opt.OldestCommit)); err != nil {620 return nil, err621 }622 args := []string{"blame", "-w", "--porcelain"}623 if opt.StartLine != 0 || opt.EndLine != 0 {624 args = append(args, fmt.Sprintf("-L%d,%d", opt.StartLine, opt.EndLine))625 }626 args = append(args, string(opt.NewestCommit), "--", filepath.ToSlash(path))627 cmd := exec.Command("git", args...)628 cmd.Dir = r.Dir629 out, err := cmd.CombinedOutput()630 if err != nil {631 return nil, fmt.Errorf("exec `git blame` failed: %s. Output was:\n\n%s", err, out)632 }633 if len(out) < 1 {634 // go 1.8.5 changed the behavior of `git blame` on empty files.635 // previously, it returned a boundary commit. now, it returns nothing.636 // TODO(sqs) TODO(beyang): make `git blame` return the boundary commit637 // on an empty file somehow, or come up with some other workaround.638 st, err := os.Stat(filepath.Join(r.Dir, path))639 if err == nil && st.Size() == 0 {640 return nil, nil641 }642 return nil, fmt.Errorf("Expected git output of length at least 1")643 }644 commits := make(map[string]vcs.Commit)645 hunks := make([]*vcs.Hunk, 0)646 remainingLines := strings.Split(string(out[:len(out)-1]), "\n")647 byteOffset := 0648 for len(remainingLines) > 0 {649 // Consume hunk650 hunkHeader := strings.Split(remainingLines[0], " ")651 if len(hunkHeader) != 4 {652 fmt.Printf("Remaining lines: %+v, %d, '%s'\n", remainingLines, len(remainingLines), remainingLines[0])653 return nil, fmt.Errorf("Expected at least 4 parts to hunkHeader, but got: '%s'", hunkHeader)654 }655 commitID := hunkHeader[0]656 lineNoCur, _ := strconv.Atoi(hunkHeader[2])657 nLines, _ := strconv.Atoi(hunkHeader[3])658 hunk := &vcs.Hunk{659 CommitID: vcs.CommitID(commitID),660 StartLine: int(lineNoCur),661 EndLine: int(lineNoCur + nLines),662 StartByte: byteOffset,663 }664 if _, in := commits[commitID]; in {665 // Already seen commit666 byteOffset += len(remainingLines[1])667 remainingLines = remainingLines[2:]668 } else {669 // New commit670 author := strings.Join(strings.Split(remainingLines[1], " ")[1:], " ")671 email := strings.Join(strings.Split(remainingLines[2], " ")[1:], " ")672 if len(email) >= 2 && email[0] == '<' && email[len(email)-1] == '>' {673 email = email[1 : len(email)-1]674 }675 authorTime, err := strconv.ParseInt(strings.Join(strings.Split(remainingLines[3], " ")[1:], " "), 10, 64)676 if err != nil {677 return nil, fmt.Errorf("Failed to parse author-time %q", remainingLines[3])678 }679 summary := strings.Join(strings.Split(remainingLines[9], " ")[1:], " ")680 commit := vcs.Commit{681 ID: vcs.CommitID(commitID),682 Message: summary,683 Author: vcs.Signature{684 Name: author,685 Email: email,686 Date: pbtypes.NewTimestamp(time.Unix(authorTime, 0).In(time.UTC)),687 },688 }689 if len(remainingLines) >= 13 && strings.HasPrefix(remainingLines[10], "previous ") {690 byteOffset += len(remainingLines[12])691 remainingLines = remainingLines[13:]692 } else if len(remainingLines) >= 13 && remainingLines[10] == "boundary" {693 byteOffset += len(remainingLines[12])694 remainingLines = remainingLines[13:]695 } else if len(remainingLines) >= 12 {696 byteOffset += len(remainingLines[11])697 remainingLines = remainingLines[12:]698 } else if len(remainingLines) == 11 {699 // Empty file700 remainingLines = remainingLines[11:]701 } else {702 return nil, fmt.Errorf("Unexpected number of remaining lines (%d):\n%s", len(remainingLines), " "+strings.Join(remainingLines, "\n "))703 }704 commits[commitID] = commit705 }706 if commit, present := commits[commitID]; present {707 // Should always be present, but check just to avoid708 // panicking in case of a (somewhat likely) bug in our709 // git-blame parser above.710 hunk.CommitID = commit.ID711 hunk.Author = commit.Author712 }713 // Consume remaining lines in hunk714 for i := 1; i < nLines; i++ {715 byteOffset += len(remainingLines[1])716 remainingLines = remainingLines[2:]717 }718 hunk.EndByte = byteOffset719 hunks = append(hunks, hunk)720 }721 return hunks, nil722}723func (r *Repository) MergeBase(a, b vcs.CommitID) (vcs.CommitID, error) {724 r.editLock.RLock()725 defer r.editLock.RUnlock()726 cmd := exec.Command("git", "merge-base", "--", string(a), string(b))727 cmd.Dir = r.Dir728 out, err := cmd.CombinedOutput()729 if err != nil {730 return "", fmt.Errorf("exec %v failed: %s. Output was:\n\n%s", cmd.Args, err, out)731 }732 return vcs.CommitID(bytes.TrimSpace(out)), nil733}734func (r *Repository) CrossRepoMergeBase(a vcs.CommitID, repoB vcs.Repository, b vcs.CommitID) (vcs.CommitID, error) {735 // git.Repository inherits GitRootDir and CrossRepo from its736 // embedded gitcmd.Repository.737 var repoBDir string // path to head repo on local filesystem738 if repoB, ok := repoB.(CrossRepo); ok {739 repoBDir = repoB.GitRootDir()740 } else {741 return "", fmt.Errorf("git cross-repo merge-base not supported against repo type %T", repoB)742 }743 if repoBDir != r.Dir {744 if err := r.fetchRemote(repoBDir); err != nil {745 return "", err746 }747 }748 return r.MergeBase(a, b)749}750func (r *Repository) Search(at vcs.CommitID, opt vcs.SearchOptions) ([]*vcs.SearchResult, error) {751 if err := checkSpecArgSafety(string(at)); err != nil {752 return nil, err753 }754 var queryType string755 switch opt.QueryType {756 case vcs.FixedQuery:757 queryType = "--fixed-strings"758 default:...

Full Screen

Full Screen

remote.go

Source:remote.go Github

copy

Full Screen

1package localrepo2import (3 "bufio"4 "bytes"5 "context"6 "errors"7 "fmt"8 "io"9 "strings"10 "gitlab.com/gitlab-org/gitaly/v14/internal/git"11 "gitlab.com/gitlab-org/gitaly/v14/internal/gitalyssh"12 "gitlab.com/gitlab-org/gitaly/v14/proto/go/gitalypb"13)14// Remote provides functionality of the 'remote' git sub-command.15type Remote struct {16 repo *Repo17}18// Add adds a new remote to the repository.19func (remote Remote) Add(ctx context.Context, name, url string, opts git.RemoteAddOpts) error {20 if err := validateNotBlank(name, "name"); err != nil {21 return err22 }23 if err := validateNotBlank(url, "url"); err != nil {24 return err25 }26 var stderr bytes.Buffer27 if err := remote.repo.ExecAndWait(ctx,28 git.SubSubCmd{29 Name: "remote",30 Action: "add",31 Flags: buildRemoteAddOptsFlags(opts),32 Args: []string{name, url},33 },34 git.WithStderr(&stderr),35 git.WithRefTxHook(ctx, remote.repo, remote.repo.cfg),36 ); err != nil {37 switch {38 case isExitWithCode(err, 3):39 // In Git v2.30.0 and newer (https://gitlab.com/git-vcs/git/commit/9144ba4cf52)40 return git.ErrAlreadyExists41 case isExitWithCode(err, 128) && bytes.HasPrefix(stderr.Bytes(), []byte("fatal: remote "+name+" already exists")):42 // ..in older versions we parse stderr43 return git.ErrAlreadyExists44 }45 return err46 }47 return nil48}49func buildRemoteAddOptsFlags(opts git.RemoteAddOpts) []git.Option {50 var flags []git.Option51 for _, b := range opts.RemoteTrackingBranches {52 flags = append(flags, git.ValueFlag{Name: "-t", Value: b})53 }54 if opts.DefaultBranch != "" {55 flags = append(flags, git.ValueFlag{Name: "-m", Value: opts.DefaultBranch})56 }57 if opts.Fetch {58 flags = append(flags, git.Flag{Name: "-f"})59 }60 if opts.Tags != git.RemoteAddOptsTagsDefault {61 flags = append(flags, git.Flag{Name: opts.Tags.String()})62 }63 if opts.Mirror != git.RemoteAddOptsMirrorDefault {64 flags = append(flags, git.ValueFlag{Name: "--mirror", Value: opts.Mirror.String()})65 }66 return flags67}68// Remove removes a named remote from the repository configuration.69func (remote Remote) Remove(ctx context.Context, name string) error {70 if err := validateNotBlank(name, "name"); err != nil {71 return err72 }73 var stderr bytes.Buffer74 if err := remote.repo.ExecAndWait(ctx,75 git.SubSubCmd{76 Name: "remote",77 Action: "remove",78 Args: []string{name},79 },80 git.WithStderr(&stderr),81 git.WithRefTxHook(ctx, remote.repo, remote.repo.cfg),82 ); err != nil {83 switch {84 case isExitWithCode(err, 2):85 // In Git v2.30.0 and newer (https://gitlab.com/git-vcs/git/commit/9144ba4cf52)86 return git.ErrNotFound87 case isExitWithCode(err, 128) && strings.HasPrefix(stderr.String(), "fatal: No such remote"):88 // ..in older versions we parse stderr89 return git.ErrNotFound90 }91 return err92 }93 return nil94}95// SetURL sets the URL for a given remote.96func (remote Remote) SetURL(ctx context.Context, name, url string, opts git.SetURLOpts) error {97 if err := validateNotBlank(name, "name"); err != nil {98 return err99 }100 if err := validateNotBlank(url, "url"); err != nil {101 return err102 }103 var stderr bytes.Buffer104 if err := remote.repo.ExecAndWait(ctx,105 git.SubSubCmd{106 Name: "remote",107 Action: "set-url",108 Flags: buildSetURLOptsFlags(opts),109 Args: []string{name, url},110 },111 git.WithStderr(&stderr),112 git.WithRefTxHook(ctx, remote.repo, remote.repo.cfg),113 ); err != nil {114 switch {115 case isExitWithCode(err, 2):116 // In Git v2.30.0 and newer (https://gitlab.com/git-vcs/git/commit/9144ba4cf52)117 return git.ErrNotFound118 case isExitWithCode(err, 128) && strings.HasPrefix(stderr.String(), "fatal: No such remote"):119 // ..in older versions we parse stderr120 return git.ErrNotFound121 }122 return err123 }124 return nil125}126// Exists determines whether a given named remote exists.127func (remote Remote) Exists(ctx context.Context, name string) (bool, error) {128 cmd, err := remote.repo.Exec(ctx,129 git.SubCmd{Name: "remote"},130 git.WithRefTxHook(ctx, remote.repo, remote.repo.cfg),131 )132 if err != nil {133 return false, err134 }135 found := false136 scanner := bufio.NewScanner(cmd)137 for scanner.Scan() {138 if scanner.Text() == name {139 found = true140 break141 }142 }143 return found, cmd.Wait()144}145func buildSetURLOptsFlags(opts git.SetURLOpts) []git.Option {146 if opts.Push {147 return []git.Option{git.Flag{Name: "--push"}}148 }149 return nil150}151// FetchOptsTags controls what tags needs to be imported on fetch.152type FetchOptsTags string153func (t FetchOptsTags) String() string {154 return string(t)155}156var (157 // FetchOptsTagsDefault enables importing of tags only on fetched branches.158 FetchOptsTagsDefault = FetchOptsTags("")159 // FetchOptsTagsAll enables importing of every tag from the remote repository.160 FetchOptsTagsAll = FetchOptsTags("--tags")161 // FetchOptsTagsNone disables importing of tags from the remote repository.162 FetchOptsTagsNone = FetchOptsTags("--no-tags")163)164// FetchOpts is used to configure invocation of the 'FetchRemote' command.165type FetchOpts struct {166 // Env is a list of env vars to pass to the cmd.167 Env []string168 // CommandOptions is a list of options to use with 'git' command.169 CommandOptions []git.CmdOpt170 // Prune if set fetch removes any remote-tracking references that no longer exist on the remote.171 // https://git-scm.com/docs/git-fetch#Documentation/git-fetch.txt---prune172 Prune bool173 // Force if set fetch overrides local references with values from remote that's174 // doesn't have the previous commit as an ancestor.175 // https://git-scm.com/docs/git-fetch#Documentation/git-fetch.txt---force176 Force bool177 // Verbose controls how much information is written to stderr. The list of178 // refs updated by the fetch will only be listed if verbose is true.179 // https://git-scm.com/docs/git-fetch#Documentation/git-fetch.txt---quiet180 // https://git-scm.com/docs/git-fetch#Documentation/git-fetch.txt---verbose181 Verbose bool182 // Tags controls whether tags will be fetched as part of the remote or not.183 // https://git-scm.com/docs/git-fetch#Documentation/git-fetch.txt---tags184 // https://git-scm.com/docs/git-fetch#Documentation/git-fetch.txt---no-tags185 Tags FetchOptsTags186 // Stderr if set it would be used to redirect stderr stream into it.187 Stderr io.Writer188}189// ErrFetchFailed indicates that the fetch has failed.190type ErrFetchFailed struct {191 err error192}193// Error returns the error message.194func (e ErrFetchFailed) Error() string {195 return e.err.Error()196}197// FetchRemote fetches changes from the specified remote. Returns an ErrFetchFailed error in case198// the fetch itself failed.199func (repo *Repo) FetchRemote(ctx context.Context, remoteName string, opts FetchOpts) error {200 if err := validateNotBlank(remoteName, "remoteName"); err != nil {201 return err202 }203 var stderr bytes.Buffer204 if opts.Stderr == nil {205 opts.Stderr = &stderr206 }207 commandOptions := []git.CmdOpt{208 git.WithEnv(opts.Env...),209 git.WithStderr(opts.Stderr),210 git.WithDisabledHooks(),211 }212 commandOptions = append(commandOptions, opts.CommandOptions...)213 cmd, err := repo.gitCmdFactory.New(ctx, repo,214 git.SubCmd{215 Name: "fetch",216 Flags: opts.buildFlags(),217 Args: []string{remoteName},218 },219 commandOptions...,220 )221 if err != nil {222 return err223 }224 if err := cmd.Wait(); err != nil {225 return ErrFetchFailed{errorWithStderr(err, stderr.Bytes())}226 }227 return nil228}229// FetchInternal performs a fetch from an internal Gitaly-hosted repository. Returns an230// ErrFetchFailed error in case git-fetch(1) failed.231func (repo *Repo) FetchInternal(232 ctx context.Context,233 remoteRepo *gitalypb.Repository,234 refspecs []string,235 opts FetchOpts,236) error {237 if len(refspecs) == 0 {238 return fmt.Errorf("fetch internal called without refspecs")239 }240 env, err := gitalyssh.UploadPackEnv(ctx, repo.cfg, &gitalypb.SSHUploadPackRequest{241 Repository: remoteRepo,242 GitConfigOptions: []string{"uploadpack.allowAnySHA1InWant=true"},243 })244 if err != nil {245 return fmt.Errorf("fetch internal: %w", err)246 }247 var stderr bytes.Buffer248 if opts.Stderr == nil {249 opts.Stderr = &stderr250 }251 commandOptions := []git.CmdOpt{252 git.WithEnv(append(env, opts.Env...)...),253 git.WithStderr(opts.Stderr),254 git.WithRefTxHook(ctx, repo, repo.cfg),255 // We've observed performance issues when fetching into big repositories part of an256 // object pool. The root cause of this seems to be the connectivity check, which by257 // default will also include references of any alternates. Given that object pools258 // often have hundreds of thousands of references, this is quite expensive to259 // compute. Below config entry will disable listing of alternate refs: they260 // shouldn't even be included in the negotiation phase, so they aren't going to261 // matter in the connectivity check either.262 git.WithConfig(git.ConfigPair{Key: "core.alternateRefsCommand", Value: "exit 0 #"}),263 }264 commandOptions = append(commandOptions, opts.CommandOptions...)265 if err := repo.ExecAndWait(ctx,266 git.SubCmd{267 Name: "fetch",268 Flags: append(opts.buildFlags(), git.Flag{Name: "--atomic"}),269 Args: append([]string{gitalyssh.GitalyInternalURL}, refspecs...),270 },271 commandOptions...,272 ); err != nil {273 return ErrFetchFailed{errorWithStderr(err, stderr.Bytes())}274 }275 return nil276}277func (opts FetchOpts) buildFlags() []git.Option {278 flags := []git.Option{}279 if !opts.Verbose {280 flags = append(flags, git.Flag{Name: "--quiet"})281 }282 if opts.Prune {283 flags = append(flags, git.Flag{Name: "--prune"})284 }285 if opts.Force {286 flags = append(flags, git.Flag{Name: "--force"})287 }288 if opts.Tags != FetchOptsTagsDefault {289 flags = append(flags, git.Flag{Name: opts.Tags.String()})290 }291 return flags292}293func validateNotBlank(val, name string) error {294 if strings.TrimSpace(val) == "" {295 return fmt.Errorf("%w: %q is blank or empty", git.ErrInvalidArg, name)296 }297 return nil298}299func envGitSSHCommand(cmd string) string {300 return "GIT_SSH_COMMAND=" + cmd301}302// PushOptions are options that can be configured for a push.303type PushOptions struct {304 // SSHCommand is the command line to use for git's SSH invocation. The command line is used305 // as is and must be verified by the caller to be safe.306 SSHCommand string307 // Force decides whether to force push all of the refspecs.308 Force bool309 // Config is the Git configuration which gets passed to the git-push(1) invocation.310 // Configuration is set up via `WithConfigEnv()`, so potential credentials won't be leaked311 // via the command line.312 Config []git.ConfigPair313}314// Push force pushes the refspecs to the remote.315func (repo *Repo) Push(ctx context.Context, remote string, refspecs []string, options PushOptions) error {316 if len(refspecs) == 0 {317 return errors.New("refspecs to push must be explicitly specified")318 }319 var env []string320 if options.SSHCommand != "" {321 env = append(env, envGitSSHCommand(options.SSHCommand))322 }323 var flags []git.Option324 if options.Force {325 flags = append(flags, git.Flag{Name: "--force"})326 }327 stderr := &bytes.Buffer{}328 if err := repo.ExecAndWait(ctx,329 git.SubCmd{330 Name: "push",331 Flags: flags,332 Args: append([]string{remote}, refspecs...),333 },334 git.WithStderr(stderr),335 git.WithEnv(env...),336 git.WithConfigEnv(options.Config...),337 ); err != nil {338 return fmt.Errorf("git push: %w, stderr: %q", err, stderr)339 }340 return nil341}...

Full Screen

Full Screen

fetchRemote

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello, playground")4}5import (6func main() {7 fmt.Println("Hello, playground")

Full Screen

Full Screen

fetchRemote

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(golenv.Get("GOPATH"))4 vcs := golvcs.Vcs{}5 vcs.FetchRemote("github.com/abhishekkr/gol")6}7import (8func main() {9 fmt.Println(golenv.Get("GOPATH"))10 vcs := golvcs.Vcs{}11 vcs.FetchRemote("github.com/abhishekkr/gol")12}13import (14func main() {15 fmt.Println(golenv.Get("GOPATH"))16 vcs := golvcs.Vcs{}17 vcs.FetchRemote("github.com/abhishekkr/gol")18}19import (20func main() {21 fmt.Println(golenv.Get("GOPATH"))22 vcs := golvcs.Vcs{}23 vcs.FetchRemote("github.com/abhishekkr/gol")24}25import (26func main() {27 fmt.Println(golenv.Get("GOPATH"))28 vcs := golvcs.Vcs{}29 vcs.FetchRemote("github.com/abhishekkr/gol")30}31import (

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful