Skip to content

Commit 7aa55fc

Browse files
committed
reset: --fetch auto fetch missing commit/tree
1 parent 4438c06 commit 7aa55fc

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

pkg/command/command_reset.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type Reset struct {
2323
Keep bool `name:"keep" help:"Reset HEAD but keep local changes"`
2424
Fetch bool `name:"fetch" help:"Fetch missing objects"`
2525
One bool `name:"one" help:"Checkout large files one after another, --hard mode only"`
26+
Limit int64 `name:"limit" short:"L" help:"Omits blobs larger than n bytes or units. n may be zero. supported units: KB,MB,GB,K,M,G" default:"-1" type:"size"`
2627
Quiet bool `name:"quiet" help:"Operate quietly. Progress is not reported to the standard error stream"`
2728
paths []string `kong:"-"`
2829
}
@@ -79,6 +80,18 @@ func (c *Reset) validateFlags() string {
7980
return ""
8081
}
8182

83+
func (c *Reset) resetAutoFetch(w *zeta.Worktree) error {
84+
oid, err := w.Prefetch(context.Background(), c.Revision, c.Limit, c.One)
85+
if err != nil {
86+
return err
87+
}
88+
if err := w.Reset(context.Background(), &zeta.ResetOptions{Commit: oid, Mode: c.ResetMode(), Fetch: c.Fetch, One: c.One, Quiet: c.Quiet}); err != nil {
89+
fmt.Fprintf(os.Stderr, "zeta reset to %s error: %v\n", oid, err)
90+
return err
91+
}
92+
return nil
93+
}
94+
8295
func (c *Reset) Run(g *Globals) error {
8396
if action := c.validateFlags(); len(action) != 0 {
8497
diev("cannot %s reset with paths.", action)
@@ -103,9 +116,16 @@ func (c *Reset) Run(g *Globals) error {
103116
}
104117
r.Close()
105118
}()
119+
w := r.Worktree()
120+
106121
if len(c.Revision) == 0 {
107122
c.Revision = string(plumbing.HEAD)
108123
}
124+
125+
if c.Fetch || c.One {
126+
return c.resetAutoFetch(w)
127+
}
128+
109129
oid, err := r.Revision(context.Background(), c.Revision)
110130
if plumbing.IsNoSuchObject(err) {
111131
fmt.Fprintf(os.Stderr, "zeta reset: %s not found\n", c.Revision)
@@ -120,20 +140,12 @@ func (c *Reset) Run(g *Globals) error {
120140
return errors.New("no such revision")
121141
}
122142
if len(c.paths) != 0 {
123-
w := r.Worktree()
124143
if err := w.ResetSpec(context.Background(), oid, slashPaths(c.paths)); err != nil {
125144
fmt.Fprintf(os.Stderr, "zeta reset: error: %v\n", err)
126145
return err
127146
}
128147
return nil
129148
}
130-
if c.Fetch || c.One {
131-
if err := r.FetchObjects(context.Background(), oid, c.One); err != nil {
132-
fmt.Fprintf(os.Stderr, "zeta reset: fetch missing objects error: %v\n", err)
133-
return err
134-
}
135-
}
136-
w := r.Worktree()
137149
if err := w.Reset(context.Background(), &zeta.ResetOptions{Commit: oid, Mode: c.ResetMode(), Fetch: c.Fetch, One: c.One, Quiet: c.Quiet}); err != nil {
138150
fmt.Fprintf(os.Stderr, "zeta reset to %s error: %v\n", oid, err)
139151
return err

pkg/zeta/worktree.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,26 @@ func (r *Repository) getTreeFromHash(ctx context.Context, oid plumbing.Hash) (*o
230230
return nil, fmt.Errorf("object '%s' not tree or commit", oid)
231231
}
232232

233+
func (w *Worktree) Prefetch(ctx context.Context, revision string, limit int64, skipLarge bool) (plumbing.Hash, error) {
234+
oid, err := w.Revision(ctx, revision)
235+
switch {
236+
case err == nil:
237+
fo, err := w.DoFetch(ctx, &DoFetchOptions{Name: revision, FetchAlways: true, Limit: limit})
238+
if err != nil {
239+
return plumbing.ZeroHash, err
240+
}
241+
oid = fo.FETCH_HEAD
242+
case plumbing.IsNoSuchObject(err):
243+
default:
244+
return plumbing.ZeroHash, err
245+
}
246+
if err := w.FetchObjects(ctx, oid, skipLarge); err != nil {
247+
fmt.Fprintf(os.Stderr, "prefetch: fetch missing objects error: %v\n", err)
248+
return plumbing.ZeroHash, err
249+
}
250+
return oid, nil
251+
}
252+
233253
func (w *Worktree) Reset(ctx context.Context, opts *ResetOptions) error {
234254
if opts.One {
235255
w.missingNotFailure = true

0 commit comments

Comments
 (0)