Merge branch '6-work-branches' into 'master'

Resolve "Работа с ветками"

Closes #6

See merge request romanov73/git-extractor!6
This commit is contained in:
Anton Romanov 2021-03-23 11:23:09 +00:00
commit 4af08bc9b8
8 changed files with 95 additions and 34 deletions

View File

@ -64,7 +64,6 @@ dependencies {
compile group: 'org.webjars', name: 'bootstrap-select', version: '1.13.8' compile group: 'org.webjars', name: 'bootstrap-select', version: '1.13.8'
compile group: 'org.webjars', name: 'font-awesome', version: '4.7.0' compile group: 'org.webjars', name: 'font-awesome', version: '4.7.0'
testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test' testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test'
} }

View File

@ -0,0 +1,14 @@
package ru.ulstu.extractor.model;
public class Branch {
private String name;
public Branch(String name) {
this.name = name;
}
public String getName() {
return name;
}
}

View File

@ -5,11 +5,16 @@ import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import ru.ulstu.extractor.model.Branch;
import ru.ulstu.extractor.mvc.model.FilterForm; import ru.ulstu.extractor.mvc.model.FilterForm;
import ru.ulstu.extractor.mvc.model.RepoForm; import ru.ulstu.extractor.mvc.model.RepoForm;
import ru.ulstu.extractor.service.FilteringService; import ru.ulstu.extractor.service.FilteringService;
import ru.ulstu.extractor.service.GitRepositoryService; import ru.ulstu.extractor.service.GitRepositoryService;
import java.util.List;
@Controller @Controller
public class GitExtractorController { public class GitExtractorController {
private final FilteringService filteringService; private final FilteringService filteringService;
@ -33,29 +38,35 @@ public class GitExtractorController {
return "filtering"; return "filtering";
} }
@PostMapping("/sendRepo")
public String sendRepo(@ModelAttribute RepoForm repoForm, Model model) {
if (repoForm.getRepo() == null || repoForm.getRepo().isEmpty()) {
model.addAttribute("error", "'Git' не должно быть пустым");
return "newRepo";
}
model.addAttribute("filterForm", new FilterForm(repoForm.getRepo()));
try {
gitRepositoryService.cloneOrUpdateRepo(repoForm.getRepo());
} catch (Exception ex) {
model.addAttribute("error", ex.getMessage());
return "newRepo";
}
return "filtering";
}
@PostMapping("/sendFilter") @PostMapping("/sendFilter")
public String sendFilter(@ModelAttribute FilterForm filterForm, Model model) { public String sendFilter(@ModelAttribute FilterForm filterForm, Model model) {
if (filterForm.getFilter() == null || filterForm.getFilter().isEmpty()) { if (filterForm.getFilter() == null || filterForm.getFilter().isEmpty()) {
model.addAttribute("error", "'Строка' не должно быть пустым"); model.addAttribute("error", "'Строка' не должно быть пустым");
return "filtering"; return "filtering";
} }
model.addAttribute("commits", filteringService.getCommits(filterForm.getFilter(), filterForm.getFilter()));
return "resultRepo"; return "resultRepo";
} }
@RequestMapping(value = "/newRepo", method = RequestMethod.POST, params = "send")
public String getBranch(@ModelAttribute RepoForm repoForm, Model model) {
try {
gitRepositoryService.cloneOrUpdateRepo(repoForm.getRepo());
List<Branch> list = gitRepositoryService.getBranches(repoForm.getRepo());
model.addAttribute("branches", list);
return "newRepo";
} catch (Exception ex) {
model.addAttribute("error", ex.getMessage());
return "newRepo";
}
}
@RequestMapping(value = "/newRepo", method = RequestMethod.POST, params = "next")
public String setBranch(@ModelAttribute RepoForm repoForm, Model model) {
model.addAttribute("filterForm", new FilterForm(repoForm.getRepo()));
if (repoForm.getBranch() == null) {
return "newRepo";
} else {
return "filtering";
}
}
} }

View File

