Skip to content

Commit 0645e6f

Browse files
committed
Tests done
1 parent f30b2ea commit 0645e6f

21 files changed

+274
-419
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,13 @@
11
[![Build Status](https://travis-ci.com/deltonvaz/sgit.svg?branch=master)](https://travis-ci.com/deltonvaz/sgit)
2-
# sgit
2+
#SGIT
3+
4+
A Scala-based git-like code source manager
5+
6+
---
7+
8+
## Instalation
9+
10+
Clone the repository ad
11+
12+
13+

src/main/scala/Sgit.scala

Lines changed: 6 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import UI.Config
22
import better.files.File
33
import better.files.Dsl.{cwd}
4-
import misc.{BranchHandler, CommitHandler, Constants, FileHandler, Functions, StageHandler, TagHandler}
4+
import functions.{BranchHandler, CommitHandler, Constants, FileHandler, Functions, StageHandler, TagHandler}
55
import objects.{Commit, Tree}
66

77
case class Sgit (var workingDirectory : String) {
@@ -49,60 +49,7 @@ case class Sgit (var workingDirectory : String) {
4949
*/
5050
def commit(message : String) : Unit = {
5151
if (!loadSystem) return
52-
53-
var outMessage : String = ""
54-
55-
//Check if staged area is sync with last commit
56-
if(!isFirstCommit && stage.isStageSync) {
57-
outMessage = s"On branch ${branch.getCurrentBranch}\nnothing to commit, working tree clean"
58-
println(outMessage)
59-
return
60-
}
61-
62-
//Get Blobs from Stage
63-
val stageBlob = stage.getStageBLOBS
64-
65-
//Create ~tree to commit
66-
val tree = Tree(head, workingDirectory, stageBlob)
67-
68-
//Save the tree
69-
val commitSHATree : File = tree.save()
70-
71-
//Default if first commit
72-
var parentCommitSHA = "NONE"
73-
if (!isFirstCommit) {
74-
parentCommitSHA = (File(workingDirectory)/Constants.DEFAULT_HEAD_PATH).lines.head
75-
}
76-
77-
val commit = Commit(commitSHATree, workingDirectory, parentCommitSHA, message)
78-
79-
parentCommitSHA = commit.save
80-
81-
//HEAD points to new commit
82-
(workingDir/Constants.SGIT_ROOT/"HEAD")
83-
.clear
84-
.appendLine("refs/heads/"+branch.getCurrentBranch)
85-
86-
//Create commit file which head points to
87-
(workingDir/Constants.SGIT_ROOT/"refs"/"heads"/branch.getCurrentBranch)
88-
.createIfNotExists()
89-
.clear
90-
.appendLine(parentCommitSHA)
91-
92-
//TODO change parent commit to current commit and files added
93-
outMessage = s"[${branch.getCurrentBranch} (root-commit) $parentCommitSHA] $message\n"+
94-
s" ${stageBlob.size} files changed, ${stageBlob.size} insertions(+)\n"
95-
96-
var newFiles : String = ""
97-
98-
//TODO check this create
99-
stageBlob.foreach(f =>{
100-
newFiles = " create "+f._1+"\n"+newFiles
101-
})
102-
103-
outMessage = outMessage+newFiles
104-
105-
println(outMessage)
52+
println(Functions.commit(workingDir, message)._1)
10653

10754
}
10855

@@ -168,7 +115,7 @@ case class Sgit (var workingDirectory : String) {
168115
println(branch.newBranch(branchCommand))
169116
}
170117
}else{
171-
println("fatal: Not a valid object name: 'master'.") //TODO remove
118+
println(s"fatal: Not a valid object name: ${branch.getCurrentBranch}.")
172119
}
173120
}
174121

@@ -179,7 +126,7 @@ case class Sgit (var workingDirectory : String) {
179126
case _ => TagHandler(workingDirectory).newTag(tagName)
180127
}
181128
}else{
182-
println("fatal: Not a valid object name: 'master'.") //TODO remove
129+
println(s"fatal: Not a valid object name: ${branch.getCurrentBranch}.")
183130
}
184131
}
185132

