# Add branchless trail creation

`bda1861`·

dipree·3w ago·2 files·+122 added/-3 removed

## Sessions

6604ab821878View transcript

[?\
Add Branchless Trail CreationPi·GPT-5.5·1 step](/content/gh/entireio/cli/session/019eef6c-ccb7-73c5-be4e-4d85c477b7a8#timeline-6604ab821878/index.html)

## Changes

2

- cmd/entire/cli

- Mtrail_cmd.go+5/-3

- Mtrail_cmd_test.go+117

```
1393 unmodified lines

1394
1395
1396
1397
1398
1399
1400
1401
9 unmodified lines

1411
1412
1413
1412
1414
1415
1416
1417
23 unmodified lines

1441
1442
1443
1442
1444
1445
1446
1447
15 unmodified lines

1463
1464
1465
1464
1466
1467
1468
1469

1393 unmodified lines

return strings.Join(names, ", ")
}

var runTrailCreateForm = func(form *huh.Form) error { return form.Run() }

// runTrailCreateInteractive runs the interactive form for trail creation.
// Prompts for title, body, branch (derived from title, unless branchless), and status.
func runTrailCreateInteractive(title, body, branch, statusStr *string, noBranch bool) error {
9 unmodified lines

Value(body),
 ),
 )
 if err := form.Run(); err != nil {
 if err := runTrailCreateForm(form); err != nil {
 return fmt.Errorf("form cancelled: %w", err)
 }
 *title = strings.TrimSpace(*title)
23 unmodified lines

Value(statusStr),
 ),
 )
 if err := form.Run(); err != nil {
 if err := runTrailCreateForm(form); err != nil {
 return fmt.Errorf("form cancelled: %w", err)
 }
 return nil
}
15 unmodified lines

Value(statusStr),
 ),
 )
 if err := form.Run(); err != nil {
 if err := runTrailCreateForm(form); err != nil {
 return fmt.Errorf("form cancelled: %w", err)
 }
 *branch = strings.TrimSpace(*branch)
}
```

Mcmd/entire/cli/trail_cmd.go+5/-3

```
4 unmodified lines

5
6
7
8
9
10
11
6 unmodified lines

18
19
20
21
22
23
24
49 unmodified lines

74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194

4 unmodified lines

"context"
 "encoding/json"
 "errors"
 "fmt"
 "io"
 "net/http"
 "net/http/httptest"
6 unmodified lines

"testing"
 "time"

"charm.land/huh/v2"
 "github.com/entireio/cli/cmd/entire/cli/api"
 "github.com/entireio/cli/cmd/entire/cli/auth"
 "github.com/entireio/cli/cmd/entire/cli/testutil"
49 unmodified lines

require.False(t, state.RemotePushed)
}

func TestValidateTrailCreateFlagCombosRejectsBranchlessConflicts(t *testing.T) {
 t.Parallel()

t.Run("branch", func(t *testing.T) {
  t.Parallel()
  cmd := newTrailCreateCmd()
  require.NoError(t, cmd.Flags().Set("branch", "feature/x"))

err := validateTrailCreateFlagCombos(cmd, false, true)

require.EqualError(t, err, "cannot combine --no-branch with --branch")
})

t.Run("checkout", func(t *testing.T) {
  t.Parallel()
  cmd := newTrailCreateCmd()

err := validateTrailCreateFlagCombos(cmd, true, true)

require.EqualError(t, err, "cannot combine --no-branch with --checkout")
})
}

func TestRunTrailCreateInteractiveBranchlessSkipsBranchPrompt(t *testing.T) {
  // No t.Parallel: runTrailCreateForm is package-global test seam.
  previous := runTrailCreateForm
  calls := 0
  runTrailCreateForm = func(*huh.Form) error {
   calls++
   return nil
  }
  t.Cleanup(func() { runTrailCreateForm = previous })

title := "  Branchless trail  "
  body := "body"
  branch := "must-be-cleared"
  status := ""

err := runTrailCreateInteractive(&title, &body, &branch, &status, true)

require.NoError(t, err)
  require.Equal(t, 2, calls)
  require.Equal(t, "Branchless trail", title)
  require.Empty(t, branch)
  require.Equal(t, string(trail.StatusOpen), status)
}

func TestRunTrailCreateBranchlessHappyPath(t *testing.T) {
  // No t.Parallel: uses t.Chdir plus auth/tokenstore package-level test seams.
  var gotCreate map[string]any
  var gotCreateAuth string
  srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   switch {
    case r.Method == http.MethodPost && r.URL.Path == "/oauth/token":
     w.Header().Set("Content-Type", "application/json")
     _, _ = fmt.Fprint(w, `{"access_token":"exchanged-token","token_type":"Bearer","expires_in":3600}`)
    case r.Method == http.MethodPost && r.URL.Path == "/api/v1/trails/gh/acme/repo":
     gotCreateAuth = r.Header.Get("Authorization")
     if err := json.NewDecoder(r.Body).Decode(&gotCreate); err != nil {
      t.Errorf("decode create request: %v", err)
      return
     }
     w.Header().Set("Content-Type", "application/json")
     if err := json.NewEncoder(w).Encode(api.TrailCreateResponse{
      Trail: api.TrailResource{ID: "trl_branchless", Title: "Branchless full path"},
     }); err != nil {
      t.Errorf("encode create response: %v", err)
     }
    default:
     t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
   }
  }))
  defer srv.Close()

t.Setenv(api.BaseURLEnvVar, srv.URL)
  t.Setenv("ENTIRE_CONFIG_DIR", t.TempDir())
  t.Cleanup(tokenstore.UseFileBackendForTesting(filepath.Join(t.TempDir(), "tokens.json")))
  service := tokenstore.CoreKeyringService(srv.URL)
  jwt := makeContextJWT(t, fmt.Sprintf(`{"iss":%q,"handle":"me","exp":%d}`, srv.URL, time.Now().Add(2*time.Hour).Unix()))
  require.NoError(t, tokenstore.Set(service, "me", tokenstore.EncodeTokenWithExpiration(jwt, 7200)))
  ctxObj := &contexts.Context{Name: "me@core", CoreURL: srv.URL, Handle: "me", KeychainService: service}
  t.Cleanup(auth.SetResolveContextForAPIForTest(t,
   func(context.Context, string, string, string, *http.Client, clusterdiscovery.DebugFunc) (*contexts.Context, error) {
    return ctxObj, nil
   }))

repoDir := t.TempDir()
  testutil.InitRepo(t, repoDir)
  runGitTrailTest(t, repoDir, "remote", "add", "origin", "https://github.com/acme/repo.git")
  t.Chdir(repoDir)

cmd := newTrailCreateCmd()
  cmd.SetContext(context.Background())
  cmd.Flags().Bool("insecure-http-auth", true, "")
  require.NoError(t, cmd.Flags().Set("insecure-http-auth", "true"))
  require.NoError(t, cmd.Flags().Set("title", "Branchless full path"))
  var out, errOut bytes.Buffer
  cmd.SetOut(&out)
  cmd.SetErr(&errOut)

err := runTrailCreate(cmd, "Branchless full path", "body", "main", "", "", false, true)

require.NoError(t, err)
  require.Contains(t, gotCreateAuth, "Bearer ")
  require.Equal(t, "Branchless full path", gotCreate["title"])
  require.Equal(t, "body", gotCreate["body"])
  require.Equal(t, "main", gotCreate["base"])
  require.Equal(t, string(trail.StatusOpen), gotCreate["status"])
  require.NotContains(t, gotCreate, "branch_name")
  require.NotContains(t, gotCreate, "branch_action")
  require.Contains(t, out.String(), `Created trail "Branchless full path" (ID: trl_branchless)`)
  require.NotContains(t, out.String(), "Pushed branch")
  require.Empty(t, errOut.String())
}

func TestCleanupCreatedTrailBranch(t *testing.T) {
 cases := []struct {
 name             string
}
