Skip to content

Updated agit doc for git-repo #27014

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

siennathesane
Copy link
Contributor

@siennathesane siennathesane commented Sep 11, 2023

Added basic reference instructions to unblock git-repo users who don't use Gerrit but use Gitea.

Added basic reference instructions to unblock `git-repo` users who don't use Gerrit but use Gitea.
@GiteaBot GiteaBot added this to the 1.20.5 milestone Sep 11, 2023
@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Sep 11, 2023
@lunny lunny requested a review from a1012112796 September 11, 2023 10:01

1. Create a file in `$GITEA_CUSTOM/public/` called `ssh_info`
2. Populate it with `{"type":"agit","version":2}`
3. Add a rewrite rule for your reverse proxy which rewrites the request path equaling `/ssh_info` to `/assets/ssh_info`
Copy link
Member

@silverwind silverwind Sep 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we already provide a mechanism to put files into root, like /robots.txt? Or maybe that path is already occupied, not familiar with /ssh_info.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only one I could find was /assets/<file>, and repo/git-repo expect it to be at the root of the host 🤷🏼‍♀️

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The quite old "assets" handler logic is: if local file exists, then use it, otherwise, pass it to other handles.

However, the logic has been changed long time ago, the local file doesn't have higher priority than other handlers.

For example: if you have a file in {Custom}/public/my-name and an org my-name

  • For quite old logic (very long time ago): the local file {Custom}/public/my-name is served
    • Users could serve any custom files without reverse proxy tweaks.
  • For current logic (has been changed long time ago): the org my-name is severed.

TBH, the quite old logic looks good to me, while I guess some nice features were changed unintentionally by many round iterations.

Copy link
Contributor

@wxiaoguang wxiaoguang Sep 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change was introduced by : "add /assets as root dir of public files #15219" and "Customization files should not require /assets path segment #15906" in 1.15

After that PR, users couldn't serve their custom files in web root anymore.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is special handling for robots.txt to remain under the root path. Maybe we could include the ssh_info in the binary directly? It would require reserving the "ssh_info" username, but I think that would be acceptable as a "breaking" change.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to take the "quite old logic" back, then end users could still have the ability to serve any file in the web root.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, gitea has defined /ssh_info router, but the type has been defined as gitea (example: https://try.gitea.io/ssh_info), That's because I'm not sure agit flow has been fully implement. looks need more work. and I hasn't checking whether gitea can be used with git-repo tool also.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whether gitea can be used with git-repo tool also.

I'm working through why it hasn't been working as intended, and this is what I've found so far. If the router has implemented /ssh_info but it's wrong, then we should fix it. It's ultimately something from Gerrit:

https://github.com/GerritCodeReview/gerrit/blob/587e12178e06e63c0ac8c31e6ec0cbc59b232eea/java/com/google/gerrit/httpd/raw/SshInfoServlet.java#L31-L48

@lunny
Copy link
Member

lunny commented Sep 13, 2023

Please send the patch to v1.21(main branch) first

@wxiaoguang
Copy link
Contributor

wxiaoguang commented Sep 13, 2023

Please send the patch to v1.21(main branch) first

Is there a clear conclusion? Before doing future changes, I guess it should clarify the details first?

There are 2 problems:

  1. Should we invite the ability of serving any file in "custom/public" directory back?
  2. Does Gitea fully support agit version 2? Whether gitea works with git-repo tool?

@a1012112796
Copy link
Member

@mxplusb maybe you need this plugin, ref: https://github.com/alibaba/git-repo-go/blob/master/helper/proto-external.go

package main

import (
	"bufio"
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"log"
	"os"
	"strings"

	"github.com/alibaba/git-repo-go/cap"
	"github.com/alibaba/git-repo-go/config"
	"github.com/alibaba/git-repo-go/encode"
	"github.com/alibaba/git-repo-go/helper"
	"github.com/urfave/cli/v2"
)

func main() {
	app := &cli.App{
		Name:   "git-repo-helper-proto-gitea",
		Usage:  "gitea protocol helper for git repo",
		Action: cmdMain,
		Flags: []cli.Flag{
			&cli.BoolFlag{
				Name:  "upload",
				Usage: "for upload hook",
			},
			&cli.BoolFlag{
				Name:  "download",
				Usage: "for download hook",
			},
			&cli.Int64Flag{
				Name:  "version",
				Usage: "hook version",
			},
		},
	}

	if err := app.Run(os.Args); err != nil {
		log.Fatal(err)
	}
}

