diff options
41 files changed, 797 insertions, 807 deletions
@@ -17,3 +17,5 @@ version.go: clean: $(RM) forge version.go vendor +htmpl.go: htmpl/* + gohtmplgen -o htmpl.go htmpl/* @@ -53,12 +53,3 @@ We have several Git repo mirrors on a few places: * [Codeberg](https://codeberg.org/lindenii/forge/) * [GitHub](https://github.com/runxiyu/forge/) -## Code style - -We follow the Lindenii Project's general code style, which has a few important -deviations from what most people may be used to: - -* We used tabs for indentation everywhere (Go, C, HTML, CSS, JS, SQL, etc.) -* All names are in `snake_case`; exported identifiers in Go use - `Capitalized_snake_case`. Type identifiers end with `_t`. -* We avoid `:=` in Go, and prefer to use `var`. @@ -9,9 +9,9 @@ import ( "github.com/jackc/pgx/v5/pgtype" ) -// get_path_perm_by_group_repo_key returns the filesystem path and direct +// getRepoInfo returns the filesystem path and direct // access permission for a given repo and a provided ssh public key. -func get_path_perm_by_group_repo_key(ctx context.Context, group_path []string, repo_name, ssh_pubkey string) (repo_id int, filesystem_path string, access bool, contrib_requirements string, user_type string, user_id int, err error) { +func getRepoInfo(ctx context.Context, groupPath []string, repoName, sshPubkey string) (repoID int, fsPath string, access bool, contribReq, userType string, userID int, err error) { err = database.QueryRow(ctx, ` WITH RECURSIVE group_path_cte AS ( -- Start: match the first name in the path where parent_group IS NULL @@ -51,7 +51,7 @@ LEFT JOIN users u ON u.id = s.user_id LEFT JOIN user_group_roles ugr ON ugr.group_id = g.id AND ugr.user_id = u.id WHERE g.depth = cardinality($1::text[]) AND r.name = $2 -`, pgtype.FlatArray[string](group_path), repo_name, ssh_pubkey, - ).Scan(&repo_id, &filesystem_path, &access, &contrib_requirements, &user_type, &user_id) +`, pgtype.FlatArray[string](groupPath), repoName, sshPubkey, + ).Scan(&repoID, &fsPath, &access, &contribReq, &userType, &userID) return } @@ -66,7 +66,7 @@ func loadConfig(path string) (err error) { return err } - global_data["forge_title"] = config.General.Title + globalData["forge_title"] = config.General.Title return nil } diff --git a/database.go b/database.go index 9068736..2406d01 100644 --- a/database.go +++ b/database.go @@ -9,9 +9,9 @@ import ( "github.com/jackc/pgx/v5" ) -// query_name_desc_list is a helper function that executes a query and returns a +// queryNameDesc is a helper function that executes a query and returns a // list of name_desc_t results. -func query_name_desc_list(ctx context.Context, query string, args ...any) (result []name_desc_t, err error) { +func queryNameDesc(ctx context.Context, query string, args ...any) (result []nameDesc, err error) { var rows pgx.Rows if rows, err = database.Query(ctx, query, args...); err != nil { @@ -24,13 +24,13 @@ func query_name_desc_list(ctx context.Context, query string, args ...any) (resul if err = rows.Scan(&name, &description); err != nil { return nil, err } - result = append(result, name_desc_t{name, description}) + result = append(result, nameDesc{name, description}) } return result, rows.Err() } -// name_desc_t holds a name and a description. -type name_desc_t struct { +// nameDesc holds a name and a description. +type nameDesc struct { Name string Description string } @@ -15,20 +15,20 @@ import ( "github.com/jackc/pgx/v5" ) -func check_and_update_federated_user_status(ctx context.Context, user_id int, service, remote_username, pubkey string) (bool, error) { +func fedauth(ctx context.Context, userID int, service, remoteUsername, pubkey string) (bool, error) { var err error var resp *http.Response matched := false - username_escaped := url.PathEscape(remote_username) + usernameEscaped := url.PathEscape(remoteUsername) switch service { case "sr.ht": - resp, err = http.Get("https://meta.sr.ht/~" + username_escaped + ".keys") + resp, err = http.Get("https://meta.sr.ht/~" + usernameEscaped + ".keys") case "github": - resp, err = http.Get("https://github.com/" + username_escaped + ".keys") + resp, err = http.Get("https://github.com/" + usernameEscaped + ".keys") case "codeberg": - resp, err = http.Get("https://codeberg.org/" + username_escaped + ".keys") + resp, err = http.Get("https://codeberg.org/" + usernameEscaped + ".keys") case "tangled": - resp, err = http.Get("https://tangled.sh/keys/" + username_escaped) + resp, err = http.Get("https://tangled.sh/keys/" + usernameEscaped) // TODO: Don't rely on one webview default: return false, errors.New("unknown federated service") @@ -51,11 +51,11 @@ func check_and_update_federated_user_status(ctx context.Context, user_id int, se return false, err } - line_split := strings.Split(line, " ") - if len(line_split) < 2 { + lineSplit := strings.Split(line, " ") + if len(lineSplit) < 2 { continue } - line = strings.Join(line_split[:2], " ") + line = strings.Join(lineSplit[:2], " ") if line == pubkey { matched = true @@ -74,10 +74,10 @@ func check_and_update_federated_user_status(ctx context.Context, user_id int, se defer func() { _ = tx.Rollback(ctx) }() - if _, err = tx.Exec(ctx, `UPDATE users SET type = 'federated' WHERE id = $1 AND type = 'pubkey_only'`, user_id); err != nil { + if _, err = tx.Exec(ctx, `UPDATE users SET type = 'federated' WHERE id = $1 AND type = 'pubkey_only'`, userID); err != nil { return false, err } - if _, err = tx.Exec(ctx, `INSERT INTO federated_identities (user_id, service, remote_username) VALUES ($1, $2, $3)`, user_id, service, remote_username); err != nil { + if _, err = tx.Exec(ctx, `INSERT INTO federated_identities (user_id, service, remote_username) VALUES ($1, $2, $3)`, userID, service, remoteUsername); err != nil { return false, err } if err = tx.Commit(ctx); err != nil { diff --git a/git_format_patch.go b/git_format_patch.go index 2cf66ed..56474d2 100644 --- a/git_format_patch.go +++ b/git_format_patch.go @@ -14,34 +14,34 @@ import ( // get_patch_from_commit formats a commit object as if it was returned by // git-format-patch. -func format_patch_from_commit(commit *object.Commit) (final string, err error) { +func fmtCommitPatch(commit *object.Commit) (final string, err error) { var patch *object.Patch var buf bytes.Buffer var author object.Signature var date string - var commit_msg_title, commit_msg_details string + var commitTitle, commitDetails string - if _, patch, err = get_patch_from_commit(commit); err != nil { + if _, patch, err = fmtCommitAsPatch(commit); err != nil { return "", err } author = commit.Author date = author.When.Format(time.RFC1123Z) - commit_msg_title, commit_msg_details, _ = strings.Cut(commit.Message, "\n") + commitTitle, commitDetails, _ = strings.Cut(commit.Message, "\n") // This date is hardcoded in Git. fmt.Fprintf(&buf, "From %s Mon Sep 17 00:00:00 2001\n", commit.Hash) fmt.Fprintf(&buf, "From: %s <%s>\n", author.Name, author.Email) fmt.Fprintf(&buf, "Date: %s\n", date) - fmt.Fprintf(&buf, "Subject: [PATCH] %s\n\n", commit_msg_title) + fmt.Fprintf(&buf, "Subject: [PATCH] %s\n\n", commitTitle) - if commit_msg_details != "" { - commit_msg_details_first_line, commit_msg_details_rest, _ := strings.Cut(commit_msg_details, "\n") - if strings.TrimSpace(commit_msg_details_first_line) == "" { - commit_msg_details = commit_msg_details_rest + if commitDetails != "" { + commitDetails1, commitDetails2, _ := strings.Cut(commitDetails, "\n") + if strings.TrimSpace(commitDetails1) == "" { + commitDetails = commitDetails2 } - buf.WriteString(commit_msg_details) + buf.WriteString(commitDetails) buf.WriteString("\n") } buf.WriteString("---\n") diff --git a/git_hooks_deploy.go b/git_hooks_deploy.go index d95d6a5..86df6e3 100644 --- a/git_hooks_deploy.go +++ b/git_hooks_deploy.go @@ -15,20 +15,20 @@ import ( // pre-compiled during the build process; see the Makefile. func deployHooks() (err error) { err = func() (err error) { - var src_fd fs.File - var dst_fd *os.File + var srcFD fs.File + var dstFD *os.File - if src_fd, err = resources_fs.Open("hookc/hookc"); err != nil { + if srcFD, err = resourcesFS.Open("hookc/hookc"); err != nil { return err } - defer src_fd.Close() + defer srcFD.Close() - if dst_fd, err = os.OpenFile(filepath.Join(config.Hooks.Execs, "hookc"), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o755); err != nil { + if dstFD, err = os.OpenFile(filepath.Join(config.Hooks.Execs, "hookc"), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o755); err != nil { return err } - defer dst_fd.Close() + defer dstFD.Close() - if _, err = io.Copy(dst_fd, src_fd); err != nil { + if _, err = io.Copy(dstFD, srcFD); err != nil { return err } @@ -44,10 +44,10 @@ func deployHooks() (err error) { return err } - for _, hook_name := range []string{ + for _, hookName := range []string{ "pre-receive", } { - if err = os.Symlink(filepath.Join(config.Hooks.Execs, "hookc"), filepath.Join(config.Hooks.Execs, hook_name)); err != nil { + if err = os.Symlink(filepath.Join(config.Hooks.Execs, "hookc"), filepath.Join(config.Hooks.Execs, hookName)); err != nil { return err } } diff --git a/git_hooks_handle.go b/git_hooks_handle.go index e23b7ce..0bf7b88 100644 --- a/git_hooks_handle.go +++ b/git_hooks_handle.go @@ -24,22 +24,22 @@ import ( ) var ( - err_get_fd = errors.New("unable to get file descriptor") - err_get_ucred = errors.New("failed getsockopt") + errGetFD = errors.New("unable to get file descriptor") + errGetUcred = errors.New("failed getsockopt") ) -// hooks_handle_connection handles a connection from hookc via the +// hooksHandler handles a connection from hookc via the // unix socket. -func hooks_handle_connection(conn net.Conn) { +func hooksHandler(conn net.Conn) { var ctx context.Context var cancel context.CancelFunc var ucred *syscall.Ucred var err error var cookie []byte - var pack_to_hook pack_to_hook_t - var ssh_stderr io.Writer + var packPass packPass + var sshStderr io.Writer var ok bool - var hook_return_value byte + var hookRet byte defer conn.Close() ctx, cancel = context.WithCancel(context.Background()) @@ -47,18 +47,18 @@ func hooks_handle_connection(conn net.Conn) { // There aren't reasonable cases where someone would run this as // another user. - if ucred, err = get_ucred(conn); err != nil { + if ucred, err = getUcred(conn); err != nil { if _, err = conn.Write([]byte{1}); err != nil { return } - wf_error(conn, "\nUnable to get peer credentials: %v", err) + writeRedError(conn, "\nUnable to get peer credentials: %v", err) return } if ucred.Uid != uint32(os.Getuid()) { if _, err = conn.Write([]byte{1}); err != nil { return } - wf_error(conn, "\nUID mismatch") + writeRedError(conn, "\nUID mismatch") return } @@ -67,27 +67,27 @@ func hooks_handle_connection(conn net.Conn) { if _, err = conn.Write([]byte{1}); err != nil { return } - wf_error(conn, "\nFailed to read cookie: %v", err) + writeRedError(conn, "\nFailed to read cookie: %v", err) return } - pack_to_hook, ok = pack_to_hook_by_cookie.Load(string(cookie)) + packPass, ok = packPasses.Load(string(cookie)) if !ok { if _, err = conn.Write([]byte{1}); err != nil { return } - wf_error(conn, "\nInvalid handler cookie") + writeRedError(conn, "\nInvalid handler cookie") return } - ssh_stderr = pack_to_hook.session.Stderr() + sshStderr = packPass.session.Stderr() - _, _ = ssh_stderr.Write([]byte{'\n'}) + _, _ = sshStderr.Write([]byte{'\n'}) - hook_return_value = func() byte { + hookRet = func() byte { var argc64 uint64 if err = binary.Read(conn, binary.NativeEndian, &argc64); err != nil { - wf_error(ssh_stderr, "Failed to read argc: %v", err) + writeRedError(sshStderr, "Failed to read argc: %v", err) return 1 } var args []string @@ -97,7 +97,7 @@ func hooks_handle_connection(conn net.Conn) { b := make([]byte, 1) n, err := conn.Read(b) if err != nil || n != 1 { - wf_error(ssh_stderr, "Failed to read arg: %v", err) + writeRedError(sshStderr, "Failed to read arg: %v", err) return 1 } if b[0] == 0 { @@ -108,215 +108,213 @@ func hooks_handle_connection(conn net.Conn) { args = append(args, arg.String()) } - git_env := make(map[string]string) + gitEnv := make(map[string]string) for { - var env_line bytes.Buffer + var envLine bytes.Buffer for { b := make([]byte, 1) n, err := conn.Read(b) if err != nil || n != 1 { - wf_error(ssh_stderr, "Failed to read environment variable: %v", err) + writeRedError(sshStderr, "Failed to read environment variable: %v", err) return 1 } if b[0] == 0 { break } - env_line.WriteByte(b[0]) + envLine.WriteByte(b[0]) } - if env_line.Len() == 0 { + if envLine.Len() == 0 { break } - kv := env_line.String() + kv := envLine.String() parts := strings.SplitN(kv, "=", 2) if len(parts) < 2 { - wf_error(ssh_stderr, "Invalid environment variable line: %v", kv) + writeRedError(sshStderr, "Invalid environment variable line: %v", kv) return 1 } - git_env[parts[0]] = parts[1] + gitEnv[parts[0]] = parts[1] } var stdin bytes.Buffer if _, err = io.Copy(&stdin, conn); err != nil { - wf_error(conn, "Failed to read to the stdin buffer: %v", err) + writeRedError(conn, "Failed to read to the stdin buffer: %v", err) } switch filepath.Base(args[0]) { case "pre-receive": - if pack_to_hook.direct_access { + if packPass.directAccess { return 0 - } else { - all_ok := true - for { - var line, old_oid, rest, new_oid, ref_name string - var found bool - var old_hash, new_hash plumbing.Hash - var old_commit, new_commit *object.Commit - var git_push_option_count int - - git_push_option_count, err = strconv.Atoi(git_env["GIT_PUSH_OPTION_COUNT"]) - if err != nil { - wf_error(ssh_stderr, "Failed to parse GIT_PUSH_OPTION_COUNT: %v", err) + } + allOK := true + for { + var line, oldOID, rest, newIOID, refName string + var found bool + var oldHash, newHash plumbing.Hash + var oldCommit, newCommit *object.Commit + var pushOptCount int + + pushOptCount, err = strconv.Atoi(gitEnv["GIT_PUSH_OPTION_COUNT"]) + if err != nil { + writeRedError(sshStderr, "Failed to parse GIT_PUSH_OPTION_COUNT: %v", err) + return 1 + } + + // TODO: Allow existing users (even if they are already federated or registered) to add a federated user ID... though perhaps this should be in the normal SSH interface instead of the git push interface? + // Also it'd be nice to be able to combine users or whatever + if packPass.contribReq == "federated" && packPass.userType != "federated" && packPass.userType != "registered" { + if pushOptCount == 0 { + writeRedError(sshStderr, "This repo requires contributors to be either federated or registered users. You must supply your federated user ID as a push option. For example, git push -o fedid=sr.ht:runxiyu") return 1 } - - // TODO: Allow existing users (even if they are already federated or registered) to add a federated user ID... though perhaps this should be in the normal SSH interface instead of the git push interface? - // Also it'd be nice to be able to combine users or whatever - if pack_to_hook.contrib_requirements == "federated" && pack_to_hook.user_type != "federated" && pack_to_hook.user_type != "registered" { - if git_push_option_count == 0 { - wf_error(ssh_stderr, "This repo requires contributors to be either federated or registered users. You must supply your federated user ID as a push option. For example, git push -o fedid=sr.ht:runxiyu") + for i := 0; i < pushOptCount; i++ { + pushOpt, ok := gitEnv[fmt.Sprintf("GIT_PUSH_OPTION_%d", i)] + if !ok { + writeRedError(sshStderr, "Failed to get push option %d", i) return 1 } - for i := 0; i < git_push_option_count; i++ { - push_option, ok := git_env[fmt.Sprintf("GIT_PUSH_OPTION_%d", i)] - if !ok { - wf_error(ssh_stderr, "Failed to get push option %d", i) + if strings.HasPrefix(pushOpt, "fedid=") { + fedUserID := strings.TrimPrefix(pushOpt, "fedid=") + service, username, found := strings.Cut(fedUserID, ":") + if !found { + writeRedError(sshStderr, "Invalid federated user identifier %#v does not contain a colon", fedUserID) return 1 } - if strings.HasPrefix(push_option, "fedid=") { - federated_user_identifier := strings.TrimPrefix(push_option, "fedid=") - service, username, found := strings.Cut(federated_user_identifier, ":") - if !found { - wf_error(ssh_stderr, "Invalid federated user identifier %#v does not contain a colon", federated_user_identifier) - return 1 - } - - ok, err := check_and_update_federated_user_status(ctx, pack_to_hook.user_id, service, username, pack_to_hook.pubkey) - if err != nil { - wf_error(ssh_stderr, "Failed to verify federated user identifier %#v: %v", federated_user_identifier, err) - return 1 - } - if !ok { - wf_error(ssh_stderr, "Failed to verify federated user identifier %#v: you don't seem to be on the list", federated_user_identifier) - return 1 - } - - break + + ok, err := fedauth(ctx, packPass.userID, service, username, packPass.pubkey) + if err != nil { + writeRedError(sshStderr, "Failed to verify federated user identifier %#v: %v", fedUserID, err) + return 1 } - if i == git_push_option_count-1 { - wf_error(ssh_stderr, "This repo requires contributors to be either federated or registered users. You must supply your federated user ID as a push option. For example, git push -o fedid=sr.ht:runxiyu") + if !ok { + writeRedError(sshStderr, "Failed to verify federated user identifier %#v: you don't seem to be on the list", fedUserID) return 1 } - } - } - - line, err = stdin.ReadString('\n') - if errors.Is(err, io.EOF) { - break - } else if err != nil { - wf_error(ssh_stderr, "Failed to read pre-receive line: %v", err) - return 1 - } - line = line[:len(line)-1] - old_oid, rest, found = strings.Cut(line, " ") - if !found { - wf_error(ssh_stderr, "Invalid pre-receive line: %v", line) - return 1 + break + } + if i == pushOptCount-1 { + writeRedError(sshStderr, "This repo requires contributors to be either federated or registered users. You must supply your federated user ID as a push option. For example, git push -o fedid=sr.ht:runxiyu") + return 1 + } } + } - new_oid, ref_name, found = strings.Cut(rest, " ") - if !found { - wf_error(ssh_stderr, "Invalid pre-receive line: %v", line) - return 1 - } + line, err = stdin.ReadString('\n') + if errors.Is(err, io.EOF) { + break + } else if err != nil { + writeRedError(sshStderr, "Failed to read pre-receive line: %v", err) + return 1 + } + line = line[:len(line)-1] - if strings.HasPrefix(ref_name, "refs/heads/contrib/") { - if all_zero_num_string(old_oid) { // New branch - fmt.Fprintln(ssh_stderr, ansiec.Blue+"POK"+ansiec.Reset, ref_name) - var new_mr_id int + oldOID, rest, found = strings.Cut(line, " ") + if !found { + writeRedError(sshStderr, "Invalid pre-receive line: %v", line) + return 1 + } - err = database.QueryRow(ctx, - "INSERT INTO merge_requests (repo_id, creator, source_ref, status) VALUES ($1, $2, $3, 'open') RETURNING id", - pack_to_hook.repo_id, pack_to_hook.user_id, strings.TrimPrefix(ref_name, "refs/heads/"), - ).Scan(&new_mr_id) - if err != nil { - wf_error(ssh_stderr, "Error creating merge request: %v", err) - return 1 - } - fmt.Fprintln(ssh_stderr, ansiec.Blue+"Created merge request at", generate_http_remote_url(pack_to_hook.group_path, pack_to_hook.repo_name)+"/contrib/"+strconv.FormatUint(uint64(new_mr_id), 10)+"/"+ansiec.Reset) - } else { // Existing contrib branch - var existing_merge_request_user_id int - var is_ancestor bool - - err = database.QueryRow(ctx, - "SELECT COALESCE(creator, 0) FROM merge_requests WHERE source_ref = $1 AND repo_id = $2", - strings.TrimPrefix(ref_name, "refs/heads/"), pack_to_hook.repo_id, - ).Scan(&existing_merge_request_user_id) - if err != nil { - if errors.Is(err, pgx.ErrNoRows) { - wf_error(ssh_stderr, "No existing merge request for existing contrib branch: %v", err) - } else { - wf_error(ssh_stderr, "Error querying for existing merge request: %v", err) - } - return 1 - } - if existing_merge_request_user_id == 0 { - all_ok = false - fmt.Fprintln(ssh_stderr, ansiec.Red+"NAK"+ansiec.Reset, ref_name, "(branch belongs to unowned MR)") - continue - } + newIOID, refName, found = strings.Cut(rest, " ") + if !found { + writeRedError(sshStderr, "Invalid pre-receive line: %v", line) + return 1 + } - if existing_merge_request_user_id != pack_to_hook.user_id { - all_ok = false - fmt.Fprintln(ssh_stderr, ansiec.Red+"NAK"+ansiec.Reset, ref_name, "(branch belongs another user's MR)") - continue + if strings.HasPrefix(refName, "refs/heads/contrib/") { + if allZero(oldOID) { // New branch + fmt.Fprintln(sshStderr, ansiec.Blue+"POK"+ansiec.Reset, refName) + var newMRID int + + err = database.QueryRow(ctx, + "INSERT INTO merge_requests (repo_id, creator, source_ref, status) VALUES ($1, $2, $3, 'open') RETURNING id", + packPass.repoID, packPass.userID, strings.TrimPrefix(refName, "refs/heads/"), + ).Scan(&newMRID) + if err != nil { + writeRedError(sshStderr, "Error creating merge request: %v", err) + return 1 + } + fmt.Fprintln(sshStderr, ansiec.Blue+"Created merge request at", genHTTPRemoteURL(packPass.groupPath, packPass.repoName)+"/contrib/"+strconv.FormatUint(uint64(newMRID), 10)+"/"+ansiec.Reset) + } else { // Existing contrib branch + var existingMRUser int + var isAncestor bool + + err = database.QueryRow(ctx, + "SELECT COALESCE(creator, 0) FROM merge_requests WHERE source_ref = $1 AND repo_id = $2", + strings.TrimPrefix(refName, "refs/heads/"), packPass.repoID, + ).Scan(&existingMRUser) + if err != nil { + if errors.Is(err, pgx.ErrNoRows) { + writeRedError(sshStderr, "No existing merge request for existing contrib branch: %v", err) + } else { + writeRedError(sshStderr, "Error querying for existing merge request: %v", err) } + return 1 + } + if existingMRUser == 0 { + allOK = false + fmt.Fprintln(sshStderr, ansiec.Red+"NAK"+ansiec.Reset, refName, "(branch belongs to unowned MR)") + continue + } - old_hash = plumbing.NewHash(old_oid) + if existingMRUser != packPass.userID { + allOK = false + fmt.Fprintln(sshStderr, ansiec.Red+"NAK"+ansiec.Reset, refName, "(branch belongs another user's MR)") + continue + } - if old_commit, err = pack_to_hook.repo.CommitObject(old_hash); err != nil { - wf_error(ssh_stderr, "Daemon failed to get old commit: %v", err) - return 1 - } + oldHash = plumbing.NewHash(oldOID) - // Potential BUG: I'm not sure if new_commit is guaranteed to be - // detectable as they haven't been merged into the main repo's - // objects yet. But it seems to work, and I don't think there's - // any reason for this to only work intermitently. - new_hash = plumbing.NewHash(new_oid) - if new_commit, err = pack_to_hook.repo.CommitObject(new_hash); err != nil { - wf_error(ssh_stderr, "Daemon failed to get new commit: %v", err) - return 1 - } + if oldCommit, err = packPass.repo.CommitObject(oldHash); err != nil { + writeRedError(sshStderr, "Daemon failed to get old commit: %v", err) + return 1 + } - if is_ancestor, err = old_commit.IsAncestor(new_commit); err != nil { - wf_error(ssh_stderr, "Daemon failed to check if old commit is ancestor: %v", err) - return 1 - } + // Potential BUG: I'm not sure if new_commit is guaranteed to be + // detectable as they haven't been merged into the main repo's + // objects yet. But it seems to work, and I don't think there's + // any reason for this to only work intermitently. + newHash = plumbing.NewHash(newIOID) + if newCommit, err = packPass.repo.CommitObject(newHash); err != nil { + writeRedError(sshStderr, "Daemon failed to get new commit: %v", err) + return 1 + } - if !is_ancestor { - // TODO: Create MR snapshot ref instead - all_ok = false - fmt.Fprintln(ssh_stderr, ansiec.Red+"NAK"+ansiec.Reset, ref_name, "(force pushes are not supported yet)") - continue - } + if isAncestor, err = oldCommit.IsAncestor(newCommit); err != nil { + writeRedError(sshStderr, "Daemon failed to check if old commit is ancestor: %v", err) + return 1 + } - fmt.Fprintln(ssh_stderr, ansiec.Blue+"POK"+ansiec.Reset, ref_name) + if !isAncestor { + // TODO: Create MR snapshot ref instead + allOK = false + fmt.Fprintln(sshStderr, ansiec.Red+"NAK"+ansiec.Reset, refName, "(force pushes are not supported yet)") + continue } - } else { // Non-contrib branch - all_ok = false - fmt.Fprintln(ssh_stderr, ansiec.Red+"NAK"+ansiec.Reset, ref_name, "(you cannot push to branches outside of contrib/*)") + + fmt.Fprintln(sshStderr, ansiec.Blue+"POK"+ansiec.Reset, refName) } + } else { // Non-contrib branch + allOK = false + fmt.Fprintln(sshStderr, ansiec.Red+"NAK"+ansiec.Reset, refName, "(you cannot push to branches outside of contrib/*)") } + } - fmt.Fprintln(ssh_stderr) - if all_ok { - fmt.Fprintln(ssh_stderr, "Overall "+ansiec.Green+"ACK"+ansiec.Reset+" (all checks passed)") - return 0 - } else { - fmt.Fprintln(ssh_stderr, "Overall "+ansiec.Red+"NAK"+ansiec.Reset+" (one or more branches failed checks)") - return 1 - } + fmt.Fprintln(sshStderr) + if allOK { + fmt.Fprintln(sshStderr, "Overall "+ansiec.Green+"ACK"+ansiec.Reset+" (all checks passed)") + return 0 } + fmt.Fprintln(sshStderr, "Overall "+ansiec.Red+"NAK"+ansiec.Reset+" (one or more branches failed checks)") + return 1 default: - fmt.Fprintln(ssh_stderr, ansiec.Red+"Invalid hook:", args[0]+ansiec.Reset) + fmt.Fprintln(sshStderr, ansiec.Red+"Invalid hook:", args[0]+ansiec.Reset) return 1 } }() - fmt.Fprintln(ssh_stderr) + fmt.Fprintln(sshStderr) - _, _ = conn.Write([]byte{hook_return_value}) + _, _ = conn.Write([]byte{hookRet}) } func serveGitHooks(listener net.Listener) error { @@ -325,26 +323,26 @@ func serveGitHooks(listener net.Listener) error { if err != nil { return err } - go hooks_handle_connection(conn) + go hooksHandler(conn) } } -func get_ucred(conn net.Conn) (ucred *syscall.Ucred, err error) { - var unix_conn *net.UnixConn = conn.(*net.UnixConn) +func getUcred(conn net.Conn) (ucred *syscall.Ucred, err error) { + var unixConn = conn.(*net.UnixConn) var fd *os.File - if fd, err = unix_conn.File(); err != nil { - return nil, err_get_fd + if fd, err = unixConn.File(); err != nil { + return nil, errGetFD } defer fd.Close() if ucred, err = syscall.GetsockoptUcred(int(fd.Fd()), syscall.SOL_SOCKET, syscall.SO_PEERCRED); err != nil { - return nil, err_get_ucred + return nil, errGetUcred } return ucred, nil } -func all_zero_num_string(s string) bool { +func allZero(s string) bool { for _, r := range s { if r != '0' { return false diff --git a/git_init.go b/git_init.go index 4313ee8..045ddd2 100644 --- a/git_init.go +++ b/git_init.go @@ -5,28 +5,28 @@ package main import ( "github.com/go-git/go-git/v5" - git_config "github.com/go-git/go-git/v5/config" - git_format_config "github.com/go-git/go-git/v5/plumbing/format/config" + gitConfig "github.com/go-git/go-git/v5/config" + gitFmtConfig "github.com/go-git/go-git/v5/plumbing/format/config" ) -// git_bare_init_with_default_hooks initializes a bare git repository with the +// gitInit initializes a bare git repository with the // forge-deployed hooks directory as the hooksPath. -func git_bare_init_with_default_hooks(repo_path string) (err error) { +func gitInit(repoPath string) (err error) { var repo *git.Repository - var git_config *git_config.Config + var gitConf *gitConfig.Config - if repo, err = git.PlainInit(repo_path, true); err != nil { + if repo, err = git.PlainInit(repoPath, true); err != nil { return err } - if git_config, err = repo.Config(); err != nil { + if gitConf, err = repo.Config(); err != nil { return err } - git_config.Raw.SetOption("core", git_format_config.NoSubsection, "hooksPath", config.Hooks.Execs) - git_config.Raw.SetOption("receive", git_format_config.NoSubsection, "advertisePushOptions", "true") + gitConf.Raw.SetOption("core", gitFmtConfig.NoSubsection, "hooksPath", config.Hooks.Execs) + gitConf.Raw.SetOption("receive", gitFmtConfig.NoSubsection, "advertisePushOptions", "true") - if err = repo.SetConfig(git_config); err != nil { + if err = repo.SetConfig(gitConf); err != nil { return err } diff --git a/git_misc.go b/git_misc.go index f2f8dba..cd2dc4d 100644 --- a/git_misc.go +++ b/git_misc.go @@ -16,9 +16,9 @@ import ( "github.com/jackc/pgx/v5/pgtype" ) -// open_git_repo opens a git repository by group and repo name. -func open_git_repo(ctx context.Context, group_path []string, repo_name string) (repo *git.Repository, description string, repo_id int, err error) { - var fs_path string +// openRepo opens a git repository by group and repo name. +func openRepo(ctx context.Context, groupPath []string, repoName string) (repo *git.Repository, description string, repoID int, err error) { + var fsPath string err = database.QueryRow(ctx, ` WITH RECURSIVE group_path_cte AS ( @@ -53,100 +53,100 @@ FROM group_path_cte g JOIN repos r ON r.group_id = g.id WHERE g.depth = cardinality($1::text[]) AND r.name = $2 - `, pgtype.FlatArray[string](group_path), repo_name).Scan(&fs_path, &description, &repo_id) + `, pgtype.FlatArray[string](groupPath), repoName).Scan(&fsPath, &description, &repoID) if err != nil { return } - repo, err = git.PlainOpen(fs_path) + repo, err = git.PlainOpen(fsPath) return } // go-git's tree entries are not friendly for use in HTML templates. -type display_git_tree_entry_t struct { - Name string - Mode string - Size int64 - Is_file bool - Is_subtree bool +type displayTreeEntry struct { + Name string + Mode string + Size int64 + IsFile bool + IsSubtree bool } -func build_display_git_tree(tree *object.Tree) (display_git_tree []display_git_tree_entry_t) { +func makeDisplayTree(tree *object.Tree) (displayTree []displayTreeEntry) { for _, entry := range tree.Entries { - display_git_tree_entry := display_git_tree_entry_t{} + displayEntry := displayTreeEntry{} var err error - var os_mode os.FileMode + var osMode os.FileMode - if os_mode, err = entry.Mode.ToOSFileMode(); err != nil { - display_git_tree_entry.Mode = "x---------" + if osMode, err = entry.Mode.ToOSFileMode(); err != nil { + displayEntry.Mode = "x---------" } else { - display_git_tree_entry.Mode = os_mode.String() + displayEntry.Mode = osMode.String() } - display_git_tree_entry.Is_file = entry.Mode.IsFile() + displayEntry.IsFile = entry.Mode.IsFile() - if display_git_tree_entry.Size, err = tree.Size(entry.Name); err != nil { - display_git_tree_entry.Size = 0 + if displayEntry.Size, err = tree.Size(entry.Name); err != nil { + displayEntry.Size = 0 } - display_git_tree_entry.Name = strings.TrimPrefix(entry.Name, "/") + displayEntry.Name = strings.TrimPrefix(entry.Name, "/") - display_git_tree = append(display_git_tree, display_git_tree_entry) + displayTree = append(displayTree, displayEntry) } - return display_git_tree + return displayTree } -func get_recent_commits(repo *git.Repository, head_hash plumbing.Hash, number_of_commits int) (recent_commits []*object.Commit, err error) { - var commit_iter object.CommitIter - var this_recent_commit *object.Commit +func getRecentCommits(repo *git.Repository, headHash plumbing.Hash, numCommits int) (recentCommits []*object.Commit, err error) { + var commitIter object.CommitIter + var thisCommit *object.Commit - commit_iter, err = repo.Log(&git.LogOptions{From: head_hash}) + commitIter, err = repo.Log(&git.LogOptions{From: headHash}) if err != nil { return nil, err } - recent_commits = make([]*object.Commit, 0) - defer commit_iter.Close() - if number_of_commits < 0 { + recentCommits = make([]*object.Commit, 0) + defer commitIter.Close() + if numCommits < 0 { for { - this_recent_commit, err = commit_iter.Next() + thisCommit, err = commitIter.Next() if errors.Is(err, io.EOF) { - return recent_commits, nil + return recentCommits, nil } else if err != nil { return nil, err } - recent_commits = append(recent_commits, this_recent_commit) + recentCommits = append(recentCommits, thisCommit) } } else { - for range number_of_commits { - this_recent_commit, err = commit_iter.Next() + for range numCommits { + thisCommit, err = commitIter.Next() if errors.Is(err, io.EOF) { - return recent_commits, nil + return recentCommits, nil } else if err != nil { return nil, err } - recent_commits = append(recent_commits, this_recent_commit) + recentCommits = append(recentCommits, thisCommit) } } - return recent_commits, err + return recentCommits, err } -func get_patch_from_commit(commit_object *object.Commit) (parent_commit_hash plumbing.Hash, patch *object.Patch, err error) { - var parent_commit_object *object.Commit - var commit_tree *object.Tree +func fmtCommitAsPatch(commit *object.Commit) (parentCommitHash plumbing.Hash, patch *object.Patch, err error) { + var parentCommit *object.Commit + var commitTree *object.Tree - parent_commit_object, err = commit_object.Parent(0) + parentCommit, err = commit.Parent(0) if errors.Is(err, object.ErrParentNotFound) { - if commit_tree, err = commit_object.Tree(); err != nil { + if commitTree, err = commit.Tree(); err != nil { return } - if patch, err = (&object.Tree{}).Patch(commit_tree); err != nil { + if patch, err = (&object.Tree{}).Patch(commitTree); err != nil { return } } else if err != nil { return } else { - parent_commit_hash = parent_commit_object.Hash - if patch, err = parent_commit_object.Patch(commit_object); err != nil { + parentCommitHash = parentCommit.Hash + if patch, err = parentCommit.Patch(commit); err != nil { return } } @@ -8,30 +8,30 @@ import ( "github.com/go-git/go-git/v5/plumbing" ) -// get_ref_hash_from_type_and_name returns the hash of a reference given its +// getRefHash returns the hash of a reference given its // type and name as supplied in URL queries. -func get_ref_hash_from_type_and_name(repo *git.Repository, ref_type, ref_name string) (ref_hash plumbing.Hash, err error) { +func getRefHash(repo *git.Repository, refType, refName string) (refHash plumbing.Hash, err error) { var ref *plumbing.Reference - switch ref_type { + switch refType { case "": if ref, err = repo.Head(); err != nil { return } - ref_hash = ref.Hash() + refHash = ref.Hash() case "commit": - ref_hash = plumbing.NewHash(ref_name) + refHash = plumbing.NewHash(refName) case "branch": - if ref, err = repo.Reference(plumbing.NewBranchReferenceName(ref_name), true); err != nil { + if ref, err = repo.Reference(plumbing.NewBranchReferenceName(refName), true); err != nil { return } - ref_hash = ref.Hash() + refHash = ref.Hash() case "tag": - if ref, err = repo.Reference(plumbing.NewTagReferenceName(ref_name), true); err != nil { + if ref, err = repo.Reference(plumbing.NewTagReferenceName(refName), true); err != nil { return } - ref_hash = ref.Hash() + refHash = ref.Hash() default: - panic("Invalid ref type " + ref_type) + panic("Invalid ref type " + refType) } return } diff --git a/http_auth.go b/http_auth.go index 4868aa4..db7df09 100644 --- a/http_auth.go +++ b/http_auth.go @@ -7,17 +7,17 @@ import ( "net/http" ) -func get_user_info_from_request(r *http.Request) (id int, username string, err error) { - var session_cookie *http.Cookie +func getUserFromRequest(r *http.Request) (id int, username string, err error) { + var sessionCookie *http.Cookie - if session_cookie, err = r.Cookie("session"); err != nil { + if sessionCookie, err = r.Cookie("session"); err != nil { return } err = database.QueryRow( r.Context(), "SELECT user_id, COALESCE(username, '') FROM users u JOIN sessions s ON u.id = s.user_id WHERE s.session_id = $1;", - session_cookie.Value, + sessionCookie.Value, ).Scan(&id, &username) return diff --git a/http_global.go b/http_global.go index 20a1cbc..f793c24 100644 --- a/http_global.go +++ b/http_global.go @@ -3,10 +3,10 @@ package main -// global_data is passed as "global" when rendering HTML templates. -var global_data = map[string]any{ - "server_public_key_string": &server_public_key_string, - "server_public_key_fingerprint": &server_public_key_fingerprint, +// globalData is passed as "global" when rendering HTML templates. +var globalData = map[string]any{ + "server_public_key_string": &serverPubkeyString, + "server_public_key_fingerprint": &serverPubkeyFP, "forge_version": VERSION, // Some other ones are populated after config parsing } diff --git a/http_handle_gc.go b/http_handle_gc.go index 3f7717c..b00ef8a 100644 --- a/http_handle_gc.go +++ b/http_handle_gc.go @@ -8,7 +8,7 @@ import ( "runtime" ) -func handle_gc(w http.ResponseWriter, r *http.Request, params map[string]any) { +func httpHandleGC(w http.ResponseWriter, r *http.Request, params map[string]any) { runtime.GC() http.Redirect(w, r, "/", http.StatusSeeOther) } diff --git a/http_handle_group_index.go b/http_handle_group_index.go index 911363b..51e4773 100644 --- a/http_handle_group_index.go +++ b/http_handle_group_index.go @@ -12,15 +12,15 @@ import ( "github.com/jackc/pgx/v5/pgtype" ) -func handle_group_index(w http.ResponseWriter, r *http.Request, params map[string]any) { - var group_path []string - var repos []name_desc_t - var subgroups []name_desc_t +func httpHandleGroupIndex(w http.ResponseWriter, r *http.Request, params map[string]any) { + var groupPath []string + var repos []nameDesc + var subgroups []nameDesc var err error - var group_id int - var group_description string + var groupID int + var groupDesc string - group_path = params["group_path"].([]string) + groupPath = params["group_path"].([]string) // The group itself err = database.QueryRow(r.Context(), ` @@ -51,8 +51,8 @@ func handle_group_index(w http.ResponseWriter, r *http.Request, params map[strin JOIN groups g ON g.id = c.id WHERE c.depth = cardinality($1::text[]) `, - pgtype.FlatArray[string](group_path), - ).Scan(&group_id, &group_description) + pgtype.FlatArray[string](groupPath), + ).Scan(&groupID, &groupDesc) if err == pgx.ErrNoRows { http.Error(w, "Group not found", http.StatusNotFound) @@ -69,64 +69,64 @@ func handle_group_index(w http.ResponseWriter, r *http.Request, params map[strin FROM user_group_roles WHERE user_id = $1 AND group_id = $2 - `, params["user_id"].(int), group_id).Scan(&count) + `, params["user_id"].(int), groupID).Scan(&count) if err != nil { http.Error(w, "Error checking access: "+err.Error(), http.StatusInternalServerError) return } - direct_access := (count > 0) + directAccess := (count > 0) if r.Method == "POST" { - if !direct_access { + if !directAccess { http.Error(w, "You do not have direct access to this group", http.StatusForbidden) return } - repo_name := r.FormValue("repo_name") - repo_description := r.FormValue("repo_desc") - contrib_requirements := r.FormValue("repo_contrib") - if repo_name == "" { + repoName := r.FormValue("repo_name") + repoDesc := r.FormValue("repo_desc") + contribReq := r.FormValue("repo_contrib") + if repoName == "" { http.Error(w, "Repo name is required", http.StatusBadRequest) return } - var new_repo_id int + var newRepoID int err := database.QueryRow( r.Context(), `INSERT INTO repos (name, description, group_id, contrib_requirements) VALUES ($1, $2, $3, $4) RETURNING id`, - repo_name, - repo_description, - group_id, - contrib_requirements, - ).Scan(&new_repo_id) + repoName, + repoDesc, + groupID, + contribReq, + ).Scan(&newRepoID) if err != nil { http.Error(w, "Error creating repo: "+err.Error(), http.StatusInternalServerError) return } - file_path := filepath.Join(config.Git.RepoDir, strconv.Itoa(new_repo_id)+".git") + filePath := filepath.Join(config.Git.RepoDir, strconv.Itoa(newRepoID)+".git") _, err = database.Exec( r.Context(), `UPDATE repos SET filesystem_path = $1 WHERE id = $2`, - file_path, - new_repo_id, + filePath, + newRepoID, ) if err != nil { http.Error(w, "Error updating repo path: "+err.Error(), http.StatusInternalServerError) return } - if err = git_bare_init_with_default_hooks(file_path); err != nil { + if err = gitInit(filePath); err != nil { http.Error(w, "Error initializing repo: "+err.Error(), http.StatusInternalServerError) return } - redirect_unconditionally(w, r) + redirectUnconditionally(w, r) return } @@ -136,7 +136,7 @@ func handle_group_index(w http.ResponseWriter, r *http.Request, params map[strin SELECT name, COALESCE(description, '') FROM repos WHERE group_id = $1 - `, group_id) + `, groupID) if err != nil { http.Error(w, "Error getting repos: "+err.Error(), http.StatusInternalServerError) return @@ -149,7 +149,7 @@ func handle_group_index(w http.ResponseWriter, r *http.Request, params map[strin http.Error(w, "Error getting repos: "+err.Error(), http.StatusInternalServerError) return } - repos = append(repos, name_desc_t{name, description}) + repos = append(repos, nameDesc{name, description}) } if err = rows.Err(); err != nil { http.Error(w, "Error getting repos: "+err.Error(), http.StatusInternalServerError) @@ -161,7 +161,7 @@ func handle_group_index(w http.ResponseWriter, r *http.Request, params map[strin SELECT name, COALESCE(description, '') FROM groups WHERE parent_group = $1 - `, group_id) + `, groupID) if err != nil { http.Error(w, "Error getting subgroups: "+err.Error(), http.StatusInternalServerError) return @@ -174,7 +174,7 @@ func handle_group_index(w http.ResponseWriter, r *http.Request, params map[strin http.Error(w, "Error getting subgroups: "+err.Error(), http.StatusInternalServerError) return } - subgroups = append(subgroups, name_desc_t{name, description}) + subgroups = append(subgroups, nameDesc{name, description}) } if err = rows.Err(); err != nil { http.Error(w, "Error getting subgroups: "+err.Error(), http.StatusInternalServerError) @@ -183,8 +183,8 @@ func handle_group_index(w http.ResponseWriter, r *http.Request, params map[strin params["repos"] = repos params["subgroups"] = subgroups - params["description"] = group_description - params["direct_access"] = direct_access + params["description"] = groupDesc + params["direct_access"] = directAccess - render_template(w, "group", params) + renderTemplate(w, "group", params) } diff --git a/http_handle_index.go b/http_handle_index.go index 144e9ab..2fc99a6 100644 --- a/http_handle_index.go +++ b/http_handle_index.go @@ -10,11 +10,11 @@ import ( "github.com/dustin/go-humanize" ) -func handle_index(w http.ResponseWriter, r *http.Request, params map[string]any) { +func httpHandleIndex(w http.ResponseWriter, r *http.Request, params map[string]any) { var err error - var groups []name_desc_t + var groups []nameDesc - groups, err = query_name_desc_list(r.Context(), "SELECT name, COALESCE(description, '') FROM groups WHERE parent_group IS NULL") + groups, err = queryNameDesc(r.Context(), "SELECT name, COALESCE(description, '') FROM groups WHERE parent_group IS NULL") if err != nil { http.Error(w, "Error querying groups: "+err.Error(), http.StatusInternalServerError) return @@ -25,5 +25,5 @@ func handle_index(w http.ResponseWriter, r *http.Request, params map[string]any) memstats := runtime.MemStats{} runtime.ReadMemStats(&memstats) params["mem"] = humanize.IBytes(memstats.Alloc) - render_template(w, "index", params) + renderTemplate(w, "index", params) } diff --git a/http_handle_login.go b/http_handle_login.go index 1d23d3c..1c5c066 100644 --- a/http_handle_login.go +++ b/http_handle_login.go @@ -15,19 +15,19 @@ import ( "github.com/jackc/pgx/v5" ) -func handle_login(w http.ResponseWriter, r *http.Request, params map[string]any) { +func httpHandleLogin(w http.ResponseWriter, r *http.Request, params map[string]any) { var username, password string - var user_id int - var password_hash string + var userID int + var passwordHash string var err error - var password_matches bool - var cookie_value string + var passwordMatches bool + var cookieValue string var now time.Time var expiry time.Time var cookie http.Cookie if r.Method != "POST" { - render_template(w, "login", params) + renderTemplate(w, "login", params) return } @@ -37,34 +37,34 @@ func handle_login(w http.ResponseWriter, r *http.Request, params map[string]any) err = database.QueryRow(r.Context(), "SELECT id, COALESCE(password, '') FROM users WHERE username = $1", username, - ).Scan(&user_id, &password_hash) + ).Scan(&userID, &passwordHash) if err != nil { if errors.Is(err, pgx.ErrNoRows) { params["login_error"] = "Unknown username" - render_template(w, "login", params) + renderTemplate(w, "login", params) return } http.Error(w, "Error querying user information: "+err.Error(), http.StatusInternalServerError) return } - if password_hash == "" { + if passwordHash == "" { params["login_error"] = "User has no password" - render_template(w, "login", params) + renderTemplate(w, "login", params) return } - if password_matches, err = argon2id.ComparePasswordAndHash(password, password_hash); err != nil { + if passwordMatches, err = argon2id.ComparePasswordAndHash(password, passwordHash); err != nil { http.Error(w, "Error comparing password and hash: "+err.Error(), http.StatusInternalServerError) return } - if !password_matches { + if !passwordMatches { params["login_error"] = "Invalid password" - render_template(w, "login", params) + renderTemplate(w, "login", params) return } - if cookie_value, err = random_urlsafe_string(16); err != nil { + if cookieValue, err = randomUrlsafeStr(16); err != nil { http.Error(w, "Error getting random string: "+err.Error(), http.StatusInternalServerError) return } @@ -74,7 +74,7 @@ func handle_login(w http.ResponseWriter, r *http.Request, params map[string]any) cookie = http.Cookie{ Name: "session", - Value: cookie_value, + Value: cookieValue, SameSite: http.SameSiteLaxMode, HttpOnly: true, Secure: false, // TODO @@ -85,7 +85,7 @@ func handle_login(w http.ResponseWriter, r *http.Request, params map[string]any) http.SetCookie(w, &cookie) - _, err = database.Exec(r.Context(), "INSERT INTO sessions (user_id, session_id) VALUES ($1, $2)", user_id, cookie_value) + _, err = database.Exec(r.Context(), "INSERT INTO sessions (user_id, session_id) VALUES ($1, $2)", userID, cookieValue) if err != nil { http.Error(w, "Error inserting session: "+err.Error(), http.StatusInternalServerError) return @@ -94,10 +94,10 @@ func handle_login(w http.ResponseWriter, r *http.Request, params map[string]any) http.Redirect(w, r, "/", http.StatusSeeOther) } -// random_urlsafe_string generates a random string of the given entropic size +// randomUrlsafeStr generates a random string of the given entropic size // using the URL-safe base64 encoding. The actual size of the string returned // will be 4*sz. -func random_urlsafe_string(sz int) (string, error) { +func randomUrlsafeStr(sz int) (string, error) { r := make([]byte, 3*sz) _, err := rand.Read(r) if err != nil { diff --git a/http_handle_repo_commit.go b/http_handle_repo_commit.go index 8f2204e..2f0fa51 100644 --- a/http_handle_repo_commit.go +++ b/http_handle_repo_commit.go @@ -18,125 +18,125 @@ import ( // The file patch type from go-git isn't really usable in HTML templates // either. -type usable_file_patch_t struct { +type usableFilePatch struct { From diff.File To diff.File - Chunks []usable_chunk + Chunks []usableChunk } -type usable_chunk struct { +type usableChunk struct { Operation diff.Operation Content string } -func handle_repo_commit(w http.ResponseWriter, r *http.Request, params map[string]any) { +func httpHandleRepoCommit(w http.ResponseWriter, r *http.Request, params map[string]any) { 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 commitIDStrSpec, commitIDStrSpecNoSuffix string + var commitID plumbing.Hash + var parentCommitHash plumbing.Hash + var commitObj *object.Commit + var commitIDStr string var err error var patch *object.Patch - repo, commit_id_specified_string = params["repo"].(*git.Repository), params["commit_id"].(string) + repo, commitIDStrSpec = 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 { + commitIDStrSpecNoSuffix = strings.TrimSuffix(commitIDStrSpec, ".patch") + commitID = plumbing.NewHash(commitIDStrSpecNoSuffix) + if commitObj, err = repo.CommitObject(commitID); 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 { - var formatted_patch string - if formatted_patch, err = format_patch_from_commit(commit_object); err != nil { + if commitIDStrSpecNoSuffix != commitIDStrSpec { + var patchStr string + if patchStr, err = fmtCommitPatch(commitObj); err != nil { http.Error(w, "Error formatting patch: "+err.Error(), http.StatusInternalServerError) return } - fmt.Fprintln(w, formatted_patch) + fmt.Fprintln(w, patchStr) return } - commit_id_string = commit_object.Hash.String() + commitIDStr = commitObj.Hash.String() - if commit_id_string != commit_id_specified_string { - http.Redirect(w, r, commit_id_string, http.StatusSeeOther) + if commitIDStr != commitIDStrSpec { + http.Redirect(w, r, commitIDStr, http.StatusSeeOther) return } - params["commit_object"] = commit_object - params["commit_id"] = commit_id_string + params["commit_object"] = commitObj + params["commit_id"] = commitIDStr - parent_commit_hash, patch, err = get_patch_from_commit(commit_object) + parentCommitHash, patch, err = fmtCommitAsPatch(commitObj) if err != nil { http.Error(w, "Error getting patch from commit: "+err.Error(), http.StatusInternalServerError) return } - params["parent_commit_hash"] = parent_commit_hash.String() + params["parent_commitHash"] = parentCommitHash.String() params["patch"] = patch - params["file_patches"] = make_usable_file_patches(patch) + params["file_patches"] = makeUsableFilePatches(patch) - render_template(w, "repo_commit", params) + renderTemplate(w, "repo_commit", params) } -type fake_diff_file struct { +type fakeDiffFile struct { hash plumbing.Hash mode filemode.FileMode path string } -func (f fake_diff_file) Hash() plumbing.Hash { +func (f fakeDiffFile) Hash() plumbing.Hash { return f.hash } -func (f fake_diff_file) Mode() filemode.FileMode { +func (f fakeDiffFile) Mode() filemode.FileMode { return f.mode } -func (f fake_diff_file) Path() string { +func (f fakeDiffFile) Path() string { return f.path } -var fake_diff_file_null = fake_diff_file{ +var nullFakeDiffFile = fakeDiffFile{ hash: plumbing.NewHash("0000000000000000000000000000000000000000"), mode: misc.First_or_panic(filemode.New("100644")), path: "", } -func make_usable_file_patches(patch diff.Patch) (usable_file_patches []usable_file_patch_t) { +func makeUsableFilePatches(patch diff.Patch) (usableFilePatches []usableFilePatch) { // TODO: Remove unnecessary context // TODO: Prepend "+"/"-"/" " instead of solely distinguishing based on color - for _, file_patch := range patch.FilePatches() { + for _, filePatch := range patch.FilePatches() { var from, to diff.File - var usable_file_patch usable_file_patch_t - chunks := []usable_chunk{} + var ufp usableFilePatch + chunks := []usableChunk{} - from, to = file_patch.Files() + from, to = filePatch.Files() if from == nil { - from = fake_diff_file_null + from = nullFakeDiffFile } if to == nil { - to = fake_diff_file_null + to = nullFakeDiffFile } - for _, chunk := range file_patch.Chunks() { + for _, chunk := range filePatch.Chunks() { 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> - chunks = append(chunks, usable_chunk{ + chunks = append(chunks, usableChunk{ Operation: chunk.Type(), Content: content, }) } - usable_file_patch = usable_file_patch_t{ + ufp = usableFilePatch{ Chunks: chunks, From: from, To: to, } - usable_file_patches = append(usable_file_patches, usable_file_patch) + usableFilePatches = append(usableFilePatches, ufp) } return } diff --git a/http_handle_repo_contrib_index.go b/http_handle_repo_contrib_index.go index f352a3f..41c4d18 100644 --- a/http_handle_repo_contrib_index.go +++ b/http_handle_repo_contrib_index.go @@ -9,15 +9,15 @@ import ( "github.com/jackc/pgx/v5" ) -type id_title_status_t struct { +type idTitleStatus struct { ID int Title string Status string } -func handle_repo_contrib_index(w http.ResponseWriter, r *http.Request, params map[string]any) { +func httpHandleRepoContribIndex(w http.ResponseWriter, r *http.Request, params map[string]any) { var rows pgx.Rows - var result []id_title_status_t + var result []idTitleStatus var err error if rows, err = database.Query(r.Context(), @@ -36,7 +36,7 @@ func handle_repo_contrib_index(w http.ResponseWriter, r *http.Request, params ma http.Error(w, "Error scanning merge request: "+err.Error(), http.StatusInternalServerError) return } - result = append(result, id_title_status_t{id, title, status}) + result = append(result, idTitleStatus{id, title, status}) } if err = rows.Err(); err != nil { http.Error(w, "Error ranging over merge requests: "+err.Error(), http.StatusInternalServerError) @@ -44,5 +44,5 @@ func handle_repo_contrib_index(w http.ResponseWriter, r *http.Request, params ma } params["merge_requests"] = result - render_template(w, "repo_contrib_index", params) + renderTemplate(w, "repo_contrib_index", params) } diff --git a/http_handle_repo_contrib_one.go b/http_handle_repo_contrib_one.go index f4db20c..74bd2ca 100644 --- a/http_handle_repo_contrib_one.go +++ b/http_handle_repo_contrib_one.go @@ -12,77 +12,77 @@ import ( "github.com/go-git/go-git/v5/plumbing/object" ) -func handle_repo_contrib_one(w http.ResponseWriter, r *http.Request, params map[string]any) { - var mr_id_string string - var mr_id int +func httpHandleRepoContribOne(w http.ResponseWriter, r *http.Request, params map[string]any) { + var mrIDStr string + var mrIDInt int var err error - var title, status, source_ref, destination_branch string + var title, status, srcRefStr, dstBranchStr string var repo *git.Repository - var source_ref_hash plumbing.Hash - var source_commit, destination_commit, merge_base *object.Commit - var merge_bases []*object.Commit + var srcRefHash plumbing.Hash + var dstBranchHash plumbing.Hash + var srcCommit, dstCommit, mergeBaseCommit *object.Commit + var mergeBases []*object.Commit - mr_id_string = params["mr_id"].(string) - mr_id_int64, err := strconv.ParseInt(mr_id_string, 10, strconv.IntSize) + mrIDStr = params["mr_id"].(string) + mrIDInt64, err := strconv.ParseInt(mrIDStr, 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) + mrIDInt = int(mrIDInt64) 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 { + mrIDInt, + ).Scan(&title, &status, &srcRefStr, &dstBranchStr); err != nil { http.Error(w, "Error querying merge request: "+err.Error(), http.StatusInternalServerError) return } repo = params["repo"].(*git.Repository) - if source_ref_hash, err = get_ref_hash_from_type_and_name(repo, "branch", source_ref); err != nil { + if srcRefHash, err = getRefHash(repo, "branch", srcRefStr); err != nil { http.Error(w, "Error getting source ref hash: "+err.Error(), http.StatusInternalServerError) return } - if source_commit, err = repo.CommitObject(source_ref_hash); err != nil { + if srcCommit, err = repo.CommitObject(srcRefHash); err != nil { http.Error(w, "Error getting source commit: "+err.Error(), http.StatusInternalServerError) return } - params["source_commit"] = source_commit + params["source_commit"] = srcCommit - var destination_branch_hash plumbing.Hash - if destination_branch == "" { - destination_branch = "HEAD" - destination_branch_hash, err = get_ref_hash_from_type_and_name(repo, "", "") + if dstBranchStr == "" { + dstBranchStr = "HEAD" + dstBranchHash, err = getRefHash(repo, "", "") } else { - destination_branch_hash, err = get_ref_hash_from_type_and_name(repo, "branch", destination_branch) + dstBranchHash, err = getRefHash(repo, "branch", dstBranchStr) } if err != nil { http.Error(w, "Error getting destination branch hash: "+err.Error(), http.StatusInternalServerError) return } - if destination_commit, err = repo.CommitObject(destination_branch_hash); err != nil { + if dstCommit, err = repo.CommitObject(dstBranchHash); err != nil { http.Error(w, "Error getting destination commit: "+err.Error(), http.StatusInternalServerError) return } - params["destination_commit"] = destination_commit + params["destination_commit"] = dstCommit - if merge_bases, err = source_commit.MergeBase(destination_commit); err != nil { + if mergeBases, err = srcCommit.MergeBase(dstCommit); err != nil { http.Error(w, "Error getting merge base: "+err.Error(), http.StatusInternalServerError) return } - merge_base = merge_bases[0] - params["merge_base"] = merge_base + mergeBaseCommit = mergeBases[0] + params["merge_base"] = mergeBaseCommit - patch, err := merge_base.Patch(source_commit) + patch, err := mergeBaseCommit.Patch(srcCommit) if err != nil { http.Error(w, "Error getting patch: "+err.Error(), http.StatusInternalServerError) return } - params["file_patches"] = make_usable_file_patches(patch) + params["file_patches"] = makeUsableFilePatches(patch) - params["mr_title"], params["mr_status"], params["mr_source_ref"], params["mr_destination_branch"] = title, status, source_ref, destination_branch + params["mr_title"], params["mr_status"], params["mr_source_ref"], params["mr_destination_branch"] = title, status, srcRefStr, dstBranchStr - render_template(w, "repo_contrib_one", params) + renderTemplate(w, "repo_contrib_one", params) } diff --git a/http_handle_repo_index.go b/http_handle_repo_index.go index e4a9f5d..cb3fcdc 100644 --- a/http_handle_repo_index.go +++ b/http_handle_repo_index.go @@ -9,64 +9,64 @@ import ( "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing" - "github.com/go-git/go-git/v5/plumbing/storer" "github.com/go-git/go-git/v5/plumbing/object" + "github.com/go-git/go-git/v5/plumbing/storer" ) -func handle_repo_index(w http.ResponseWriter, r *http.Request, params map[string]any) { +func httpHandleRepoIndex(w http.ResponseWriter, r *http.Request, params map[string]any) { var repo *git.Repository - var repo_name string - var group_path []string - var ref_hash plumbing.Hash + var repoName string + var groupPath []string + var refHash plumbing.Hash var err error - var recent_commits []*object.Commit - var commit_object *object.Commit + var recentCommits []*object.Commit + var commitObj *object.Commit var tree *object.Tree var notes []string var branches []string - var branches_ storer.ReferenceIter + var branchesIter storer.ReferenceIter - repo, repo_name, group_path = params["repo"].(*git.Repository), params["repo_name"].(string), params["group_path"].([]string) + repo, repoName, groupPath = params["repo"].(*git.Repository), params["repo_name"].(string), params["group_path"].([]string) - if strings.Contains(repo_name, "\n") || slice_contains_newline(group_path) { + if strings.Contains(repoName, "\n") || sliceContainsNewlines(groupPath) { notes = append(notes, "Path contains newlines; HTTP Git access impossible") } - ref_hash, err = get_ref_hash_from_type_and_name(repo, params["ref_type"].(string), params["ref_name"].(string)) + refHash, err = getRefHash(repo, params["ref_type"].(string), params["ref_name"].(string)) if err != nil { goto no_ref } - branches_, err = repo.Branches() - if err != nil {} - err = branches_.ForEach(func (branch *plumbing.Reference) error { - branches = append(branches, branch.Name().Short()) - return nil - }) - if err != nil {} + branchesIter, err = repo.Branches() + if err == nil { + _ = branchesIter.ForEach(func(branch *plumbing.Reference) error { + branches = append(branches, branch.Name().Short()) + return nil + }) + } params["branches"] = branches - if recent_commits, err = get_recent_commits(repo, ref_hash, 3); err != nil { + if recentCommits, err = getRecentCommits(repo, refHash, 3); err != nil { goto no_ref } - params["commits"] = recent_commits + params["commits"] = recentCommits - if commit_object, err = repo.CommitObject(ref_hash); err != nil { + if commitObj, err = repo.CommitObject(refHash); err != nil { goto no_ref } - if tree, err = commit_object.Tree(); err != nil { + if tree, err = commitObj.Tree(); err != nil { goto no_ref } - params["files"] = build_display_git_tree(tree) - params["readme_filename"], params["readme"] = render_readme_at_tree(tree) + params["files"] = makeDisplayTree(tree) + params["readme_filename"], params["readme"] = renderReadmeAtTree(tree) no_ref: - params["http_clone_url"] = generate_http_remote_url(group_path, repo_name) - params["ssh_clone_url"] = generate_ssh_remote_url(group_path, repo_name) + params["http_clone_url"] = genHTTPRemoteURL(groupPath, repoName) + params["ssh_clone_url"] = genSSHRemoteURL(groupPath, repoName) params["notes"] = notes - render_template(w, "repo_index", params) + renderTemplate(w, "repo_index", params) } diff --git a/http_handle_repo_info.go b/http_handle_repo_info.go index 466e0bb..44689c7 100644 --- a/http_handle_repo_info.go +++ b/http_handle_repo_info.go @@ -12,9 +12,9 @@ import ( "github.com/jackc/pgx/v5/pgtype" ) -func handle_repo_info(w http.ResponseWriter, r *http.Request, params map[string]any) (err error) { - var group_path []string - var repo_name, repo_path string +func httpHandleRepoInfo(w http.ResponseWriter, r *http.Request, params map[string]any) (err error) { + var groupPath []string + var repoName, repoPath string if err := database.QueryRow(r.Context(), ` WITH RECURSIVE group_path_cte AS ( @@ -47,16 +47,16 @@ func handle_repo_info(w http.ResponseWriter, r *http.Request, params map[string] WHERE c.depth = cardinality($1::text[]) AND r.name = $2 `, - pgtype.FlatArray[string](group_path), - repo_name, - ).Scan(&repo_path); err != nil { + pgtype.FlatArray[string](groupPath), + repoName, + ).Scan(&repoPath); err != nil { return err } w.Header().Set("Content-Type", "application/x-git-upload-pack-advertisement") w.WriteHeader(http.StatusOK) - cmd := exec.Command("git", "upload-pack", "--stateless-rpc", "--advertise-refs", repo_path) + cmd := exec.Command("git", "upload-pack", "--stateless-rpc", "--advertise-refs", repoPath) stdout, err := cmd.StdoutPipe() if err != nil { return err @@ -70,11 +70,11 @@ func handle_repo_info(w http.ResponseWriter, r *http.Request, params map[string] return err } - if err = pack_line(w, "# service=git-upload-pack\n"); err != nil { + if err = packLine(w, "# service=git-upload-pack\n"); err != nil { return err } - if err = pack_flush(w); err != nil { + if err = packFlush(w); err != nil { return } @@ -90,13 +90,13 @@ func handle_repo_info(w http.ResponseWriter, r *http.Request, params map[string] } // Taken from https://github.com/icyphox/legit, MIT license -func pack_line(w io.Writer, s string) error { +func packLine(w io.Writer, s string) error { _, err := fmt.Fprintf(w, "%04x%s", len(s)+4, s) return err } // Taken from https://github.com/icyphox/legit, MIT license -func pack_flush(w io.Writer) error { +func packFlush(w io.Writer) error { _, err := fmt.Fprint(w, "0000") return err } diff --git a/http_handle_repo_log.go b/http_handle_repo_log.go index 65719e4..39be231 100644 --- a/http_handle_repo_log.go +++ b/http_handle_repo_log.go @@ -12,24 +12,24 @@ import ( ) // TODO: I probably shouldn't include *all* commits here... -func handle_repo_log(w http.ResponseWriter, r *http.Request, params map[string]any) { +func httpHandleRepoLog(w http.ResponseWriter, r *http.Request, params map[string]any) { var repo *git.Repository - var ref_hash plumbing.Hash + var refHash plumbing.Hash var err error var commits []*object.Commit 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 { + if refHash, err = getRefHash(repo, params["ref_type"].(string), params["ref_name"].(string)); err != nil { http.Error(w, "Error getting ref hash: "+err.Error(), http.StatusInternalServerError) return } - if commits, err = get_recent_commits(repo, ref_hash, -1); err != nil { + if commits, err = getRecentCommits(repo, refHash, -1); err != nil { http.Error(w, "Error getting recent commits: "+err.Error(), http.StatusInternalServerError) return } params["commits"] = commits - render_template(w, "repo_log", params) + renderTemplate(w, "repo_log", params) } diff --git a/http_handle_repo_raw.go b/http_handle_repo_raw.go index a0f7430..e398856 100644 --- a/http_handle_repo_raw.go +++ b/http_handle_repo_raw.go @@ -14,62 +14,62 @@ import ( "github.com/go-git/go-git/v5/plumbing/object" ) -func handle_repo_raw(w http.ResponseWriter, r *http.Request, params map[string]any) { - var raw_path_spec, path_spec string +func httpHandleRepoRaw(w http.ResponseWriter, r *http.Request, params map[string]any) { + var rawPathSpec, pathSpec string var repo *git.Repository - var ref_hash plumbing.Hash - var commit_object *object.Commit + var refHash plumbing.Hash + var commitObj *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 + rawPathSpec = params["rest"].(string) + repo, pathSpec = params["repo"].(*git.Repository), strings.TrimSuffix(rawPathSpec, "/") + params["path_spec"] = pathSpec - if ref_hash, err = get_ref_hash_from_type_and_name(repo, params["ref_type"].(string), params["ref_name"].(string)); err != nil { + if refHash, err = getRefHash(repo, params["ref_type"].(string), params["ref_name"].(string)); err != nil { http.Error(w, "Error getting ref hash: "+err.Error(), http.StatusInternalServerError) return } - if commit_object, err = repo.CommitObject(ref_hash); err != nil { + if commitObj, err = repo.CommitObject(refHash); err != nil { http.Error(w, "Error getting commit object: "+err.Error(), http.StatusInternalServerError) return } - if tree, err = commit_object.Tree(); err != nil { + if tree, err = commitObj.Tree(); err != nil { http.Error(w, "Error getting file tree: "+err.Error(), http.StatusInternalServerError) return } var target *object.Tree - if path_spec == "" { + if pathSpec == "" { target = tree } else { - if target, err = tree.Tree(path_spec); err != nil { + if target, err = tree.Tree(pathSpec); err != nil { var file *object.File - var file_contents string - if file, err = tree.File(path_spec); err != nil { + var fileContent string + if file, err = tree.File(pathSpec); err != nil { http.Error(w, "Error retrieving path: "+err.Error(), http.StatusInternalServerError) return } - if len(raw_path_spec) != 0 && raw_path_spec[len(raw_path_spec)-1] == '/' { - http.Redirect(w, r, "../"+path_spec, http.StatusSeeOther) + if len(rawPathSpec) != 0 && rawPathSpec[len(rawPathSpec)-1] == '/' { + http.Redirect(w, r, "../"+pathSpec, http.StatusSeeOther) return } - if file_contents, err = file.Contents(); err != nil { + if fileContent, err = file.Contents(); err != nil { http.Error(w, "Error reading file: "+err.Error(), http.StatusInternalServerError) return } - fmt.Fprint(w, file_contents) + fmt.Fprint(w, fileContent) return } } - if len(raw_path_spec) != 0 && raw_path_spec[len(raw_path_spec)-1] != '/' { - http.Redirect(w, r, path.Base(path_spec)+"/", http.StatusSeeOther) + if len(rawPathSpec) != 0 && rawPathSpec[len(rawPathSpec)-1] != '/' { + http.Redirect(w, r, path.Base(pathSpec)+"/", http.StatusSeeOther) return } - params["files"] = build_display_git_tree(target) + params["files"] = makeDisplayTree(target) - render_template(w, "repo_raw_dir", params) + renderTemplate(w, "repo_raw_dir", params) } diff --git a/http_handle_repo_tree.go b/http_handle_repo_tree.go index ae9efbc..a1a3ca4 100644 --- a/http_handle_repo_tree.go +++ b/http_handle_repo_tree.go @@ -11,94 +11,94 @@ import ( "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" + chromaHTML "github.com/alecthomas/chroma/v2/formatters/html" + chromaLexers "github.com/alecthomas/chroma/v2/lexers" + chromaStyles "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) { - var raw_path_spec, path_spec string +func httpHandleRepoTree(w http.ResponseWriter, r *http.Request, params map[string]any) { + var rawPathSpec, pathSpec string var repo *git.Repository - var ref_hash plumbing.Hash - var commit_object *object.Commit + var refHash plumbing.Hash + var commitObject *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 + rawPathSpec = params["rest"].(string) + repo, pathSpec = params["repo"].(*git.Repository), strings.TrimSuffix(rawPathSpec, "/") + params["path_spec"] = pathSpec - if ref_hash, err = get_ref_hash_from_type_and_name(repo, params["ref_type"].(string), params["ref_name"].(string)); err != nil { + if refHash, err = getRefHash(repo, params["ref_type"].(string), params["ref_name"].(string)); err != nil { http.Error(w, "Error getting ref hash: "+err.Error(), http.StatusInternalServerError) return } - if commit_object, err = repo.CommitObject(ref_hash); err != nil { + if commitObject, err = repo.CommitObject(refHash); err != nil { http.Error(w, "Error getting commit object: "+err.Error(), http.StatusInternalServerError) return } - if tree, err = commit_object.Tree(); err != nil { + if tree, err = commitObject.Tree(); err != nil { http.Error(w, "Error getting file tree: "+err.Error(), http.StatusInternalServerError) return } var target *object.Tree - if path_spec == "" { + if pathSpec == "" { target = tree } else { - if target, err = tree.Tree(path_spec); err != nil { + if target, err = tree.Tree(pathSpec); err != nil { var file *object.File - var file_contents string + var fileContent string var lexer chroma.Lexer var iterator chroma.Iterator var style *chroma.Style - var formatter *chroma_formatters_html.Formatter - var formatted_encapsulated template.HTML + var formatter *chromaHTML.Formatter + var formattedHTML template.HTML - if file, err = tree.File(path_spec); err != nil { + if file, err = tree.File(pathSpec); err != nil { http.Error(w, "Error retrieving path: "+err.Error(), http.StatusInternalServerError) return } - if len(raw_path_spec) != 0 && raw_path_spec[len(raw_path_spec)-1] == '/' { - http.Redirect(w, r, "../"+path_spec, http.StatusSeeOther) + if len(rawPathSpec) != 0 && rawPathSpec[len(rawPathSpec)-1] == '/' { + http.Redirect(w, r, "../"+pathSpec, http.StatusSeeOther) return } - if file_contents, err = file.Contents(); err != nil { + if fileContent, err = file.Contents(); err != nil { http.Error(w, "Error reading file: "+err.Error(), http.StatusInternalServerError) return } - lexer = chroma_lexers.Match(path_spec) + lexer = chromaLexers.Match(pathSpec) if lexer == nil { - lexer = chroma_lexers.Fallback + lexer = chromaLexers.Fallback } - if iterator, err = lexer.Tokenise(nil, file_contents); err != nil { + if iterator, err = lexer.Tokenise(nil, fileContent); 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)) - if err = formatter.Format(&formatted_unencapsulated, style, iterator); err != nil { + var formattedHTMLStr bytes.Buffer + style = chromaStyles.Get("autumn") + formatter = chromaHTML.New(chromaHTML.WithClasses(true), chromaHTML.TabWidth(8)) + if err = formatter.Format(&formattedHTMLStr, style, iterator); err != nil { http.Error(w, "Error formatting code: "+err.Error(), http.StatusInternalServerError) return } - formatted_encapsulated = template.HTML(formatted_unencapsulated.Bytes()) //#nosec G203 - params["file_contents"] = formatted_encapsulated + formattedHTML = template.HTML(formattedHTMLStr.Bytes()) //#nosec G203 + params["file_contents"] = formattedHTML - render_template(w, "repo_tree_file", params) + renderTemplate(w, "repo_tree_file", params) return } } - if len(raw_path_spec) != 0 && raw_path_spec[len(raw_path_spec)-1] != '/' { - http.Redirect(w, r, path.Base(path_spec)+"/", http.StatusSeeOther) + if len(rawPathSpec) != 0 && rawPathSpec[len(rawPathSpec)-1] != '/' { + http.Redirect(w, r, path.Base(pathSpec)+"/", http.StatusSeeOther) return } - params["readme_filename"], params["readme"] = render_readme_at_tree(target) - params["files"] = build_display_git_tree(target) + params["readme_filename"], params["readme"] = renderReadmeAtTree(target) + params["files"] = makeDisplayTree(target) - render_template(w, "repo_tree_dir", params) + renderTemplate(w, "repo_tree_dir", params) } diff --git a/http_handle_repo_upload_pack.go b/http_handle_repo_upload_pack.go index 04ffe57..a88297d 100644 --- a/http_handle_repo_upload_pack.go +++ b/http_handle_repo_upload_pack.go @@ -12,15 +12,15 @@ import ( "github.com/jackc/pgx/v5/pgtype" ) -func handle_upload_pack(w http.ResponseWriter, r *http.Request, params map[string]any) (err error) { - var group_path []string - var repo_name string - var repo_path string +func httpHandleUploadPack(w http.ResponseWriter, r *http.Request, params map[string]any) (err error) { + var groupPath []string + var repoName string + var repoPath string var stdout io.ReadCloser var stdin io.WriteCloser var cmd *exec.Cmd - group_path, repo_name = params["group_path"].([]string), params["repo_name"].(string) + groupPath, repoName = params["group_path"].([]string), params["repo_name"].(string) if err := database.QueryRow(r.Context(), ` WITH RECURSIVE group_path_cte AS ( @@ -53,9 +53,9 @@ func handle_upload_pack(w http.ResponseWriter, r *http.Request, params map[strin WHERE c.depth = cardinality($1::text[]) AND r.name = $2 `, - pgtype.FlatArray[string](group_path), - repo_name, - ).Scan(&repo_path); err != nil { + pgtype.FlatArray[string](groupPath), + repoName, + ).Scan(&repoPath); err != nil { return err } @@ -64,7 +64,7 @@ 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", repoPath) cmd.Env = append(os.Environ(), "LINDENII_FORGE_HOOKS_SOCKET_PATH="+config.Hooks.Socket) if stdout, err = cmd.StdoutPipe(); err != nil { return err diff --git a/http_handle_users.go b/http_handle_users.go index 8a66d56..c657bad 100644 --- a/http_handle_users.go +++ b/http_handle_users.go @@ -7,6 +7,6 @@ import ( "net/http" ) -func handle_users(w http.ResponseWriter, r *http.Request, params map[string]any) { +func httpHandleUsers(w http.ResponseWriter, r *http.Request, params map[string]any) { http.Error(w, "Not implemented", http.StatusNotImplemented) } diff --git a/http_server.go b/http_server.go index 9548a65..afe8000 100644 --- a/http_server.go +++ b/http_server.go @@ -21,42 +21,42 @@ func (router *httpRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) { var segments []string var err error - var non_empty_last_segments_len int - var separator_index int + var contentfulSegmentsLen int + var sepIndex int params := make(map[string]any) - if segments, _, err = parse_request_uri(r.RequestURI); err != nil { + if segments, _, err = parseReqURI(r.RequestURI); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } - non_empty_last_segments_len = len(segments) + contentfulSegmentsLen = len(segments) if segments[len(segments)-1] == "" { - non_empty_last_segments_len-- + contentfulSegmentsLen-- } if segments[0] == ":" { if len(segments) < 2 { http.Error(w, "Blank system endpoint", http.StatusNotFound) return - } else if len(segments) == 2 && redirect_with_slash(w, r) { + } else if len(segments) == 2 && redirectDir(w, r) { return } switch segments[1] { case "static": - static_handler.ServeHTTP(w, r) + staticHandler.ServeHTTP(w, r) return case "source": - source_handler.ServeHTTP(w, r) + sourceHandler.ServeHTTP(w, r) return } } params["url_segments"] = segments - params["global"] = global_data - var _user_id int // 0 for none - _user_id, params["username"], err = get_user_info_from_request(r) - params["user_id"] = _user_id + params["global"] = globalData + var userID int // 0 for none + userID, params["username"], err = getUserFromRequest(r) + params["user_id"] = userID if errors.Is(err, http.ErrNoCookie) { } else if errors.Is(err, pgx.ErrNoRows) { } else if err != nil { @@ -64,22 +64,22 @@ func (router *httpRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } - if _user_id == 0 { + if userID == 0 { params["user_id_string"] = "" } else { - params["user_id_string"] = strconv.Itoa(_user_id) + params["user_id_string"] = strconv.Itoa(userID) } if segments[0] == ":" { switch segments[1] { case "login": - handle_login(w, r, params) + httpHandleLogin(w, r, params) return case "users": - handle_users(w, r, params) + httpHandleUsers(w, r, params) return case "gc": - handle_gc(w, r, params) + httpHandleGC(w, r, params) return default: http.Error(w, fmt.Sprintf("Unknown system module type: %s", segments[1]), http.StatusNotFound) @@ -87,65 +87,65 @@ func (router *httpRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } - separator_index = -1 + sepIndex = -1 for i, part := range segments { if part == ":" { - separator_index = i + sepIndex = i break } } - params["separator_index"] = separator_index + params["separator_index"] = sepIndex - var group_path []string - var module_type string - var module_name string + var groupPath []string + var moduleType string + var moduleName string - if separator_index > 0 { - group_path = segments[:separator_index] + if sepIndex > 0 { + groupPath = segments[:sepIndex] } else { - group_path = segments[:len(segments)-1] + groupPath = segments[:len(segments)-1] } - params["group_path"] = group_path + params["group_path"] = groupPath switch { - case non_empty_last_segments_len == 0: - handle_index(w, r, params) - case separator_index == -1: - if redirect_with_slash(w, r) { + case contentfulSegmentsLen == 0: + httpHandleIndex(w, r, params) + case sepIndex == -1: + if redirectDir(w, r) { return } - handle_group_index(w, r, params) - case non_empty_last_segments_len == separator_index+1: + httpHandleGroupIndex(w, r, params) + case contentfulSegmentsLen == sepIndex+1: http.Error(w, "Illegal path 1", http.StatusNotImplemented) return - case non_empty_last_segments_len == separator_index+2: + case contentfulSegmentsLen == sepIndex+2: http.Error(w, "Illegal path 2", http.StatusNotImplemented) return default: - module_type = segments[separator_index+1] - module_name = segments[separator_index+2] - switch module_type { + moduleType = segments[sepIndex+1] + moduleName = segments[sepIndex+2] + switch moduleType { case "repos": - params["repo_name"] = module_name + params["repo_name"] = moduleName - if non_empty_last_segments_len > separator_index+3 { - switch segments[separator_index+3] { + if contentfulSegmentsLen > sepIndex+3 { + switch segments[sepIndex+3] { case "info": - if err = handle_repo_info(w, r, params); err != nil { + if err = httpHandleRepoInfo(w, r, params); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } return case "git-upload-pack": - if err = handle_upload_pack(w, r, params); err != nil { + if err = httpHandleUploadPack(w, r, params); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } return } } - if params["ref_type"], params["ref_name"], err = get_param_ref_and_type(r); err != nil { - if errors.Is(err, err_no_ref_spec) { + if params["ref_type"], params["ref_name"], err = getParamRefTypeName(r); err != nil { + if errors.Is(err, errNoRefSpec) { params["ref_type"] = "" } else { http.Error(w, "Error querying ref type: "+err.Error(), http.StatusInternalServerError) @@ -155,66 +155,66 @@ func (router *httpRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) { // TODO: subgroups - if params["repo"], params["repo_description"], params["repo_id"], err = open_git_repo(r.Context(), group_path, module_name); err != nil { + if params["repo"], params["repo_description"], params["repo_id"], err = openRepo(r.Context(), groupPath, moduleName); err != nil { http.Error(w, "Error opening repo: "+err.Error(), http.StatusInternalServerError) return } - if non_empty_last_segments_len == separator_index+3 { - if redirect_with_slash(w, r) { + if contentfulSegmentsLen == sepIndex+3 { + if redirectDir(w, r) { return } - handle_repo_index(w, r, params) + httpHandleRepoIndex(w, r, params) return } - repo_feature := segments[separator_index+3] - switch repo_feature { + repoFeature := segments[sepIndex+3] + switch repoFeature { case "tree": - params["rest"] = strings.Join(segments[separator_index+4:], "/") - if len(segments) < separator_index+5 && redirect_with_slash(w, r) { + params["rest"] = strings.Join(segments[sepIndex+4:], "/") + if len(segments) < sepIndex+5 && redirectDir(w, r) { return } - handle_repo_tree(w, r, params) + httpHandleRepoTree(w, r, params) case "raw": - params["rest"] = strings.Join(segments[separator_index+4:], "/") - if len(segments) < separator_index+5 && redirect_with_slash(w, r) { + params["rest"] = strings.Join(segments[sepIndex+4:], "/") + if len(segments) < sepIndex+5 && redirectDir(w, r) { return } - handle_repo_raw(w, r, params) + httpHandleRepoRaw(w, r, params) case "log": - if non_empty_last_segments_len > separator_index+4 { + if contentfulSegmentsLen > sepIndex+4 { http.Error(w, "Too many parameters", http.StatusBadRequest) return } - if redirect_with_slash(w, r) { + if redirectDir(w, r) { return } - handle_repo_log(w, r, params) + httpHandleRepoLog(w, r, params) case "commit": - if redirect_without_slash(w, r) { + if redirectNoDir(w, r) { return } - params["commit_id"] = segments[separator_index+4] - handle_repo_commit(w, r, params) + params["commit_id"] = segments[sepIndex+4] + httpHandleRepoCommit(w, r, params) case "contrib": - if redirect_with_slash(w, r) { + if redirectDir(w, r) { return } - switch non_empty_last_segments_len { - case separator_index + 4: - handle_repo_contrib_index(w, r, params) - case separator_index + 5: - params["mr_id"] = segments[separator_index+4] - handle_repo_contrib_one(w, r, params) + switch contentfulSegmentsLen { + case sepIndex + 4: + httpHandleRepoContribIndex(w, r, params) + case sepIndex + 5: + params["mr_id"] = segments[sepIndex+4] + httpHandleRepoContribOne(w, r, params) default: http.Error(w, "Too many parameters", http.StatusBadRequest) } default: - http.Error(w, fmt.Sprintf("Unknown repo feature: %s", repo_feature), http.StatusNotFound) + http.Error(w, fmt.Sprintf("Unknown repo feature: %s", repoFeature), http.StatusNotFound) } default: - http.Error(w, fmt.Sprintf("Unknown module type: %s", module_type), http.StatusNotFound) + http.Error(w, fmt.Sprintf("Unknown module type: %s", moduleType), http.StatusNotFound) } } } diff --git a/http_template.go b/http_template.go index 377ad4c..e8520a9 100644 --- a/http_template.go +++ b/http_template.go @@ -5,10 +5,10 @@ package main import "net/http" -// render_template abstracts out the annoyances of reporting template rendering +// renderTemplate abstracts out the annoyances of reporting template rendering // errors. -func render_template(w http.ResponseWriter, template_name string, params map[string]any) { - if err := templates.ExecuteTemplate(w, template_name, params); err != nil { +func renderTemplate(w http.ResponseWriter, templateName string, params map[string]any) { + if err := templates.ExecuteTemplate(w, templateName, params); err != nil { http.Error(w, "Error rendering template: "+err.Error(), http.StatusInternalServerError) } } diff --git a/http_template_funcs.go b/http_template_funcs.go index 016d268..9609e61 100644 --- a/http_template_funcs.go +++ b/http_template_funcs.go @@ -9,19 +9,19 @@ import ( "strings" ) -func first_line(s string) string { +func firstLine(s string) string { before, _, _ := strings.Cut(s, "\n") return before } -func base_name(s string) string { +func baseName(s string) string { return path.Base(s) } -func path_escape(s string) string { +func pathEscape(s string) string { return url.PathEscape(s) } -func query_escape(s string) string { +func queryEscape(s string) string { return url.QueryEscape(s) } diff --git a/readme_to_html.go b/readme_to_html.go index 29c61ca..a7c7cb6 100644 --- a/readme_to_html.go +++ b/readme_to_html.go @@ -16,50 +16,50 @@ import ( "github.com/yuin/goldmark/extension" ) -var markdown_converter = goldmark.New(goldmark.WithExtensions(extension.GFM)) +var markdownConverter = goldmark.New(goldmark.WithExtensions(extension.GFM)) -func render_readme_at_tree(tree *object.Tree) (readme_filename string, readme_content template.HTML) { - var readme_rendered_unsafe bytes.Buffer - var readme_file *object.File - var readme_file_contents string +func renderReadmeAtTree(tree *object.Tree) (readmeFilename string, readmeRenderedSafeHTML template.HTML) { + var readmeRenderedUnsafe bytes.Buffer + var readmeFile *object.File + var readmeFileContents string var err error - if readme_file, err = tree.File("README"); err == nil { - if readme_file_contents, err = readme_file.Contents(); err != nil { - return "Error fetching README", string_escape_html("Unable to fetch contents of README: " + err.Error()) + if readmeFile, err = tree.File("README"); err == nil { + if readmeFileContents, err = readmeFile.Contents(); err != nil { + return "Error fetching README", escapeHTML("Unable to fetch contents of README: " + err.Error()) } - return "README", template.HTML("<pre>" + html.EscapeString(readme_file_contents) + "</pre>") //#nosec G203 + return "README", template.HTML("<pre>" + html.EscapeString(readmeFileContents) + "</pre>") //#nosec G203 } - if readme_file, err = tree.File("README.md"); err == nil { - if readme_file_contents, err = readme_file.Contents(); err != nil { - return "Error fetching README", string_escape_html("Unable to fetch contents of README: " + err.Error()) + if readmeFile, err = tree.File("README.md"); err == nil { + if readmeFileContents, err = readmeFile.Contents(); err != nil { + return "Error fetching README", escapeHTML("Unable to fetch contents of README: " + err.Error()) } - if err = markdown_converter.Convert([]byte(readme_file_contents), &readme_rendered_unsafe); err != nil { - return "Error fetching README", string_escape_html("Unable to render README: " + err.Error()) + if err = markdownConverter.Convert([]byte(readmeFileContents), &readmeRenderedUnsafe); err != nil { + return "Error fetching README", escapeHTML("Unable to render README: " + err.Error()) } - return "README.md", template.HTML(bluemonday.UGCPolicy().SanitizeBytes(readme_rendered_unsafe.Bytes())) //#nosec G203 + return "README.md", template.HTML(bluemonday.UGCPolicy().SanitizeBytes(readmeRenderedUnsafe.Bytes())) //#nosec G203 } - if readme_file, err = tree.File("README.org"); err == nil { - if readme_file_contents, err = readme_file.Contents(); err != nil { - return "Error fetching README", string_escape_html("Unable to fetch contents of README: " + err.Error()) + if readmeFile, err = tree.File("README.org"); err == nil { + if readmeFileContents, err = readmeFile.Contents(); err != nil { + return "Error fetching README", escapeHTML("Unable to fetch contents of README: " + err.Error()) } - org_html, err := org.New().Parse(strings.NewReader(readme_file_contents), readme_filename).Write(org.NewHTMLWriter()) + orgHTML, err := org.New().Parse(strings.NewReader(readmeFileContents), readmeFilename).Write(org.NewHTMLWriter()) if err != nil { - return "Error fetching README", string_escape_html("Unable to render README: " + err.Error()) + return "Error fetching README", escapeHTML("Unable to render README: " + err.Error()) } - return "README.org", template.HTML(bluemonday.UGCPolicy().Sanitize(org_html)) //#nosec G203 + return "README.org", template.HTML(bluemonday.UGCPolicy().Sanitize(orgHTML)) //#nosec G203 } return "", "" } -func string_escape_html(s string) template.HTML { +func escapeHTML(s string) template.HTML { return template.HTML(html.EscapeString(s)) //#nosec G203 } diff --git a/remote_url.go b/remote_url.go index 506e35c..f5ebfcd 100644 --- a/remote_url.go +++ b/remote_url.go @@ -10,10 +10,10 @@ import ( // We don't use path.Join because it collapses multiple slashes into one. -func generate_ssh_remote_url(group_path []string, repo_name string) string { - return strings.TrimSuffix(config.SSH.Root, "/") + "/" + path_escape_cat_segments(group_path) + "/:/repos/" + url.PathEscape(repo_name) +func genSSHRemoteURL(groupPath []string, repoName string) string { + return strings.TrimSuffix(config.SSH.Root, "/") + "/" + segmentsToURL(groupPath) + "/:/repos/" + url.PathEscape(repoName) } -func generate_http_remote_url(group_path []string, repo_name string) string { - return strings.TrimSuffix(config.HTTP.Root, "/") + "/" + path_escape_cat_segments(group_path) + "/:/repos/" + url.PathEscape(repo_name) +func genHTTPRemoteURL(groupPath []string, repoName string) string { + return strings.TrimSuffix(config.HTTP.Root, "/") + "/" + segmentsToURL(groupPath) + "/:/repos/" + url.PathEscape(repoName) } diff --git a/resources.go b/resources.go index 1a2b706..d689e6f 100644 --- a/resources.go +++ b/resources.go @@ -20,18 +20,18 @@ import ( //go:embed *.go go.mod go.sum //go:embed *.scfg //go:embed Makefile -//go:embed static/* templates/* scripts/* sql/* +//go:embed static/* htmpl/* templates/* scripts/* sql/* //go:embed hookc/*.c //go:embed vendor/* -var source_fs embed.FS +var sourceFS embed.FS -var source_handler = http.StripPrefix( +var sourceHandler = http.StripPrefix( "/:/source/", - http.FileServer(http.FS(source_fs)), + http.FileServer(http.FS(sourceFS)), ) //go:embed templates/* static/* hookc/hookc -var resources_fs embed.FS +var resourcesFS embed.FS var templates *template.Template @@ -40,18 +40,18 @@ func loadTemplates() (err error) { m.Add("text/html", &html.Minifier{TemplateDelims: [2]string{"{{", "}}"}, KeepDefaultAttrVals: true}) templates = template.New("templates").Funcs(template.FuncMap{ - "first_line": first_line, - "base_name": base_name, - "path_escape": path_escape, - "query_escape": query_escape, + "first_line": firstLine, + "base_name": baseName, + "path_escape": pathEscape, + "query_escape": queryEscape, }) - err = fs.WalkDir(resources_fs, "templates", func(path string, d fs.DirEntry, err error) error { + err = fs.WalkDir(resourcesFS, "templates", func(path string, d fs.DirEntry, err error) error { if err != nil { return err } if !d.IsDir() { - content, err := fs.ReadFile(resources_fs, path) + content, err := fs.ReadFile(resourcesFS, path) if err != nil { return err } @@ -71,12 +71,12 @@ func loadTemplates() (err error) { return err } -var static_handler http.Handler +var staticHandler http.Handler func init() { - static_fs, err := fs.Sub(resources_fs, "static") + staticFS, err := fs.Sub(resourcesFS, "static") if err != nil { panic(err) } - static_handler = http.StripPrefix("/:/static/", http.FileServer(http.FS(static_fs))) + staticHandler = http.StripPrefix("/:/static/", http.FileServer(http.FS(staticFS))) } diff --git a/ssh_handle_receive_pack.go b/ssh_handle_receive_pack.go index b77b717..ed1f765 100644 --- a/ssh_handle_receive_pack.go +++ b/ssh_handle_receive_pack.go @@ -9,107 +9,107 @@ import ( "os" "os/exec" - glider_ssh "github.com/gliderlabs/ssh" + gliderSSH "github.com/gliderlabs/ssh" "github.com/go-git/go-git/v5" "go.lindenii.runxiyu.org/lindenii-common/cmap" ) -type pack_to_hook_t struct { - session glider_ssh.Session - repo *git.Repository - pubkey string - direct_access bool - repo_path string - user_id int - user_type string - repo_id int - group_path []string - repo_name string - contrib_requirements string +type packPass struct { + session gliderSSH.Session + repo *git.Repository + pubkey string + directAccess bool + repoPath string + userID int + userType string + repoID int + groupPath []string + repoName string + contribReq string } -var pack_to_hook_by_cookie = cmap.Map[string, pack_to_hook_t]{} +var packPasses = cmap.Map[string, packPass]{} -// ssh_handle_receive_pack handles attempts to push to repos. -func ssh_handle_receive_pack(session glider_ssh.Session, pubkey string, repo_identifier string) (err error) { - group_path, repo_name, repo_id, repo_path, direct_access, contrib_requirements, user_type, user_id, err := get_repo_path_perms_from_ssh_path_pubkey(session.Context(), repo_identifier, pubkey) +// sshHandleRecvPack handles attempts to push to repos. +func sshHandleRecvPack(session gliderSSH.Session, pubkey, repoIdentifier string) (err error) { + groupPath, repoName, repoID, repoPath, directAccess, contribReq, userType, userID, err := getRepoInfo2(session.Context(), repoIdentifier, pubkey) if err != nil { return err } - repo, err := git.PlainOpen(repo_path) + repo, err := git.PlainOpen(repoPath) if err != nil { return err } - repo_config, err := repo.Config() + repoConf, err := repo.Config() if err != nil { return err } - repo_config_core := repo_config.Raw.Section("core") - if repo_config_core == nil { - return errors.New("Repository has no core section in config") + repoConfCore := repoConf.Raw.Section("core") + if repoConfCore == nil { + return errors.New("repository has no core section in config") } - hooksPath := repo_config_core.OptionAll("hooksPath") + hooksPath := repoConfCore.OptionAll("hooksPath") if len(hooksPath) != 1 || hooksPath[0] != config.Hooks.Execs { - return errors.New("Repository has hooksPath set to an unexpected value") + return errors.New("repository has hooksPath set to an unexpected value") } - if !direct_access { - switch contrib_requirements { + if !directAccess { + switch contribReq { case "closed": - if !direct_access { - return errors.New("You need direct access to push to this repo.") + if !directAccess { + return errors.New("you need direct access to push to this repo") } case "registered_user": - if user_type != "registered" { - return errors.New("You need to be a registered user to push to this repo.") + if userType != "registered" { + return errors.New("you need to be a registered user to push to this repo") } case "ssh_pubkey": fallthrough case "federated": if pubkey == "" { - return errors.New("You need to have an SSH public key to push to this repo.") + return errors.New("you need to have an SSH public key to push to this repo") } - if user_type == "" { - user_id, err = add_user_ssh(session.Context(), pubkey) + if userType == "" { + userID, err = addUserSSH(session.Context(), pubkey) if err != nil { return err } - fmt.Fprintln(session.Stderr(), "You are now registered as user ID", user_id) - user_type = "pubkey_only" + fmt.Fprintln(session.Stderr(), "you are now registered as user ID", userID) + userType = "pubkey_only" } case "public": default: - panic("unknown contrib_requirements value " + contrib_requirements) + panic("unknown contrib_requirements value " + contribReq) } } - cookie, err := random_urlsafe_string(16) + cookie, err := randomUrlsafeStr(16) if err != nil { fmt.Fprintln(session.Stderr(), "Error while generating cookie:", err) } - pack_to_hook_by_cookie.Store(cookie, pack_to_hook_t{ - session: session, - pubkey: pubkey, - direct_access: direct_access, - repo_path: repo_path, - user_id: user_id, - repo_id: repo_id, - group_path: group_path, - repo_name: repo_name, - repo: repo, - contrib_requirements: contrib_requirements, - user_type: user_type, + packPasses.Store(cookie, packPass{ + session: session, + pubkey: pubkey, + directAccess: directAccess, + repoPath: repoPath, + userID: userID, + repoID: repoID, + groupPath: groupPath, + repoName: repoName, + repo: repo, + contribReq: contribReq, + userType: userType, }) - defer pack_to_hook_by_cookie.Delete(cookie) + defer packPasses.Delete(cookie) // The Delete won't execute until proc.Wait returns unless something // horribly wrong such as a panic occurs. - proc := exec.CommandContext(session.Context(), "git-receive-pack", repo_path) + proc := exec.CommandContext(session.Context(), "git-receive-pack", repoPath) proc.Env = append(os.Environ(), "LINDENII_FORGE_HOOKS_SOCKET_PATH="+config.Hooks.Socket, "LINDENII_FORGE_HOOKS_COOKIE="+cookie, diff --git a/ssh_handle_upload_pack.go b/ssh_handle_upload_pack.go index fbbdfda..ab62533 100644 --- a/ssh_handle_upload_pack.go +++ b/ssh_handle_upload_pack.go @@ -11,15 +11,15 @@ import ( glider_ssh "github.com/gliderlabs/ssh" ) -// ssh_handle_upload_pack handles clones/fetches. It just uses git-upload-pack +// sshHandleUploadPack handles clones/fetches. It just uses git-upload-pack // and has no ACL checks. -func ssh_handle_upload_pack(session glider_ssh.Session, pubkey string, repo_identifier string) (err error) { - var repo_path string - if _, _, _, repo_path, _, _, _, _, err = get_repo_path_perms_from_ssh_path_pubkey(session.Context(), repo_identifier, pubkey); err != nil { +func sshHandleUploadPack(session glider_ssh.Session, pubkey, repoIdentifier string) (err error) { + var repoPath string + if _, _, _, repoPath, _, _, _, _, err = getRepoInfo2(session.Context(), repoIdentifier, pubkey); err != nil { return err } - proc := exec.CommandContext(session.Context(), "git-upload-pack", repo_path) + proc := exec.CommandContext(session.Context(), "git-upload-pack", repoPath) proc.Env = append(os.Environ(), "LINDENII_FORGE_HOOKS_SOCKET_PATH="+config.Hooks.Socket) proc.Stdin = session proc.Stdout = session diff --git a/ssh_server.go b/ssh_server.go index 7fd31c0..58a5acd 100644 --- a/ssh_server.go +++ b/ssh_server.go @@ -9,45 +9,45 @@ import ( "os" "strings" - glider_ssh "github.com/gliderlabs/ssh" + gliderSSH "github.com/gliderlabs/ssh" "go.lindenii.runxiyu.org/lindenii-common/ansiec" "go.lindenii.runxiyu.org/lindenii-common/clog" - go_ssh "golang.org/x/crypto/ssh" + goSSH "golang.org/x/crypto/ssh" ) var ( - server_public_key_string string - server_public_key_fingerprint string - server_public_key go_ssh.PublicKey + serverPubkeyString string + serverPubkeyFP string + serverPubkey goSSH.PublicKey ) func serveSSH(listener net.Listener) error { - var host_key_bytes []byte - var host_key go_ssh.Signer + var hostKeyBytes []byte + var hostKey goSSH.Signer var err error - var server *glider_ssh.Server + var server *gliderSSH.Server - if host_key_bytes, err = os.ReadFile(config.SSH.Key); err != nil { + if hostKeyBytes, err = os.ReadFile(config.SSH.Key); err != nil { return err } - if host_key, err = go_ssh.ParsePrivateKey(host_key_bytes); err != nil { + if hostKey, err = goSSH.ParsePrivateKey(hostKeyBytes); err != nil { return err } - server_public_key = host_key.PublicKey() - server_public_key_string = string(go_ssh.MarshalAuthorizedKey(server_public_key)) - server_public_key_fingerprint = go_ssh.FingerprintSHA256(server_public_key) + serverPubkey = hostKey.PublicKey() + serverPubkeyString = string(goSSH.MarshalAuthorizedKey(serverPubkey)) + serverPubkeyFP = goSSH.FingerprintSHA256(serverPubkey) - server = &glider_ssh.Server{ - Handler: func(session glider_ssh.Session) { - client_public_key := session.PublicKey() - var client_public_key_string string - if client_public_key != nil { - client_public_key_string = strings.TrimSuffix(string(go_ssh.MarshalAuthorizedKey(client_public_key)), "\n") + server = &gliderSSH.Server{ + Handler: func(session gliderSSH.Session) { + clientPubkey := session.PublicKey() + var clientPubkeyStr string + if clientPubkey != nil { + clientPubkeyStr = strings.TrimSuffix(string(goSSH.MarshalAuthorizedKey(clientPubkey)), "\n") } - clog.Info("Incoming SSH: " + session.RemoteAddr().String() + " " + client_public_key_string + " " + session.RawCommand()) + clog.Info("Incoming SSH: " + session.RemoteAddr().String() + " " + clientPubkeyStr + " " + session.RawCommand()) fmt.Fprintln(session.Stderr(), ansiec.Blue+"Lindenii Forge "+VERSION+", source at "+strings.TrimSuffix(config.HTTP.Root, "/")+"/:/source/"+ansiec.Reset+"\r") cmd := session.Command() @@ -63,13 +63,13 @@ func serveSSH(listener net.Listener) error { fmt.Fprintln(session.Stderr(), "Too many arguments\r") return } - err = ssh_handle_upload_pack(session, client_public_key_string, cmd[1]) + err = sshHandleUploadPack(session, clientPubkeyStr, cmd[1]) case "git-receive-pack": if len(cmd) > 2 { fmt.Fprintln(session.Stderr(), "Too many arguments\r") return } - err = ssh_handle_receive_pack(session, client_public_key_string, cmd[1]) + err = sshHandleRecvPack(session, clientPubkeyStr, cmd[1]) default: fmt.Fprintln(session.Stderr(), "Unsupported command: "+cmd[0]+"\r") return @@ -79,15 +79,15 @@ func serveSSH(listener net.Listener) error { return } }, - PublicKeyHandler: func(ctx glider_ssh.Context, key glider_ssh.PublicKey) bool { return true }, - KeyboardInteractiveHandler: func(ctx glider_ssh.Context, challenge go_ssh.KeyboardInteractiveChallenge) bool { return true }, + PublicKeyHandler: func(ctx gliderSSH.Context, key gliderSSH.PublicKey) bool { return true }, + KeyboardInteractiveHandler: func(ctx gliderSSH.Context, challenge goSSH.KeyboardInteractiveChallenge) bool { return true }, // It is intentional that we do not check any credentials and accept all connections. // This allows all users to connect and clone repositories. However, the public key // is passed to handlers, so e.g. the push handler could check the key and reject the // push if it needs to. } - server.AddHostKey(host_key) + server.AddHostKey(hostKey) if err = server.Serve(listener); err != nil { clog.Fatal(1, "Serving SSH: "+err.Error()) diff --git a/ssh_utils.go b/ssh_utils.go index d40facf..562234e 100644 --- a/ssh_utils.go +++ b/ssh_utils.go @@ -14,14 +14,14 @@ import ( "go.lindenii.runxiyu.org/lindenii-common/ansiec" ) -var err_ssh_illegal_endpoint = errors.New("illegal endpoint during SSH access") +var errIllegalSSHRepoPath = errors.New("illegal SSH repo path") -func get_repo_path_perms_from_ssh_path_pubkey(ctx context.Context, ssh_path string, ssh_pubkey string) (group_path []string, repo_name string, repo_id int, repo_path string, direct_access bool, contrib_requirements string, user_type string, user_id int, err error) { +func getRepoInfo2(ctx context.Context, sshPath, sshPubkey string) (groupPath []string, repoName string, repoID int, repoPath string, directAccess bool, contribReq, userType string, userID int, err error) { var segments []string - var separator_index int - var module_type, module_name string + var sepIndex int + var moduleType, moduleName string - segments = strings.Split(strings.TrimPrefix(ssh_path, "/"), "/") + segments = strings.Split(strings.TrimPrefix(sshPath, "/"), "/") for i, segment := range segments { var err error @@ -32,13 +32,13 @@ func get_repo_path_perms_from_ssh_path_pubkey(ctx context.Context, ssh_path stri } if segments[0] == ":" { - return []string{}, "", 0, "", false, "", "", 0, err_ssh_illegal_endpoint + return []string{}, "", 0, "", false, "", "", 0, errIllegalSSHRepoPath } - separator_index = -1 + sepIndex = -1 for i, part := range segments { if part == ":" { - separator_index = i + sepIndex = i break } } @@ -47,25 +47,25 @@ func get_repo_path_perms_from_ssh_path_pubkey(ctx context.Context, ssh_path stri } switch { - case separator_index == -1: - return []string{}, "", 0, "", false, "", "", 0, err_ssh_illegal_endpoint - case len(segments) <= separator_index+2: - return []string{}, "", 0, "", false, "", "", 0, err_ssh_illegal_endpoint + case sepIndex == -1: + return []string{}, "", 0, "", false, "", "", 0, errIllegalSSHRepoPath + case len(segments) <= sepIndex+2: + return []string{}, "", 0, "", false, "", "", 0, errIllegalSSHRepoPath } - group_path = segments[:separator_index] - module_type = segments[separator_index+1] - module_name = segments[separator_index+2] - repo_name = module_name - switch module_type { + groupPath = segments[:sepIndex] + moduleType = segments[sepIndex+1] + moduleName = segments[sepIndex+2] + repoName = moduleName + switch moduleType { case "repos": - _1, _2, _3, _4, _5, _6, _7 := get_path_perm_by_group_repo_key(ctx, group_path, module_name, ssh_pubkey) - return group_path, repo_name, _1, _2, _3, _4, _5, _6, _7 + _1, _2, _3, _4, _5, _6, _7 := getRepoInfo(ctx, groupPath, moduleName, sshPubkey) + return groupPath, repoName, _1, _2, _3, _4, _5, _6, _7 default: - return []string{}, "", 0, "", false, "", "", 0, err_ssh_illegal_endpoint + return []string{}, "", 0, "", false, "", "", 0, errIllegalSSHRepoPath } } -func wf_error(w io.Writer, format string, args ...any) { +func writeRedError(w io.Writer, format string, args ...any) { fmt.Fprintln(w, ansiec.Red+fmt.Sprintf(format, args...)+ansiec.Reset) } @@ -11,42 +11,41 @@ import ( ) var ( - err_duplicate_ref_spec = errors.New("duplicate ref spec") - err_no_ref_spec = errors.New("no ref spec") + errDupRefSpec = errors.New("duplicate ref spec") + errNoRefSpec = errors.New("no ref spec") ) -func get_param_ref_and_type(r *http.Request) (ref_type, ref string, err error) { +func getParamRefTypeName(r *http.Request) (retRefType, retRefName string, err error) { qr := r.URL.RawQuery q, err := url.ParseQuery(qr) if err != nil { return } done := false - for _, _ref_type := range []string{"commit", "branch", "tag"} { - _ref, ok := q[_ref_type] + for _, refType := range []string{"commit", "branch", "tag"} { + refName, ok := q[refType] if ok { if done { - err = err_duplicate_ref_spec + err = errDupRefSpec return - } else { - done = true - if len(_ref) != 1 { - err = err_duplicate_ref_spec - return - } - ref = _ref[0] - ref_type = _ref_type } + done = true + if len(refName) != 1 { + err = errDupRefSpec + return + } + retRefName = refName[0] + retRefType = refType } } if !done { - err = err_no_ref_spec + err = errNoRefSpec } return } -func parse_request_uri(request_uri string) (segments []string, params url.Values, err error) { - path, params_string, _ := strings.Cut(request_uri, "?") +func parseReqURI(requestURI string) (segments []string, params url.Values, err error) { + path, paramsStr, _ := strings.Cut(requestURI, "?") segments = strings.Split(strings.TrimPrefix(path, "/"), "/") @@ -57,20 +56,20 @@ func parse_request_uri(request_uri string) (segments []string, params url.Values } } - params, err = url.ParseQuery(params_string) + params, err = url.ParseQuery(paramsStr) return } -func redirect_with_slash(w http.ResponseWriter, r *http.Request) bool { - request_uri := r.RequestURI +func redirectDir(w http.ResponseWriter, r *http.Request) bool { + requestURI := r.RequestURI - path_end := strings.IndexAny(request_uri, "?#") + pathEnd := strings.IndexAny(requestURI, "?#") var path, rest string - if path_end == -1 { - path = request_uri + if pathEnd == -1 { + path = requestURI } else { - path = request_uri[:path_end] - rest = request_uri[path_end:] + path = requestURI[:pathEnd] + rest = requestURI[pathEnd:] } if !strings.HasSuffix(path, "/") { @@ -80,16 +79,16 @@ func redirect_with_slash(w http.ResponseWriter, r *http.Request) bool { return false } -func redirect_without_slash(w http.ResponseWriter, r *http.Request) bool { - request_uri := r.RequestURI +func redirectNoDir(w http.ResponseWriter, r *http.Request) bool { + requestURI := r.RequestURI - path_end := strings.IndexAny(request_uri, "?#") + pathEnd := strings.IndexAny(requestURI, "?#") var path, rest string - if path_end == -1 { - path = request_uri + if pathEnd == -1 { + path = requestURI } else { - path = request_uri[:path_end] - rest = request_uri[path_end:] + path = requestURI[:pathEnd] + rest = requestURI[pathEnd:] } if strings.HasSuffix(path, "/") { @@ -99,22 +98,22 @@ func redirect_without_slash(w http.ResponseWriter, r *http.Request) bool { return false } -func redirect_unconditionally(w http.ResponseWriter, r *http.Request) { - request_uri := r.RequestURI +func redirectUnconditionally(w http.ResponseWriter, r *http.Request) { + requestURI := r.RequestURI - path_end := strings.IndexAny(request_uri, "?#") + pathEnd := strings.IndexAny(requestURI, "?#") var path, rest string - if path_end == -1 { - path = request_uri + if pathEnd == -1 { + path = requestURI } else { - path = request_uri[:path_end] - rest = request_uri[path_end:] + path = requestURI[:pathEnd] + rest = requestURI[pathEnd:] } http.Redirect(w, r, path+rest, http.StatusSeeOther) } -func path_escape_cat_segments(segments []string) string { +func segmentsToURL(segments []string) string { for i, segment := range segments { segments[i] = url.PathEscape(segment) } @@ -9,7 +9,7 @@ import ( "github.com/jackc/pgx/v5" ) -func add_user_ssh(ctx context.Context, pubkey string) (user_id int, err error) { +func addUserSSH(ctx context.Context, pubkey string) (userID int, err error) { var tx pgx.Tx if tx, err = database.Begin(ctx); err != nil { @@ -19,11 +19,11 @@ func add_user_ssh(ctx context.Context, pubkey string) (user_id int, err error) { _ = tx.Rollback(ctx) }() - if err = tx.QueryRow(ctx, `INSERT INTO users (type) VALUES ('pubkey_only') RETURNING id`).Scan(&user_id); err != nil { + if err = tx.QueryRow(ctx, `INSERT INTO users (type) VALUES ('pubkey_only') RETURNING id`).Scan(&userID); err != nil { return } - if _, err = tx.Exec(ctx, `INSERT INTO ssh_public_keys (key_string, user_id) VALUES ($1, $2)`, pubkey, user_id); err != nil { + if _, err = tx.Exec(ctx, `INSERT INTO ssh_public_keys (key_string, user_id) VALUES ($1, $2)`, pubkey, userID); err != nil { return } @@ -5,7 +5,7 @@ package main import "strings" -func slice_contains_newline(s []string) bool { +func sliceContainsNewlines(s []string) bool { for _, v := range s { if strings.Contains(v, "\n") { return true |