From db0c5c91a52c9339954b176c6eb659491ca91372 Mon Sep 17 00:00:00 2001 From: Jason Hall Date: Wed, 23 Jul 2025 22:56:40 -0400 Subject: [PATCH] Update to use single hello.txt file instead of creating new files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of creating a new file for each pull (pull_1.txt, pull_2.txt, etc.), now updates a single hello.txt file with the pull number and timestamp with nanosecond precision. Changes: - Modified commit generator to update hello.txt instead of creating new files - Added hello.txt to initial repository commit - Updated tests to verify hello.txt content instead of checking for new files - Fixed test expectations to account for counter increment during clone 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- internal/generator/commit.go | 18 ++++++++++-------- internal/repo/repo.go | 26 ++++++++++++++++++++------ main_test.go | 16 ++++++++++++---- 3 files changed, 42 insertions(+), 18 deletions(-) diff --git a/internal/generator/commit.go b/internal/generator/commit.go index 1d48b29..db4141c 100644 --- a/internal/generator/commit.go +++ b/internal/generator/commit.go @@ -63,26 +63,28 @@ func (g *Generator) GenerateCommit() (string, error) { // Parse existing tree entries existingEntries := parseTree(parentTreeData) - // Create new file content - filename := fmt.Sprintf("pull_%d.txt", count) - content := fmt.Sprintf("Pull request #%d\nTimestamp: %s\n", count, time.Now().Format(time.RFC3339)) + // Create updated hello.txt content with nanosecond precision + filename := "hello.txt" + content := fmt.Sprintf("Pull #%d\nTimestamp: %s\n", count, time.Now().Format("2006-01-02 15:04:05.999999999")) - // Create blob for new file + // Create blob for updated file blob := object.NewBlob([]byte(content)) blobHash, err := g.repo.WriteObject(blob) if err != nil { return "", fmt.Errorf("writing blob: %w", err) } - // Create new tree with all existing entries plus new file + // Create new tree with all existing entries, replacing hello.txt tree := object.NewTree() - // Add existing entries + // Add existing entries, but skip hello.txt as we'll add the updated one for _, entry := range existingEntries { - tree.AddEntry(entry.Mode, entry.Name, entry.Hash) + if entry.Name != filename { + tree.AddEntry(entry.Mode, entry.Name, entry.Hash) + } } - // Add new file + // Add updated hello.txt tree.AddEntry("100644", filename, blobHash) treeHash, err := g.repo.WriteObject(tree) diff --git a/internal/repo/repo.go b/internal/repo/repo.go index 6e217c5..ceb739e 100644 --- a/internal/repo/repo.go +++ b/internal/repo/repo.go @@ -91,15 +91,24 @@ func (r *Repository) createInitialCommit() error { readmeContent := []byte("# Infinite Git Repository\n\nThis repository generates a new commit every time you pull.\n") // Create blob for README - blob := object.NewBlob(readmeContent) - blobHash, err := object.Write(r.gitDir, blob) + readmeBlob := object.NewBlob(readmeContent) + readmeBlobHash, err := object.Write(r.gitDir, readmeBlob) if err != nil { - return fmt.Errorf("writing blob: %w", err) + return fmt.Errorf("writing README blob: %w", err) } - // Create tree with README + // Create initial hello.txt content + helloContent := []byte("Pull #0\nTimestamp: Initial commit\n") + helloBlob := object.NewBlob(helloContent) + helloBlobHash, err := object.Write(r.gitDir, helloBlob) + if err != nil { + return fmt.Errorf("writing hello.txt blob: %w", err) + } + + // Create tree with README and hello.txt tree := object.NewTree() - tree.AddEntry("100644", "README.md", blobHash) + tree.AddEntry("100644", "README.md", readmeBlobHash) + tree.AddEntry("100644", "hello.txt", helloBlobHash) treeHash, err := object.Write(r.gitDir, tree) if err != nil { return fmt.Errorf("writing tree: %w", err) @@ -124,12 +133,17 @@ func (r *Repository) createInitialCommit() error { return fmt.Errorf("updating ref: %w", err) } - // Also write README to working directory + // Also write README and hello.txt to working directory readmePath := filepath.Join(r.path, "README.md") if err := os.WriteFile(readmePath, readmeContent, 0644); err != nil { return fmt.Errorf("writing README to working directory: %w", err) } + helloPath := filepath.Join(r.path, "hello.txt") + if err := os.WriteFile(helloPath, helloContent, 0644); err != nil { + return fmt.Errorf("writing hello.txt to working directory: %w", err) + } + return nil } diff --git a/main_test.go b/main_test.go index bed2de8..008bef4 100644 --- a/main_test.go +++ b/main_test.go @@ -5,6 +5,7 @@ import ( "net/http/httptest" "os" "path/filepath" + "strings" "sync" "testing" @@ -89,10 +90,17 @@ func TestCloneAndPull(t *testing.T) { t.Errorf("expected %d commits after pull %d, got %d", initialCount+i+1, i+1, newCount) } - // Verify new files exist - expectedFile := filepath.Join(clientRepoDir, fmt.Sprintf("pull_%d.txt", i+1)) - if _, err := os.Stat(expectedFile); os.IsNotExist(err) { - t.Errorf("expected file %s does not exist", expectedFile) + // Verify hello.txt exists and has been updated + helloFile := filepath.Join(clientRepoDir, "hello.txt") + content, err := os.ReadFile(helloFile) + if err != nil { + t.Errorf("failed to read hello.txt: %v", err) + } else { + // Clone increments counter to 1, so first pull is #2, second is #3, etc. + expectedContent := fmt.Sprintf("Pull #%d\n", i+2) + if !strings.Contains(string(content), expectedContent) { + t.Errorf("hello.txt does not contain expected content %q, got: %s", expectedContent, content) + } } }