func getGitPushCommand(o *config.UploadOptions) (*helper.GitPushCommand, error) {
	var (
		gitPushCmd = helper.GitPushCommand{}
	)

	cmds := []string{"git", "push"}

	if o.RemoteURL == "" {
		return nil, errors.New("empty review url for helper")
	}
	gitURL := config.ParseGitURL(o.RemoteURL)
	if gitURL == nil || (gitURL.Proto != "ssh" && gitURL.Proto != "http" && gitURL.Proto != "https") {
		return nil, fmt.Errorf("bad review URL: %s", o.RemoteURL)
	}

	if !cap.GitCanPushOptions() {
		log.Fatalln("cannot send push options, for your git version is too low")
	}

	for _, pushOption := range o.PushOptions {
		cmds = append(cmds, "-o", pushOption)
	}

	refSpec := ""
	localBranch := strings.TrimPrefix(o.LocalBranch, config.RefsHeads)
	destBranch := strings.TrimPrefix(o.DestBranch, config.RefsHeads)
	if localBranch == "" {
		refSpec = "HEAD"
	} else {
		refSpec = config.RefsHeads + localBranch
	}

	if !o.CodeReview.Empty() {
		log.Fatalln("gitea not support for-review now")
	} else {
		refSpec += fmt.Sprintf(":refs/for/%s/%s",
			destBranch,
			localBranch)
	}

	if o.Title != "" {
		cmds = append(cmds, "-o", "title="+encode.B64Encode(o.Title))
	}
	if o.Description != "" {
		cmds = append(cmds, "-o", "description="+encode.B64Encode(o.Description))
	}

	// TODO:
	// if o.Issue != "" {
	// 	cmds = append(cmds, "-o", "issue="+encode.B64Encode(o.Issue))
	// }
	// if o.People != nil && len(o.People) > 0 && len(o.People[0]) > 0 {
	// 	reviewers := strings.Join(o.People[0], ",")
	// 	cmds = append(cmds, "-o", "reviewers="+encode.B64Encode(reviewers))
	// }
	// if o.People != nil && len(o.People) > 1 && len(o.People[1]) > 0 {
	// 	cc := strings.Join(o.People[1], ",")
	// 	cmds = append(cmds, "-o", "cc="+encode.B64Encode(cc))
	// }

	// if o.NoEmails {
	// 	cmds = append(cmds, "-o", "notify=no")
	// }
	// if o.Private {
	// 	cmds = append(cmds, "-o", "private=yes")
	// }
	// if o.WIP {
	// 	cmds = append(cmds, "-o", "wip=yes")
	// }
	// if o.OldOid != "" {
	// 	cmds = append(cmds, "-o", "old-oid="+o.OldOid)
	// }

	if o.RemoteName != "" {
		cmds = append(cmds, o.RemoteName)
	} else {
		cmds = append(cmds, o.RemoteURL)
	}
	cmds = append(cmds, refSpec)

	gitPushCmd.Cmd = cmds[0]
	gitPushCmd.Args = cmds[1:]
	return &gitPushCmd, nil
}

func cmdUpload() error {
	log.Printf("cmdUpload ...\n")

	input, err := io.ReadAll(os.Stdin)
	if err != nil {
		return err
	}

	cfg := config.UploadOptions{}
	err = json.Unmarshal(input, &cfg)
	if err != nil {
		log.Printf("Error: cmdUpload: %v\n", err)
		return err
	}
	log.Printf("Debug: cmdUpload: %#v\n", cfg)

	result, err := getGitPushCommand(&cfg)
	if err != nil {
		return err
	}
	resultBytes, err := json.Marshal(result)
	if err != nil {
		return err
	}

	_, err = os.Stdout.Write(resultBytes)

	return err
}

func cmdDownload() error {
	scan := bufio.NewScanner(os.Stdin)
	if !scan.Scan() {
		return errors.New("no input data")
	}
	parts := strings.SplitN(scan.Text(), " ", 3)
	if len(parts) != 2 {
		return errors.New("no enough params")
	}

	log.Println("cmdDownload: ", parts[0], parts[1])
	fmt.Printf("refs/pull/%s/head\n", parts[0])

	return nil
}

var logFp *os.File

type emptyWriter struct{}

func (e *emptyWriter) Write(p []byte) (n int, err error) {
	return len(p), nil
}

func initLogFile() error {
	debugFile := os.Getenv("GIT_REPO_GITEA_DEBUG")
	if len(debugFile) == 0 {
		log.SetOutput(&emptyWriter{})
		return nil
	}

	var err error
	logFp, err = os.OpenFile(debugFile, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o644)
	if err == nil {
		log.SetOutput(logFp)
	}

	return err
}

func cmdMain(ctx *cli.Context) error {
	if err := initLogFile(); err != nil {
		return err
	}
	defer func() {
		if logFp != nil {
			logFp.Close()
		}
	}()

	if ctx.Bool("upload") {
		return cmdUpload()
	}

	if ctx.Bool("download") {
		return cmdDownload()
	}

	return nil
}

@lunny lunny modified the milestones: 1.20.5, 1.20.6 Oct 4, 2023
@lunny lunny removed this from the 1.20.6 milestone Nov 26, 2023
@go-gitea go-gitea locked as resolved and limited conversation to collaborators Sep 14, 2024
lunny pushed a commit that referenced this pull request Jun 12, 2025
* Fix #34707
* Fix dropdown filter handling
* Fix #27014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants