From c47962684d8c52b9f901e1df959e5e6f02f18dc2 Mon Sep 17 00:00:00 2001 From: Vitor Avila Date: Tue, 7 Jul 2026 12:45:31 -0300 Subject: [PATCH] feat: Expose branch.base_ref and branch.ref_type to env variables --- apps/agor-daemon/src/services/branches.ts | 2 ++ .../guide/environment-configuration.mdx | 2 ++ .../core/src/environment/render-snapshot.ts | 4 +++ .../src/templates/handlebars-helpers.test.ts | 29 +++++++++++++++++++ .../core/src/templates/handlebars-helpers.ts | 7 +++++ packages/executor/src/commands/git.ts | 2 ++ 6 files changed, 46 insertions(+) diff --git a/apps/agor-daemon/src/services/branches.ts b/apps/agor-daemon/src/services/branches.ts index 3e5d3ebac..d96ce4a49 100644 --- a/apps/agor-daemon/src/services/branches.ts +++ b/apps/agor-daemon/src/services/branches.ts @@ -2414,6 +2414,8 @@ export class BranchesService extends DrizzleService, Bra custom_context: branch.custom_context, unix_gid: unixGid, host_ip_address: hostIpAddress, + base_ref: branch.base_ref, + ref_type: branch.ref_type, }, requestedVariant ); diff --git a/apps/agor-docs/content/guide/environment-configuration.mdx b/apps/agor-docs/content/guide/environment-configuration.mdx index a5dc1dc96..fd20605e8 100644 --- a/apps/agor-docs/content/guide/environment-configuration.mdx +++ b/apps/agor-docs/content/guide/environment-configuration.mdx @@ -81,6 +81,8 @@ Every field in a variant is a Handlebars template. The render context is built i | `{{branch.name}}` | Branch | `feat-new-filter` | Slugified branch name. Good for `docker compose -p`. | | `{{branch.path}}` | Branch | `/home/you/agor/feat-new-filter` | Absolute path on disk. | | `{{branch.gid}}` | Branch | `1042` | Unix GID of the branch's isolation group. | +| `{{branch.base_ref}}` | Branch | `main` | Source branch/tag name the branch was created from (the "Base Branch"/"Base Tag" in the create dialog). The **name**, not a SHA. Empty string if unknown. | +| `{{branch.ref_type}}` | Branch | `branch` | `branch` or `tag` — whether `base_ref` names a branch or a tag. Defaults to `branch`. | | `{{repo.slug}}` | Repo | `preset-io/superset` | Repository slug. | | `{{host.ip_address}}` | Auto-detected | `10.0.1.42` | Fallback order: `template_overrides.host.ip_address` → `daemon.host_ip_address` in `~/.agor/config.yaml` → autodetect. | | `{{custom.}}` | Branch custom context | `{{custom.compose_profile}}` | Anything you store in `branch.custom_context`. | diff --git a/packages/core/src/environment/render-snapshot.ts b/packages/core/src/environment/render-snapshot.ts index ed24306b0..57cd54cf1 100644 --- a/packages/core/src/environment/render-snapshot.ts +++ b/packages/core/src/environment/render-snapshot.ts @@ -58,6 +58,8 @@ export interface RenderBranchInput { custom_context?: Record; unix_gid?: number; host_ip_address?: string; + base_ref?: string; + ref_type?: 'branch' | 'tag'; } /** @@ -137,6 +139,8 @@ export function renderBranchSnapshot( custom_context: branch.custom_context, unix_gid: branch.unix_gid, host_ip_address: branch.host_ip_address, + base_ref: branch.base_ref, + ref_type: branch.ref_type, }); // Per §5 of the design: defaults → template_overrides → custom. diff --git a/packages/core/src/templates/handlebars-helpers.test.ts b/packages/core/src/templates/handlebars-helpers.test.ts index 7d5b4ce76..c215b16b8 100644 --- a/packages/core/src/templates/handlebars-helpers.test.ts +++ b/packages/core/src/templates/handlebars-helpers.test.ts @@ -1056,6 +1056,8 @@ NAME={{replace (uppercase branch.name) "-" "_"}} name: 'my-branch', path: '/path/to/branch', gid: undefined, + base_ref: '', + ref_type: 'branch', }; expect(context).toEqual({ branch: expectedBranchEntity, @@ -1071,6 +1073,33 @@ NAME={{replace (uppercase branch.name) "-" "_"}} }); }); + it('should expose base_ref/ref_type under branch.* and the worktree.* alias', () => { + const context = buildBranchContext({ + branch_unique_id: 1, + name: 'feat-x', + path: '/test', + base_ref: 'main', + ref_type: 'branch', + }); + + const rendered = renderTemplate( + 'git fetch origin {{branch.base_ref}} ({{worktree.ref_type}})', + context + ); + expect(rendered).toBe('git fetch origin main (branch)'); + }); + + it('should default base_ref to empty string and ref_type to "branch" when unset', () => { + const context = buildBranchContext({ + branch_unique_id: 1, + name: 'test', + path: '/test', + }); + + const rendered = renderTemplate('[{{branch.base_ref}}][{{branch.ref_type}}]', context); + expect(rendered).toBe('[][branch]'); + }); + it('should expose host.ip_address when provided', () => { const context = buildBranchContext({ branch_unique_id: 1, diff --git a/packages/core/src/templates/handlebars-helpers.ts b/packages/core/src/templates/handlebars-helpers.ts index c826fca01..d3ffe5b00 100644 --- a/packages/core/src/templates/handlebars-helpers.ts +++ b/packages/core/src/templates/handlebars-helpers.ts @@ -292,6 +292,9 @@ export function renderTemplate( * - {{branch.name}} - Branch name (slug format) * - {{branch.path}} - Absolute path to branch directory * - {{branch.gid}} - Unix GID of branch's unix_group (resolved dynamically at execution time) + * - {{branch.base_ref}} - Source branch/tag name this branch was created from + * (the "Base Branch"/"Base Tag" from the create dialog). Empty string if unknown. + * - {{branch.ref_type}} - 'branch' | 'tag': whether base_ref names a branch or a tag * - {{repo.slug}} - Repository slug * - {{host.ip_address}} - Primary non-loopback IPv4 of the daemon host * (for health checks/URLs that must reach the host from inside a container). @@ -312,12 +315,16 @@ export function buildBranchContext(branch: { custom_context?: Record; unix_gid?: number; host_ip_address?: string; + base_ref?: string; + ref_type?: 'branch' | 'tag'; }): Record { const branchEntity = { unique_id: branch.branch_unique_id, name: branch.name, path: branch.path, gid: branch.unix_gid, + base_ref: branch.base_ref || '', + ref_type: branch.ref_type || 'branch', }; return { // Scoped entities (accessible as {{entity.property}}) diff --git a/packages/executor/src/commands/git.ts b/packages/executor/src/commands/git.ts index c04921d3b..96a454aa3 100644 --- a/packages/executor/src/commands/git.ts +++ b/packages/executor/src/commands/git.ts @@ -978,6 +978,8 @@ async function renderEnvironmentTemplates( custom_context: branch.custom_context, unix_gid: unixGid, host_ip_address: hostIpAddress, + base_ref: branch.base_ref, + ref_type: branch.ref_type, }, branch.environment_variant );