- Compile:
java Git.java - Run:
java Git
- Creates a
gitdirectory if missing - Creates
git/objectsif missing - Creates
git/indexif missing - Creates
git/HEADif missing - Prints
Git Repository Createdif anything was created - Prints
Git Repository Already Existsif all items already exist
How to run:
- Compile:
Git.java GitTester.java - Run:
java GitTester - Optionally give a number for how many cycles:
java GitTester 5
What it does:
- Verifies that git/, git/objects, git/index, git/HEAD exist after initialization
- Cleans up by deleting the git folder
- Runs multiple cycles of init + cleanup to check reliability
This milestone adds a SHA-1 hashing function that generates a unique 40-character hex string for any file's contents. The hash acts as an identifier for the file, similar to how Git tracks file objects.
- Compile: Git.java
- Run: java Git
- The program will initialize the repository and also print the SHA-1 hash of git/index.
- Method: sha1FromFile(File file)
- Reads the file with FileInputStream
- Updates a MessageDigest with SHA-1 algorithm
- Converts the result into a 40-character hexadecimal string
The output hash was checked against the online SHA-1 tool: https://emn178.github.io/online-tools/sha1.html
- Computes the SHA-1 hash of the source file's contents.
- Creates a file in
git/objects/named exactly the 40-char SHA-1. - Stores an exact copy of the source file's bytes in that object file.
- Compile: Blob.java
- Create a sample file: echo "hello" > hello.txt
- Create a BLOB: java Blob
- Check:
ls git/objects
The printed hash file should exist in
git/objects/.
- If
git/objects/does not exist, it will be created. - If the BLOB file already exists, it is not recreated.
- Compression toggle is available in
Blob.COMPRESS. Default is false.
- Compile: Blob.java BlobTester.java
- Run: java BlobTester
- The tester creates a source file, creates a BLOB, verifies the object exists, resets the objects directory, verifies removal, then creates it again.
- Staging support using a plain-text index at
git/index. - Each line is exactly
<sha1><space><filename>. - If a filename is already in the index, its line is replaced with the new hash.
- No trailing spaces on lines and no extra newline at the end of the file.
Use the Index class from your own Java code
Notes:
- This uses the existing BLOB logic to compute the SHA-1 and store the file.
- The method creates
git/,git/objects/, andgit/indexif they do not exist.
- Updates the
git/indexfile format to store the relative path of each staged file instead of just the filename. - Each entry now has the form
<sha1><space><pathname>. - Handles duplicates and modifications:
- If the same file with the same hash is added again, it is ignored.
- If identical files exist in different directories, both are tracked with separate paths but the same hash.
- If a file’s contents change, the index updates with the new hash.
Before (old format): 0acc46ad73849ea9832f600de83a014c9db9cdf0 Hello.txt
After (new format): 4377a91cdfd44db9a9bbf056849c7da0fc6cc7be myProgram/README.md ca2cb4da9485e7bbce664bf4f5ee2216a36af4fb myProgram/Hello.txt 0acc46ad73849ea9832f600de83a014c9db9cdf0 myProgram/scripts/Cat.java
- Compile: Index.java Blob.java
- Create some files and directories: mkdir -p myProgram/scripts echo "hello" > myProgram/Hello.txt echo "cat code" > myProgram/scripts/Cat.java
- Stage the files from Java code: Index idx = new Index(); idx.add("myProgram/Hello.txt"); idx.add("myProgram/scripts/Cat.java");
- Check git/index to see updated entries with relative paths.
- Implements tree creation for directories.
- A tree represents the structure of files and subdirectories inside a folder.
- Each tree is stored as an object in
git/objectswith a unique SHA-1 hash. - Trees contain:
blob <sha1> <pathname>entries for filestree <sha1> <pathname>entries for subdirectories
- Trees are created recursively so that subdirectories generate their own tree objects, which are then referenced by their parent tree.
For a directory myProgram/ with files and a scripts/ folder:
myProgram/
README.md
Hello.txt
scripts/
Cat.java
goCopy code
The scripts/ directory produces:
blob 0acc46ad73849ea9832f600de83a014c9db9cdf0 scripts/Cat.java
goCopy code
The myProgram/ directory tree includes:
blob 4377a91cdfd44db9a9bbf056849c7da0fc6cc7be myProgram/README.md
blob 0a4d55a8d778e5022fab701977c5d840bbc486d0 myProgram/Hello.txt
tree 483b5e082cf5502b303ba3dd4f3469a49495c9ef myProgram/scripts
bashCopy code
The SHA-1 of the tree file becomes its filename inside git/objects/.
- Compile: Tree.java Blob.java
- Create a directory and sample files: mkdir -p myProgram/scripts echo "readme content" > myProgram/README.md echo "hello" > myProgram/Hello.txt echo "cat code" > myProgram/scripts/Cat.java
- Run the tree creation from Java code: Tree t = new Tree(); String treeHash = t.createTree("myProgram"); System.out.println("Tree hash: " + treeHash);
- Check inside
git/objects/to verify the tree file exists.
- Generates tree objects directly from the index instead of scanning directories.
- Uses a working list that starts with
blob <sha1> <path>for each index entry. - Collapses directories bottom-up into
tree <sha1> <dirname>entries until only the root tree remains.
- WorkingList.java — builds trees from index
- WorkingListTester.java — simple runner, builds one root tree and prints its hash and contents
- Compile: Blob.java Index.java WorkingList.java WorkingListTester.java
- Run: java WorkingListTester
- Output:
- Prints the root tree SHA-1
- Prints the contents of the root tree stored under
git/objects/<sha1>