@@ -188,7 +135,7 @@ case class Sgit (var workingDirectory : String) {
188135
* @param changes
189136
*/
190137
def log(changes : Boolean, stat : Boolean) : Unit = {
191-
CommitHandler(workingDirectory).getCommitsHistoric(changes)
138+
CommitHandler(workingDirectory).getCommitsHistoric(changes, false)
192139
}
193140

194141
}
@@ -229,7 +176,7 @@ object Main extends App{
229176
.children(
230177
opt[String]('m', "message")
231178
.text("Commit's message")
232-
.optional()
179+
.required()
233180
.action((message, c) => c.copy(cmessage = message))
234181
),
235182
cmd("branch")

src/main/scala/misc/BranchHandler.scala renamed to src/main/scala/functions/BranchHandler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package misc
1+
package functions
22

33
import better.files.File
44

src/main/scala/misc/CommitHandler.scala renamed to src/main/scala/functions/CommitHandler.scala

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package misc
1+
package functions
22

33
import better.files.File
44

@@ -109,15 +109,17 @@ case class CommitHandler (workingDir : String) {
109109
/**
110110
* show all commits starting with the newest
111111
*/
112-
def getCommitsHistoric(historic : Boolean): Boolean = {
112+
def getCommitsHistoric(historic : Boolean, debug : Boolean): Boolean = {
113113
if (isFirstCommit) {
114-
println(Constants.MSG_FATAL_NO_COMMITS)
114+
if(!debug){
115+
println(Constants.MSG_FATAL_NO_COMMITS)
116+
}
115117
false
116118
}
117119
else{
118120
val currentBranch = (File(workingDir)/Constants.SGIT_ROOT/"HEAD").lines.head
119121
val lastCommitSHA = (File(workingDir)/Constants.SGIT_ROOT/currentBranch).lines.head
120-
commitsRecursion(lastCommitSHA, historic)
122+
commitsRecursion(lastCommitSHA, historic, debug)
121123
true
122124
}
123125
}
@@ -127,7 +129,7 @@ case class CommitHandler (workingDir : String) {
127129
* @param commitSha - commit's sha
128130
*/
129131
@tailrec
130-
final def commitsRecursion(commitSha:String, historic : Boolean): Unit ={
132+
final def commitsRecursion(commitSha:String, historic : Boolean, debug : Boolean): Unit ={
131133
if(commitSha == "NONE") return
132134
val lastCommitTree = (File(workingDir)/Constants.OBJECTS_FOLDER/commitSha).lines.head
133135
lastCommitTree match {
@@ -136,23 +138,24 @@ case class CommitHandler (workingDir : String) {
136138
msg = msg + "Author: "+user + "\n"
137139
msg = msg + "Date: "+date + "\n\n"
138140
msg = msg + "\t\t"+comment + "\n\n"
139-
println(msg)
140-
if(historic){
141-
val lines = FileHandler(File(workingDir)).getDiffLinesWithParent(commitSha)
142-
lines.foreach(f => {
143-
println("Modifications in " + f._1 + " file")
144-
f._2.foreach(lines => {
145-
if(lines._2 != "")
146-
lines._1 match {
147-
case "added(+)" => Functions.printDiff(Console.GREEN, lines)
148-
case "removed(-)" => Functions.printDiff(Console.RED, lines)
149-
}
141+
if(!debug) {
142+
println(msg)
143+
if(historic){
144+
val lines = FileHandler(File(workingDir)).getDiffLinesWithParent(commitSha)
145+
lines.foreach(f => {
146+
println("Modifications in " + f._1 + " file")
147+
f._2.foreach(lines => {
148+
if(lines._2 != "")
149+
lines._1 match {
150+
case "added(+)" => Functions.printDiff(Console.GREEN, lines)
151+
case "removed(-)" => Functions.printDiff(Console.RED, lines)
152+
}
153+
})
154+
println()
150155
})
151-
println()
152-
})
156+
}
153157
}
154-
155-
commitsRecursion(parentSha, historic)
158+
commitsRecursion(parentSha, historic, debug)
156159
}
157160
case _ => println(Console.RED + "Error reading commited files" + Console.RESET)
158161
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package misc
1+
package functions
22
import scala.util.matching.Regex
33

44
object Constants {

src/main/scala/misc/FileHandler.scala renamed to src/main/scala/functions/FileHandler.scala

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package misc
1+
package functions
22

33
import better.files.File
44

@@ -79,8 +79,6 @@ case class FileHandler(var workingDirectory : File){
7979
)
8080
}
8181

82-
//TODO fix: create a function to get only working dir files
83-
//TODO make a copy in objects
8482
/**
8583
* @return a Set with the filename in the workingSet
8684
*/
@@ -89,11 +87,11 @@ case class FileHandler(var workingDirectory : File){
8987
workingDirectory.listRecursively
9088
.filter(!_. isChildOf(workingDirectory/Constants.SGIT_ROOT))
9189
.filter(_.isRegularFile)
92-
.filter(!_.name.contains("DS_Store"))//TODO remove macOs
90+
//.filter(!_.name.contains("DS_Store"))//TODO remove macOs
9391
.filter(!_.name.contains("sgit"))
94-
.filter(!_. isChildOf(workingDirectory/"project")) //TODO remove
95-
.filter(!_. isChildOf(workingDirectory/"target")) //TODO remove
96-
.filter(!_. isChildOf(workingDirectory/".git")) //TODO remove
92+
//.filter(!_. isChildOf(workingDirectory/"project")) //TODO remove
93+
//.filter(!_. isChildOf(workingDirectory/"target")) //TODO remove
94+
//.filter(!_. isChildOf(workingDirectory/".git")) //TODO remove
9795
.foreach(f => {
9896
repFiles = repFiles.+(f.path.toString diff workingDirectory+"/")
9997
})

0 commit comments

Comments
 (0)