Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
# java_homeworks
## Мой GIT.
Пример использования:

```

./mygit commit "добавлен первый файл в репозиторий"

```
3 changes: 3 additions & 0 deletions hw_git/mygit
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 "$@"
103 changes: 103 additions & 0 deletions hw_git/pom.xml
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>
8 changes: 8 additions & 0 deletions hw_git/src/main/java/hw_git/BranchProblemException.java
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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super(message), см конструкторы Exception

}
}
125 changes: 125 additions & 0 deletions hw_git/src/main/java/hw_git/GitCli.java
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")
)
);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String.join("\n", processArgs(args))

}

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);

Choose a reason for hiding this comment

The 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");
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

лучше ловить исключения как можно ближе к месту появления, например, ArrayIndexOutOfBoundsException может вылететь не по той причине, что будет написана в консоли


return res;
}
}
Loading