Title: Reimagine how the app determines its context · Issue #2 · cli/cli · GitHub
Open Graph Title: Reimagine how the app determines its context · Issue #2 · cli/cli
X Title: Reimagine how the app determines its context · Issue #2 · cli/cli
Description: The app currently determines the context it's operating under from several sources: the filesystem (e.g. current directory name) app config file (currently ~/.config/hub in YAML format) environment variables (e.g. GH_REPO, GITHUB_TOKEN, ...
Open Graph Description: The app currently determines the context it's operating under from several sources: the filesystem (e.g. current directory name) app config file (currently ~/.config/hub in YAML format) environment...
X Description: The app currently determines the context it's operating under from several sources: the filesystem (e.g. current directory name) app config file (currently ~/.config/hub in YAML format) environ...
Opengraph URL: https://github.com/cli/cli/issues/2
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Reimagine how the app determines its context","articleBody":"The app currently determines the context it's operating under from several sources:\r\n\r\n- the filesystem (e.g. current directory name)\r\n- app config file (currently `~/.config/hub` in YAML format)\r\n- environment variables (e.g. `GH_REPO`, `GITHUB_TOKEN`, etc.)\r\n- git config (via `git config` \u0026 `git remote -v`, sources: `.git/config` \u0026 `~/.gitconfig`)\r\n- git tree (e.g. current branch, current commit SHA)\r\n- SSH config (sources: `~/.ssh/config` \u0026 `/etc/ssh_config`) - resolves SSH aliases for hostnames listed under git remotes\r\n- GitHub API (e.g. info about the current repo)\r\n\r\nWhen something like `gh pr create` gets run, quite a lot happens under the hood. For example:\r\n1. Current GitHub user and OAuth token are obtained from app config;\r\n1. The list of git remotes gets queried and parsed;\r\n2. The \"main\" remote (i.e. one pointing to the canonical GitHub repo) is determined by searching for the first one in this list: `upstream`, `github`, `origin`;\r\n3. The base branch is determined via `git symbolic-ref refs/remotes/\u003cREMOTE\u003e/HEAD` (alternatively, by querying repo information via API);\r\n4. The head branch is determined by looking at the push target for the current branch:\r\n - explicit upstream branch configuration is first looked up;\r\n - otherwise, the first remote that has a same-named tracking branch is the likely push target;\r\n - otherwise, assume the branch isn't pushed yet, so determine the first remote that points to a GitHub project that the current user has write capabilities to;\r\n - if such a remote doesn't exist, create one by forking;\r\n5. A person's preferred text editor is looked up for authoring PR description text;\r\n6. PR creation operation proceeds.\r\n\r\nTo facilitate all these lookups, the current codebase has a loose system of mapping one piece of information to another. For example:\r\n- current git repo 👉 default (\"base\") branch\r\n- current git repo 👉 \"main\" remote\r\n- git remote 👉 GitHub repository (a.k.a. \"project\") it maps to\r\n- tracking branch 👉 git remote it belongs to\r\n- current user 👉 the person's fork\r\n\r\nSince a lot of lookups start from the current git repo (based on the current working directory at the time that the CLI app runs), there is a LocalRepo struct that encapsulates performing some of these mappings, while additional mapping logic is scattered across individual methods such as `Branch.RemoteName()`, `Remote.Project()`, etc. Some problems I find with the current system:\r\n- Inconsistent naming (e.g. \"repository\" vs. \"project\", the ambiguity of \"branch\")\r\n- Blurred responsibility between objects (e.g. why would a Branch have to know how to map itself to a Remote, or a Remote to a Project?)\r\n- Methods that do too much but don't sufficiently betray intent (e.g. `LocalRepo.RemoteBranchAndProject()`)\r\n- Hard to stub out in tests (ideally, unit tests should be able to set up a mock context in memory rather than having to write a test git repo out to fileystem)\r\n- **All** of this code is under the same Go package: \"github\".\r\n\r\nMy rough proposal for starting to address this:\r\n- [x] Get rid of methods and structs named \"Project\" from the codebase;\r\n- [x] Get rid of LocalRepo;\r\n- [x] Design an abstraction around `git config` that is able to mock responses in memory;\r\n- [x] Consider moving `github/branch.go` to under the \"git\" package;\r\n- [ ] Consider isolating `git/ssh_config.go` to its own package;\r\n- [ ] Minimize the implementation of Remote and instead perform necessary mappings through a separate service object;\r\n- [x] Minimize the \"github\" package until it's ideally gone;\r\n- [ ] Write Go unit tests along the way to confirm the testability of these implementations.\r\n\r\nLet's revise all this as we go along! Thank you for reading. 🙇 ","author":{"url":"https://github.com/mislav","@type":"Person","name":"mislav"},"datePublished":"2019-10-07T17:49:57.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":4},"url":"https://github.com/2/cli/issues/2"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:dc164030-8b40-794a-db48-ef7ddffdaeb8 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EA98:39E76C:2D0CF92:3D69311:6A545662 |
| html-safe-nonce | fc8f535cbe0cd58e3da30b47ad42c7bbf8cbb5d6a572a02afa2f11a83ecc763f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQTk4OjM5RTc2QzoyRDBDRjkyOjNENjkzMTE6NkE1NDU2NjIiLCJ2aXNpdG9yX2lkIjoiMzc0NTI3MTY5NTA0MjQzNDY1OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 02cbb8fd7610c27f71c26f12debe4f0a7bdd79d5ba4a7755b700908ec56ed464 |
| hovercard-subject-tag | issue:503598192 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/cli/cli/2/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f4d7c3b30e1d1d3a4df6f3ac0a7954464672c6060cae9d7627c8ca5204cab33f/cli/cli/issues/2 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f4d7c3b30e1d1d3a4df6f3ac0a7954464672c6060cae9d7627c8ca5204cab33f/cli/cli/issues/2 |
| og:image:alt | The app currently determines the context it's operating under from several sources: the filesystem (e.g. current directory name) app config file (currently ~/.config/hub in YAML format) environment... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mislav |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-preview |
| go-import | github.com/cli/cli git https://github.com/cli/cli.git |
| octolytics-dimension-user_id | 59704711 |
| octolytics-dimension-user_login | cli |
| octolytics-dimension-repository_id | 212613049 |
| octolytics-dimension-repository_nwo | cli/cli |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 212613049 |
| octolytics-dimension-repository_network_root_nwo | cli/cli |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 07a982c1d40157c619b364352b704c3ce66bb332 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width