I could get the diff between content of two directories by running the diff
command through the os
package as follows,
command := "diff --brief -r /home/kasun/Documents/Old-directory/ " +
"/home/kasun/Documents/New-directory/"
out, err := exec.Command("sh", "-c", command).Output()
if err != nil {
fmt.Println("error occured")
fmt.Printf("%s", err)
}
fmt.Printf("%s", out)
since diff
is a command only available in bash
, the binary formed by building the above code cannot be used to get diff in a Windows
system. Is there any other way to achieve the same so that it will work on any platform, like performing the diff using standard go libraries. Thanks in advance
since diff is a command only available in bash, the binary formed by building the above code cannot be used to get diff in a Windows system
It can, if you install (unzip anywhere you want) the latest Portable Git (like PortableGit-2.14.1-64-bit.7z.exe
)
If you add that installation to your PATH, you do have diff (to be used in your Go program)
vonc@VONCAVN7 C:\
> where diff
D:\prgs\git\latest\usr\bin\diff.exe
But if you don't want to rely on an existing thrid-party installed software (here Git), you might consider vendoring in your project spcau/godiff
.
If does have a diff
implementation for Windows.
Note that, with Git 2.17 (Q2 2018), Go is better recognized by Git, since "git diff
" and friends learned funcname
patterns for Go language source files.
See commit 1dbf0c0 (01 Mar 2018) by Alban Gruin (``).
(Merged by Junio C Hamano -- gitster
-- in commit 077cde9, 08 Mar 2018)
userdiff
: add built-in pattern for GolangThis adds
xfuncname
andword_regex
patterns for Golang, a quite popular programming language. It also includes test cases for thexfuncname
regex (t4018) and updated documentation.The
xfuncname
regex finds functions, structs and interfaces.
Although the Go language prohibits the opening brace from being on its own line, the regex does not makes it mandatory, to be able to matchfunc
statements like this:func foo(bar int, baz int) { }
This is covered by the test case
t4018/golang-long-func
.The
word_regex
pattern finds identifiers, integers, floats, complex numbers and operators, according to the go specification.
The right way may be to recursively walk the directory and check & match md5 checksum for each of the corresponding files. However that's tedious, if you are sure your file structure is not too big or it's allowed, then just create a tar gz of both the directories and then compute & compare the md5 checksum or sha256sum on the tarballs.