@samaksh/git-worker 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/README.md +114 -2
  2. package/dist/app.d.ts +2 -0
  3. package/dist/app.js +48 -0
  4. package/dist/cli/commands.d.ts +6 -0
  5. package/dist/cli/commands.js +111 -0
  6. package/dist/cli/index.d.ts +2 -0
  7. package/dist/cli/index.js +115 -0
  8. package/dist/config/defaults.d.ts +2 -0
  9. package/dist/config/defaults.js +44 -0
  10. package/dist/config/load.d.ts +3 -0
  11. package/dist/config/load.js +65 -0
  12. package/dist/config/schema.d.ts +200 -0
  13. package/dist/config/schema.js +45 -0
  14. package/dist/core/errors.d.ts +12 -0
  15. package/dist/core/errors.js +34 -0
  16. package/dist/core/pipeline.d.ts +3 -0
  17. package/dist/core/pipeline.js +14 -0
  18. package/dist/core/types.d.ts +153 -0
  19. package/dist/core/types.js +16 -0
  20. package/dist/index.d.ts +3 -0
  21. package/dist/index.js +3 -0
  22. package/dist/services/editor/index.d.ts +9 -0
  23. package/dist/services/editor/index.js +59 -0
  24. package/dist/services/git/ops.d.ts +30 -0
  25. package/dist/services/git/ops.js +139 -0
  26. package/dist/services/git/reader.d.ts +14 -0
  27. package/dist/services/git/reader.js +195 -0
  28. package/dist/services/report/index.d.ts +18 -0
  29. package/dist/services/report/index.js +68 -0
  30. package/dist/services/review/index.d.ts +30 -0
  31. package/dist/services/review/index.js +59 -0
  32. package/dist/services/state/index.d.ts +20 -0
  33. package/dist/services/state/index.js +49 -0
  34. package/dist/services/template/env.d.ts +23 -0
  35. package/dist/services/template/env.js +146 -0
  36. package/dist/services/template/interpolator.d.ts +30 -0
  37. package/dist/services/template/interpolator.js +75 -0
  38. package/dist/services/template/transform.d.ts +22 -0
  39. package/dist/services/template/transform.js +75 -0
  40. package/dist/services/template/validator.d.ts +5 -0
  41. package/dist/services/template/validator.js +68 -0
  42. package/dist/services/workspace/cache.d.ts +32 -0
  43. package/dist/services/workspace/cache.js +107 -0
  44. package/dist/services/workspace/manager.d.ts +44 -0
  45. package/dist/services/workspace/manager.js +301 -0
  46. package/dist/stages/apply.d.ts +3 -0
  47. package/dist/stages/apply.js +161 -0
  48. package/dist/stages/edit-template.d.ts +3 -0
  49. package/dist/stages/edit-template.js +198 -0
  50. package/dist/stages/eligibility.d.ts +3 -0
  51. package/dist/stages/eligibility.js +68 -0
  52. package/dist/stages/load-template.d.ts +3 -0
  53. package/dist/stages/load-template.js +168 -0
  54. package/dist/stages/plan.d.ts +3 -0
  55. package/dist/stages/plan.js +270 -0
  56. package/dist/stages/push.d.ts +3 -0
  57. package/dist/stages/push.js +99 -0
  58. package/dist/stages/report.d.ts +3 -0
  59. package/dist/stages/report.js +103 -0
  60. package/dist/stages/resolve-project.d.ts +3 -0
  61. package/dist/stages/resolve-project.js +294 -0
  62. package/dist/stages/review.d.ts +3 -0
  63. package/dist/stages/review.js +197 -0
  64. package/dist/stages/select.d.ts +13 -0
  65. package/dist/stages/select.js +265 -0
  66. package/dist/stages/sync.d.ts +3 -0
  67. package/dist/stages/sync.js +47 -0
  68. package/dist/utils/hash.d.ts +2 -0
  69. package/dist/utils/hash.js +7 -0
  70. package/dist/utils/ui.d.ts +21 -0
  71. package/dist/utils/ui.js +113 -0
  72. package/dist/utils/which.d.ts +5 -0
  73. package/dist/utils/which.js +16 -0
  74. package/docs/FEATURES.md +84 -0
  75. package/docs/SERVICES.md +57 -0
  76. package/docs/architecture.md +38 -0
  77. package/docs/usage.md +337 -0
  78. package/package.json +58 -4