@ -3,6 +3,8 @@ package ru.ulstu.extractor.mvc.model;
public class RepoForm { public class RepoForm {
private String repo; private String repo;
private String branch;
public String getRepo() { public String getRepo() {
return repo; return repo;
} }
@ -11,6 +13,14 @@ public class RepoForm {
this.repo = repo; this.repo = repo;
} }
public String getBranch() {
return branch;
}
public void setBranch(String branch) {
this.branch = branch;
}
@Override @Override
public String toString() { public String toString() {
return "RepoForm{" + return "RepoForm{" +

View File

@ -1,6 +1,7 @@
package ru.ulstu.extractor.service; package ru.ulstu.extractor.service;
import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ListBranchCommand;
import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.diff.DiffFormatter; import org.eclipse.jgit.diff.DiffFormatter;
import org.eclipse.jgit.internal.storage.file.FileRepository; import org.eclipse.jgit.internal.storage.file.FileRepository;
@ -8,6 +9,7 @@ import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevCommit;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import ru.ulstu.extractor.model.Branch;
import ru.ulstu.extractor.model.Changes; import ru.ulstu.extractor.model.Changes;
import ru.ulstu.extractor.model.Commit; import ru.ulstu.extractor.model.Commit;
import ru.ulstu.extractor.model.FileChange; import ru.ulstu.extractor.model.FileChange;
@ -22,6 +24,7 @@ import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors;
import static org.apache.logging.log4j.util.Strings.isBlank; import static org.apache.logging.log4j.util.Strings.isBlank;
@ -188,4 +191,15 @@ public class GitRepositoryService {
} }
return Optional.empty(); return Optional.empty();
} }
public List<Branch> getBranches(String url) throws GitAPIException, IOException {
cloneOrUpdateRepo(url);
Repository localRepo = new FileRepository(getProjectGitDirectory(url));
Git git = new Git(localRepo);
return git.branchList().setListMode(ListBranchCommand.ListMode.REMOTE)
.call()
.stream()
.map(r -> new Branch(r.getName()))
.collect(Collectors.toList());
}
} }

View File

@ -6,7 +6,7 @@
<title th:text="#{messages.app-name}">app-name</title> <title th:text="#{messages.app-name}">app-name</title>
<meta name="viewport" content="width=device-width, initial-scale=1"/> <meta name="viewport" content="width=device-width, initial-scale=1"/>
<script type="text/javascript" src="/webjars/jquery/3.6.0/jquery.min.js"></script> <script type="text/javascript" src="/webjars/jquery/3.6.0/jquery.min.js"></script>
<script type="text/javascript" src="/webjars/bootstrap/4.6.0/js/bootstrap.min.js"></script> <script type="text/javascript" src="/webjars/bootstrap/4.6.0/js/bootstrap.bundle.min.js"></script>
<script type="text/javascript" src="/webjars/bootstrap-select/1.13.8/js/bootstrap-select.min.js"></script> <script type="text/javascript" src="/webjars/bootstrap-select/1.13.8/js/bootstrap-select.min.js"></script>
<link rel="stylesheet" href="/webjars/bootstrap/4.6.0/css/bootstrap.min.css"/> <link rel="stylesheet" href="/webjars/bootstrap/4.6.0/css/bootstrap.min.css"/>
<link rel="stylesheet" href="/webjars/bootstrap-select/1.13.8/css/bootstrap-select.min.css"/> <link rel="stylesheet" href="/webjars/bootstrap-select/1.13.8/css/bootstrap-select.min.css"/>

View File

@ -30,19 +30,18 @@
<input type="hidden" th:field="*{url}"> <input type="hidden" th:field="*{url}">
</p> </p>
<p style="color:red" th:text="${error}"></p> <p style="color:red" th:text="${error}"></p>
<table> <!-- <table>-->
<tr> <!-- <tr>-->
<th>Author</th> <!-- <th>Author</th>-->
<th>Date</th> <!-- <th>Date</th>-->
<th>Commit</th> <!-- <th>Commit</th>-->
</tr> <!-- </tr>-->
<tr th:each="commit : ${commits}"> <!-- <tr th:each="commit : ${commits}">-->
<td th:text="${commit.author}">Onions</td> <!-- <td th:text="${commit.author}">Onions</td>-->
<td th:text="${commit.date}">2.41</td> <!-- <td th:text="${commit.date}">2.41</td>-->
<td th:text="${commit.message}">yes</td> <!-- <td th:text="${commit.message}">yes</td>-->
</tr> <!-- </tr>-->
</table> <!-- </table>-->
<p> <p>
<input type="submit" value="Отправить"/> <input type="submit" value="Отправить"/>
</p> </p>

View File

@ -8,15 +8,29 @@
</head> </head>
<div class="container" layout:fragment="content"> <div class="container" layout:fragment="content">
<body> <body>
<form action="#" th:action="@{/sendRepo}" th:object="${repoForm}" method="post"> <form action="#" th:action="@{/newRepo}" th:object="${repoForm}" method="post">
<p style="color:red" th:text="${error}"></p> <p style="color:red" th:text="${error}"></p>
<p><b>Ваш git репозиторий:</b><br> <p><b>Ваш git репозиторий:</b><br>
<input type="text" size="40" th:field="*{repo}"> <input type="text" size="40" th:field="*{repo}">
</p> </p>
<p> <p>
<input type="submit" value="Отправить"/> <input type="submit" name="send" value="Отправить"/>
</p>
<p>Ветки:<br>
<select id="select-branch" class="selectpicker" data-live-search="true" th:field="*{branch}">
<option th:each="branch : ${branches}"
th:value="${branch.name}"
th:utext="${branch.name}"/>
</option>
</select>
</p>
<p>
<input type="submit" name="next" value="Продолжить"/>
</p> </p>
</form> </form>
</body> </body>
</div> </div>
<script>
$('#select-branch').selectpicker('refresh');
</script>
</html> </html>