diff options
Diffstat (limited to '')
-rw-r--r-- | http_handle_repo_commit.go | 48 | ||||
-rw-r--r-- | http_handle_repo_contrib_index.go | 17 | ||||
-rw-r--r-- | http_handle_repo_contrib_one.go | 29 | ||||
-rw-r--r-- | http_handle_repo_index.go | 22 | ||||
-rw-r--r-- | http_handle_repo_info.go | 29 | ||||
-rw-r--r-- | http_handle_repo_log.go | 15 | ||||
-rw-r--r-- | http_handle_repo_raw.go | 34 | ||||
-rw-r--r-- | http_handle_repo_tree.go | 53 | ||||
-rw-r--r-- | http_handle_repo_upload_pack.go | 37 |
9 files changed, 169 insertions, 115 deletions
diff --git a/http_handle_repo_commit.go b/http_handle_repo_commit.go index ba09d25..094a862 100644 --- a/http_handle_repo_commit.go +++ b/http_handle_repo_commit.go @@ -12,12 +12,13 @@ import ( "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/filemode" "github.com/go-git/go-git/v5/plumbing/format/diff" + "github.com/go-git/go-git/v5/plumbing/object" "go.lindenii.runxiyu.org/lindenii-common/misc" ) // The file patch type from go-git isn't really usable in HTML templates // either. -type usable_file_patch struct { +type usable_file_patch_t struct { From diff.File To diff.File Chunks []usable_chunk @@ -29,25 +30,33 @@ type usable_chunk struct { } func handle_repo_commit(w http.ResponseWriter, r *http.Request, params map[string]any) { - repo, commit_id_specified_string := params["repo"].(*git.Repository), params["commit_id"].(string) - - commit_id_specified_string_without_suffix := strings.TrimSuffix(commit_id_specified_string, ".patch") - commit_id := plumbing.NewHash(commit_id_specified_string_without_suffix) - commit_object, err := repo.CommitObject(commit_id) - if err != nil { + var repo *git.Repository + var commit_id_specified_string, commit_id_specified_string_without_suffix string + var commit_id plumbing.Hash + var parent_commit_hash plumbing.Hash + var commit_object *object.Commit + var commit_id_string string + var err error + var patch *object.Patch + + repo, commit_id_specified_string = params["repo"].(*git.Repository), params["commit_id"].(string) + + commit_id_specified_string_without_suffix = strings.TrimSuffix(commit_id_specified_string, ".patch") + commit_id = plumbing.NewHash(commit_id_specified_string_without_suffix) + if commit_object, err = repo.CommitObject(commit_id); err != nil { http.Error(w, "Error getting commit object: "+err.Error(), http.StatusInternalServerError) return } if commit_id_specified_string_without_suffix != commit_id_specified_string { - patch, err := format_patch_from_commit(commit_object) - if err != nil { + var formatted_patch string + if formatted_patch, err = format_patch_from_commit(commit_object); err != nil { http.Error(w, "Error formatting patch: "+err.Error(), http.StatusInternalServerError) return } - fmt.Fprintln(w, patch) + fmt.Fprintln(w, formatted_patch) return } - commit_id_string := commit_object.Hash.String() + commit_id_string = commit_object.Hash.String() if commit_id_string != commit_id_specified_string { http.Redirect(w, r, commit_id_string, http.StatusSeeOther) @@ -57,7 +66,7 @@ func handle_repo_commit(w http.ResponseWriter, r *http.Request, params map[strin params["commit_object"] = commit_object params["commit_id"] = commit_id_string - parent_commit_hash, patch, err := get_patch_from_commit(commit_object) + parent_commit_hash, patch, err = get_patch_from_commit(commit_object) if err != nil { http.Error(w, "Error getting patch from commit: "+err.Error(), http.StatusInternalServerError) return @@ -97,18 +106,23 @@ var fake_diff_file_null = fake_diff_file{ func make_usable_file_patches(patch diff.Patch) (usable_file_patches []usable_file_patch) { // TODO: Remove unnecessary context // TODO: Prepend "+"/"-"/" " instead of solely distinguishing based on color - usable_file_patches = make([]usable_file_patch, 0) + for _, file_patch := range patch.FilePatches() { - from, to := file_patch.Files() + var from, to diff.File + var usable_file_patch usable_file_patch_t + chunks := []usable_chunk{} + + from, to = file_patch.Files() if from == nil { from = fake_diff_file_null } if to == nil { to = fake_diff_file_null } - chunks := []usable_chunk{} for _, chunk := range file_patch.Chunks() { - content := chunk.Content() + var content string + + content = chunk.Content() if len(content) > 0 && content[0] == '\n' { content = "\n" + content } // Horrible hack to fix how browsers newlines that immediately proceed <pre> @@ -117,7 +131,7 @@ func make_usable_file_patches(patch diff.Patch) (usable_file_patches []usable_fi Content: content, }) } - usable_file_patch := usable_file_patch{ + usable_file_patch = usable_file_patch_t{ Chunks: chunks, From: from, To: to, diff --git a/http_handle_repo_contrib_index.go b/http_handle_repo_contrib_index.go index 2e0dc1d..f352a3f 100644 --- a/http_handle_repo_contrib_index.go +++ b/http_handle_repo_contrib_index.go @@ -5,6 +5,8 @@ package main import ( "net/http" + + "github.com/jackc/pgx/v5" ) type id_title_status_t struct { @@ -14,24 +16,29 @@ type id_title_status_t struct { } func handle_repo_contrib_index(w http.ResponseWriter, r *http.Request, params map[string]any) { - rows, err := database.Query(r.Context(), "SELECT id, COALESCE(title, 'Untitled'), status FROM merge_requests WHERE repo_id = $1", params["repo_id"]) - if err != nil { + var rows pgx.Rows + var result []id_title_status_t + var err error + + if rows, err = database.Query(r.Context(), + "SELECT id, COALESCE(title, 'Untitled'), status FROM merge_requests WHERE repo_id = $1", + params["repo_id"], + ); err != nil { http.Error(w, "Error querying merge requests: "+err.Error(), http.StatusInternalServerError) return } defer rows.Close() - result := []id_title_status_t{} for rows.Next() { var id int var title, status string - if err := rows.Scan(&id, &title, &status); err != nil { + if err = rows.Scan(&id, &title, &status); err != nil { http.Error(w, "Error scanning merge request: "+err.Error(), http.StatusInternalServerError) return } result = append(result, id_title_status_t{id, title, status}) } - if err := rows.Err(); err != nil { + if err = rows.Err(); err != nil { http.Error(w, "Error ranging over merge requests: "+err.Error(), http.StatusInternalServerError) return } diff --git a/http_handle_repo_contrib_one.go b/http_handle_repo_contrib_one.go index 43c8996..91acfea 100644 --- a/http_handle_repo_contrib_one.go +++ b/http_handle_repo_contrib_one.go @@ -9,32 +9,41 @@ import ( "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing" + "github.com/go-git/go-git/v5/plumbing/object" ) func handle_repo_contrib_one(w http.ResponseWriter, r *http.Request, params map[string]any) { - mr_id_string := params["mr_id"].(string) - mr_id, err := strconv.ParseInt(mr_id_string, 10, strconv.IntSize) + var mr_id_string string + var mr_id int + var err error + var title, status, source_ref, destination_branch string + var repo *git.Repository + var source_ref_hash plumbing.Hash + var source_commit *object.Commit + + mr_id_string = params["mr_id"].(string) + mr_id_int64, err := strconv.ParseInt(mr_id_string, 10, strconv.IntSize) if err != nil { http.Error(w, "Merge request ID not an integer: "+err.Error(), http.StatusBadRequest) return } + mr_id = int(mr_id_int64) - var title, status, source_ref, destination_branch string - err = database.QueryRow(r.Context(), "SELECT COALESCE(title, ''), status, source_ref, COALESCE(destination_branch, '') FROM merge_requests WHERE id = $1", mr_id).Scan(&title, &status, &source_ref, &destination_branch) - if err != nil { + if err = database.QueryRow(r.Context(), + "SELECT COALESCE(title, ''), status, source_ref, COALESCE(destination_branch, '') FROM merge_requests WHERE id = $1", + mr_id, + ).Scan(&title, &status, &source_ref, &destination_branch); err != nil { http.Error(w, "Error querying merge request: "+err.Error(), http.StatusInternalServerError) return } - repo := params["repo"].(*git.Repository) + repo = params["repo"].(*git.Repository) - source_ref_hash, err := get_ref_hash_from_type_and_name(repo, "branch", source_ref) - if err != nil { + if source_ref_hash, err = get_ref_hash_from_type_and_name(repo, "branch", source_ref); err != nil { http.Error(w, "Error getting source ref hash: "+err.Error(), http.StatusInternalServerError) return } - source_commit, err := repo.CommitObject(source_ref_hash) - if err != nil { + if source_commit, err = repo.CommitObject(source_ref_hash); err != nil { http.Error(w, "Error getting source commit: "+err.Error(), http.StatusInternalServerError) return } diff --git a/http_handle_repo_index.go b/http_handle_repo_index.go index d83739e..73f1007 100644 --- a/http_handle_repo_index.go +++ b/http_handle_repo_index.go @@ -7,29 +7,37 @@ import ( "net/http" "github.com/go-git/go-git/v5" + "github.com/go-git/go-git/v5/plumbing" + "github.com/go-git/go-git/v5/plumbing/object" ) func handle_repo_index(w http.ResponseWriter, r *http.Request, params map[string]any) { - repo, repo_name, group_name := params["repo"].(*git.Repository), params["repo_name"].(string), params["group_name"].(string) + var repo *git.Repository + var repo_name, group_name string + var ref_hash plumbing.Hash + var err error + var recent_commits []*object.Commit + var commit_object *object.Commit + var tree *object.Tree - ref_hash, err := get_ref_hash_from_type_and_name(repo, params["ref_type"].(string), params["ref_name"].(string)) - if err != nil { + repo, repo_name, group_name = params["repo"].(*git.Repository), params["repo_name"].(string), params["group_name"].(string) + + if ref_hash, err = get_ref_hash_from_type_and_name(repo, params["ref_type"].(string), params["ref_name"].(string)); err != nil { http.Error(w, "Error getting ref hash: "+err.Error(), http.StatusInternalServerError) return } - recent_commits, err := get_recent_commits(repo, ref_hash, 3) - if err != nil { + if recent_commits, err = get_recent_commits(repo, ref_hash, 3); err != nil { http.Error(w, "Error getting recent commits: "+err.Error(), http.StatusInternalServerError) return } params["commits"] = recent_commits - commit_object, err := repo.CommitObject(ref_hash) + commit_object, err = repo.CommitObject(ref_hash) if err != nil { http.Error(w, "Error getting commit object: "+err.Error(), http.StatusInternalServerError) return } - tree, err := commit_object.Tree() + tree, err = commit_object.Tree() if err != nil { http.Error(w, "Error getting file tree: "+err.Error(), http.StatusInternalServerError) return diff --git a/http_handle_repo_info.go b/http_handle_repo_info.go index 5ff8c58..d8227c9 100644 --- a/http_handle_repo_info.go +++ b/http_handle_repo_info.go @@ -11,10 +11,13 @@ import ( ) func handle_repo_info(w http.ResponseWriter, r *http.Request, params map[string]any) (err error) { - group_name, repo_name := params["group_name"].(string), params["repo_name"].(string) - var repo_path string - err = database.QueryRow(r.Context(), "SELECT r.filesystem_path FROM repos r JOIN groups g ON r.group_id = g.id WHERE g.name = $1 AND r.name = $2;", group_name, repo_name).Scan(&repo_path) - if err != nil { + var group_name, repo_name, repo_path string + + group_name, repo_name = params["group_name"].(string), params["repo_name"].(string) + if err = database.QueryRow(r.Context(), + "SELECT r.filesystem_path FROM repos r JOIN groups g ON r.group_id = g.id WHERE g.name = $1 AND r.name = $2;", + group_name, repo_name, + ).Scan(&repo_path); err != nil { return err } @@ -22,38 +25,32 @@ func handle_repo_info(w http.ResponseWriter, r *http.Request, params map[string] w.WriteHeader(http.StatusOK) cmd := exec.Command("git", "upload-pack", "--stateless-rpc", "--advertise-refs", repo_path) - stdout, err := cmd.StdoutPipe() if err != nil { return err } - cmd.Stderr = cmd.Stdout defer func() { _ = stdout.Close() }() + cmd.Stderr = cmd.Stdout - err = cmd.Start() - if err != nil { + if err = cmd.Start(); err != nil { return err } - err = pack_line(w, "# service=git-upload-pack\n") - if err != nil { + if err = pack_line(w, "# service=git-upload-pack\n"); err != nil { return err } - err = pack_flush(w) - if err != nil { + if err = pack_flush(w); err != nil { return } - _, err = io.Copy(w, stdout) - if err != nil { + if _, err = io.Copy(w, stdout); err != nil { return err } - err = cmd.Wait() - if err != nil { + if err = cmd.Wait(); err != nil { return err } diff --git a/http_handle_repo_log.go b/http_handle_repo_log.go index 52762a3..65719e4 100644 --- a/http_handle_repo_log.go +++ b/http_handle_repo_log.go @@ -7,20 +7,25 @@ import ( "net/http" "github.com/go-git/go-git/v5" + "github.com/go-git/go-git/v5/plumbing" + "github.com/go-git/go-git/v5/plumbing/object" ) // TODO: I probably shouldn't include *all* commits here... func handle_repo_log(w http.ResponseWriter, r *http.Request, params map[string]any) { - repo := params["repo"].(*git.Repository) + var repo *git.Repository + var ref_hash plumbing.Hash + var err error + var commits []*object.Commit - ref_hash, err := get_ref_hash_from_type_and_name(repo, params["ref_type"].(string), params["ref_name"].(string)) - if err != nil { + repo = params["repo"].(*git.Repository) + + if ref_hash, err = get_ref_hash_from_type_and_name(repo, params["ref_type"].(string), params["ref_name"].(string)); err != nil { http.Error(w, "Error getting ref hash: "+err.Error(), http.StatusInternalServerError) return } - commits, err := get_recent_commits(repo, ref_hash, -1) - if err != nil { + if commits, err = get_recent_commits(repo, ref_hash, -1); err != nil { http.Error(w, "Error getting recent commits: "+err.Error(), http.StatusInternalServerError) return } diff --git a/http_handle_repo_raw.go b/http_handle_repo_raw.go index 68b69f6..a0f7430 100644 --- a/http_handle_repo_raw.go +++ b/http_handle_repo_raw.go @@ -10,27 +10,32 @@ import ( "strings" "github.com/go-git/go-git/v5" + "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/object" ) func handle_repo_raw(w http.ResponseWriter, r *http.Request, params map[string]any) { - raw_path_spec := params["rest"].(string) - repo, path_spec := params["repo"].(*git.Repository), strings.TrimSuffix(raw_path_spec, "/") + var raw_path_spec, path_spec string + var repo *git.Repository + var ref_hash plumbing.Hash + var commit_object *object.Commit + var tree *object.Tree + var err error + + raw_path_spec = params["rest"].(string) + repo, path_spec = params["repo"].(*git.Repository), strings.TrimSuffix(raw_path_spec, "/") params["path_spec"] = path_spec - ref_hash, err := get_ref_hash_from_type_and_name(repo, params["ref_type"].(string), params["ref_name"].(string)) - if err != nil { + if ref_hash, err = get_ref_hash_from_type_and_name(repo, params["ref_type"].(string), params["ref_name"].(string)); err != nil { http.Error(w, "Error getting ref hash: "+err.Error(), http.StatusInternalServerError) return } - commit_object, err := repo.CommitObject(ref_hash) - if err != nil { + if commit_object, err = repo.CommitObject(ref_hash); err != nil { http.Error(w, "Error getting commit object: "+err.Error(), http.StatusInternalServerError) return } - tree, err := commit_object.Tree() - if err != nil { + if tree, err = commit_object.Tree(); err != nil { http.Error(w, "Error getting file tree: "+err.Error(), http.StatusInternalServerError) return } @@ -39,10 +44,10 @@ func handle_repo_raw(w http.ResponseWriter, r *http.Request, params map[string]a if path_spec == "" { target = tree } else { - target, err = tree.Tree(path_spec) - if err != nil { - file, err := tree.File(path_spec) - if err != nil { + if target, err = tree.Tree(path_spec); err != nil { + var file *object.File + var file_contents string + if file, err = tree.File(path_spec); err != nil { http.Error(w, "Error retrieving path: "+err.Error(), http.StatusInternalServerError) return } @@ -50,12 +55,11 @@ func handle_repo_raw(w http.ResponseWriter, r *http.Request, params map[string]a http.Redirect(w, r, "../"+path_spec, http.StatusSeeOther) return } - file_contents, err := file.Contents() - if err != nil { + if file_contents, err = file.Contents(); err != nil { http.Error(w, "Error reading file: "+err.Error(), http.StatusInternalServerError) return } - fmt.Fprintln(w, file_contents) + fmt.Fprint(w, file_contents) return } } diff --git a/http_handle_repo_tree.go b/http_handle_repo_tree.go index a6173ae..190f082 100644 --- a/http_handle_repo_tree.go +++ b/http_handle_repo_tree.go @@ -10,30 +10,36 @@ import ( "path" "strings" + "github.com/alecthomas/chroma/v2" chroma_formatters_html "github.com/alecthomas/chroma/v2/formatters/html" chroma_lexers "github.com/alecthomas/chroma/v2/lexers" chroma_styles "github.com/alecthomas/chroma/v2/styles" "github.com/go-git/go-git/v5" + "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/object" ) func handle_repo_tree(w http.ResponseWriter, r *http.Request, params map[string]any) { - raw_path_spec := params["rest"].(string) - repo, path_spec := params["repo"].(*git.Repository), strings.TrimSuffix(raw_path_spec, "/") + var raw_path_spec, path_spec string + var repo *git.Repository + var ref_hash plumbing.Hash + var commit_object *object.Commit + var tree *object.Tree + var err error + + raw_path_spec = params["rest"].(string) + repo, path_spec = params["repo"].(*git.Repository), strings.TrimSuffix(raw_path_spec, "/") params["path_spec"] = path_spec - ref_hash, err := get_ref_hash_from_type_and_name(repo, params["ref_type"].(string), params["ref_name"].(string)) - if err != nil { + if ref_hash, err = get_ref_hash_from_type_and_name(repo, params["ref_type"].(string), params["ref_name"].(string)); err != nil { http.Error(w, "Error getting ref hash: "+err.Error(), http.StatusInternalServerError) return } - commit_object, err := repo.CommitObject(ref_hash) - if err != nil { + if commit_object, err = repo.CommitObject(ref_hash); err != nil { http.Error(w, "Error getting commit object: "+err.Error(), http.StatusInternalServerError) return } - tree, err := commit_object.Tree() - if err != nil { + if tree, err = commit_object.Tree(); err != nil { http.Error(w, "Error getting file tree: "+err.Error(), http.StatusInternalServerError) return } @@ -42,10 +48,16 @@ func handle_repo_tree(w http.ResponseWriter, r *http.Request, params map[string] if path_spec == "" { target = tree } else { - target, err = tree.Tree(path_spec) - if err != nil { - file, err := tree.File(path_spec) - if err != nil { + if target, err = tree.Tree(path_spec); err != nil { + var file *object.File + var file_contents string + var lexer chroma.Lexer + var iterator chroma.Iterator + var style *chroma.Style + var formatter *chroma_formatters_html.Formatter + var formatted_encapsulated template.HTML + + if file, err = tree.File(path_spec); err != nil { http.Error(w, "Error retrieving path: "+err.Error(), http.StatusInternalServerError) return } @@ -53,29 +65,26 @@ func handle_repo_tree(w http.ResponseWriter, r *http.Request, params map[string] http.Redirect(w, r, "../"+path_spec, http.StatusSeeOther) return } - file_contents, err := file.Contents() - if err != nil { + if file_contents, err = file.Contents(); err != nil { http.Error(w, "Error reading file: "+err.Error(), http.StatusInternalServerError) return } - lexer := chroma_lexers.Match(path_spec) + lexer = chroma_lexers.Match(path_spec) if lexer == nil { lexer = chroma_lexers.Fallback } - iterator, err := lexer.Tokenise(nil, file_contents) - if err != nil { + if iterator, err = lexer.Tokenise(nil, file_contents); err != nil { http.Error(w, "Error tokenizing code: "+err.Error(), http.StatusInternalServerError) return } var formatted_unencapsulated bytes.Buffer - style := chroma_styles.Get("autumn") - formatter := chroma_formatters_html.New(chroma_formatters_html.WithClasses(true), chroma_formatters_html.TabWidth(8)) - err = formatter.Format(&formatted_unencapsulated, style, iterator) - if err != nil { + style = chroma_styles.Get("autumn") + formatter = chroma_formatters_html.New(chroma_formatters_html.WithClasses(true), chroma_formatters_html.TabWidth(8)) + if err = formatter.Format(&formatted_unencapsulated, style, iterator); err != nil { http.Error(w, "Error formatting code: "+err.Error(), http.StatusInternalServerError) return } - formatted_encapsulated := template.HTML(formatted_unencapsulated.Bytes()) + formatted_encapsulated = template.HTML(formatted_unencapsulated.Bytes()) params["file_contents"] = formatted_encapsulated render_template(w, "repo_tree_file", params) diff --git a/http_handle_repo_upload_pack.go b/http_handle_repo_upload_pack.go index 95003f7..b273da9 100644 --- a/http_handle_repo_upload_pack.go +++ b/http_handle_repo_upload_pack.go @@ -11,10 +11,18 @@ import ( ) func handle_upload_pack(w http.ResponseWriter, r *http.Request, params map[string]any) (err error) { - group_name, repo_name := params["group_name"].(string), params["repo_name"].(string) + var group_name, repo_name string var repo_path string - err = database.QueryRow(r.Context(), "SELECT r.filesystem_path FROM repos r JOIN groups g ON r.group_id = g.id WHERE g.name = $1 AND r.name = $2;", group_name, repo_name).Scan(&repo_path) - if err != nil { + var stdout io.ReadCloser + var stdin io.WriteCloser + var cmd *exec.Cmd + + group_name, repo_name = params["group_name"].(string), params["repo_name"].(string) + + if err = database.QueryRow(r.Context(), + "SELECT r.filesystem_path FROM repos r JOIN groups g ON r.group_id = g.id WHERE g.name = $1 AND r.name = $2;", + group_name, repo_name, + ).Scan(&repo_path); err != nil { return err } @@ -23,10 +31,9 @@ func handle_upload_pack(w http.ResponseWriter, r *http.Request, params map[strin w.Header().Set("Transfer-Encoding", "chunked") w.WriteHeader(http.StatusOK) - cmd := exec.Command("git", "upload-pack", "--stateless-rpc", repo_path) + cmd = exec.Command("git", "upload-pack", "--stateless-rpc", repo_path) cmd.Env = append(os.Environ(), "LINDENII_FORGE_HOOKS_SOCKET_PATH="+config.Hooks.Socket) - stdout, err := cmd.StdoutPipe() - if err != nil { + if stdout, err = cmd.StdoutPipe(); err != nil { return err } cmd.Stderr = cmd.Stdout @@ -34,36 +41,30 @@ func handle_upload_pack(w http.ResponseWriter, r *http.Request, params map[strin _ = stdout.Close() }() - stdin, err := cmd.StdinPipe() - if err != nil { + if stdin, err = cmd.StdinPipe(); err != nil { return err } defer func() { _ = stdin.Close() }() - err = cmd.Start() - if err != nil { + if err = cmd.Start(); err != nil { return err } - _, err = io.Copy(stdin, r.Body) - if err != nil { + if _, err = io.Copy(stdin, r.Body); err != nil { return err } - err = stdin.Close() - if err != nil { + if err = stdin.Close(); err != nil { return err } - _, err = io.Copy(w, stdout) - if err != nil { + if _, err = io.Copy(w, stdout); err != nil { return err } - err = cmd.Wait() - if err != nil { + if err = cmd.Wait(); err != nil { return err } |