Skip to content

refactor(gc): use path helpers and add automatic large log cleanup#160

Merged
AnnatarHe merged 1 commit intomainfrom
refactor/gc-path-helpers-and-large-log-cleanup
Dec 25, 2025
Merged

refactor(gc): use path helpers and add automatic large log cleanup#160
AnnatarHe merged 1 commit intomainfrom
refactor/gc-path-helpers-and-large-log-cleanup

Conversation

@AnnatarHe
Copy link
Copy Markdown
Contributor

Summary

  • Replace os.ExpandEnv path construction with model.GetXxxPath() helpers for cross-platform compatibility
  • Add automatic cleanup of log files exceeding 50MB threshold
  • Extract helper functions (cleanLogFile, cleanLargeLogFiles, backupAndWriteFile, cleanCommandFiles) for better code organization

Test plan

  • Run shelltime gc and verify command files are cleaned correctly
  • Create a log file >50MB and verify it gets auto-cleaned
  • Run shelltime gc --withLog and verify all log files are removed regardless of size
  • Verify backup files (.bak) are created before overwriting

🤖 Generated with Claude Code

- Replace os.ExpandEnv path construction with model.GetXxxPath() helpers
- Add cleanLogFile() and cleanLargeLogFiles() for size-based cleanup
- Add backupAndWriteFile() helper for consistent backup pattern
- Extract cleanCommandFiles() for better code organization
- Auto-clean log files >50MB; --withLog forces cleanup regardless of size

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @AnnatarHe, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refactors the garbage collection (GC) command to enhance its robustness and maintainability. It standardizes file path management using dedicated helper functions, introduces an automated mechanism for cleaning up large log files, and reorganizes the GC logic into more focused, reusable functions. These changes aim to improve cross-platform compatibility and prevent storage issues caused by growing log files.

Highlights

  • Cross-Platform Path Handling: Replaced os.ExpandEnv with model.GetXxxPath() helper functions to ensure consistent and cross-platform compatible path construction for internal storage files.
  • Automatic Large Log Cleanup: Implemented automatic cleanup for log files exceeding a 50MB threshold, preventing excessive disk usage. A --withLog flag is also available for forced log removal.
  • Code Organization and Readability: Extracted core logic into new helper functions (cleanLogFile, cleanLargeLogFiles, backupAndWriteFile, cleanCommandFiles) to improve modularity and maintainability of the gc command.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@AnnatarHe AnnatarHe merged commit dc03563 into main Dec 25, 2025
2 checks passed
@AnnatarHe AnnatarHe deleted the refactor/gc-path-helpers-and-large-log-cleanup branch December 25, 2025 13:59
@codecov
Copy link
Copy Markdown

codecov Bot commented Dec 25, 2025

Codecov Report

❌ Patch coverage is 43.93939% with 37 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
commands/gc.go 43.93% 26 Missing and 11 partials ⚠️
Flag Coverage Δ
unittests 20.64% <43.93%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
commands/gc.go 43.75% <43.93%> (-2.98%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request is a solid refactoring of the gc command. It successfully replaces platform-specific path construction with helper functions, improving cross-platform compatibility. The introduction of helper functions like cleanLogFile, cleanLargeLogFiles, and backupAndWriteFile greatly improves code organization and readability. The new feature for automatic cleanup of large log files is also a valuable addition. My review includes a high-severity comment on a potential data loss scenario in the file backup logic and a medium-severity suggestion to improve error reporting during log file cleanup. All comments align with the repository's guidelines. Overall, these are great improvements to the codebase.

Comment thread commands/gc.go
Comment on lines +87 to +92
if _, err := os.Stat(filePath); err == nil {
if err := os.Rename(filePath, backupFile); err != nil {
slog.Warn("failed to backup file", slog.String("file", filePath), slog.Any("err", err))
return fmt.Errorf("failed to backup file %s: %w", filePath, err)
}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The current implementation of the os.Stat check only considers the err == nil case. If os.Stat fails for any reason other than the file not existing (e.g., a permissions error), the backup is silently skipped. This could lead to data loss if os.WriteFile later succeeds in overwriting the file. It's safer to explicitly check for os.IsNotExist and treat other errors as fatal for this operation to prevent potential data loss.

 if _, err := os.Stat(filePath); err == nil {
  if err := os.Rename(filePath, backupFile); err != nil {
   slog.Warn("failed to backup file", slog.String("file", filePath), slog.Any("err", err))
   return fmt.Errorf("failed to backup file %s: %w", filePath, err)
  }
 } else if !os.IsNotExist(err) {
  slog.Warn("failed to stat file for backup", slog.String("file", filePath), slog.Any("err", err))
  return fmt.Errorf("failed to stat file for backup %s: %w", filePath, err)
 }

Comment thread commands/gc.go
Comment on lines +70 to +80
var totalFreed int64
for _, filePath := range logFiles {
freed, err := cleanLogFile(filePath, logFileSizeThreshold, force)
if err != nil {
slog.Warn("failed to clean log file", slog.String("file", filePath), slog.Any("err", err))
continue
}
totalFreed += freed
}

if !c.Bool("skipLogCreation") {
// only can setup logger after the log file clean
SetupLogger(storageFolder)
defer CloseLogger()
return totalFreed, nil
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This function currently suppresses errors from cleanLogFile and always returns a nil error. This prevents the caller from knowing if any of the cleanup operations failed. It would be more robust to aggregate errors and return them to the caller. Using errors.Join (available in Go 1.20+) is a good way to achieve this.

This change requires importing the errors package.

 var totalFreed int64
 var multiErr error
 for _, filePath := range logFiles {
  freed, err := cleanLogFile(filePath, logFileSizeThreshold, force)
  if err != nil {
   slog.Warn("failed to clean log file", slog.String("file", filePath), slog.Any("err", err))
   multiErr = errors.Join(multiErr, err)
   continue
  }
  totalFreed += freed
 }
 return totalFreed, multiErr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant