Skip to content

Commit cd14db1

Browse files
committed
revert branch changes: remove prefixes
1 parent 0818950 commit cd14db1

File tree

7 files changed

+56
-35
lines changed

7 files changed

+56
-35
lines changed

engine/internal/provision/mode_local_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func (m mockFSManager) ListBranches() (map[string]string, error) {
134134
return nil, nil
135135
}
136136

137-
func (m mockFSManager) ListAllBranches() (map[string]string, error) {
137+
func (m mockFSManager) ListAllBranches() ([]models.BranchEntity, error) {
138138
return nil, nil
139139
}
140140

engine/internal/provision/pool/manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ type Branching interface {
5757
VerifyBranchMetadata() error
5858
CreateBranch(branchName, snapshotID string) error
5959
ListBranches() (map[string]string, error)
60-
ListAllBranches() (map[string]string, error)
60+
ListAllBranches() ([]models.BranchEntity, error)
6161
GetRepo() (*models.Repo, error)
6262
GetAllRepo() (*models.Repo, error)
6363
SetRelation(parent, snapshotName string) error

engine/internal/provision/thinclones/lvm/lvmanager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func (m *LVManager) ListBranches() (map[string]string, error) {
185185
}
186186

187187
// ListAllBranches lists all branches.
188-
func (m *LVManager) ListAllBranches() (map[string]string, error) {
188+
func (m *LVManager) ListAllBranches() ([]models.BranchEntity, error) {
189189
log.Msg("ListAllBranches is not supported for LVM. Skip the operation")
190190

191191
return nil, nil

engine/internal/provision/thinclones/zfs/branching.go

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -210,28 +210,52 @@ func (m *Manager) SetMountpoint(path, name string) error {
210210

211211
// ListBranches lists data pool branches.
212212
func (m *Manager) ListBranches() (map[string]string, error) {
213-
return m.listBranches(cmdCfg{pool: m.config.Pool.Name})
213+
return m.listBranches()
214214
}
215215

216216
// ListAllBranches lists all branches.
217-
func (m *Manager) ListAllBranches() (map[string]string, error) {
218-
return m.listBranches(cmdCfg{})
219-
}
217+
func (m *Manager) ListAllBranches() ([]models.BranchEntity, error) {
218+
cmd := fmt.Sprintf(
219+
// Get all ZFS snapshots (-t) with options (-o) without output headers (-H).
220+
// Excluding snapshots without "dle:branch" property ("grep -v").
221+
`zfs list -H -t snapshot -o %s,name | grep -v "^-" | cat`, branchProp,
222+
)
220223

221-
func (m *Manager) listBranches(cfg cmdCfg) (map[string]string, error) {
222-
filter := ""
223-
args := []any{branchProp}
224+
out, err := m.runner.Run(cmd)
225+
if err != nil {
226+
return nil, fmt.Errorf("failed to list branches: %w. Out: %v", err, out)
227+
}
228+
229+
branches := make([]models.BranchEntity, 0)
230+
lines := strings.Split(strings.TrimSpace(out), "\n")
231+
232+
const expectedColumns = 2
233+
234+
for _, line := range lines {
235+
fields := strings.Fields(line)
224236

225-
if cfg.pool != "" {
226-
filter = "-r %s"
227-
228-
args = append(args, cfg.pool)
237+
if len(fields) != expectedColumns {
238+
continue
239+
}
240+
241+
if !strings.Contains(fields[0], branchSep) {
242+
branches = append(branches, models.BranchEntity{Name: fields[0], SnapshotID: fields[1]})
243+
continue
244+
}
245+
246+
for _, branchName := range strings.Split(fields[0], branchSep) {
247+
branches = append(branches, models.BranchEntity{Name: branchName, SnapshotID: fields[1]})
248+
}
229249
}
230250

251+
return branches, nil
252+
}
253+
254+
func (m *Manager) listBranches() (map[string]string, error) {
231255
cmd := fmt.Sprintf(
232256
// Get ZFS snapshots (-t) with options (-o) without output headers (-H) filtered by pool (-r).
233257
// Excluding snapshots without "dle:branch" property ("grep -v").
234-
`zfs list -H -t snapshot -o %s,name `+filter+` | grep -v "^-" | cat`, args...,
258+
`zfs list -H -t snapshot -o %s,name -r %s | grep -v "^-" | cat`, branchProp, m.config.Pool.Name,
235259
)
236260

237261
out, err := m.runner.Run(cmd)
@@ -251,15 +275,13 @@ func (m *Manager) listBranches(cfg cmdCfg) (map[string]string, error) {
251275
continue
252276
}
253277

254-
dataset, _, _ := strings.Cut(fields[1], "@")
255-
256278
if !strings.Contains(fields[0], branchSep) {
257-
branches[models.BranchName(dataset, fields[0])] = fields[1]
279+
branches[fields[0]] = fields[1]
258280
continue
259281
}
260282

261283
for _, branchName := range strings.Split(fields[0], branchSep) {
262-
branches[models.BranchName(dataset, branchName)] = fields[1]
284+
branches[branchName] = fields[1]
263285
}
264286
}
265287

@@ -329,7 +351,7 @@ func (m *Manager) getRepo(cmdCfg cmdCfg) (*models.Repo, error) {
329351
continue
330352
}
331353

332-
repo.Branches[models.BranchName(dataset, sn)] = fields[0]
354+
repo.Branches[sn] = fields[0]
333355
}
334356
}
335357

engine/internal/srv/branch.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,16 @@ func (s *Server) listBranches(w http.ResponseWriter, r *http.Request) {
4040

4141
branchDetails := make([]models.BranchView, 0, len(branches))
4242

43-
for branchName, snapshotID := range branches {
44-
snapshotDetails, ok := repo.Snapshots[snapshotID]
43+
for _, branchEntity := range branches {
44+
snapshotDetails, ok := repo.Snapshots[branchEntity.SnapshotID]
4545
if !ok {
4646
continue
4747
}
4848

49-
_, branchNam, _ := strings.Cut(branchName, "_")
50-
5149
branchDetails = append(branchDetails,
5250
models.BranchView{
53-
Name: branchNam,
54-
Parent: findBranchParent(repo.Snapshots, snapshotDetails.ID, branchNam),
51+
Name: branchEntity.Name,
52+
Parent: findBranchParent(repo.Snapshots, snapshotDetails.ID, branchEntity.Name),
5553
DataStateAt: snapshotDetails.DataStateAt,
5654
SnapshotID: snapshotDetails.ID,
5755
Dataset: snapshotDetails.Dataset,
@@ -126,7 +124,7 @@ func (s *Server) createBranch(w http.ResponseWriter, r *http.Request) {
126124
return
127125
}
128126

129-
if _, ok := branches[models.BranchName(fsm.Pool().Name, createRequest.BranchName)]; ok {
127+
if _, ok := branches[createRequest.BranchName]; ok {
130128
api.SendBadRequestError(w, r, fmt.Sprintf("branch '%s' already exists", createRequest.BranchName))
131129
return
132130
}
@@ -139,7 +137,7 @@ func (s *Server) createBranch(w http.ResponseWriter, r *http.Request) {
139137
return
140138
}
141139

142-
branchPointer, ok := branches[models.BranchName(fsm.Pool().Name, createRequest.BaseBranch)]
140+
branchPointer, ok := branches[createRequest.BaseBranch]
143141
if !ok {
144142
api.SendBadRequestError(w, r, "base branch not found")
145143
return
@@ -256,7 +254,7 @@ func (s *Server) snapshot(w http.ResponseWriter, r *http.Request) {
256254
return
257255
}
258256

259-
currentSnapshotID, ok := branches[models.BranchName(fsm.Pool().Name, clone.Branch)]
257+
currentSnapshotID, ok := branches[clone.Branch]
260258
if !ok {
261259
api.SendBadRequestError(w, r, "branch not found: "+clone.Branch)
262260
return
@@ -346,7 +344,7 @@ func (s *Server) log(w http.ResponseWriter, r *http.Request) {
346344
return
347345
}
348346

349-
snapshotID, ok := repo.Branches[models.BranchName(fsm.Pool().Name, logRequest.BranchName)]
347+
snapshotID, ok := repo.Branches[logRequest.BranchName]
350348
if !ok {
351349
api.SendBadRequestError(w, r, "branch not found: "+logRequest.BranchName)
352350
return
@@ -392,7 +390,7 @@ func (s *Server) deleteBranch(w http.ResponseWriter, r *http.Request) {
392390
return
393391
}
394392

395-
snapshotID, ok := repo.Branches[models.BranchName(fsm.Pool().Name, deleteRequest.BranchName)]
393+
snapshotID, ok := repo.Branches[deleteRequest.BranchName]
396394
if !ok {
397395
api.SendBadRequestError(w, r, "branch not found: "+deleteRequest.BranchName)
398396
return

engine/internal/srv/routes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ func (s *Server) createClone(w http.ResponseWriter, r *http.Request) {
344344
return
345345
}
346346

347-
snapshotID, ok := branches[models.BranchName(fsm.Pool().Name, cloneRequest.Branch)]
347+
snapshotID, ok := branches[cloneRequest.Branch]
348348
if !ok {
349349
api.SendBadRequestError(w, r, "branch not found")
350350
return

engine/pkg/models/branch.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ type BranchView struct {
4040
Dataset string `json:"dataset"`
4141
}
4242

43-
// BranchName returns full branch name.
44-
func BranchName(pool, branch string) string {
45-
return pool + "|" + branch
43+
// BranchEntity defines a branch-snapshot pair.
44+
type BranchEntity struct {
45+
Name string
46+
SnapshotID string
4647
}

0 commit comments

Comments
 (0)