From 54926a719de46b3c359f2f7ce3c29ff4fe55397b Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Wed, 19 Mar 2025 11:20:24 +0800 Subject: [PATCH] Remove underscores from Go code, pt 3 --- fedauth.go | 6 +++--- git_format_patch.go | 2 +- git_hooks_handle.go | 188 +++++++++++++++++++++++++++--------------------------- git_init.go | 20 ++++++++++---------- git_misc.go | 86 +++++++++++++++++++++++++++--------------------------- git_ref.go | 4 ++-- http_handle_group_index.go | 2 +- http_handle_repo_commit.go | 2 +- http_handle_repo_contrib_one.go | 6 +++--- http_handle_repo_index.go | 6 +++--- http_handle_repo_log.go | 4 ++-- http_handle_repo_raw.go | 4 ++-- http_handle_repo_tree.go | 4 ++-- http_server.go | 2 +- ssh_handle_receive_pack.go | 28 ++++++++++++++-------------- ssh_utils.go | 2 +- diff --git a/fedauth.go b/fedauth.go index 6687fe301c7f7a5b98b2a1ec826652154056244f..2084eff8f6cba46398f40b40f9bcd4a051ae97ef 100644 --- a/fedauth.go +++ b/fedauth.go @@ -15,7 +15,7 @@ "github.com/jackc/pgx/v5" ) -func fedauth(ctx context.Context, user_id int, service, remote_username, pubkey string) (bool, error) { +func fedauth(ctx context.Context, userID int, service, remote_username, pubkey string) (bool, error) { var err error var resp *http.Response matched := false @@ -74,10 +74,10 @@ } 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, remote_username); 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 3533ecad1d7bbedaefb76975998fb565281a6d41..56474d216245085c72883bcc280f9fae5e2761f7 100644 --- a/git_format_patch.go +++ b/git_format_patch.go @@ -21,7 +21,7 @@ var author object.Signature var date string var commitTitle, commitDetails string - if _, patch, err = get_patch_from_commit(commit); err != nil { + if _, patch, err = fmtCommitAsPatch(commit); err != nil { return "", err } diff --git a/git_hooks_handle.go b/git_hooks_handle.go index e02619fb1b8b8cd191d96dc2089959abfd78891b..b93138730cb64cccaabd42f94ada351db1020ea6 100644 --- a/git_hooks_handle.go +++ b/git_hooks_handle.go @@ -36,10 +36,10 @@ 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()) @@ -51,14 +51,14 @@ 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 @@ if _, err = conn.Read(cookie); err != nil { 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 @@ for { 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,92 +108,92 @@ } 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 + allOK := true for { - var line, old_oid, rest, new_oid, ref_name string + var line, oldOID, rest, newIOID, refName string var found bool - var old_hash, new_hash plumbing.Hash - var old_commit, new_commit *object.Commit - var git_push_option_count int + var oldHash, newHash plumbing.Hash + var oldCommit, newCommit *object.Commit + var pushOptCount int - git_push_option_count, err = strconv.Atoi(git_env["GIT_PUSH_OPTION_COUNT"]) + pushOptCount, err = strconv.Atoi(gitEnv["GIT_PUSH_OPTION_COUNT"]) if err != nil { - wf_error(ssh_stderr, "Failed to parse GIT_PUSH_OPTION_COUNT: %v", err) + 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 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") + 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 } - for i := 0; i < git_push_option_count; i++ { - push_option, ok := git_env[fmt.Sprintf("GIT_PUSH_OPTION_%d", i)] + for i := 0; i < pushOptCount; i++ { + pushOpt, ok := gitEnv[fmt.Sprintf("GIT_PUSH_OPTION_%d", i)] if !ok { - wf_error(ssh_stderr, "Failed to get push option %d", i) + writeRedError(sshStderr, "Failed to get push option %d", i) 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 strings.HasPrefix(pushOpt, "fedid=") { + fedUserID := strings.TrimPrefix(pushOpt, "fedid=") + service, username, found := strings.Cut(fedUserID, ":") if !found { - wf_error(ssh_stderr, "Invalid federated user identifier %#v does not contain a colon", federated_user_identifier) + writeRedError(sshStderr, "Invalid federated user identifier %#v does not contain a colon", fedUserID) return 1 } - ok, err := fedauth(ctx, pack_to_hook.user_id, service, username, pack_to_hook.pubkey) + ok, err := fedauth(ctx, packPass.userID, service, username, packPass.pubkey) if err != nil { - wf_error(ssh_stderr, "Failed to verify federated user identifier %#v: %v", federated_user_identifier, err) + writeRedError(sshStderr, "Failed to verify federated user identifier %#v: %v", fedUserID, 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) + writeRedError(sshStderr, "Failed to verify federated user identifier %#v: you don't seem to be on the list", fedUserID) return 1 } break } - 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 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 } } @@ -203,69 +203,69 @@ 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) + writeRedError(sshStderr, "Failed to read pre-receive line: %v", err) return 1 } line = line[:len(line)-1] - old_oid, rest, found = strings.Cut(line, " ") + oldOID, rest, found = strings.Cut(line, " ") if !found { - wf_error(ssh_stderr, "Invalid pre-receive line: %v", line) + writeRedError(sshStderr, "Invalid pre-receive line: %v", line) return 1 } - new_oid, ref_name, found = strings.Cut(rest, " ") + newIOID, refName, found = strings.Cut(rest, " ") if !found { - wf_error(ssh_stderr, "Invalid pre-receive line: %v", line) + writeRedError(sshStderr, "Invalid pre-receive line: %v", line) return 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 + 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", - pack_to_hook.repo_id, pack_to_hook.user_id, strings.TrimPrefix(ref_name, "refs/heads/"), - ).Scan(&new_mr_id) + packPass.repoID, packPass.userID, strings.TrimPrefix(refName, "refs/heads/"), + ).Scan(&newMRID) if err != nil { - wf_error(ssh_stderr, "Error creating merge request: %v", err) + writeRedError(sshStderr, "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) + fmt.Fprintln(sshStderr, ansiec.Blue+"Created merge request at", generate_http_remote_url(packPass.group_path, packPass.repo_name)+"/contrib/"+strconv.FormatUint(uint64(newMRID), 10)+"/"+ansiec.Reset) } else { // Existing contrib branch - var existing_merge_request_user_id int - var is_ancestor bool + 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(ref_name, "refs/heads/"), pack_to_hook.repo_id, - ).Scan(&existing_merge_request_user_id) + strings.TrimPrefix(refName, "refs/heads/"), packPass.repoID, + ).Scan(&existingMRUser) if err != nil { if errors.Is(err, pgx.ErrNoRows) { - wf_error(ssh_stderr, "No existing merge request for existing contrib branch: %v", err) + writeRedError(sshStderr, "No existing merge request for existing contrib branch: %v", err) } else { - wf_error(ssh_stderr, "Error querying for existing merge request: %v", err) + writeRedError(sshStderr, "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)") + if existingMRUser == 0 { + allOK = false + fmt.Fprintln(sshStderr, ansiec.Red+"NAK"+ansiec.Reset, refName, "(branch belongs to unowned MR)") continue } - 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)") + if existingMRUser != packPass.userID { + allOK = false + fmt.Fprintln(sshStderr, ansiec.Red+"NAK"+ansiec.Reset, refName, "(branch belongs another user's MR)") continue } - old_hash = plumbing.NewHash(old_oid) + oldHash = plumbing.NewHash(oldOID) - 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) + if oldCommit, err = packPass.repo.CommitObject(oldHash); err != nil { + writeRedError(sshStderr, "Daemon failed to get old commit: %v", err) return 1 } @@ -273,50 +273,50 @@ // 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) + 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, err = old_commit.IsAncestor(new_commit); err != nil { - wf_error(ssh_stderr, "Daemon failed to check if old commit is ancestor: %v", err) + if isAncestor, err = oldCommit.IsAncestor(newCommit); err != nil { + writeRedError(sshStderr, "Daemon failed to check if old commit is ancestor: %v", err) return 1 } - if !is_ancestor { + if !isAncestor { // 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)") + allOK = false + fmt.Fprintln(sshStderr, ansiec.Red+"NAK"+ansiec.Reset, refName, "(force pushes are not supported yet)") continue } - fmt.Fprintln(ssh_stderr, ansiec.Blue+"POK"+ansiec.Reset, ref_name) + fmt.Fprintln(sshStderr, ansiec.Blue+"POK"+ansiec.Reset, refName) } } 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/*)") + 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)") + fmt.Fprintln(sshStderr) + if allOK { + fmt.Fprintln(sshStderr, "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)") + 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 { @@ -330,10 +330,10 @@ } } func getUcred(conn net.Conn) (ucred *syscall.Ucred, err error) { - var unix_conn *net.UnixConn = conn.(*net.UnixConn) + var unixConn *net.UnixConn = conn.(*net.UnixConn) var fd *os.File - if fd, err = unix_conn.File(); err != nil { + if fd, err = unixConn.File(); err != nil { return nil, errGetFD } defer fd.Close() @@ -344,7 +344,7 @@ } 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 4313ee847acbf79a77d5557e34b48ac813251ee3..045ddd2335eb6716f6825d723c97ce245fcd7822 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 f2f8dba40ace4df3b4f4270d372d124e7cf79666..3f170d0c4a7d5a67e7d28e6761c2ac2ae40a81fc 100644 --- a/git_misc.go +++ b/git_misc.go @@ -16,9 +16,9 @@ "github.com/go-git/go-git/v5/plumbing/object" "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 { +type displayTreeEntry struct { Name string Mode string Size int64 - Is_file bool - Is_subtree bool + 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 } } diff --git a/git_ref.go b/git_ref.go index 65b90bf94ea1a92f6ee9850e7a8899667ae258d5..ab8e35d93d939293bb9db481e07bfa64942b2b4f 100644 --- a/git_ref.go +++ b/git_ref.go @@ -8,9 +8,9 @@ "github.com/go-git/go-git/v5" "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, ref_type, ref_name string) (ref_hash plumbing.Hash, err error) { var ref *plumbing.Reference switch ref_type { case "": diff --git a/http_handle_group_index.go b/http_handle_group_index.go index 2c27117a834f110d833ae91e565c46763683b10e..3ed3a855a7497671a19005a5bf6148c8509494f3 100644 --- a/http_handle_group_index.go +++ b/http_handle_group_index.go @@ -121,7 +121,7 @@ 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(file_path); err != nil { http.Error(w, "Error initializing repo: "+err.Error(), http.StatusInternalServerError) return } diff --git a/http_handle_repo_commit.go b/http_handle_repo_commit.go index f9b7dc5d07623a34118aa7f524af685b045c3af4..07f24fbc61a1a2690efbe66fe6b5d5b8d293fd48 100644 --- a/http_handle_repo_commit.go +++ b/http_handle_repo_commit.go @@ -66,7 +66,7 @@ params["commit_object"] = commit_object params["commit_id"] = commit_id_string - parent_commit_hash, patch, err = get_patch_from_commit(commit_object) + parent_commit_hash, patch, err = fmtCommitAsPatch(commit_object) if err != nil { http.Error(w, "Error getting patch from commit: "+err.Error(), http.StatusInternalServerError) return diff --git a/http_handle_repo_contrib_one.go b/http_handle_repo_contrib_one.go index f4db20c8fe735cde19840cbc566992859933d65e..cc17a6732853d744adf1c6e08ad331bc78fb3180 100644 --- a/http_handle_repo_contrib_one.go +++ b/http_handle_repo_contrib_one.go @@ -40,7 +40,7 @@ } repo = params["repo"].(*git.Repository) - if source_ref_hash, err = get_ref_hash_from_type_and_name(repo, "branch", source_ref); err != nil { + if source_ref_hash, err = getRefHash(repo, "branch", source_ref); err != nil { http.Error(w, "Error getting source ref hash: "+err.Error(), http.StatusInternalServerError) return } @@ -53,9 +53,9 @@ var destination_branch_hash plumbing.Hash if destination_branch == "" { destination_branch = "HEAD" - destination_branch_hash, err = get_ref_hash_from_type_and_name(repo, "", "") + destination_branch_hash, err = getRefHash(repo, "", "") } else { - destination_branch_hash, err = get_ref_hash_from_type_and_name(repo, "branch", destination_branch) + destination_branch_hash, err = getRefHash(repo, "branch", destination_branch) } if err != nil { http.Error(w, "Error getting destination branch hash: "+err.Error(), http.StatusInternalServerError) diff --git a/http_handle_repo_index.go b/http_handle_repo_index.go index da06500f1d3e69b4f67b5a23a67aceaacd989686..b38863cd3fddeb36915e77bdc196e5cda450accf 100644 --- a/http_handle_repo_index.go +++ b/http_handle_repo_index.go @@ -32,7 +32,7 @@ if strings.Contains(repo_name, "\n") || slice_contains_newline(group_path) { 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)) + ref_hash, err = getRefHash(repo, params["ref_type"].(string), params["ref_name"].(string)) if err != nil { goto no_ref } @@ -48,7 +48,7 @@ if err != nil { } params["branches"] = branches - if recent_commits, err = get_recent_commits(repo, ref_hash, 3); err != nil { + if recent_commits, err = getRecentCommits(repo, ref_hash, 3); err != nil { goto no_ref } params["commits"] = recent_commits @@ -61,7 +61,7 @@ if tree, err = commit_object.Tree(); err != nil { goto no_ref } - params["files"] = build_display_git_tree(tree) + params["files"] = makeDisplayTree(tree) params["readme_filename"], params["readme"] = render_readme_at_tree(tree) no_ref: diff --git a/http_handle_repo_log.go b/http_handle_repo_log.go index 65719e414384d8a5422f4a8cee2887365118fd3c..c7dde82e1aff37e9b815042cdc4a05906de531d8 100644 --- a/http_handle_repo_log.go +++ b/http_handle_repo_log.go @@ -20,12 +20,12 @@ 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 ref_hash, 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, ref_hash, -1); err != nil { http.Error(w, "Error getting recent commits: "+err.Error(), http.StatusInternalServerError) return } diff --git a/http_handle_repo_raw.go b/http_handle_repo_raw.go index a0f743018edea0c0bd1cee6f2ceca0c940e462fe..cfe03b694a86fcfc3a977b9f46ecdc3712246030 100644 --- a/http_handle_repo_raw.go +++ b/http_handle_repo_raw.go @@ -26,7 +26,7 @@ raw_path_spec = params["rest"].(string) repo, path_spec = params["repo"].(*git.Repository), strings.TrimSuffix(raw_path_spec, "/") params["path_spec"] = path_spec - if ref_hash, err = get_ref_hash_from_type_and_name(repo, params["ref_type"].(string), params["ref_name"].(string)); err != nil { + if ref_hash, 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 } @@ -69,7 +69,7 @@ http.Redirect(w, r, path.Base(path_spec)+"/", http.StatusSeeOther) return } - params["files"] = build_display_git_tree(target) + params["files"] = makeDisplayTree(target) render_template(w, "repo_raw_dir", params) } diff --git a/http_handle_repo_tree.go b/http_handle_repo_tree.go index ae9efbc2709b8e61a2fd4606d78ea1a27e9c94de..121e76a6939114c483d39af2bdfaf915ca4a20cb 100644 --- a/http_handle_repo_tree.go +++ b/http_handle_repo_tree.go @@ -31,7 +31,7 @@ raw_path_spec = params["rest"].(string) repo, path_spec = params["repo"].(*git.Repository), strings.TrimSuffix(raw_path_spec, "/") params["path_spec"] = path_spec - if ref_hash, err = get_ref_hash_from_type_and_name(repo, params["ref_type"].(string), params["ref_name"].(string)); err != nil { + if ref_hash, 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 } @@ -98,7 +98,7 @@ return } params["readme_filename"], params["readme"] = render_readme_at_tree(target) - params["files"] = build_display_git_tree(target) + params["files"] = makeDisplayTree(target) render_template(w, "repo_tree_dir", params) } diff --git a/http_server.go b/http_server.go index 747f66898f0eaec46a8ee39bc10363db322d3c55..d61dc8f7f3d94d077dcbc7686d0c40a3c7c175cb 100644 --- a/http_server.go +++ b/http_server.go @@ -155,7 +155,7 @@ } // 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(), group_path, module_name); err != nil { http.Error(w, "Error opening repo: "+err.Error(), http.StatusInternalServerError) return } diff --git a/ssh_handle_receive_pack.go b/ssh_handle_receive_pack.go index 3f7c1f1404941ca5ad4428bf0f639862aca11734..b93078419074edc8702827b7b80772432cc67f4d 100644 --- a/ssh_handle_receive_pack.go +++ b/ssh_handle_receive_pack.go @@ -14,21 +14,21 @@ "github.com/go-git/go-git/v5" "go.lindenii.runxiyu.org/lindenii-common/cmap" ) -type pack_to_hook_t struct { +type packPass struct { session glider_ssh.Session repo *git.Repository pubkey string - direct_access bool + directAccess bool repo_path string - user_id int - user_type string - repo_id int + userID int + userType string + repoID int group_path []string repo_name string - contrib_requirements 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, repo_identifier string) (err error) { @@ -92,20 +92,20 @@ if err != nil { fmt.Fprintln(session.Stderr(), "Error while generating cookie:", err) } - pack_to_hook_by_cookie.Store(cookie, pack_to_hook_t{ + packPasses.Store(cookie, packPass{ session: session, pubkey: pubkey, - direct_access: direct_access, + directAccess: direct_access, repo_path: repo_path, - user_id: user_id, - repo_id: repo_id, + userID: user_id, + repoID: repo_id, group_path: group_path, repo_name: repo_name, repo: repo, - contrib_requirements: contrib_requirements, - user_type: user_type, + contribReq: contrib_requirements, + userType: user_type, }) - 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. diff --git a/ssh_utils.go b/ssh_utils.go index d9c11da1dbd3296ffbada0a1332e204e1f06bd8a..092cb6e7d7349fbcfebd321518f2762502b47b3a 100644 --- a/ssh_utils.go +++ b/ssh_utils.go @@ -66,6 +66,6 @@ return []string{}, "", 0, "", false, "", "", 0, err_ssh_illegal_endpoint } } -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) } -- 2.48.1