Skip to content

Commit

Permalink
Added base functionality from git object size analyser, need tests an…
Browse files Browse the repository at this point in the history
…d QA on the data retrived is correct - References #24
  • Loading branch information
kryptag committed Nov 14, 2018
1 parent 670cd7e commit 0207533
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 5 deletions.
145 changes: 145 additions & 0 deletions src/main/java/com/praqma/gitrepodoctor/GitObjectInformation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.praqma.gitrepodoctor;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;

/**
*
* @author florenthaxha
*/
public class GitObjectInformation {

private String sha;
private String type;
private Long sizeByte;

public GitObjectInformation(String sha, String type, Long sizeByte) {
this.sha = sha;
this.type = type;
this.sizeByte = sizeByte;
}

public String getSha() {
return sha;
}

public void setSha(String sha) {
this.sha = sha;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public Long getSizeByte() {
return sizeByte;
}

public void setSizeByte(Long sizeByte) {
this.sizeByte = sizeByte;
}

@Override
public int hashCode() {
int hash = 7;
hash = 41 * hash + Objects.hashCode(this.sha);
hash = 41 * hash + Objects.hashCode(this.type);
hash = 41 * hash + Objects.hashCode(this.sizeByte);
return hash;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final GitObjectInformation other = (GitObjectInformation) obj;
if (!Objects.equals(this.sha, other.sha)) {
return false;
}
if (!Objects.equals(this.type, other.type)) {
return false;
}
if (!Objects.equals(this.sizeByte, other.sizeByte)) {
return false;
}
return true;
}

@Override
public String toString() {
return "GitObjectInformation{" + "sha=" + sha + ", type=" + type + ", sizeByte=" + sizeByte + '}';
}

public static List<GitObjectInformation> build(String pathToRepository) {

String idxPath = getIDXPath(pathToRepository);
BufferedReader reader = null;
List<GitObjectInformation> gitObjInfos = new ArrayList<>();
try {
Process exec = Runtime.getRuntime().exec("git verify-pack -v "+ idxPath);
exec.waitFor();
reader = new BufferedReader(new InputStreamReader(exec.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
String[] strs = line.split(" ");
if(strs[1].equals("blob")){
GitObjectInformation goi = new GitObjectInformation(strs[0], strs[1], Long.parseLong(strs[4]));
gitObjInfos.add(goi);
}

}
} catch (IOException | InterruptedException e) {

} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
Logger.getLogger(GitObjectInformation.class.getName()).log(Level.SEVERE, null, e);
}
}
}
return gitObjInfos;
}

private static String getIDXPath(String pathToRepository) {

Optional<Path> idxPath = null;
Path path = Paths.get(pathToRepository);
try (Stream<Path> fileStream = Files.find(path, 999, (p, bfa) -> bfa.isRegularFile())) {
idxPath = fileStream
.filter(file -> file.toString().matches("^.*\\.idx$")).findFirst();
} catch (IOException e) {
Logger.getLogger(GitObjectInformation.class.getName()).log(Level.SEVERE, null, e);
}

return idxPath.get().toString();
}
}
12 changes: 8 additions & 4 deletions src/main/java/com/praqma/gitrepodoctor/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

/**
*
Expand All @@ -24,10 +25,13 @@ public static void main(String[] args) {
// cs.createAndSaveReport("/Users/florenthaxha/Work/GitRepoDoctor", "/Users/florenthaxha/School");
// RepositoryInformation rii = new RepositoryInformation().fromJson(json);
// System.out.println(rii);

assert args.length == 1;
assert Files.exists(Paths.get(args[0]));
System.out.println(RepositoryInformation.build(args[0]).toJson());
List<GitObjectInformation> objects = GitObjectInformation.build("/Users/florenthaxha/School/Databases/gutenberg-backend");

objects.forEach(GitObjectInformation -> System.out.println(GitObjectInformation.toString()));

//assert args.length == 1;
//assert Files.exists(Paths.get(args[0]));
//System.out.println(RepositoryInformation.build(args[0]).toJson());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;
Expand Down

0 comments on commit 0207533

Please sign in to comment.