-
Notifications
You must be signed in to change notification settings - Fork 0
Java06. ДЗ 03, Егоров Антон #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
4deb0f4
72b616a
d0cb5a4
307a2cc
168449d
4f096cc
4095ec9
bcf815e
1723c0c
7266812
e307d15
f8c8bc1
dbf8f00
3184bd1
e2d9550
8eb8bea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,8 @@ | ||
| # java_homeworks | ||
| ## Мой GIT. | ||
| Пример использования: | ||
|
|
||
| ``` | ||
|
|
||
| ./mygit commit "добавлен первый файл в репозиторий" | ||
|
|
||
| ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| #!/bin/bash | ||
|
|
||
| java -jar target/hw_git-0.0.1-SNAPSHOT.jar "$@" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <groupId>1</groupId> | ||
| <artifactId>hw_git</artifactId> | ||
| <version>0.0.1-SNAPSHOT</version> | ||
| <packaging>jar</packaging> | ||
|
|
||
| <name>hw_git</name> | ||
| <url>http://maven.apache.org</url> | ||
|
|
||
| <properties> | ||
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| </properties> | ||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <artifactId>maven-compiler-plugin</artifactId> | ||
| <configuration> | ||
| <source>1.8</source> | ||
| <target>1.8</target> | ||
| <encoding>UTF-8</encoding> | ||
| </configuration> | ||
| </plugin> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-surefire-plugin</artifactId> | ||
| <version>3.0.0-M1</version> | ||
| </plugin> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-jar-plugin</artifactId> | ||
| <version>3.1.0</version> | ||
| <configuration> | ||
| <archive> | ||
| <manifest> | ||
| <addClasspath>true</addClasspath> | ||
| <mainClass>hw_git.GitCli</mainClass> | ||
| <packageName>hw_git</packageName> | ||
| <addClasspath>true</addClasspath> | ||
| <classpathPrefix>lib/</classpathPrefix> | ||
| </manifest> | ||
| </archive> | ||
| </configuration> | ||
| </plugin> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-dependency-plugin</artifactId> | ||
| <version>3.1.0</version> | ||
| <executions> | ||
| <execution> | ||
| <id>copy-dependencies</id> | ||
| <phase>package</phase> | ||
| <goals> | ||
| <goal>copy-dependencies</goal> | ||
| </goals> | ||
| <configuration> | ||
| <includeScope>compile</includeScope> | ||
| <outputDirectory>${project.build.directory}/lib</outputDirectory> | ||
| </configuration> | ||
| </execution> | ||
| </executions> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| <dependencies> | ||
| <!-- https://mvnrepository.com/artifact/junit/junit --> | ||
| <dependency> | ||
| <groupId>junit</groupId> | ||
| <artifactId>junit</artifactId> | ||
| <version>4.12</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
|
|
||
|
|
||
| <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --> | ||
| <dependency> | ||
| <groupId>com.fasterxml.jackson.core</groupId> | ||
| <artifactId>jackson-core</artifactId> | ||
| <version>2.9.6</version> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>com.fasterxml.jackson.core</groupId> | ||
| <artifactId>jackson-annotations</artifactId> | ||
| <version>2.9.6</version> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>com.fasterxml.jackson.core</groupId> | ||
| <artifactId>jackson-databind</artifactId> | ||
| <version>2.9.6</version> | ||
| </dependency> | ||
|
|
||
| <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-io --> | ||
| <dependency> | ||
| <groupId>org.apache.commons</groupId> | ||
| <artifactId>commons-io</artifactId> | ||
| <version>1.3.2</version> | ||
| </dependency> | ||
| </dependencies> | ||
| </project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package hw_git; | ||
|
|
||
| public class BranchProblemException extends Exception { | ||
| String message; | ||
| public BranchProblemException(String s) { | ||
| message = s; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| package hw_git; | ||
|
|
||
| import java.io.File; | ||
| import java.io.FileNotFoundException; | ||
| import java.io.IOException; | ||
| import java.nio.file.Path; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Scanner; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonGenerationException; | ||
| import com.fasterxml.jackson.databind.JsonMappingException; | ||
|
|
||
| public class GitCli { | ||
| public static void printFile(Path p) { | ||
| try (Scanner in = new Scanner(new File(p.toString()))) { | ||
| System.out.println(p.toString()); | ||
| System.out.println("--------------------------------"); | ||
| while (in.hasNextLine()) { | ||
| System.out.println(in.nextLine()); | ||
| } | ||
| System.out.println("--------------------------------"); | ||
| } catch (FileNotFoundException e) { | ||
| System.out.println("File " + p.toString() + " not found"); | ||
| } | ||
| } | ||
|
|
||
| public static void main(String[] args) throws JsonGenerationException, JsonMappingException { | ||
| System.out.println( | ||
| processArgs(args) | ||
| .stream() | ||
| .collect( | ||
| Collectors.joining("\n") | ||
| ) | ||
| ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| public static ArrayList<String> processArgs(String[] args) throws JsonGenerationException, JsonMappingException{ | ||
|
|
||
| ArrayList<String> res = new ArrayList<>(); | ||
|
|
||
| GitCore core = new GitCore(); | ||
| int revision; | ||
|
|
||
| try { | ||
| switch (args[0]) { | ||
| case "init": | ||
| core.makeInit(); | ||
| res.add("Repository initiated."); | ||
| break; | ||
| case "add": | ||
| res.add("Addition..."); | ||
| core.makeAdd(Arrays.copyOfRange(args, 1, args.length)); | ||
| break; | ||
| case "commit": | ||
| res.add("Commiting..."); | ||
| core.makeCommit(args[1]); | ||
| res.add("Commit made at revision " + (core.getCurrentRevision() + 1)); | ||
| break; | ||
| case "checkout": | ||
| if (args[1].equals("--")) { | ||
| res.add("Checking out files..."); | ||
| core.makeCheckout(Arrays.copyOfRange(args, 2, args.length)); | ||
| } else { | ||
| try { | ||
| revision = Integer.parseInt(args[1]); | ||
| res.add("Checkout to revision " + revision); | ||
| core.makeCheckout(revision - 1); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. конвертация ревизии в представление для пользователя и обратно должна быть в одном месте (+/-1) |
||
| res.add("HEAD detached on revison " + revision); | ||
| } catch (NumberFormatException e) { | ||
| res.add("Checking out branch..."); | ||
| core.makeCheckout(args[1]); | ||
| } | ||
| } | ||
| break; | ||
| case "reset": | ||
| revision = Integer.parseInt(args[1]); | ||
| res.add("Performing reset to revision " + revision); | ||
| core.makeReset(revision - 1); | ||
| break; | ||
| case "log": | ||
| revision = args.length == 2 ? Integer.parseInt(args[1]) : 0; | ||
| res.add("Log:"); | ||
| res.addAll(core.getLog(revision - 1)); | ||
| break; | ||
| case "rm": | ||
| res.add("Removing..."); | ||
| core.makeRM(Arrays.copyOfRange(args, 1, args.length)); | ||
| break; | ||
| case "status": | ||
| res.addAll(core.getStatus()); | ||
| break; | ||
| case "branch": | ||
| if (args[1].equals("-d")) { | ||
| res.add("Deleting branch " + args[2]); | ||
| core.makeDeleteBranch(args[2]); | ||
| } else { | ||
| res.add("Making branch " + args[1]); | ||
| core.makeBranch(args[1]); | ||
| } | ||
| break; | ||
| case "merge": | ||
| res.add("Merging branch " + args[1] + " to current state." | ||
| + "\nYou should make commit then."); | ||
| res.addAll(core.makeMerge(args[1])); | ||
| break; | ||
| default: | ||
| res.add("Unknown argument: " + args[0]); | ||
| } | ||
| } catch (UnversionedException e) { | ||
| res.add("This directory is not versioned"); | ||
| } catch (BranchProblemException e) { | ||
| res.add(e.message); | ||
| } catch (FileNotFoundException e) { | ||
| res.add(e.getMessage()); | ||
| } catch (IOException e) { | ||
| res.add("IOException: " + e.getMessage()); | ||
| } catch (ArrayIndexOutOfBoundsException e) { | ||
| res.add("Lack of arguments"); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. лучше ловить исключения как можно ближе к месту появления, например, |
||
|
|
||
| return res; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
super(message), см конструкторы Exception