diff --git a/out/production/java-course-project/commander/CLI.class b/out/production/java-course-project/commander/CLI.class index 1ab754ee9cfc45b0682bece1bd7e5d905e79671b..8a415d25346c1bbca53420c221a82b75eec4616f 100644 Binary files a/out/production/java-course-project/commander/CLI.class and b/out/production/java-course-project/commander/CLI.class differ diff --git a/out/production/java-course-project/core/JitHash.class b/out/production/java-course-project/core/JitHash.class index f8f64e331404e4fa339d28471b827a81ba8fd1d8..2f0b278c9a6a6bb4dd18a3dde1200e215a237aea 100644 Binary files a/out/production/java-course-project/core/JitHash.class and b/out/production/java-course-project/core/JitHash.class differ diff --git a/out/production/java-course-project/fileoperation/FileCreation.class b/out/production/java-course-project/fileoperation/FileCreation.class index bdb9a90174076a77ac2ad2b0745ae1bcab865e1f..df5c9f646982692d1e1d18dd8f3bd8f6e5bfad93 100644 Binary files a/out/production/java-course-project/fileoperation/FileCreation.class and b/out/production/java-course-project/fileoperation/FileCreation.class differ diff --git a/out/production/java-course-project/gitobject/Blob.class b/out/production/java-course-project/gitobject/Blob.class index d0d0bdf32e14d254788288e74016aea2604de746..9062734d92ed8074ff25ef738a2f7b8dcf367a96 100644 Binary files a/out/production/java-course-project/gitobject/Blob.class and b/out/production/java-course-project/gitobject/Blob.class differ diff --git a/out/production/java-course-project/gitobject/Commit.class b/out/production/java-course-project/gitobject/Commit.class index 13f84496f0e41161bd627d882183ad50d7d6b78d..c9ecec3e8c609d3252cf2ef5fd49ccec0c068db6 100644 Binary files a/out/production/java-course-project/gitobject/Commit.class and b/out/production/java-course-project/gitobject/Commit.class differ diff --git a/out/production/java-course-project/gitobject/GitObject.class b/out/production/java-course-project/gitobject/GitObject.class index 7e0a5491dd03478fefa7f433102ec8a325f8c58f..1b7c292a6a25e85fc4581b77a5a33772847508fc 100644 Binary files a/out/production/java-course-project/gitobject/GitObject.class and b/out/production/java-course-project/gitobject/GitObject.class differ diff --git a/out/production/java-course-project/gitobject/Tree$1.class b/out/production/java-course-project/gitobject/Tree$1.class index b089e62ac74f2f4d5beebf60cdfd2c0a6f4a710c..43f9c650fe7ed491416d3198d736d90fb12011e7 100644 Binary files a/out/production/java-course-project/gitobject/Tree$1.class and b/out/production/java-course-project/gitobject/Tree$1.class differ diff --git a/out/production/java-course-project/gitobject/Tree.class b/out/production/java-course-project/gitobject/Tree.class index 8018e1774fa4a86072da7fa48761b747125d6c0a..e55ac10a5c67ee7694744e68ea4af1f23ef246ec 100644 Binary files a/out/production/java-course-project/gitobject/Tree.class and b/out/production/java-course-project/gitobject/Tree.class differ diff --git a/out/production/java-course-project/repository/Repository.class b/out/production/java-course-project/repository/Repository.class index 378316dd0c9bbfb46b77d4b633465c60d4fd81e7..a5fbf5fcb4bea7a00ead6900a40c111204c777b1 100644 Binary files a/out/production/java-course-project/repository/Repository.class and b/out/production/java-course-project/repository/Repository.class differ diff --git a/src/commander/CLI.java b/src/commander/CLI.java index 96ab054d92e00b91ba94ba6a17105cd68b7e5399..a6f6c71c2f321decc9fec1e874108803078f12ae 100644 --- a/src/commander/CLI.java +++ b/src/commander/CLI.java @@ -2,12 +2,9 @@ package commander; import java.io.File; import java.io.IOException; -import java.util.LinkedList; import core.*; -import gitobject.Commit; -import repository.Repository; @@ -20,7 +17,7 @@ public class CLI { */ public static void jitInit(String[] args) throws IOException { String path = ""; - if(args.length <= 2) { //get default working path + if(args.length <= 2) { path = new File(".").getCanonicalPath(); JitInit.init(path); }else if(args[2].equals("-help")){ //see help @@ -37,6 +34,30 @@ public class CLI { } } + /** + * Command 'jit hash' + * @param args + * @throws IOException + */ + public static void jitHash(String[] args) throws IOException { + String path = ""; + if(args.length <= 2) { //get default working path + path = new File(".").getCanonicalPath(); + JitHash.hash(path); + }else if(args[2].equals("-help")){ //see help + System.out.println("usage: jit hash [] [-help]\r\n" + + "\r\n" + + "jit hash []: Hash the jit repository."); + }else { + path = args[2]; + if(!new File(path).isDirectory()) { //if the working path input is illegal + System.out.println(path + "is not a legal directory. Please init your reposiroty again. See 'jit init -help'."); + }else { + JitHash.hash(path); + } + } + } + /** * Command 'jit help'. @@ -80,7 +101,11 @@ public class CLI { }else { if(args[1].equals("init")) { jitInit(args); - } else { + } + else if(args[1].equals("hash")) { + jitHash(args); + } + else { System.out.println("jit: " + args[1] + "is not a git command. See 'git help'."); } } diff --git a/src/core/JitHash.java b/src/core/JitHash.java index 8d3f28a05f63b43f8874c096ec3d96a8e149d960..0dd4abc628c4c191785865e35b7ed3fad6e72b32 100644 --- a/src/core/JitHash.java +++ b/src/core/JitHash.java @@ -1,18 +1,34 @@ package core; -import fileoperation.FileDeletion; +import gitobject.Tree; import repository.Repository; +import java.io.File; import java.io.IOException; public class JitHash { /** - * Init repository in your working area. + * Generate a hash file in your repository. * @param filename - * @throws IOException + * @throws IOException */ - public static void hash(String filename) throws IOException { + @SuppressWarnings("unused") + public static void hash(String filename) throws IOException { /* Todo: You should pass the filename in this function, and generate a hash file in your repository. * Add your code here.*/ + //Repository repo = new Repository("C:\\Users\\xyz80\\eclipse-workspace\\SimpleGit"); + File dir = new File(filename); + if(dir.exists()){ + try { + Tree tree = new Tree(dir); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + System.out.println("Hash file has been generated successfully."); + } + else { + throw new IOException("filename is not exist, please check"); + } } } diff --git a/src/fileoperation/FileCreation.java b/src/fileoperation/FileCreation.java index 00628d8c47c8907506d006f967b4585c31cb2967..47522b8a894de0bff954cb60297920dc98ff595c 100644 --- a/src/fileoperation/FileCreation.java +++ b/src/fileoperation/FileCreation.java @@ -26,6 +26,7 @@ public class FileCreation { if(file.isDirectory()) { throw new IOException(filename + " already exists, and it's not a file."); } + FileWriter fileWriter = new FileWriter(file); fileWriter.write(text); fileWriter.close(); @@ -42,13 +43,12 @@ public class FileCreation { if(!new File(parentPath).isDirectory()){ throw new IOException("The path doesn't exist!"); } - String path = parentPath; - FileDeletion.deleteFile(path + File.separator + paths[0]); for(int i = 0; i < paths.length; i++) { - String folderPath = path + File.separator + paths[i]; - new File(folderPath).mkdirs(); + String path = parentPath; + FileDeletion.deleteFile(path + File.separator + paths[i]); + path += File.separator + paths[i]; + new File(path).mkdirs(); } - } /** diff --git a/src/gitobject/Blob.java b/src/gitobject/Blob.java index dc2c378cb8d0378179c3c1b3133067860d1d6a49..1b99ad6fc5028ca3487a274747bf413b2d13c1f0 100644 --- a/src/gitobject/Blob.java +++ b/src/gitobject/Blob.java @@ -1,7 +1,6 @@ package gitobject; import sha1.SHA1; -import zlib.ZLibUtils; import java.io.*; @@ -31,8 +30,8 @@ public class Blob extends GitObject{ fmt = "blob"; mode = "100644"; value = getValue(file); - String key = genKey(file); - compressWrite(key); + key = genKey(file); + writeObject(); } /** @@ -46,7 +45,7 @@ public class Blob extends GitObject{ /** * Todo: Add your code here. */ - path = path + "//" + Id.substring(0, 2) + "//" + Id.substring(2); + path = path + File.separator + Id.substring(0, 2) + File.separator + Id.substring(2); ObjectInputStream file = new ObjectInputStream(new FileInputStream(path)); blob = (Blob)file.readObject(); file.close(); diff --git a/src/gitobject/Commit.java b/src/gitobject/Commit.java index 402065ff421c86a481ff1827490dd6e353307c08..7c5e51d70d915c947b2ee289f5b38ac5f53b38c3 100644 --- a/src/gitobject/Commit.java +++ b/src/gitobject/Commit.java @@ -1,15 +1,14 @@ package gitobject; import java.io.*; -import java.util.ArrayList; -import fileoperation.FileReader; import sha1.SHA1; import repository.Repository; -import zlib.ZLibUtils; public class Commit extends GitObject{ - protected String tree; // the sha1 value of present committed tree + + private static final long serialVersionUID = -5753132822294252545L; + protected String tree; // the sha1 value of present committed tree protected String parent; // the sha1 value of the parent commit protected String author; // the author's name and timestamp protected String committer; // the committer's info @@ -25,7 +24,7 @@ public class Commit extends GitObject{ /** * Construct a commit directly from a file. * @param - * author, committer, message参数在git commit命令里创建 + * author, committer, message鍙傛暟鍦╣it commit鍛戒护閲屽垱寤� * @throws Exception */ public Commit(String treePath, String author, String committer, String message) throws Exception { @@ -41,19 +40,19 @@ public class Commit extends GitObject{ *parent d62cf8ef977082319d8d8a0cf5150dfa1573c2b7 *author xxx 1502331401 +0800 *committer xxx 1502331401 +0800 - *修复增量bug + *淇澧為噺bug * */ this.value = "tree " + this.tree + "\nparent " + this.parent+ "\nauthor " + this.author + "\ncommitter " + this.committer + "\n" + this.message; key = genKey(); - compressWrite(key); + writeObject(); } /** * Construct a commit from a built tree. * @param - * author, committer, message参数在git commit命令里创建 + * author, committer, message鍙傛暟鍦╣it commit鍛戒护閲屽垱寤� * @throws Exception */ public Commit(Tree t, String author, String committer, String message) throws Exception { @@ -69,11 +68,11 @@ public class Commit extends GitObject{ *parent d62cf8ef977082319d8d8a0cf5150dfa1573c2b7 *author xxx 1502331401 +0800 *committer xxx 1502331401 +0800 - *修复增量bug + *淇澧為噺bug * */ this.value = "tree " + this.tree + "\nparent " + this.parent+ "\nauthor " + this.author + "\ncommitter " + this.committer + "\n" + this.message; - key = genKey(); - compressWrite(key); + this.key = genKey(); + writeObject(); } /** @@ -81,15 +80,24 @@ public class Commit extends GitObject{ * @param commitId * @throws IOException */ - public Commit(String commitId) throws IOException { - + public static Commit deserialize(String commitId) throws IOException { + Commit commit = null; + try{ + path = path + File.separator + commitId.substring(0, 2) + File.separator + commitId.substring(2); + ObjectInputStream file = new ObjectInputStream(new FileInputStream(path)); + commit = (Commit)file.readObject(); + file.close(); + }catch (Exception e){ + e.printStackTrace(); + } + return commit; } /** * Generate the hash value of this commit. * @return key * */ public String genKey() throws Exception { - key = SHA1.getHash(value); + key = SHA1.getHash(this.value); return key; } diff --git a/src/gitobject/GitObject.java b/src/gitobject/GitObject.java index 013a0e4d3bf424fd5012a714beada7b7e595e83a..460e248346b4d0880579bb69b533a6d371bd9174 100644 --- a/src/gitobject/GitObject.java +++ b/src/gitobject/GitObject.java @@ -57,12 +57,12 @@ public class GitObject implements Serializable { * Todo: Serialize the object to the local repository. * @throws Exception */ - public void writeObject(String key) throws Exception{ + public void writeObject() throws Exception{ /** * Add your code here. */ - FileCreation.createDirectory(path, key.substring(0, 2)); - ObjectOutputStream file = new ObjectOutputStream(new FileOutputStream(path + "//" + key.substring(0, 2) + "//" + key.substring(2))); + FileCreation.createDirectory(path, this.key.substring(0, 2)); + ObjectOutputStream file = new ObjectOutputStream(new FileOutputStream(path + File.separator + this.key.substring(0, 2) + File.separator + this.key.substring(2))); GitObject object = this; file.writeObject(object); file.close(); @@ -72,20 +72,19 @@ public class GitObject implements Serializable { * Todo: Serialize the object and compress with zlib. * @throws Exception */ - public void compressWrite(String key) throws Exception{ + public void compressWrite() throws Exception{ /** * Add your code here. */ GitObject object = this; - FileCreation.createDirectory(path, key.substring(0, 2)); - OutputStream os = new FileOutputStream(path + "//" + key.substring(0, 2) + "//" + key.substring(2)); + FileCreation.createDirectory(path, this.key.substring(0, 2)); + OutputStream os = new FileOutputStream(path + File.separator + this.key.substring(0, 2) + File.separator + this.key.substring(2)); ByteArrayOutputStream out = new ByteArrayOutputStream(); ObjectOutputStream file = new ObjectOutputStream(out); file.writeObject(object); file.flush(); byte[] bytes = out.toByteArray(); ZLibUtils.compress(bytes, os); - } } diff --git a/src/gitobject/Tree.java b/src/gitobject/Tree.java index 8120ac2ad27cf19e672eccd4be60ac43ee6ce717..38491d385fb4ea9dca110a66f38bedbaeec1ed11 100644 --- a/src/gitobject/Tree.java +++ b/src/gitobject/Tree.java @@ -4,12 +4,7 @@ package gitobject; import sha1.SHA1; -import zlib.ZLibUtils; -import fileoperation.FileReader; - import java.io.*; -import java.lang.reflect.Array; -import java.security.MessageDigest; import java.util.*; @@ -34,8 +29,8 @@ public class Tree extends GitObject{ this.fmt = "tree"; this.mode = "040000"; this.value = ""; - String key = genKey(file); - compressWrite(key); + this.key = genKey(file); + writeObject(); } /** @@ -73,8 +68,13 @@ public class Tree extends GitObject{ @Override public int compare(File o1, File o2) { /* Todo: Add your code here. */ - - return o1.compareTo(o2); + if(o1.isDirectory() && o2.isFile()) { + return -1; + } + if(o1.isFile() && o2.isDirectory()) { + return 1; + } + return o1.getName().compareTo(o2.getName()); } }); @@ -89,8 +89,20 @@ public class Tree extends GitObject{ */ public String genKey(File dir) throws Exception{ /* Todo: Add your code here. */ - String key = SHA1.getHash(dir.getName()); - return key; + File[] fs = dir.listFiles(); + List filelist = sortFile(fs); + for(File item: filelist) { + if(item.isFile()) { + Blob blob = new Blob(item); + this.value += blob.toString() + " " + item.getName() + "\n"; + } + else if(item.isDirectory()) { + Tree tree = new Tree(item); + this.value += tree.toString() + " " + item.getName() + "\n"; + } + } + String key = SHA1.getHash(this.value); + return key; } @Override diff --git a/src/repository/Repository.java b/src/repository/Repository.java index eec94352567512d734f76835492aac77803d85bc..249cd44f4ee9246c31d309f78c73ff23078971eb 100644 --- a/src/repository/Repository.java +++ b/src/repository/Repository.java @@ -60,10 +60,10 @@ public class Repository { * @throws IOException */ public void createRepo() throws IOException { - File folder = new File(workTree + "\\.jit"); + File folder = new File(workTree + File.separator + ".jit"); folder.mkdir(); - Runtime.getRuntime().exec("attrib +H " + folder.getAbsolutePath()); - String path = workTree + "\\.jit"; + Runtime.getRuntime().exec("attrib +H " + folder.getAbsolutePath()); //设置为隐藏文件夹 + String path = workTree + File.separator + ".jit" + File.separator; String[] folderlist = {"objects", "refs", "logs"}; String[] filelist = {"index", "HEAD"};