|
| 1 | +[](https://github.com/xsh-lib/git/tags) |
| 2 | +[](https://github.com/xsh-lib/git/) |
| 3 | +[](https://github.com/xsh-lib/git/commits/main) |
| 4 | + |
| 5 | +[](https://github.com/xsh-lib/git/actions/workflows/ci-unittest.yml) |
| 6 | +[](https://github.com/xsh-lib/git/issues) |
| 7 | +[](https://github.com/xsh-lib/git/pulls) |
| 8 | + |
1 | 9 | # xsh-lib/git |
2 | 10 |
|
3 | 11 | xsh Library - git. |
4 | 12 |
|
5 | | -About xsh and its libraries, check out [xsh document](https://github.com/alexzhangs/xsh) |
| 13 | +Utilities for working with `git` and GitHub from the shell, packaged as an |
| 14 | +[xsh](https://github.com/alexzhangs/xsh) library. |
| 15 | + |
| 16 | +About xsh and its libraries, check out the [xsh document](https://github.com/alexzhangs/xsh). |
| 17 | + |
6 | 18 |
|
7 | 19 | ## Requirements |
8 | 20 |
|
9 | | -1. bash |
| 21 | +Tested with bash: |
| 22 | + |
| 23 | +* 5.x and 4.x on Linux (ubuntu-latest in CI) |
| 24 | +* 3.2.57 on macOS (macos-latest in CI) |
| 25 | + |
| 26 | +This project is still at version `0.x` and should be considered immature. |
10 | 27 |
|
11 | | - Tested with bash: |
12 | | - * 4.3.48 on Linux |
13 | | - * 3.2.57 on macOS |
14 | 28 |
|
15 | 29 | ## Dependency |
16 | 30 |
|
17 | | -1. xsh-lib/core |
| 31 | +1. [xsh-lib/core](https://github.com/xsh-lib/core) — should be loaded before |
| 32 | + this library. |
| 33 | + |
| 34 | + ```bash |
| 35 | + xsh load xsh-lib/core |
| 36 | + ``` |
18 | 37 |
|
19 | | - This library depends on [xsh-lib/core](https://github.com/xsh-lib/core) which should be loaded first before to use this library. |
| 38 | +2. Some utilities have additional dependencies (e.g. `gh`, `w3m`, |
| 39 | + `collaborator`). See the per-utility help for details. |
20 | 40 |
|
21 | | - ```bash |
22 | | - xsh load xsh-lib/core |
23 | | - ``` |
24 | 41 |
|
25 | 42 | ## Installation |
26 | 43 |
|
27 | | -Assume [xsh](https://github.com/alexzhangs/xsh) is already installed at your local. |
| 44 | +Assume [xsh](https://github.com/alexzhangs/xsh) is already installed. |
28 | 45 |
|
29 | | -To load this library into `xsh` issue below command: |
| 46 | +To load this library into `xsh`: |
30 | 47 |
|
31 | 48 | ```bash |
32 | 49 | xsh load xsh-lib/git |
33 | 50 | ``` |
34 | 51 |
|
35 | | -The loaded library can be referred as name `git`. |
| 52 | +The loaded library is referenced by the name `git`. |
| 53 | + |
| 54 | +> The leading slash in an LPUR (e.g. `xsh /string/upper`) is reserved for the |
| 55 | +> default library `xsh-lib/core`. For this library, write the lib name |
| 56 | +> explicitly: `xsh git/<package>/<util>`. |
| 57 | +
|
36 | 58 |
|
37 | 59 | ## Usage |
38 | 60 |
|
39 | | -List available utilities for this library: |
| 61 | +List the utilities exposed by this library: |
40 | 62 |
|
41 | 63 | ```bash |
42 | 64 | xsh list git |
43 | 65 | ``` |
| 66 | + |
| 67 | +Get help on any utility: |
| 68 | + |
| 69 | +```bash |
| 70 | +xsh help git/<package>/<util> |
| 71 | +``` |
| 72 | + |
| 73 | + |
| 74 | +### Available utilities |
| 75 | + |
| 76 | +| Utility | Kind | Purpose | |
| 77 | +|-------------------------------|----------|------------------------------------------------------------------------------------------------------------------| |
| 78 | +| `git/hub/account-for-email` | function | Look up a `gh` account name for an email via `XSH_GIT_HUB_ACCOUNT_MAP`. | |
| 79 | +| `git/hub/account-for-repo` | function | Derive the `gh` account for the current repo from `git config user.email`, using the same mapping. | |
| 80 | +| `git/hub/run` | function | Run a command with a chosen `gh` account active, isolated from concurrent shell sessions via per-call `GH_CONFIG_DIR`. | |
| 81 | +| `git/hub/collaborator` | function | Add, remove, or list collaborators of the current GitHub repo. Requires `collaborator` and `w3m`. | |
| 82 | +| `git/rebase-i-in-dumb-term` | script | Helper for running `git rebase -i` in dumb terminals. | |
| 83 | + |
| 84 | + |
| 85 | +### Multi-`gh`-account workflow |
| 86 | + |
| 87 | +If you operate multiple GitHub accounts simultaneously (e.g. personal + |
| 88 | +work) and rely on `~/.gitconfig`'s `includeIf "gitdir:..."` rules to switch |
| 89 | +identities per directory tree, the `git/hub/*` utilities make it transparent |
| 90 | +to push/PR/etc. against the right account without ever mutating the global |
| 91 | +active account in `~/.config/gh`. |
| 92 | + |
| 93 | +1. Map your emails to `gh` account names via an env var: |
| 94 | + |
| 95 | + ```bash |
| 96 | + export XSH_GIT_HUB_ACCOUNT_MAP="alice@personal.com=alice alice@corp.io=alice-corp" |
| 97 | + ``` |
| 98 | + |
| 99 | +2. Use `git/hub/run` as a transparent wrapper around any `git` or `gh` |
| 100 | + command. The account is auto-derived from the current repo's |
| 101 | + `user.email`: |
| 102 | + |
| 103 | + ```bash |
| 104 | + xsh git/hub/run -- git push origin main |
| 105 | + xsh git/hub/run -- gh pr create --fill |
| 106 | + xsh git/hub/run -u alice-corp -- gh pr list # explicit override |
| 107 | + ``` |
| 108 | + |
| 109 | + Each call snapshots `~/.config/gh` to a private mode-700 tempdir, |
| 110 | + `gh auth switch -u <account>` runs against the **copy**, and |
| 111 | + `GH_CONFIG_DIR` is exported only for the wrapped command. The real |
| 112 | + config is never mutated, so other shell sessions and credential-helper |
| 113 | + invocations are unaffected — even when several `git/hub/run` calls are |
| 114 | + in flight at the same time. |
| 115 | + |
| 116 | + |
| 117 | +## Development |
| 118 | + |
| 119 | +Run the test suite: |
| 120 | + |
| 121 | +```bash |
| 122 | +xsh load xsh-lib/git |
| 123 | +bash test.sh |
| 124 | +``` |
| 125 | + |
| 126 | +For local iteration against a working copy that hasn't been pushed yet, link |
| 127 | +the workspace as a development library and re-run with `XSH_DEV=1`: |
| 128 | + |
| 129 | +```bash |
| 130 | +xsh lib-dev-manager link xsh-lib/git /path/to/parent-of-this-repo |
| 131 | +XSH_DEV=1 bash test.sh |
| 132 | +``` |
| 133 | + |
| 134 | +Network-dependent tests (real `gh` round-trips) are skipped by default. |
| 135 | +Enable them with `XSH_GIT_TEST_NETWORK=1` once you have a logged-in `gh` |
| 136 | +account available. |
| 137 | + |
| 138 | +CI runs the same `test.sh` on `ubuntu-latest` and `macos-latest` for every |
| 139 | +push and PR. See the |
| 140 | +[ci-unittest workflow](.github/workflows/ci-unittest.yml). |
0 commit comments