github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/libfs/unstage_file.go (about)

     1  // Copyright 2016 Keybase Inc. All rights reserved.
     2  // Use of this source code is governed by a BSD
     3  // license that can be found in the LICENSE file.
     4  
     5  package libfs
     6  
     7  import (
     8  	"bytes"
     9  
    10  	"github.com/keybase/client/go/kbfs/data"
    11  	"github.com/keybase/client/go/kbfs/libkbfs"
    12  	"github.com/keybase/client/go/logger"
    13  	"golang.org/x/net/context"
    14  )
    15  
    16  // UnstageForTesting unstages all unmerged commits and fast-forwards
    17  // to the current master, if the given data is non-empty. If the given
    18  // data is empty, it does nothing.
    19  //
    20  // This should only be needed if there is a bug in automatic conflict
    21  // resolution.
    22  //
    23  // If the given data begins with the bytes "async", the unstaging is
    24  // done asynchronously, i.e. this function returns immediately and the
    25  // unstaging happens in the background. (Other subsequent IO
    26  // operations may be blocked, though.) You can figure out when the
    27  // unstage succeeds by consulting .kbfs_status.
    28  func UnstageForTesting(ctx context.Context, log logger.Logger,
    29  	config libkbfs.Config, fb data.FolderBranch,
    30  	data []byte) (int, error) {
    31  	log.CDebugf(ctx, "UnstageForTesting(%v, %v)", fb, data)
    32  	if len(data) == 0 {
    33  		return 0, nil
    34  	}
    35  
    36  	if bytes.HasPrefix(data, []byte("async")) {
    37  		go func() {
    38  			ctx := context.Background()
    39  			err := config.KBFSOps().UnstageForTesting(ctx, fb)
    40  			if err != nil {
    41  				log.Warning("Async UnstageForTesting error: %v", err)
    42  			}
    43  		}()
    44  	} else {
    45  		err := config.KBFSOps().UnstageForTesting(ctx, fb)
    46  		if err != nil {
    47  			return 0, err
    48  		}
    49  	}
    50  	return len(data), nil
    51  }