Skip to content

Commit

Permalink
[ADD] get patch form local git
Browse files Browse the repository at this point in the history
  • Loading branch information
gevorg95 committed Mar 5, 2018
1 parent e46074b commit a115333
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 34 deletions.
32 changes: 22 additions & 10 deletions sc.code.cls → cls/sc/code.cls
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,26 @@ ClassMethod import(filemask = "*.xml;*.cls;*.mac;*.int;*.inc;*.dfi", qspec = "ck
Q sc
}

ClassMethod patch(filename = "", CommitFrom = "", CommitTo = "") As %Status
ClassMethod patch(filename = "", commitFrom = "", commitTo = "") As %Status
{
s gln = ..gln()

set owner = @gln@("owner")
set repository = @gln@("repository")
set user = @gln@("user")
set password = @gln@("password")

set sc = ##class(sc.GitHub).Get(.items, owner, repository, user, password, CommitFrom, CommitTo)
return:$$$ISERR(sc) sc
s git = @gln@("git")

set:commitTo="" commitTo=1
if (git = 0) {
set:(commitFrom="")||(commitFrom=1) commitFrom=0
set sc = ##class(sc.diff.gitLocal).buildDiff(..workdir(), "HEAD~"_commitTo, "HEAD~"_commitFrom, .items)
return:$$$ISERR(sc) sc
}elseif(git = 1) {
set owner = @gln@("owner")
set repository = @gln@("repository")
set user = @gln@("user")
set password = @gln@("password")
set:commitFrom="" commitFrom=1

set sc = ##class(sc.diff.gitHub).Get(.items, owner, repository, user, password, commitFrom, commitTo)
return:$$$ISERR(sc) sc
}

if (filename="") {
set filename=$G(@gln@("projectName"),"patch")
Expand Down Expand Up @@ -244,14 +253,16 @@ ClassMethod init(confile = "isc.json")
s @gln@("repository")=conf.repository
s @gln@("user")=conf.user
s @gln@("password")=conf.password
s @gln@("git")=conf.git

w "compileList="_conf.compileList,!
w "projectName="_conf.projectName,!
w "owner="_conf.owner,!
w "repository="_conf.repository,!
w "user="_conf.user,!
w "password="_conf.password,!

w "git="_conf.git,!

quit $$$OK
}

Expand Down Expand Up @@ -418,3 +429,4 @@ ClassMethod fixDashIntoName()
}

}

29 changes: 5 additions & 24 deletions sc.GitHub.cls → cls/sc/diff/gitHub.cls
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
Class sc.GitHub
Class sc.diff.gitHub
{

ClassMethod Get(Output Items, Owner As %String = "", Repository As %String = "", Username As %String = "clminstaller", Password As %String = "clminstaller2016", CommitFrom As %Integer = "", CommitTo As %Integer = "") As %Status
ClassMethod Get(Output items, Owner As %String = "", Repository As %String = "", Username As %String = "clminstaller", Password As %String = "clminstaller2016", CommitFrom As %Integer = "", CommitTo As %Integer = "") As %Status
{
#dim req As %Net.HttpRequest
set req = ..CreateRequest(Username, Password)
set req.Location = "repos/" _ Owner _ "/" _ Repository _ "/commits" // as described in https://developer.github.com/v3/repos/

set links = ##class(%ListOfDataTypes).%New()
set sc = ..GetSHAs(.req, CommitFrom, CommitTo,.Items)
set sc = ..GetSHAs(.req, CommitFrom, CommitTo, .items)

return sc
}
Expand All @@ -24,13 +24,6 @@ ClassMethod GetSHAs(Request As %Net.HttpRequest, CommitFrom As %Integer, CommitT

#dim objects As List of %ZEN.proxyObject
set st = ##class(%ZEN.Auxiliary.jsonProvider).%ConvertJSONToObject(Request.HttpResponse.Data,,.objects,1)

set:CommitFrom="" CommitFrom=1
set:CommitTo="" CommitTo=1
if (CommitFrom > CommitTo) {
w "CommitFrom can't be more than CommitTo",!
return $$$NO
}

if ((CommitFrom >= 1) && (CommitTo <= objects.Size)) {
set list = ""
Expand Down Expand Up @@ -73,7 +66,8 @@ ClassMethod GetFileNameForReleaseBySHAs(Request As %Net.HttpRequest, SHAsList, O
set fileName = files.GetAt(j).%data("filename")
set:$L(fileName,".xml")'=1 fileName = $extract(fileName,1,*-4)
continue:..IsCacheFile(fileName)=0
set filesForRelease(..ToCacheName(fileName)) = ""
do ##class(sc.diff.utils).ToCacheName(.fileName)
set filesForRelease(fileName) = ""
}
}
}
Expand Down Expand Up @@ -110,18 +104,5 @@ ClassMethod IsCacheFile(FileName As %String) As %Boolean
return $F(extensions,","_$ZCVT(fileExtension,"l")_",")
}

