From 0816e807c9f9e29239f905856cf25d205a562bad Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sat, 1 May 2021 15:41:23 -0600 Subject: That's what Commit.Force was for! --- fi-prune-empty2/main.go | 5 ++++- fi-prune-empty2/prune.go | 5 +++++ fi-prune-empty2/stream.go | 40 +++++++++++++++++++++++++++++++--------- 3 files changed, 40 insertions(+), 10 deletions(-) diff --git a/fi-prune-empty2/main.go b/fi-prune-empty2/main.go index 0278a66..dd2594d 100644 --- a/fi-prune-empty2/main.go +++ b/fi-prune-empty2/main.go @@ -31,6 +31,8 @@ type Args struct { help bool srcdir string + allowExcludedParents bool + pruneEmpty PruneArg pruneFastForward PruneArg existingReplace ExistingReplaceArg @@ -44,6 +46,7 @@ func main() { argparser.BoolVarP(&args.help, "help", "h", false, "Show this message") argparser.StringVar(&args.srcdir, "srcdir", ".", "Path to the source git repository; this is required for '--prune-empty=auto' and '--prune-fastforward=auto'") + argparser.BoolVar(&args.allowExcludedParents, "allow-excluded-parents", false, "Whether to allow excluded parents to be referenced by commit-hash rather than by mark") args.pruneEmpty = PruneAuto argparser.Var(&args.pruneEmpty, "prune-empty", "Whether to prune empty commits; 'auto' means only prune commits which have become empty but are not empty in the --srcdir repo with GIT_NO_REPLACE_OBJECTS") @@ -86,7 +89,7 @@ func main() { frontend := libfastimport.NewFrontend(os.Stdin, os.Stdin, nil) backend := libfastimport.NewBackend(os.Stdout, os.Stdout, nil) - filter := NewHandler(backend, NewDriver(args, backend)) + filter := NewHandler(backend, NewDriver(args, backend), args.allowExcludedParents) if err := fiutil.RunHandler(frontend, filter); err != nil { fmt.Fprintf(os.Stderr, "%s: error: %v\n", os.Args[0], err) os.Exit(1) diff --git a/fi-prune-empty2/prune.go b/fi-prune-empty2/prune.go index e3181b2..517f9c5 100644 --- a/fi-prune-empty2/prune.go +++ b/fi-prune-empty2/prune.go @@ -291,6 +291,11 @@ func (p *Pruner) isFastForward(commit Commit) (bool, error) { } func (p *Pruner) shouldPrune(commit Commit) (bool, error) { + if commit.ExcludedParents { + // If there are any excluded parents, then we can't + // reason about it. Keep the commit. + return false, nil + } switch p.pruneEmpty { case PruneAlways: isEmpty, err := p.isEmpty(commit) diff --git a/fi-prune-empty2/stream.go b/fi-prune-empty2/stream.go index be36d54..cadaf8b 100644 --- a/fi-prune-empty2/stream.go +++ b/fi-prune-empty2/stream.go @@ -75,6 +75,8 @@ type Commit struct { OriginalOID Hash Parents []Mark Tree Tree + + ExcludedParents bool } type Driver interface { @@ -123,6 +125,8 @@ type Handler struct { backend *libfastimport.Backend driver Driver + allowExcludedParents bool + // current refs refs map[string]Mark @@ -136,11 +140,13 @@ type Handler struct { beg time.Time } -func NewHandler(backend *libfastimport.Backend, driver Driver) *Handler { +func NewHandler(backend *libfastimport.Backend, driver Driver, allowExcludedParents bool) *Handler { return &Handler{ backend: backend, driver: driver, + allowExcludedParents: allowExcludedParents, + // current refs refs: map[string]Mark{}, @@ -214,7 +220,11 @@ func (h *Handler) CmdCommitEnd(cmd libfastimport.CmdCommitEnd) error { return parents, nil }() if err != nil { - return err + if h.allowExcludedParents { + cmt.ExcludedParents = true + } else { + return err + } } sort.Stable(h.commitFile) @@ -255,14 +265,26 @@ func (h *Handler) CmdCommitEnd(cmd libfastimport.CmdCommitEnd) error { // apply the mutations from ProcessCommit to // h.commitMeta - if len(cmtptr.Parents) == 0 { - h.commitMeta.From = EmptyHash - h.commitMeta.Merge = nil + if cmt.ExcludedParents { + h.commitMeta.From = h.driver.FixCommitIsh(h.commitMeta.From) + merge := make([]string, 0, len(h.commitMeta.Merge)) + for i := range h.commitMeta.Merge { + fixed := h.driver.FixCommitIsh(h.commitMeta.Merge[i]) + if fixed != "" { + merge = append(merge, fixed) + } + } + h.commitMeta.Merge = merge } else { - h.commitMeta.From = fmt.Sprintf(":%d", cmtptr.Parents[0]) - h.commitMeta.Merge = nil - for _, merge := range cmtptr.Parents[1:] { - h.commitMeta.Merge = append(h.commitMeta.Merge, fmt.Sprintf(":%d", merge)) + if len(cmtptr.Parents) == 0 { + h.commitMeta.From = EmptyHash + h.commitMeta.Merge = nil + } else { + h.commitMeta.From = fmt.Sprintf(":%d", cmtptr.Parents[0]) + h.commitMeta.Merge = nil + for _, merge := range cmtptr.Parents[1:] { + h.commitMeta.Merge = append(h.commitMeta.Merge, fmt.Sprintf(":%d", merge)) + } } } -- cgit v1.2.2