Skip to content

Instantly share code, notes, and snippets.

@ambrose40
Created February 28, 2024 13:25
Show Gist options
  • Select an option

  • Save ambrose40/865e8f2f72b9d0b84c955c35a012420f to your computer and use it in GitHub Desktop.

Select an option

Save ambrose40/865e8f2f72b9d0b84c955c35a012420f to your computer and use it in GitHub Desktop.
Batch create git commit history from scratch, using max commit size
import os
import subprocess
# Set the maximum allowed size in MB
MAX_COMMIT_SIZE = 200
def get_files_and_sizes(directory):
files_and_sizes = []
for root, _, files in os.walk(directory):
for f in files:
filepath = os.path.join(root, f)
# Prefix the file path with '\\?\' to handle long paths on Windows
long_filepath = '\\\\?\\' + os.path.abspath(filepath)
size = os.path.getsize(long_filepath)
files_and_sizes.append((long_filepath, size))
return files_and_sizes
def create_batches(files_and_sizes):
batches = []
current_batch = []
current_size = 0
for file, size in files_and_sizes:
# Convert size to MB
size_mb = size / (1024 * 1024)
if current_size + size_mb > MAX_COMMIT_SIZE:
batches.append(current_batch)
current_batch = []
current_size = 0
current_batch.append(file)
current_size += size_mb
# Add the last batch if it has files
if current_batch:
batches.append(current_batch)
return batches
def commit_batches(batches):
for i, batch in enumerate(batches, start=1):
for file in batch:
subprocess.run(['git', 'add', file])
commit_message = f"Commit {i} with batched files"
subprocess.run(['git', 'commit', '-m', commit_message])
print(f"Batch {i} committed.")
# Usage
directory = "C:/repo-folder-name/"
files_and_sizes = get_files_and_sizes(directory)
batches = create_batches(files_and_sizes)
commit_batches(batches)
@ambrose40

Copy link
Copy Markdown
Author

@ambrose40

Copy link
Copy Markdown
Author

To work with longpaths properly on Windows, Git also needs this config to be set from elevated command line:
git config --system core.longpaths true

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