From 08c352c6c3f3134306dd8d128c24b5d28f0f5a6c Mon Sep 17 00:00:00 2001 From: Anton Date: Mon, 17 Sep 2018 21:23:07 +0300 Subject: [PATCH 1/5] mygit initial commit --- hw_git_1/pom.xml | 55 +++++++ hw_git_1/src/main/java/hw_git/GitCli.java | 47 ++++++ hw_git_1/src/main/java/hw_git/GitCore.java | 151 ++++++++++++++++++ .../src/main/java/hw_git/RepInformation.java | 41 +++++ .../java/hw_git/UnversionedException.java | 5 + hw_git_1/src/test/java/hw_git/AppTest.java | 16 ++ 6 files changed, 315 insertions(+) create mode 100644 hw_git_1/pom.xml create mode 100644 hw_git_1/src/main/java/hw_git/GitCli.java create mode 100644 hw_git_1/src/main/java/hw_git/GitCore.java create mode 100644 hw_git_1/src/main/java/hw_git/RepInformation.java create mode 100644 hw_git_1/src/main/java/hw_git/UnversionedException.java create mode 100644 hw_git_1/src/test/java/hw_git/AppTest.java diff --git a/hw_git_1/pom.xml b/hw_git_1/pom.xml new file mode 100644 index 0000000..643d287 --- /dev/null +++ b/hw_git_1/pom.xml @@ -0,0 +1,55 @@ + + 4.0.0 + + 1 + hw_git_1 + 0.0.1-SNAPSHOT + jar + + hw_git_1 + http://maven.apache.org + + + UTF-8 + + + + + maven-compiler-plugin + + 1.8 + 1.8 + UTF-8 + + + + + + + junit + junit + 3.8.1 + test + + + + + com.fasterxml.jackson.core + jackson-core + 2.9.6 + + + + com.fasterxml.jackson.core + jackson-annotations + 2.9.6 + + + + com.fasterxml.jackson.core + jackson-databind + 2.9.6 + + + diff --git a/hw_git_1/src/main/java/hw_git/GitCli.java b/hw_git_1/src/main/java/hw_git/GitCli.java new file mode 100644 index 0000000..899d64d --- /dev/null +++ b/hw_git_1/src/main/java/hw_git/GitCli.java @@ -0,0 +1,47 @@ +package hw_git; + +import java.io.IOException; +import java.util.Arrays; + +import com.fasterxml.jackson.core.JsonGenerationException; +import com.fasterxml.jackson.databind.JsonMappingException; + +public class GitCli { + public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException { + System.out.println("args"); + for (int i = 0; i < args.length; i++) { + System.out.println(args[i]); + } + + GitCore core = new GitCore(); + int revision; + + try { + switch (args[0]) { + case "init": + core.makeInit(); + break; + case "commit": + System.out.println("Commiting..."); + core.makeCommit(args[1], Arrays.copyOfRange(args, 2, args.length)); + break; + case "checkout": + revision = Integer.parseInt(args[1]); + System.out.println("Check out to revision " + revision); + core.makeCheckout(revision); + break; + case "reset": + revision = Integer.parseInt(args[1]); + System.out.println("Performing reset to revision " + revision); + core.makeReset(revision); + break; + case "log": + revision = args.length == 2 ? Integer.parseInt(args[1]) : -1; + System.out.println("Log: " + core.getLog(revision)); + break; + } + } catch (UnversionedException e) { + System.out.println("This directory is not versioned"); + } + } +} diff --git a/hw_git_1/src/main/java/hw_git/GitCore.java b/hw_git_1/src/main/java/hw_git/GitCore.java new file mode 100644 index 0000000..d27d1c0 --- /dev/null +++ b/hw_git_1/src/main/java/hw_git/GitCore.java @@ -0,0 +1,151 @@ +package hw_git; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.sql.Timestamp; +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; + +import com.fasterxml.jackson.core.JsonGenerationException; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class GitCore { + RepInformation inform = null; + Path informPath = null; + final String infoFileName = ".myGitData"; + final String storageFolder = ".mygitdata"; + + private void findRepInformation() throws JsonParseException, JsonMappingException, IOException, UnversionedException { + RepInformation result = null; + Path p = Paths.get(""); + + while (p != null && !Files.exists(p.resolve(infoFileName))) { + p = p.getParent(); + } + + if (p != null) { + ObjectMapper omapper = new ObjectMapper(); + result = omapper.readValue(p.resolve(infoFileName).toFile(), RepInformation.class); + } + + if (result == null) { + throw new UnversionedException(); + } + inform = result; + informPath = p; + } + + private void updateRepInformation() throws JsonGenerationException, JsonMappingException, IOException { + ObjectMapper omapper = new ObjectMapper(); + System.out.println("writing to path: " + informPath.toString()); + omapper.writeValue(informPath.resolve(infoFileName).toFile(), inform); + } + + void makeInit() throws JsonGenerationException, JsonMappingException, IOException, UnversionedException { + try { + findRepInformation(); + } catch (UnversionedException e) { + //ObjectMapper omapper = new ObjectMapper(); + //omapper.writeValue(Paths.get("").resolve(filename).toFile(), new RepInformation()); + informPath = Paths.get(""); + inform = new RepInformation(); + //inform.allFiles.put(Paths.get(""), 0); + updateRepInformation(); + System.out.println("Ok."); + } + } + + void increaseRevisionNumber() { + inform.revision++; + } + + private Path getPathRealRelative(String filename) { + return informPath.relativize(Paths.get("")).resolve(filename); + } + + private Path getStoragePath(String filename, int revision) { + Path rel = getPathRealRelative(filename); + String fname = rel.getFileName().toString(); + + rel = rel.getParent(); + + Path storage = informPath.resolve(rel).resolve(fname + revision); + + return storage; + } + + private void addFile(String filename) throws IOException { + int revision = inform.revision; + Files.copy(Paths.get("").resolve(filename), getStoragePath(filename, revision)); + inform.allFiles.put(getPathRealRelative(filename).toString(), revision); + } + + void makeCommit(String message, String[] filenames) throws IOException, UnversionedException { + findRepInformation(); + increaseRevisionNumber(); + inform.commitMessages.add(message); + inform.timestamps.add(new Timestamp(0)); + for (String fname : filenames) { + addFile(fname); + } + } + + private void deleteVersionedFiles(File root) { + if (root.isFile()) { + if (inform.allFiles.containsKey(root.toPath().relativize(informPath))) { + System.out.println("deleting" + root.getName()); + } + return; + } + if (root.isDirectory()) { + for (File f : root.listFiles()) { + if (f.getName().equals(infoFileName) || f.getName().equals(storageFolder)) { + continue; + } + deleteVersionedFiles(f); + } + } + } + + private void restoreVersionedFiles(int revision) throws IOException { + for (Map.Entry ent : inform.allFiles.entrySet()) { + if (ent.getValue() <= revision) { + Files.copy(informPath.resolve(storageFolder).resolve(ent.getKey()), + informPath.resolve(ent.getKey())); + } + } + } + + void makeCheckout(int revision) throws IOException, UnversionedException { + findRepInformation(); + deleteVersionedFiles(informPath.toFile()); + restoreVersionedFiles(revision); + } + + void makeReset(int revision) throws JsonParseException, JsonMappingException, IOException, UnversionedException { + findRepInformation(); + Iterator> it = inform.allFiles.entrySet().iterator(); + while(it.hasNext()) { + Map.Entry ent = it.next(); + if (ent.getValue() > revision) { + it.remove(); + } + } + updateRepInformation(); + } + + String getLog(int revision) throws JsonParseException, JsonMappingException, IOException, UnversionedException { + findRepInformation(); + if (revision == -1) { + revision = inform.revision; + } + return inform.commitMessages.get(revision) + "\n" + + inform.timestamps.get(revision); + } +} diff --git a/hw_git_1/src/main/java/hw_git/RepInformation.java b/hw_git_1/src/main/java/hw_git/RepInformation.java new file mode 100644 index 0000000..e897903 --- /dev/null +++ b/hw_git_1/src/main/java/hw_git/RepInformation.java @@ -0,0 +1,41 @@ +package hw_git; + +import java.nio.file.Path; +import java.sql.Timestamp; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; + +public class RepInformation { + int revision = 0; + ArrayList commitMessages = new ArrayList<>(); + ArrayList timestamps = new ArrayList<>(); + Map allFiles = new TreeMap<>(); + + public int getRevision() { + return revision; + } + public void setRevision(int revision) { + this.revision = revision; + } + public List getCommitMessages() { + return commitMessages; + } + public void setCommitMessages(ArrayList commitMessages) { + this.commitMessages = commitMessages; + } + public List getTimestamps() { + return timestamps; + } + public void setTimestamps(ArrayList timestamps) { + this.timestamps = timestamps; + } + public Map getAllFiles() { + return allFiles; + } + public void setAllFiles(Map allFiles) { + this.allFiles = allFiles; + } +} diff --git a/hw_git_1/src/main/java/hw_git/UnversionedException.java b/hw_git_1/src/main/java/hw_git/UnversionedException.java new file mode 100644 index 0000000..33f9cf9 --- /dev/null +++ b/hw_git_1/src/main/java/hw_git/UnversionedException.java @@ -0,0 +1,5 @@ +package hw_git; + +public class UnversionedException extends Exception { + +} diff --git a/hw_git_1/src/test/java/hw_git/AppTest.java b/hw_git_1/src/test/java/hw_git/AppTest.java new file mode 100644 index 0000000..5f5385f --- /dev/null +++ b/hw_git_1/src/test/java/hw_git/AppTest.java @@ -0,0 +1,16 @@ +package hw_git; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest extends TestCase +{ + + public void testInformationLoad() { + + } +} From a798fc0442e634e8d4d6f0adec1d49d55bc49d85 Mon Sep 17 00:00:00 2001 From: Anton Date: Mon, 17 Sep 2018 23:43:40 +0300 Subject: [PATCH 2/5] Revisions lists added --- README.md | 8 ++- hw_git_1/src/main/java/hw_git/GitCli.java | 4 +- hw_git_1/src/main/java/hw_git/GitCore.java | 52 +++++++++++++++---- .../src/main/java/hw_git/RepInformation.java | 6 +-- 4 files changed, 55 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 4314260..2a79e5e 100644 --- a/README.md +++ b/README.md @@ -1 +1,7 @@ -# java_homeworks \ No newline at end of file +## Система контроля версий +Для запуска системы контроля версий выполните +``` + +mvn exec:java -Dexec.mainClass=hw_git.GitCli -Dexec.args="commit commessage testdir/file1" + +``` diff --git a/hw_git_1/src/main/java/hw_git/GitCli.java b/hw_git_1/src/main/java/hw_git/GitCli.java index 899d64d..702f13b 100644 --- a/hw_git_1/src/main/java/hw_git/GitCli.java +++ b/hw_git_1/src/main/java/hw_git/GitCli.java @@ -8,7 +8,7 @@ public class GitCli { public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException { - System.out.println("args"); + System.out.println("number of args = " + args.length); for (int i = 0; i < args.length; i++) { System.out.println(args[i]); } @@ -39,6 +39,8 @@ public static void main(String[] args) throws JsonGenerationException, JsonMappi revision = args.length == 2 ? Integer.parseInt(args[1]) : -1; System.out.println("Log: " + core.getLog(revision)); break; + default: + System.out.println("Unknown argument: " + args[0]); } } catch (UnversionedException e) { System.out.println("This directory is not versioned"); diff --git a/hw_git_1/src/main/java/hw_git/GitCore.java b/hw_git_1/src/main/java/hw_git/GitCore.java index d27d1c0..4ed10c0 100644 --- a/hw_git_1/src/main/java/hw_git/GitCore.java +++ b/hw_git_1/src/main/java/hw_git/GitCore.java @@ -6,7 +6,10 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.sql.Timestamp; +import java.util.ArrayList; +import java.util.Arrays; import java.util.Iterator; +import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -75,15 +78,23 @@ private Path getStoragePath(String filename, int revision) { rel = rel.getParent(); - Path storage = informPath.resolve(rel).resolve(fname + revision); + Path storage = informPath.resolve(storageFolder).resolve(rel).resolve(fname + revision); return storage; } private void addFile(String filename) throws IOException { int revision = inform.revision; + Path storage = getStoragePath(filename, revision); + storage.getParent().toFile().mkdirs(); + System.out.println("trying write file to " + storage); Files.copy(Paths.get("").resolve(filename), getStoragePath(filename, revision)); - inform.allFiles.put(getPathRealRelative(filename).toString(), revision); + ArrayList revisions = inform.allFiles.get(getPathRealRelative(filename).toString()); + if (revisions == null) { + revisions = new ArrayList<>(); + inform.allFiles.put(getPathRealRelative(filename).toString(), revisions); + } + revisions.add(revision); } void makeCommit(String message, String[] filenames) throws IOException, UnversionedException { @@ -94,6 +105,7 @@ void makeCommit(String message, String[] filenames) throws IOException, Unversio for (String fname : filenames) { addFile(fname); } + updateRepInformation(); } private void deleteVersionedFiles(File root) { @@ -113,10 +125,23 @@ private void deleteVersionedFiles(File root) { } } + private int getIndexOfLessEq(ArrayList list, int val) { + int curr = 0; + int prev = -1; + while (curr < list.size() && list.get(curr) <= val) { + prev = curr; + curr++; + } + + return prev; + } + private void restoreVersionedFiles(int revision) throws IOException { - for (Map.Entry ent : inform.allFiles.entrySet()) { - if (ent.getValue() <= revision) { - Files.copy(informPath.resolve(storageFolder).resolve(ent.getKey()), + for (Map.Entry> ent : inform.allFiles.entrySet()) { + int revisionIdx = getIndexOfLessEq(ent.getValue(), revision); + if (revisionIdx >= 0) { + int revNumber = ent.getValue().get(revisionIdx); + Files.copy(informPath.resolve(storageFolder).resolve(ent.getKey() + revNumber), informPath.resolve(ent.getKey())); } } @@ -130,12 +155,16 @@ void makeCheckout(int revision) throws IOException, UnversionedException { void makeReset(int revision) throws JsonParseException, JsonMappingException, IOException, UnversionedException { findRepInformation(); - Iterator> it = inform.allFiles.entrySet().iterator(); + Iterator>> it = inform.allFiles.entrySet().iterator(); while(it.hasNext()) { - Map.Entry ent = it.next(); - if (ent.getValue() > revision) { + Map.Entry> ent = it.next(); + int revisionIndex = getIndexOfLessEq(ent.getValue(), revision); + if (revisionIndex == -1) { it.remove(); + } else { + ent.getValue().subList(revisionIndex + 1, ent.getValue().size()).clear(); } + } updateRepInformation(); } @@ -145,7 +174,10 @@ String getLog(int revision) throws JsonParseException, JsonMappingException, IOE if (revision == -1) { revision = inform.revision; } - return inform.commitMessages.get(revision) + "\n" - + inform.timestamps.get(revision); + if(revision == 0) { + return "Empty log"; + } + return inform.commitMessages.get(revision - 1) + "\n" + + inform.timestamps.get(revision - 1); } } diff --git a/hw_git_1/src/main/java/hw_git/RepInformation.java b/hw_git_1/src/main/java/hw_git/RepInformation.java index e897903..b0d58dc 100644 --- a/hw_git_1/src/main/java/hw_git/RepInformation.java +++ b/hw_git_1/src/main/java/hw_git/RepInformation.java @@ -12,7 +12,7 @@ public class RepInformation { int revision = 0; ArrayList commitMessages = new ArrayList<>(); ArrayList timestamps = new ArrayList<>(); - Map allFiles = new TreeMap<>(); + Map> allFiles = new TreeMap<>(); public int getRevision() { return revision; @@ -32,10 +32,10 @@ public List getTimestamps() { public void setTimestamps(ArrayList timestamps) { this.timestamps = timestamps; } - public Map getAllFiles() { + public Map> getAllFiles() { return allFiles; } - public void setAllFiles(Map allFiles) { + public void setAllFiles(Map> allFiles) { this.allFiles = allFiles; } } From 9af5d2e5b60b99ac3e441b111c00870b41c0fdea Mon Sep 17 00:00:00 2001 From: Anton Date: Mon, 17 Sep 2018 23:46:51 +0300 Subject: [PATCH 3/5] time fixed --- hw_git_1/src/main/java/hw_git/GitCore.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hw_git_1/src/main/java/hw_git/GitCore.java b/hw_git_1/src/main/java/hw_git/GitCore.java index 4ed10c0..4bc608f 100644 --- a/hw_git_1/src/main/java/hw_git/GitCore.java +++ b/hw_git_1/src/main/java/hw_git/GitCore.java @@ -101,7 +101,7 @@ void makeCommit(String message, String[] filenames) throws IOException, Unversio findRepInformation(); increaseRevisionNumber(); inform.commitMessages.add(message); - inform.timestamps.add(new Timestamp(0)); + inform.timestamps.add(new Timestamp(System.currentTimeMillis())); for (String fname : filenames) { addFile(fname); } From 6dba0a1008524ee32e7a052df2e415474b0d396f Mon Sep 17 00:00:00 2001 From: Anton Date: Tue, 18 Sep 2018 01:28:45 +0300 Subject: [PATCH 4/5] some bugs fixed --- hw_git_1/src/main/java/hw_git/GitCli.java | 5 +-- hw_git_1/src/main/java/hw_git/GitCore.java | 36 +++++++++++++------ .../src/main/java/hw_git/RepInformation.java | 2 -- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/hw_git_1/src/main/java/hw_git/GitCli.java b/hw_git_1/src/main/java/hw_git/GitCli.java index 702f13b..ffa7e0c 100644 --- a/hw_git_1/src/main/java/hw_git/GitCli.java +++ b/hw_git_1/src/main/java/hw_git/GitCli.java @@ -8,10 +8,6 @@ public class GitCli { public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException { - System.out.println("number of args = " + args.length); - for (int i = 0; i < args.length; i++) { - System.out.println(args[i]); - } GitCore core = new GitCore(); int revision; @@ -24,6 +20,7 @@ public static void main(String[] args) throws JsonGenerationException, JsonMappi case "commit": System.out.println("Commiting..."); core.makeCommit(args[1], Arrays.copyOfRange(args, 2, args.length)); + System.out.println("Commit made at revision " + core.getCurrentRevision()); break; case "checkout": revision = Integer.parseInt(args[1]); diff --git a/hw_git_1/src/main/java/hw_git/GitCore.java b/hw_git_1/src/main/java/hw_git/GitCore.java index 4bc608f..a7bf96a 100644 --- a/hw_git_1/src/main/java/hw_git/GitCore.java +++ b/hw_git_1/src/main/java/hw_git/GitCore.java @@ -7,9 +7,7 @@ import java.nio.file.Paths; import java.sql.Timestamp; import java.util.ArrayList; -import java.util.Arrays; import java.util.Iterator; -import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -19,10 +17,10 @@ import com.fasterxml.jackson.databind.ObjectMapper; public class GitCore { - RepInformation inform = null; - Path informPath = null; - final String infoFileName = ".myGitData"; - final String storageFolder = ".mygitdata"; + private RepInformation inform = null; + private Path informPath = null; + private final String infoFileName = ".myGitData"; + private final String storageFolder = ".mygitdata"; private void findRepInformation() throws JsonParseException, JsonMappingException, IOException, UnversionedException { RepInformation result = null; @@ -87,7 +85,7 @@ private void addFile(String filename) throws IOException { int revision = inform.revision; Path storage = getStoragePath(filename, revision); storage.getParent().toFile().mkdirs(); - System.out.println("trying write file to " + storage); + //System.out.println("trying write file to " + storage); Files.copy(Paths.get("").resolve(filename), getStoragePath(filename, revision)); ArrayList revisions = inform.allFiles.get(getPathRealRelative(filename).toString()); if (revisions == null) { @@ -110,8 +108,15 @@ void makeCommit(String message, String[] filenames) throws IOException, Unversio private void deleteVersionedFiles(File root) { if (root.isFile()) { - if (inform.allFiles.containsKey(root.toPath().relativize(informPath))) { - System.out.println("deleting" + root.getName()); + String key = informPath.toAbsolutePath() + .relativize(Paths.get(root.getAbsolutePath())) + .toString(); + + //System.out.println("key: " + key); + + if (inform.allFiles.containsKey(key)) { + System.out.println("deleting " + root.getName()); + root.delete(); } return; } @@ -143,13 +148,14 @@ private void restoreVersionedFiles(int revision) throws IOException { int revNumber = ent.getValue().get(revisionIdx); Files.copy(informPath.resolve(storageFolder).resolve(ent.getKey() + revNumber), informPath.resolve(ent.getKey())); + System.out.println("restored " + ent.getKey()); } } } void makeCheckout(int revision) throws IOException, UnversionedException { findRepInformation(); - deleteVersionedFiles(informPath.toFile()); + deleteVersionedFiles(informPath.toAbsolutePath().toFile()); restoreVersionedFiles(revision); } @@ -166,6 +172,9 @@ void makeReset(int revision) throws JsonParseException, JsonMappingException, IO } } + inform.revision = revision; + inform.commitMessages.subList(revision , inform.commitMessages.size()).clear(); + inform.timestamps.subList(revision, inform.timestamps.size()).clear(); updateRepInformation(); } @@ -177,7 +186,12 @@ String getLog(int revision) throws JsonParseException, JsonMappingException, IOE if(revision == 0) { return "Empty log"; } - return inform.commitMessages.get(revision - 1) + "\n" + return "revision: " + revision + "\n" + + inform.commitMessages.get(revision - 1) + "\n" + inform.timestamps.get(revision - 1); } + + int getCurrentRevision() { + return inform.revision; + } } diff --git a/hw_git_1/src/main/java/hw_git/RepInformation.java b/hw_git_1/src/main/java/hw_git/RepInformation.java index b0d58dc..89c288e 100644 --- a/hw_git_1/src/main/java/hw_git/RepInformation.java +++ b/hw_git_1/src/main/java/hw_git/RepInformation.java @@ -1,11 +1,9 @@ package hw_git; -import java.nio.file.Path; import java.sql.Timestamp; import java.util.ArrayList; import java.util.List; import java.util.Map; -import java.util.Set; import java.util.TreeMap; public class RepInformation { From 435e67bfec4b49dc87636b1cb758c90f268dc37f Mon Sep 17 00:00:00 2001 From: Anton Date: Fri, 21 Sep 2018 01:07:52 +0300 Subject: [PATCH 5/5] tests improved --- hw_git_1/pom.xml | 21 ++-- hw_git_1/src/main/java/hw_git/GitCore.java | 2 +- hw_git_1/src/test/java/hw_git/AppTest.java | 119 +++++++++++++++++++-- 3 files changed, 125 insertions(+), 17 deletions(-) diff --git a/hw_git_1/pom.xml b/hw_git_1/pom.xml index 643d287..8284e27 100644 --- a/hw_git_1/pom.xml +++ b/hw_git_1/pom.xml @@ -26,12 +26,14 @@ - - junit - junit - 3.8.1 - test - + + + junit + junit + 4.12 + test + + @@ -51,5 +53,12 @@ jackson-databind 2.9.6 + + + + org.apache.commons + commons-io + 1.3.2 + diff --git a/hw_git_1/src/main/java/hw_git/GitCore.java b/hw_git_1/src/main/java/hw_git/GitCore.java index a7bf96a..004116b 100644 --- a/hw_git_1/src/main/java/hw_git/GitCore.java +++ b/hw_git_1/src/main/java/hw_git/GitCore.java @@ -22,7 +22,7 @@ public class GitCore { private final String infoFileName = ".myGitData"; private final String storageFolder = ".mygitdata"; - private void findRepInformation() throws JsonParseException, JsonMappingException, IOException, UnversionedException { + void findRepInformation() throws JsonParseException, JsonMappingException, IOException, UnversionedException { RepInformation result = null; Path p = Paths.get(""); diff --git a/hw_git_1/src/test/java/hw_git/AppTest.java b/hw_git_1/src/test/java/hw_git/AppTest.java index 5f5385f..7fbeb02 100644 --- a/hw_git_1/src/test/java/hw_git/AppTest.java +++ b/hw_git_1/src/test/java/hw_git/AppTest.java @@ -1,16 +1,115 @@ package hw_git; -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.PrintWriter; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Scanner; -/** - * Unit test for simple App. - */ -public class AppTest extends TestCase -{ - - public void testInformationLoad() { +import org.apache.commons.io.FileUtils; + + +import com.fasterxml.jackson.core.JsonGenerationException; +import com.fasterxml.jackson.databind.JsonMappingException; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class AppTest extends Assert { + + @Before + public void setUp() throws IOException { + //System.out.println("setUp"); + Files.createDirectories(Paths.get("testdir/dir1")); + Files.createFile(Paths.get("testdir/dir1/file_d1.txt")); + Files.createFile(Paths.get("testdir/file.txt")); + } + + @After + public void tearDown() throws IOException { + //System.out.println("tearDown"); + FileUtils.deleteDirectory(Paths.get("testdir").toFile()); + FileUtils.deleteDirectory(Paths.get(".mygitdata").toFile()); + if (Files.exists(Paths.get(".myGitData"))) { + Files.delete(Paths.get(".myGitData")); + } + } + + @Test + public void testInformationLoad() throws JsonGenerationException, JsonMappingException, IOException, UnversionedException { + GitCli.main(new String[] {"init"}); + GitCore core = new GitCore(); + core.findRepInformation(); + assertEquals(core.getCurrentRevision(), 0); + //Files.delete(Paths.get(".myGitData")); + } + + @Test(expected = UnversionedException.class) + public void testUnversioned() throws IOException, UnversionedException { + GitCore core = new GitCore(); + core.findRepInformation(); + //core.makeCheckout(0); + } + + @Test + public void testCheckout() throws JsonGenerationException, JsonMappingException, IOException { + GitCli.main(new String[] {"init"}); + GitCli.main(new String[] {"commit", "message 1", "testdir/file.txt"}); + GitCli.main(new String[] {"commit", "message 2", "testdir/dir1/file_d1.txt"}); + GitCli.main(new String[] {"checkout", "1"}); + assertTrue(Files.exists(Paths.get("testdir/file.txt"))); + assertFalse(Files.exists(Paths.get("testdir/dir1/file_d1.txt"))); + + GitCli.main(new String[] {"checkout", "2"}); + assertTrue(Files.exists(Paths.get("testdir/file.txt"))); + assertTrue(Files.exists(Paths.get("testdir/dir1/file_d1.txt"))); + } + + @Test + public void testFileChangeBetweenCommits() throws JsonGenerationException, JsonMappingException, IOException { + GitCli.main(new String[] {"init"}); + try (PrintWriter out = new PrintWriter(new File("testdir/file.txt"))) { + out.print("commit 1 content"); + } + GitCli.main(new String[] {"commit", "message 1", "testdir/file.txt"}); + GitCli.main(new String[] {"commit", "message 2", "testdir/dir1/file_d1.txt"}); + + try (PrintWriter out = new PrintWriter(new File("testdir/file.txt"))) { + out.print("commit 3 content"); + } + + GitCli.main(new String[] {"commit", "message 3", "testdir/file.txt"}); + + GitCli.main(new String[] {"checkout", "2"}); + try (Scanner in = new Scanner(new File("testdir/file.txt"))) { + assertEquals(in.nextLine(), "commit 1 content"); + } + + GitCli.main(new String[] {"checkout", "3"}); + try (Scanner in = new Scanner(new File("testdir/file.txt"))) { + assertEquals(in.nextLine(), "commit 3 content"); + } + } + + @Test + public void testReset() throws JsonGenerationException, JsonMappingException, IOException { + GitCli.main(new String[] {"init"}); + GitCli.main(new String[] {"commit", "message 1", "testdir/file.txt"}); + String s1; + try (Scanner in = new Scanner(new File(".myGitData"))) { + s1 = in.nextLine(); + } + GitCli.main(new String[] {"commit", "message 2", "testdir/dir1/file_d1.txt"}); + GitCli.main(new String[] {"reset", "1"}); + String s2; + try (Scanner in = new Scanner(new File(".myGitData"))) { + s2 = in.nextLine(); + } + assertEquals(s1, s2); } }