MCPcopy
hub / github.com/ddworken/hishtory / ImportHistory

Function ImportHistory

client/lib/lib.go:218–354  ·  view source on GitHub ↗
(ctx context.Context, shouldReadStdin, force bool)

Source from the content-addressed store, hash-verified

216const NUM_IMPORTED_ENTRIES_SLOW int = 20_000
217
218func ImportHistory(ctx context.Context, shouldReadStdin, force bool) (int, error) {
219 config := hctx.GetConf(ctx)
220 if config.HaveCompletedInitialImport && !force {
221 // Don't run an import if we already have run one. This avoids importing the same entry multiple times.
222 return 0, nil
223 }
224 homedir := hctx.GetHome(ctx)
225 inputFiles := []string{
226 filepath.Join(homedir, ".bash_history"),
227 filepath.Join(homedir, ".zsh_history"),
228 }
229 if histfile := os.Getenv("HISTFILE"); histfile != "" && !slices.Contains(inputFiles, histfile) {
230 inputFiles = append(inputFiles, histfile)
231 }
232 zHistPath := filepath.Join(homedir, ".zhistory")
233 if !slices.Contains(inputFiles, zHistPath) {
234 inputFiles = append(inputFiles, zHistPath)
235 }
236 entriesIter := parseFishHistory(homedir)
237 for _, file := range inputFiles {
238 entriesIter = concatIterators(entriesIter, readFileToIterator(file))
239 }
240 totalNumEntries, err := countLinesInFiles(inputFiles...)
241 if err != nil {
242 return 0, fmt.Errorf("failed to count input lines during hishtory import: %w", err)
243 }
244 if shouldReadStdin {
245 extraEntries, err := ReadStdin()
246 if err != nil {
247 return 0, fmt.Errorf("failed to read stdin: %w", err)
248 }
249 entriesIter = concatIterators(entriesIter, Values(extraEntries))
250 totalNumEntries += len(extraEntries)
251 }
252 fishLines, err := countLinesInFile(getFishHistoryPath(homedir))
253 if err != nil {
254 return 0, fmt.Errorf("failed to count fish history lines during hishtory import: %w", err)
255 }
256 totalNumEntries += fishLines
257 db := hctx.GetDb(ctx)
258 currentUser, err := user.Current()
259 if err != nil {
260 return 0, err
261 }
262 hostname, err := os.Hostname()
263 if err != nil {
264 return 0, err
265 }
266 numEntriesImported := 0
267 var iteratorError error = nil
268 var batch []data.HistoryEntry
269 importTimestamp := time.Now().UTC()
270 importEntryId := uuid.Must(uuid.NewRandom()).String()
271 var bar *progressbar.ProgressBar
272 if totalNumEntries > NUM_IMPORTED_ENTRIES_SLOW {
273 fmt.Println("Importing existing history entries")
274 bar = progressbar.Default(int64(totalNumEntries))
275 defer bar.Finish()

Callers 7

TestImportHistoryFunction · 0.92
BenchmarkImportFunction · 0.92
BenchmarkQueryFunction · 0.92
TestTuiBenchFunction · 0.92
BenchmarkGetRowsFunction · 0.92
import.goFile · 0.92
install.goFile · 0.92

Calls 15

GetConfFunction · 0.92
GetHomeFunction · 0.92
GetDbFunction · 0.92
SetConfigFunction · 0.92
parseFishHistoryFunction · 0.85
concatIteratorsFunction · 0.85
readFileToIteratorFunction · 0.85
countLinesInFilesFunction · 0.85
ReadStdinFunction · 0.85
ValuesFunction · 0.85
countLinesInFileFunction · 0.85
getFishHistoryPathFunction · 0.85

Tested by 5

TestImportHistoryFunction · 0.74
BenchmarkImportFunction · 0.74
BenchmarkQueryFunction · 0.74
TestTuiBenchFunction · 0.74
BenchmarkGetRowsFunction · 0.74