package/docs/usage.md ADDED
@@ -0,0 +1,337 @@
1
+ # Git Worker CLI & Usage Guide
2
+
3
+ `git-worker` is a safe, interactive CLI tool for synchronizing templates and environment files across many Git branches without needing to checkout each branch manually in your working directory.
4
+
5
+ ---
6
+
7
+ ## Quick Start
8
+
9
+ ```bash
10
+ # 1. Interactive wizard (run from anywhere)
11
+ git-worker
12
+
13
+ # 2. Target a remote repository URL directly
14
+ git-worker --repo https://github.com/my-org/my-project.git
15
+
16
+ # 3. Target a local repository with specific branches and file
17
+ git-worker --repo ./my-project --branch "uat/*" --file .env
18
+
19
+ # 4. Non-interactive CI mode (auto-approve, never push)
20
+ git-worker --repo https://github.com/my-org/my-project.git --branch "uat/*" --file .env --ci --auto-approve --push never
21
+ ```
22
+
23
+ ---
24
+
25
+ ## 1. Repository Resolution & First Step
26
+
27
+ When you execute `git-worker` without arguments, it begins by checking your current environment and available cached repositories:
28
+
29
+ 1. **Inside a Git Repository:**
30
+ If the current working directory is a Git repository, `git-worker` detects it and gives you the choice to use it, select from cached repositories, clone a new remote, or target a different path:
31
+ ```text
32
+ ◆ What repository would you like to target?
33
+ │ ● Use current repository (/Users/username/project)
34
+ │ ○ 📦 Select from cached repositories (2 available)
35
+ │ ○ Clone a remote repository URL
36
+ │ ○ Specify a different local repository path
37
+ ```
38
+ 2. **Outside a Git Repository:**
39
+ If run from a non-git directory, the TUI presents:
40
+ ```text
41
+ ◆ How would you like to target a repository?
42
+ │ ● 📦 Select from cached repositories (2 available)
43
+ │ ○ Clone a remote repository URL
44
+ │ ○ Specify path to a local Git repository
45
+ ```
46
+
47
+ ### Listing & Selecting Cached Repositories
48
+ Choosing **Select from cached repositories** displays all previously cloned and cached repositories in `~/.git-worker`, showing their **name**, **remote URL**, and cached date:
49
+
50
+ ```text
51
+ Name Remote URL / Path Cached Date
52
+ ────────────────────────────────────────────────────────────────────────────────
53
+ remote https://github.com/my-org/my-project.git 2026-10-29
54
+ express https://github.com/expressjs/express.git 2026-10-28
55
+ ────────────────────────────────────────────────────────────────────────────────
56
+
57
+ ? Select a cached repository to target:
58
+ ● ◆ my-project (https://github.com/my-org/my-project.git)
59
+ ○ ◆ express (https://github.com/expressjs/express.git)
60
+ ○ ← Back to repository menu
61
+ ```
62
+
63
+ Selecting any repository immediately loads it and fetches any new branch updates from the remote, skipping the full clone.
64
+
65
+ > [!TIP]
66
+ > - Jump straight to cached repository selection from the CLI using `-c` or `--cached`:
67
+ > ```bash
68
+ > git-worker --cached
69
+ > ```
70
+ > - Bypass the prompt at any time by passing `--repo <url-or-path>` or `-r <url-or-path>`.
71
+
72
+ ---
73
+
74
+ ## 2. Interactive Branch Selection
75
+
76
+ In the branch selection stage, `git-worker` provides an interactive selection menu designed for repositories with many branches:
77
+
78
+ ```text
79
+ Branch & Target Selection
80
+ ────────────────────────────────────────
81
+ │
82
+ ◇ Found 10 branches
83
+
84
+ ? Branch selection (0 of 10 branches selected):
85
+ ● 🔍 Filter by pattern or regex (glob e.g. uat/*, or regex /uat.*/)
86
+ ○ 🌟 Select ALL branches (10 branches)
87
+ ○ 📋 Choose individually from list
88
+ ○ 🔄 Invert selection (select all 10 branches)
89
+ ```
90
+
91
+ ### Selection Capabilities
92
+
93
+ 1. **Regex & Glob Pattern Matching:**
94
+ Choose **Filter by pattern or regex** and enter:
95
+ - **Regex:** `/uat.*/`, `/^feat\//`, or `/release/i`
96
+ - **Glob:** `uat/*`, `*orders`, or `uat*`
97
+ - **Prefix / Substring:** `uat/` or `payments`
98
+
99
+ Matched branches are highlighted and selected immediately:
100
+ ```text
101
+ ✓ Pattern '/uat.*/' matched 4 branch(es):
102
+ • uat/inventory
103
+ • uat/notifications
104
+ • uat/orders
105
+ • uat/payments
106
+ ```
107
+
108
+ 2. **Select ALL Branches:**
109
+ Selects every branch in the repository with a single action.
110
+
111
+ 3. **Invert Selection:**
112
+ Flips the current selection against all repository branches. For example, if 4 `uat/*` branches are selected out of 10, choosing **Invert selection** selects the other 6 branches.
113
+
114
+ 4. **Customize from List:**
115
+ Opens a checkbox multiselect with current selections pre-checked, allowing you to toggle individual branches on or off using the Space key.
116
+
117
+ 5. **Instant Confirmation:**
118
+ As soon as branches are selected, **➜ Confirm and proceed** is the top default option: press Enter to immediately advance to target file selection.
119
+
120
+ > [!TIP]
121
+ > From the CLI, you can combine `--branch <pattern>` with `--invert-branch` (`-i`) to filter or invert non-interactively:
122
+ > ```bash
123
+ > git-worker --branch "uat/*" --invert-branch
124
+ > ```
125
+
126
+ ---
127
+
128
+ ## 3. Workspace Storage in `~/.git-worker`
129
+
130
+ To guarantee that your active working tree and uncommitted changes are never touched or corrupted, `git-worker` performs all operations inside an isolated, dedicated directory in your home folder:
131
+
132
+ ```text
133
+ ~/.git-worker/
134
+ └── projects/
135
+ └── <project-id>/ # Deterministic SHA-256 hash of repository remote/path
136
+ ├── repo/ # Managed Git clone with all remotes/branches
137
+ ├── state/ # Saved templates and branch resolution states
138
+ ├── reports/ # Run summaries and execution audit logs
139
+ ├── .git-worker-marker # Workspace ownership marker
140
+ └── .git-worker-lock # Process lock (prevents concurrent collisions)
141
+ ```
142
+
143
+ - **Zero pollution:** Your project worktree stays clean and untouched.
144
+ - **Safe isolation:** Re-indexes, resets, and path-scoped commits happen only within `repo/`.
145
+ - **Fault-tolerant locking:** Stale lock files from crashed processes are automatically detected and cleaned.
146
+
147
+ ---
148
+
149
+ ## 4. Cached Clone Detection & Reuse
150
+
151
+ When targeting a remote repository URL that was previously cloned into `~/.git-worker`:
152
+
153
+ 1. `git-worker` detects that a managed clone is already present for this remote.
154
+ 2. It notifies you in the console:
155
+ ```text
156
+ ◇ Project workspace ready
157
+ • Using cached repository clone (already downloaded)
158
+ https://github.com/org/repo.git
159
+ │
160
+ ◒ Using cached repository — fetching latest branches…
161
+ ◇ Repository synced
162
+ ```
163
+ 3. Instead of re-downloading the entire repository, it executes a fast `git fetch origin` to retrieve any new branches or updated commits.
164
+
165
+ ---
166
+
167
+ ## 5. Cache Management (`cache list` & `cache clean`)
168
+
169
+ `git-worker` provides built-in commands to inspect and clean cached workspaces and templates in `~/.git-worker`.
170
+
171
+ ### List Cached Projects
172
+
173
+ View all cached repositories, their project IDs, remote URLs, and creation dates:
174
+
175
+ ```bash
176
+ git-worker cache list
177
+ # or shorthand alias:
178
+ git-worker cache ls
179
+ ```
180
+
181
+ **Example output:**
182
+ ```text
183
+ Cached Projects (~/.git-worker)
184
+ ────────────────────────────────────────
185
+
186
+ ID Target Repository / Remote Cached Date
187
+ ──────────────────────────────────────────────────────────────────────────────
188
+ b3676ac05b37f54c https://github.com/my-org/my-project.git 2026-10-28 09:50:52
189
+ ──────────────────────────────────────────────────────────────────────────────
190
+ Total: 1 project(s) cached in /Users/username/.git-worker/projects
191
+ ```
192
+
193
+ ---
194
+
195
+ ### Clean Cache
196
+
197
+ Remove cached repositories to reclaim disk space or force a clean re-clone.
198
+
199
+ #### A. Interactive Clean Menu
200
+ Running clean without arguments displays an interactive picker:
201
+ ```bash
202
+ git-worker cache clean
203
+ # or shorthand alias:
204
+ git-worker clean
205
+ ```
206
+ ```text
207
+ Select cache to remove:
208
+ ● Clear ALL cached projects (1 projects)
209
+ ○ https://github.com/my-org/my-project.git (ID: b3676ac05b37f54c)
210
+ ```
211
+
212
+ #### B. Clean a Specific Project
213
+ Specify a project ID, remote URL, or path fragment:
214
+ ```bash
215
+ git-worker cache clean b3676ac05b37f54c
216
+ # or
217
+ git-worker clean https://github.com/my-org/my-project.git
218
+ ```
219
+
220
+ #### C. Clean All Projects
221
+ Remove all cached repositories from `~/.git-worker/projects`:
222
+ ```bash
223
+ git-worker cache clean --all
224
+ # or with non-interactive confirmation:
225
+ git-worker clean -a -y
226
+ ```
227
+
228
+ ---
229
+
230
+ ## 6. CLI Command Reference
231
+
232
+ ### Main Command Options
233
+
234
+ ```text
235
+ git-worker [options]
236
+ ```
237
+
238
+ | Option | Shorthand | Description | Default |
239
+ |---|---|---|---|
240
+ | `--repo <path-or-url>` | `-r` | Target local repository path or remote URL | CWD or interactive prompt |
241
+ | `--cached` | `-c` | Select from previously cloned and cached repositories | `false` |
242
+ | `--branch <pattern>` | `-b` | Glob (e.g. `uat/*`) or regex (e.g. `/uat.*/`) pattern | Interactive selection |
243
+ | `--invert-branch` | `-i` | Invert the branch selection pattern | `false` |
244
+ | `--file <path>` | `-f` | Path to target file within repo (e.g. `.env`) | Interactive prompt |
245
+ | `--editor <editor>` | `-e` | Editor for template editing (`vscode`, `vim`, or command) | Configured default |
246
+ | `--commit-msg <msg>`| `-m` | Git commit message to use for applied changes | `"chore(config): sync <file> via git-worker"` |
247
+ | `--push <policy>` | `-p` | Push policy: `prompt`, `always`, or `never` | `prompt` |
248
+ | `--remote <remote>` | | Git remote name to push to | `origin` |
249
+ | `--auto-approve` | | Auto-approve all planned branch changes | `false` |
250
+ | `--ci` | | Non-interactive CI mode (fails on missing input) | `false` |
251
+ | `--version` | `-v` | Output current git-worker version | |
252
+ | `--help` | `-h` | Display CLI help text | |
253
+
254
+ ### Cache Subcommands
255
+
256
+ | Command | Shorthand Alias | Description |
257
+ |---|---|---|
258
+ | `git-worker cache list` | `git-worker cache ls` | List all cached repositories in `~/.git-worker` |
259
+ | `git-worker cache clean [target]` | `git-worker clean [target]` | Remove cache for target ID/URL or prompt interactively |
260
+ | `git-worker cache clean -a -y` | `git-worker clean -a -y` | Remove all cached checkouts without confirmation prompt |
261
+
262
+ ---
263
+
264
+ ## 7. `.env` Template Syntax & Rules
265
+
266
+ ### Static Values (Enforced on All Branches)
267
+ Values written as standard `KEY=VALUE` are applied identically across every selected branch:
268
+ ```dotenv
269
+ DB_NAME=uat_database
270
+ PORT=8080
271
+ LOG_LEVEL=info
272
+ ```
273
+ If `DB_NAME` had a different value on `uat/service-a`, it is updated to `uat_database`.
274
+
275
+ ### Dynamic Placeholders (Read from Branch)
276
+ Values written with curly braces `{KEY}` instruct git-worker to look up the existing value on each branch:
277
+ ```dotenv
278
+ SERVICE_NAME={SERVICE_NAME}
279
+ CLIENT_SECRET={CLIENT_SECRET}
280
+ ```
281
+ - If the branch already has `SERVICE_NAME=billing`, `billing` is preserved.
282
+ - If the branch **does not** contain `CLIENT_SECRET`, git-worker prompts you for that branch's value before generating the diff. In CI mode, missing values fail cleanly without hanging.
283
+
284
+ ### Processing Rules
285
+ - **Unmanaged keys preserved:** Any key in a branch's `.env` that is **not** mentioned in the template is kept intact in its original location.
286
+ - **Export syntax preserved:** `export KEY=value` declarations retain their `export` prefix.
287
+ - **Comments and spacing:** Comments (`# ...`) and blank lines are maintained.
288
+ - **Path-scoped staging:** Git worker stages strictly the target file (`git add -- .env`).
289
+ - **Safety checks:** Pre-apply branch tip verification ensures no commits were pushed upstream while the review was pending.
290
+
291
+ ---
292
+
293
+ ## 8. Source-of-Truth Templates (Dockerfile, JSON, YAML, configs)
294
+
295
+ For any file format other than `.env` (such as `Dockerfile`, `package.json`, `.github/workflows/deploy.yml`, `config.json`), the template serves as the **declarative Source of Truth**.
296
+
297
+ ### How It Works
298
+ - The template content is interpolated with variables and completely replaces the target file on each target branch (or creates a brand-new file if it didn't exist).
299
+ - Variables use standard `${VAR}` and `${VAR:-default}` syntax.
300
+ - Values for `${VAR}` are pulled automatically from each branch's own `.env` file!
301
+
302
+ ### Example: Multi-Branch `Dockerfile`
303
+ ```dockerfile
304
+ # Source of truth Dockerfile template
305
+ FROM node:20-alpine AS base
306
+ ENV SERVICE_NAME=${SERVICE_NAME}
307
+ ENV BRANCH_NAME=${BRANCH_NAME}
308
+ ENV BRANCH_SLUG=${BRANCH_SLUG}
309
+ EXPOSE ${PORT:-8080}
310
+ ```
311
+
312
+ ### Available Built-in Variables
313
+ | Variable | Description | Example |
314
+ |---|---|---|
315
+ | `${BRANCH_NAME}` | Full current branch name | `uat/payments-service` |
316
+ | `${BRANCH_SLUG}` | Sanitized DNS/Docker-safe branch name | `uat-payments-service` |
317
+ | `${REPO_NAME}` | Current repository name | `gitworker` |
318
+ | `${COMMIT_SHA}` | Branch commit SHA-256 | `99b3cff8...` |
319
+ | `${SHORT_SHA}` | 7-character commit SHA | `99b3cff` |
320
+
321
+ ### Fallback Defaults (`${VAR:-default}`)
322
+ If a variable is optional or has a standard fallback:
323
+ ```yaml
324
+ # deploy.yaml
325
+ apiVersion: apps/v1
326
+ kind: Deployment
327
+ metadata:
328
+ name: ${BRANCH_SLUG}
329
+ spec:
330
+ replicas: ${REPLICAS:-2}
331
+ ```
332
+ If `REPLICAS` is not defined in a branch's `.env`, it defaults to `2`.
333
+
334
+ ### Syntax Validation & State Persistence
335
+ - **Automatic Syntax Checking:** Immediately upon saving in your editor, `git-worker` validates that JSON is valid JSON, YAML is valid YAML, and all `${...}` delimiters are properly closed. If an error is found, you can re-edit to fix it before any changes are planned.
336
+ - **Persistent Template State:** When a template is applied, it is saved in `~/.git-worker/projects/<id>/state/`. The next time you open `git-worker` for that file, it loads your previously applied template so you don't lose any changes.
337
+
package/package.json CHANGED
@@ -1,6 +1,60 @@
1
1
  {
2
2
  "name": "@samaksh/git-worker",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.0",
4
+ "description": "Safe, composable TypeScript CLI/TUI for applying reviewed file changes across many Git branches",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "bin": {
9
+ "git-worker": "dist/cli/index.js",
10
+ "gitworker": "dist/cli/index.js"
11
+ },
12
+ "publishConfig": {
13
+ "access": "public"
14
+ },
15
+ "files": [
16
+ "dist",
17
+ "README.md",
18
+ "docs"
19
+ ],
20
+ "scripts": {
21
+ "build": "tsc",
22
+ "prepublishOnly": "npm run build",
23
+ "dev": "tsx src/cli/index.ts",
24
+ "demo": "tsx demo/index.ts",
25
+ "demo:ci": "tsx demo/index.ts --ci",
26
+ "demo:clean": "tsx demo/index.ts --clean",
27
+ "test": "vitest",
28
+ "lint": "eslint src --ext .ts",
29
+ "typecheck": "tsc --noEmit"
30
+ },
31
+ "keywords": [
32
+ "git",
33
+ "cli",
34
+ "env",
35
+ "branches",
36
+ "multi-branch"
37
+ ],
38
+ "author": "",
39
+ "license": "MIT",
40
+ "dependencies": {
41
+ "@clack/prompts": "^1.8.1",
42
+ "chalk": "^5.3.0",
43
+ "commander": "^12.0.0",
44
+ "diff": "^5.2.0",
45
+ "inquirer": "^9.2.0",
46
+ "picocolors": "^1.1.1",
47
+ "simple-git": "^3.22.0",
48
+ "yaml": "^2.4.0",
49
+ "zod": "^3.22.0"
50
+ },
51
+ "devDependencies": {
52
+ "@types/diff": "^5.0.0",
53
+ "@types/inquirer": "^9.0.0",
54
+ "@types/node": "^20.11.0",
55
+ "eslint": "^8.56.0",
56
+ "tsx": "^4.7.0",
57
+ "typescript": "^5.3.0",
58
+ "vitest": "^1.2.0"
59
+ }
60
+ }