-
Notifications
You must be signed in to change notification settings - Fork 8
Support code uploads without Git hoster #372
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
Draft
schmijos
wants to merge
2
commits into
ninech:main
Choose a base branch
from
schmijos:feature/support-git-once
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,260 @@ | ||
| // Package gitonce provides a client to upload local directories to the gitonce | ||
| // service, which converts them to one-time-use git repositories. | ||
| package gitonce | ||
|
|
||
| import ( | ||
| "archive/zip" | ||
| "bufio" | ||
| "bytes" | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "mime/multipart" | ||
| "net/http" | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "strings" | ||
| ) | ||
|
|
||
| const DefaultUploadURL = "https://gitonce.21ddead.deploio.app/upload" | ||
|
|
||
| type uploadResponse struct { | ||
| Message string `json:"message"` | ||
| URL string `json:"url"` | ||
| Commit string `json:"commit"` | ||
| } | ||
|
|
||
| // UploadResult holds the result of a successful gitonce upload. | ||
| type UploadResult struct { | ||
| // URL is the one-time-use git repository URL. | ||
| URL string | ||
| // Commit is the commit hash of the uploaded content. | ||
| Commit string | ||
| } | ||
|
|
||
| // UploadDirectory zips the given local directory and uploads it to the gitonce | ||
| // service at uploadURL. It returns the upload result containing the URL and | ||
| // commit hash. | ||
| func UploadDirectory(ctx context.Context, dir, uploadURL string) (UploadResult, error) { | ||
| buf, err := zipDirectory(dir) | ||
| if err != nil { | ||
| return UploadResult{}, fmt.Errorf("zipping directory %q: %w", dir, err) | ||
| } | ||
|
|
||
| var body bytes.Buffer | ||
| mw := multipart.NewWriter(&body) | ||
| fw, err := mw.CreateFormFile("zipfile", "source.zip") | ||
| if err != nil { | ||
| return UploadResult{}, fmt.Errorf("creating form file: %w", err) | ||
| } | ||
| if _, err := io.Copy(fw, buf); err != nil { | ||
| return UploadResult{}, fmt.Errorf("writing zip to form: %w", err) | ||
| } | ||
| if err := mw.Close(); err != nil { | ||
| return UploadResult{}, fmt.Errorf("closing multipart writer: %w", err) | ||
| } | ||
|
|
||
| req, err := http.NewRequestWithContext(ctx, http.MethodPost, uploadURL, &body) | ||
| if err != nil { | ||
| return UploadResult{}, fmt.Errorf("creating upload request: %w", err) | ||
| } | ||
| req.Header.Set("Content-Type", mw.FormDataContentType()) | ||
|
|
||
| resp, err := http.DefaultClient.Do(req) | ||
| if err != nil { | ||
| return UploadResult{}, fmt.Errorf("uploading to gitonce: %w", err) | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| respBody, err := io.ReadAll(resp.Body) | ||
| if err != nil { | ||
| return UploadResult{}, fmt.Errorf("reading response body: %w", err) | ||
| } | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| return UploadResult{}, fmt.Errorf("gitonce upload failed with status %d: %s", resp.StatusCode, string(respBody)) | ||
| } | ||
|
|
||
| var uploadResp uploadResponse | ||
| if err := json.Unmarshal(respBody, &uploadResp); err != nil { | ||
| return UploadResult{}, fmt.Errorf("decoding gitonce response: %w", err) | ||
| } | ||
|
|
||
| if uploadResp.URL == "" { | ||
| return UploadResult{}, fmt.Errorf("gitonce returned empty URL") | ||
| } | ||
|
|
||
| return UploadResult{URL: uploadResp.URL, Commit: uploadResp.Commit}, nil | ||
| } | ||
|
|
||
| func zipDirectory(dir string) (*bytes.Buffer, error) { | ||
| files, err := filesToZip(dir) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| buf := new(bytes.Buffer) | ||
| zw := zip.NewWriter(buf) | ||
|
|
||
| for _, rel := range files { | ||
| f, err := zw.Create(rel) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| src, err := os.Open(filepath.Join(dir, rel)) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| _, cpErr := io.Copy(f, src) | ||
| src.Close() | ||
| if cpErr != nil { | ||
| return nil, cpErr | ||
| } | ||
| } | ||
|
|
||
| if err := zw.Close(); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return buf, nil | ||
| } | ||
|
|
||
| // filesToZip returns the list of relative file paths to include in the zip. | ||
| // If dir is a git repository, only tracked files are included. | ||
| // Otherwise, all files are included while respecting .gitignore if present. | ||
| func filesToZip(dir string) ([]string, error) { | ||
| if isGitRepo(dir) { | ||
| return gitTrackedFiles(dir) | ||
| } | ||
| return walkWithGitignore(dir) | ||
| } | ||
|
|
||
| // isGitRepo reports whether dir is inside a git repository. | ||
| func isGitRepo(dir string) bool { | ||
| _, err := os.Stat(filepath.Join(dir, ".git")) | ||
| return err == nil | ||
| } | ||
|
|
||
| // gitTrackedFiles returns relative paths of files tracked by git in dir. | ||
| func gitTrackedFiles(dir string) ([]string, error) { | ||
| cmd := exec.Command("git", "ls-files") | ||
| cmd.Dir = dir | ||
| out, err := cmd.Output() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("running git ls-files: %w", err) | ||
| } | ||
| var files []string | ||
| for _, line := range strings.Split(strings.TrimRight(string(out), "\n"), "\n") { | ||
| if line != "" { | ||
| files = append(files, line) | ||
| } | ||
| } | ||
| return files, nil | ||
| } | ||
|
|
||
| // walkWithGitignore walks dir and returns relative file paths, excluding files | ||
| // matched by .gitignore patterns if a .gitignore file is present at dir root. | ||
| func walkWithGitignore(dir string) ([]string, error) { | ||
| patterns, err := loadGitignorePatterns(filepath.Join(dir, ".gitignore")) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| var files []string | ||
| err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { | ||
| if err != nil { | ||
| return err | ||
| } | ||
| rel, err := filepath.Rel(dir, path) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if info.IsDir() { | ||
| if rel == "." { | ||
| return nil | ||
| } | ||
| if matchesGitignore(patterns, rel, true) { | ||
| return filepath.SkipDir | ||
| } | ||
| return nil | ||
| } | ||
| if !matchesGitignore(patterns, rel, false) { | ||
| files = append(files, rel) | ||
| } | ||
| return nil | ||
| }) | ||
| return files, err | ||
| } | ||
|
|
||
| // gitignorePattern holds a parsed .gitignore rule. | ||
| type gitignorePattern struct { | ||
| pattern string | ||
| negate bool | ||
| dirOnly bool | ||
| anchored bool // pattern contains a slash (other than trailing) | ||
| } | ||
|
|
||
| func loadGitignorePatterns(path string) ([]gitignorePattern, error) { | ||
| f, err := os.Open(path) | ||
| if os.IsNotExist(err) { | ||
| return nil, nil | ||
| } | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer f.Close() | ||
|
|
||
| var patterns []gitignorePattern | ||
| sc := bufio.NewScanner(f) | ||
| for sc.Scan() { | ||
| line := sc.Text() | ||
| if line == "" || strings.HasPrefix(line, "#") { | ||
| continue | ||
| } | ||
| p := gitignorePattern{} | ||
| if strings.HasPrefix(line, "!") { | ||
| p.negate = true | ||
| line = line[1:] | ||
| } | ||
| if strings.HasSuffix(line, "/") { | ||
| p.dirOnly = true | ||
| line = strings.TrimSuffix(line, "/") | ||
| } | ||
| // A pattern is anchored if it contains a slash after stripping a | ||
| // possible leading slash. | ||
| trimmed := strings.TrimPrefix(line, "/") | ||
| p.anchored = strings.Contains(trimmed, "/") | ||
| p.pattern = trimmed | ||
| patterns = append(patterns, p) | ||
| } | ||
| return patterns, sc.Err() | ||
| } | ||
|
|
||
| // matchesGitignore reports whether rel (a slash-separated relative path) is | ||
| // matched (i.e. should be ignored) by the given patterns. | ||
| func matchesGitignore(patterns []gitignorePattern, rel string, isDir bool) bool { | ||
| rel = filepath.ToSlash(rel) | ||
| ignored := false | ||
| for _, p := range patterns { | ||
| if p.dirOnly && !isDir { | ||
| continue | ||
| } | ||
| var matched bool | ||
| if p.anchored { | ||
| matched, _ = filepath.Match(p.pattern, rel) | ||
| } else { | ||
| // match against the base name or any path component | ||
| base := filepath.Base(rel) | ||
| matched, _ = filepath.Match(p.pattern, base) | ||
| if !matched { | ||
| matched, _ = filepath.Match(p.pattern, rel) | ||
| } | ||
| } | ||
| if matched { | ||
| ignored = !p.negate | ||
| } | ||
| } | ||
| return ignored | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's for sure not the final idea.