Skip to content

Commit fdaa11f

Browse files
committed
updated storage format
1 parent 3bf143c commit fdaa11f

6 files changed

Lines changed: 68 additions & 16 deletions

File tree

cmd/add.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ func runAdd(cfg *config.Config, args []string) error {
9898
return nil
9999
}
100100

101-
head, _ := git.HeadSHA(branchName)
102-
s.Branches = append(s.Branches, stack.BranchRef{Branch: branchName, Head: head})
101+
base, _ := git.HeadSHA(currentBranch)
102+
s.Branches = append(s.Branches, stack.BranchRef{Branch: branchName, Base: base})
103103

104104
if err := stack.Save(gitDir, sf); err != nil {
105105
cfg.Errorf("failed to save stack state: %s", err)

cmd/init.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func runInit(cfg *config.Config, opts *initOptions) error {
7171
// Set repository context
7272
repo, err := cfg.Repo()
7373
if err == nil {
74-
sf.Repository = repo.Owner + "/" + repo.Name
74+
sf.Repository = repo.Host + ":" + repo.Owner + "/" + repo.Name
7575
}
7676

7777
currentBranch, _ := git.CurrentBranch()
@@ -169,8 +169,12 @@ func runInit(cfg *config.Config, opts *initOptions) error {
169169
trunkSHA, _ := git.HeadSHA(trunk)
170170
branchRefs := make([]stack.BranchRef, len(branches))
171171
for i, b := range branches {
172-
sha, _ := git.HeadSHA(b)
173-
branchRefs[i] = stack.BranchRef{Branch: b, Head: sha}
172+
parent := trunk
173+
if i > 0 {
174+
parent = branches[i-1]
175+
}
176+
base, _ := git.MergeBase(b, parent)
177+
branchRefs[i] = stack.BranchRef{Branch: b, Base: base}
174178
}
175179

176180
newStack := stack.Stack{

cmd/push.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ func runPush(cfg *config.Config, opts *pushOptions) error {
114114
continue
115115
}
116116
cfg.Successf("Created PR #%d for %s\n", newPR.Number, b.Branch)
117+
s.Branches[i].PullRequest = &stack.PullRequestRef{
118+
Number: newPR.Number,
119+
ID: newPR.ID,
120+
URL: newPR.URL,
121+
Title: newPR.Title,
122+
}
117123
} else {
118124
// Update base if needed
119125
if pr.BaseRefName != baseBranch {
@@ -125,6 +131,14 @@ func runPush(cfg *config.Config, opts *pushOptions) error {
125131
} else {
126132
cfg.Printf("PR #%d for %s is up to date\n", pr.Number, b.Branch)
127133
}
134+
if s.Branches[i].PullRequest == nil {
135+
s.Branches[i].PullRequest = &stack.PullRequestRef{
136+
Number: pr.Number,
137+
ID: pr.ID,
138+
URL: pr.URL,
139+
Title: pr.Title,
140+
}
141+
}
128142
}
129143
}
130144

@@ -138,10 +152,14 @@ func runPush(cfg *config.Config, opts *pushOptions) error {
138152
fmt.Fprintf(cfg.Err, " Once the GitHub Stacks API is available, PRs will be automatically\n")
139153
fmt.Fprintf(cfg.Err, " grouped into a Stack.\n")
140154

141-
// Update head SHAs
142-
for i, b := range s.Branches {
143-
if sha, err := git.HeadSHA(b.Branch); err == nil {
144-
s.Branches[i].Head = sha
155+
// Update base commit hashes
156+
for i := range s.Branches {
157+
parent := s.Trunk.Branch
158+
if i > 0 {
159+
parent = s.Branches[i-1].Branch
160+
}
161+
if base, err := git.HeadSHA(parent); err == nil {
162+
s.Branches[i].Base = base
145163
}
146164
}
147165

cmd/rebase.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,12 @@ func runRebase(cfg *config.Config, opts *rebaseOptions) error {
200200
_ = git.CheckoutBranch(currentBranch)
201201

202202
for i := range s.Branches {
203-
sha, _ := git.HeadSHA(s.Branches[i].Branch)
204-
s.Branches[i].Head = sha
203+
parent := s.Trunk.Branch
204+
if i > 0 {
205+
parent = s.Branches[i-1].Branch
206+
}
207+
base, _ := git.HeadSHA(parent)
208+
s.Branches[i].Base = base
205209
}
206210
_ = stack.Save(gitDir, sf)
207211

@@ -312,8 +316,12 @@ func continueRebase(cfg *config.Config, gitDir string) error {
312316
_ = git.CheckoutBranch(state.OriginalBranch)
313317

314318
for i := range s.Branches {
315-
sha, _ := git.HeadSHA(s.Branches[i].Branch)
316-
s.Branches[i].Head = sha
319+
parent := s.Trunk.Branch
320+
if i > 0 {
321+
parent = s.Branches[i-1].Branch
322+
}
323+
base, _ := git.HeadSHA(parent)
324+
s.Branches[i].Base = base
317325
}
318326
_ = stack.Save(gitDir, sf)
319327

internal/git/git.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,11 @@ func HeadSHA(ref string) (string, error) {
207207
return run("rev-parse", ref)
208208
}
209209

210+
// MergeBase returns the best common ancestor commit between two refs.
211+
func MergeBase(a, b string) (string, error) {
212+
return run("merge-base", a, b)
213+
}
214+
210215
// Log returns recent commits for the given branch.
211216
func Log(ref string, maxCount int) ([]CommitInfo, error) {
212217
format := "%H\t%s\t%at"

internal/stack/stack.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,30 @@ const (
1313
stackFileName = "gh-stack"
1414
)
1515

16-
// BranchRef represents a branch and its HEAD commit.
16+
// PullRequestRef holds relatively immutable metadata about an associated PR.
17+
type PullRequestRef struct {
18+
Number int `json:"number"`
19+
ID string `json:"id,omitempty"`
20+
URL string `json:"url,omitempty"`
21+
Title string `json:"title,omitempty"`
22+
}
23+
24+
// BranchRef represents a branch and its associated commit hash.
25+
// For the trunk, Head stores the HEAD commit SHA.
26+
// For stacked branches, Base stores the merge-base commit SHA
27+
// (the last common commit before divergence from the parent branch).
1728
type BranchRef struct {
18-
Branch string `json:"branch"`
19-
Head string `json:"head"`
29+
Branch string `json:"branch"`
30+
Head string `json:"head,omitempty"`
31+
Base string `json:"base,omitempty"`
32+
PullRequest *PullRequestRef `json:"pullRequest,omitempty"`
2033
}
2134

2235
// Stack represents a single stack of branches.
2336
type Stack struct {
37+
ID string `json:"id,omitempty"`
38+
State string `json:"state,omitempty"`
39+
Open bool `json:"open,omitempty"`
2440
Trunk BranchRef `json:"trunk"`
2541
Branches []BranchRef `json:"branches"`
2642
}
@@ -144,6 +160,7 @@ func Load(gitDir string) (*StackFile, error) {
144160
if err := json.Unmarshal(data, &sf); err != nil {
145161
return nil, fmt.Errorf("parsing stack file: %w", err)
146162
}
163+
147164
return &sf, nil
148165
}
149166

0 commit comments

Comments
 (0)