Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 6 additions & 70 deletions licensecheck/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,6 @@ func parseYearFromGitOutput(output []byte, useFirstLine bool) (int, error) {
// calculateYearUpdates determines if a copyright needs updating and calculates new years
// Returns: (shouldUpdate bool, newStartYear int, newEndYear int)
func calculateYearUpdates(
filePath string,
info *CopyrightInfo,
canonicalStartYear int,
lastCommitYear int,
Expand All @@ -341,16 +340,10 @@ func calculateYearUpdates(
}

// Condition 2: Only update end year if file was modified after the copyright end year, or forceCurrentYear is true
// But only if there are non-copyright changes in the file
if lastCommitYear > info.EndYear {
// Check for non-copyright changes between HEAD and HEAD~1
currentContent, err1 := getFileContentExcludingCopyright(filePath)
prevCommittedContent, err2 := getPreviousCommittedFileContent(filePath)
if err1 == nil && err2 == nil && currentContent != prevCommittedContent {
if info.EndYear < currentYear {
newEndYear = currentYear
shouldUpdate = true
}
if info.EndYear < currentYear {
newEndYear = currentYear
shouldUpdate = true
}
}

Expand Down Expand Up @@ -441,7 +434,6 @@ func GetRepoLastCommitYear(workingDir string) (int, error) {
// evaluateCopyrightUpdates evaluates all copyrights in a file and returns which ones need updating
// This is shared logic between UpdateCopyrightHeader and NeedsUpdate
func evaluateCopyrightUpdates(
filePath string,
copyrights []*CopyrightInfo,
targetHolder string,
configYear int,
Expand Down Expand Up @@ -474,7 +466,7 @@ func evaluateCopyrightUpdates(
}

shouldUpdate, newStartYear, newEndYear := calculateYearUpdates(
filePath, info, canonicalStartYear, lastCommitYear, currentYear, forceCurrentYear,
info, canonicalStartYear, lastCommitYear, currentYear, forceCurrentYear,
)

if shouldUpdate {
Expand Down Expand Up @@ -530,7 +522,7 @@ func UpdateCopyrightHeader(filePath string, targetHolder string, configYear int,

// Evaluate which copyrights need updating
updates := evaluateCopyrightUpdates(
filePath, copyrights, targetHolder, configYear, lastCommitYear, currentYear, forceCurrentYear, repoFirstYear,
copyrights, targetHolder, configYear, lastCommitYear, currentYear, forceCurrentYear, repoFirstYear,
)

if len(updates) == 0 {
Expand Down Expand Up @@ -617,64 +609,8 @@ func NeedsUpdate(filePath string, targetHolder string, configYear int, forceCurr

// Evaluate which copyrights need updating
updates := evaluateCopyrightUpdates(
filePath, copyrights, targetHolder, configYear, lastCommitYear, currentYear, forceCurrentYear, repoFirstYear,
copyrights, targetHolder, configYear, lastCommitYear, currentYear, forceCurrentYear, repoFirstYear,
)

return len(updates) > 0, nil
}

// getFileContentExcludingCopyright returns the file content with copyright lines removed
func getFileContentExcludingCopyright(filePath string) (string, error) {
content, err := os.ReadFile(filePath)
if err != nil {
return "", err
}
lines := strings.Split(string(content), "\n")
// Get all copyright info (line numbers)
copyrights, err := extractAllCopyrightInfo(filePath)
if err != nil {
return "", err
}
copyrightLineNums := make(map[int]struct{})
for _, info := range copyrights {
copyrightLineNums[info.LineNumber] = struct{}{}
}
var filtered []string
for i, line := range lines {
// Line numbers are 1-based in CopyrightInfo
if _, isCopyright := copyrightLineNums[i+1]; !isCopyright {
filtered = append(filtered, line)
}
}
return strings.Join(filtered, "\n"), nil
}

// getPreviousCommittedFileContent returns the previous committed version (HEAD~1) of the file (excluding copyright lines)
func getPreviousCommittedFileContent(filePath string) (string, error) {
absPath, err := filepath.Abs(filePath)
if err != nil {
return "", err
}
repoRoot, err := getRepoRoot(filepath.Dir(absPath))
if err != nil {
return "", err
}
relPath, err := filepath.Rel(repoRoot, absPath)
if err != nil {
return "", err
}
output, err := executeGitCommand(repoRoot, "show", "HEAD~1:"+relPath)
if err != nil {
return "", err
}
lines := strings.Split(string(output), "\n")
// Use parseCopyrightLine to check each line
var filtered []string
for i, line := range lines {
// parseCopyrightLine returns non-nil if line is a valid copyright
if parseCopyrightLine(line, i+1, filePath) == nil {
filtered = append(filtered, line)
}
}
return strings.Join(filtered, "\n"), nil
}
13 changes: 4 additions & 9 deletions licensecheck/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -799,15 +799,10 @@ package main
func TestCalculateYearUpdates(t *testing.T) {
currentYear := time.Now().Year()

tempDir := t.TempDir()
tempFile := filepath.Join(tempDir, "dummy.go")
// write a minimal file so functions that read the file can operate
require.NoError(t, os.WriteFile(tempFile, []byte("package main\n"), 0644))

t.Run("Update start year when canonical differs", func(t *testing.T) {
info := &CopyrightInfo{StartYear: 2023, EndYear: 2023}
shouldUpdate, newStart, newEnd := calculateYearUpdates(
tempFile, info, 2020, 2023, currentYear, false,
info, 2020, 2023, currentYear, false,
)
assert.True(t, shouldUpdate)
assert.Equal(t, 2020, newStart)
Expand All @@ -817,15 +812,15 @@ func TestCalculateYearUpdates(t *testing.T) {
t.Run("No update when already current", func(t *testing.T) {
info := &CopyrightInfo{StartYear: 2020, EndYear: currentYear}
shouldUpdate, _, _ := calculateYearUpdates(
tempFile, info, 2020, currentYear, currentYear, false,
info, 2020, currentYear, currentYear, false,
)
assert.False(t, shouldUpdate)
})

t.Run("Force current year updates end year", func(t *testing.T) {
info := &CopyrightInfo{StartYear: 2020, EndYear: currentYear - 1}
shouldUpdate, newStart, newEnd := calculateYearUpdates(
tempFile, info, 2020, currentYear-1, currentYear, true,
info, 2020, currentYear-1, currentYear, true,
)
assert.True(t, shouldUpdate)
assert.Equal(t, 2020, newStart)
Expand All @@ -835,7 +830,7 @@ func TestCalculateYearUpdates(t *testing.T) {
t.Run("No years uses config and force updates end", func(t *testing.T) {
info := &CopyrightInfo{StartYear: 0, EndYear: 0}
shouldUpdate, newStart, newEnd := calculateYearUpdates(
tempFile, info, 2022, 0, currentYear, true,
info, 2022, 0, currentYear, true,
)
assert.True(t, shouldUpdate)
assert.Equal(t, 2022, newStart)
Expand Down
Loading