ClassMethod ToCacheName(FileName) As %String
{
set:$L(FileName,".xml")'=1 FileName = $extract(FileName,1,*-4)
set str = $Select($L(FileName,"cls/")>1:$Replace($P(FileName,"cls/",2),"/","."),
$L(FileName,"csp/")>1:$Replace($P(FileName,"csp/",2),"/","."),
$L(FileName,"dfi/")>1:$Replace($P(FileName,"dfi/",2),"/","-"),
$L(FileName,"inc/")>1:$Replace($P(FileName,"inc/",2),"/","."),
$L(FileName,"web/")>1:$Replace($P(FileName,"web/",2),"/","."),
1:$Replace(FileName,"/",".")
)
set str = $P(str,".",1,*-1)_"."_ $ZCVT($P(str,".",*),"U")
quit str
}

}
133 changes: 133 additions & 0 deletions cls/sc/diff/gitLocal.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
Class sc.diff.gitLocal
{

/// Get diff between two points in repository
/// repo - repository root directory
/// sha1, sha2 - poins of history in repository
///
/// Internal diff statuses:
/// M modified - File has been modified
/// C copy-edit - File has been copied and modified //3
/// R rename-edit - File has been renamed and modified //3
/// A added - File has been added
/// D deleted - File has been deleted
/// U unmerged - File has conflicts after a merge
///
/// do ##class(isc.diff.gitLocal).buildDiff("C:\\temp\cache-udl\", "HEAD~10", "HEAD", release)
ClassMethod buildDiff(repo As %String, sha1 As %String, sha2 As %String, Output items) As %Status
{
$$$TOE(sc, ..createFile(.tempFile))
do $system.Process.CurrentDirectory(repo)
$$$TOE(sc, ..execute($$$FormatText("git diff --name-status %1 %2 > %3 2>&3", sha1, sha2, tempFile)))
$$$TOE(sc, ..fileToString(tempFile, .diffRaw))

for i=1:1:$length(diffRaw, $c(10))
{
set element = $piece(diffRaw, $c(10), i)
set status = $e($piece(element, $c(9)))
set file = $piece(element, $c(9), 2)

if ($l(file,"src/") '= 2) continue

set isRelevantFile = ##class(sc.diff.utils).isRelevantFile(file)
if ((element="") || ('isRelevantFile)) continue

if $length(element, $c(9))=2
{
if ((status="M") || (status="U")) || (status="A")
{
do ##class(sc.diff.utils).ToCacheName(.file)
set items(file) = ""
}
}elseif $length(element, $c(9))=3
{
set file = $piece(element, $c(9), 3)
if ($l(file,"src/") '= 2) continue
if ((status="C") || (status="R"))
{
do ##class(sc.diff.utils).ToCacheName(.newFile)
set items(file) = ""
}
}
}
return sc
}

/// Create file name.
/// Если name не задан, то возвращается имя созданного файла (в папке Temp).
/// Если name - расширение, то возвращается имя созданного файла (в папке Temp) с заданным расширением.
/// stream - стрим файла
/// content - строка или stream который записывается в файл
ClassMethod createFile(ByRef name As %String = "", Output stream As %Stream.FileBinary, content As %String) As %Status
{
#dim sc As %Status = $$$OK

if name="" {
set name = ##class(%File).TempFilename()
} elseif $length(name, ".")=1 {
set name = ##class(%File).TempFilename(name)
}

set stream = ##class(%Stream.FileBinary).%New()
set sc = stream.LinkToFile(name)

if $data(content) {
if $isObject(content) {
set sc = stream.CopyFrom(content)
} else {
set sc = stream.Write(content)
}
quit:$$$ISERR(sc) sc
set sc = stream.%Save()
do stream.Rewind()
}

quit sc
}

/// Прочитать файл в строку
ClassMethod fileToString(name As %String, Output content As %String, delete As %Boolean = {$$$YES}) As %Status
{
#dim sc As %Status = $$$OK
set stream = ##class(%Stream.FileBinary).%New()
set sc = stream.LinkToFile(name)

set content = stream.Read($$$MaxStringLength)

if delete {
kill stream
set sc = ..deleteFile(name)
}

quit sc
}

/// Удалить файл
ClassMethod deleteFile(name As %String) As %Status
{
#dim sc As %Status = $$$OK
set success = ##class(%File).Delete(name, .code)
set:success'=$$$YES sc = $$$ERROR($$$GeneralError, $$$FormatText("Error deleting file %1 with code %2", name, code))
quit sc
}

/// Выполнить команду ОС
ClassMethod execute(cmd, debug As %Boolean = {$$$NO}) As %Status
{
#dim sc As %Status = $$$OK
set code = ""
//set out = ""
write:debug !, "cmd: ", cmd
//set sc = ##class(%Net.Remote.Utility).RunCommandViaZF(cmd, , .out, timeout, $$$YES, .code)
set code = $zf(-1, cmd)
write:debug !,"code: ", code

if code'=0 {
set sc1 = $$$ERROR($$$GeneralError, $$$FormatText("Комманда ОС: `%1` завершилась с кодом: `%2`", cmd, code))
set sc = $$$ADDSC(sc, sc1)
}
return sc
}

}

26 changes: 26 additions & 0 deletions cls/sc/diff/utils.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Class sc.diff.utils
{

Parameter TRACKEDEXT As List = {$lb("xml", "cls", "csp", "csr", "mac", "int", "bas", "inc", "gbl", "prj", "obj", "pkg", "gof", "dfi", "pivot", "dashboard")};

ClassMethod ToCacheName(ByRef FileName)
{
set:$L(FileName,".xml")'=1 FileName = $extract(FileName,1,*-4)
set str = $Select($L(FileName,"cls/")>1:$Replace($P(FileName,"cls/",2),"/","."),
$L(FileName,"csp/")>1:$Replace($P(FileName,"csp/",2),"/","."),
$L(FileName,"dfi/")>1:$Replace($P(FileName,"dfi/",2),"/","-"),
$L(FileName,"inc/")>1:$Replace($P(FileName,"inc/",2),"/","."),
$L(FileName,"web/")>1:$Replace($P(FileName,"web/",2),"/","."),
1:$Replace(FileName,"/",".")
)
set FileName = $P(str,".",1,*-1)_"."_ $ZCVT($P(str,".",*),"U")
}

ClassMethod isRelevantFile(file As %String) As %Boolean
{
set ext = $select($length(file, ".")=1:"", 1:$piece(file, ".", *))
quit $lf(..#TRACKEDEXT, ext)>0
}

}

0 comments on commit a115333

Please sign in to comment.