# How to get the commit of HEAD of a Julia package?

**URL:** <https://discourse.julialang.org/t/how-to-get-the-commit-of-head-of-a-julia-package/10877>\
**Category:** General Usage\
**Tags:** git\
**Created:** [May 14, 2018, 1:42am UTC](https://discourse.julialang.org/t/how-to-get-the-commit-of-head-of-a-julia-package/10877 "2018-05-14T01:42:30Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [May 14, 2018, 1:42am UTC](https://discourse.julialang.org/t/how-to-get-the-commit-of-head-of-a-julia-package/10877/1 "2018-05-14T01:42:30Z")

</div>

Is there a way to get the commit of HEAD of a Julia package from within Julia without using git commands in shell? Googling gave the following command `git rev-parse HEAD` in git, so is there an equivalent in `LibGit2` or `Pkg`?

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [May 14, 2018, 3:05am UTC](https://discourse.julialang.org/t/how-to-get-the-commit-of-head-of-a-julia-package/10877/2 "2018-05-14T03:05:51Z")

</div>

The following works:

```julia
gitdir = joinpath(Pkg.dir(package_name), ".git")
commit = strip(readstring(`git --git-dir $gitdir rev-parse HEAD`))

```

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [May 14, 2018, 4:54am UTC](https://discourse.julialang.org/t/how-to-get-the-commit-of-head-of-a-julia-package/10877/3 "2018-05-14T04:54:54Z")

</div>

It seems that `Pkg.dir(package_name)` returns an incorrect path when the package is located in a custom path in the `LOAD_PATH` variable. Should I open an issue? And what is the alternative, searching myself in `LOAD_PATH`?

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [May 14, 2018, 5:49am UTC](https://discourse.julialang.org/t/how-to-get-the-commit-of-head-of-a-julia-package/10877/4 "2018-05-14T05:49:28Z")

</div>

> [@mohamed82008](#):
>
> It seems that `Pkg.dir(package_name)` returns an incorrect path when the package is located in a custom path in the LOAD\_PATH variable. Should I open an issue?

The `Pkg.dir()` function essentially return `JULIA_PKGDIR`, so this is known. What is your usecase? To record the current state of a package? In Pkg3 this will all be recorded in the `Manifest.toml` file.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [May 14, 2018, 9:23am UTC](https://discourse.julialang.org/t/how-to-get-the-commit-of-head-of-a-julia-package/10877/5 "2018-05-14T09:23:41Z")

</div>

> [@fredrikekre](#):
>
> To record the current state of a package?

Yes. For now, I can search the paths myself.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [May 14, 2018, 9:28am UTC](https://discourse.julialang.org/t/how-to-get-the-commit-of-head-of-a-julia-package/10877/6 "2018-05-14T09:28:35Z")

</div>

This is one way, not sure if it is the best.

```julia
julia> r = LibGit2.GitRepo(".")
LibGit2.GitRepo("/Users/kristoffer/julia")

julia> LibGit2.peel(LibGit2.GitCommit, LibGit2.head(r))
Git Commit:
Commit Author: Name: Kristoffer Carlsson, Email: kcarlsson89@gmail.com, Time: 2018-03-12 11:24:27+01:00
Committer: Name: Kristoffer Carlsson, Email: kcarlsson89@gmail.com, Time: 2018-05-14 11:18:17+02:00
SHA: c3cf88ad03abbac9d5d7074863d7277a186cce82
Message:
use Pkg3 for docs

julia> LibGit2.GitHash(ans)
GitHash("c3cf88ad03abbac9d5d7074863d7277a186cce82")

julia> string(ans)
"c3cf88ad03abbac9d5d7074863d7277a186cce82"

```

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [May 14, 2018, 10:44am UTC](https://discourse.julialang.org/t/how-to-get-the-commit-of-head-of-a-julia-package/10877/7 "2018-05-14T10:44:51Z")

</div>

This is what I am getting:

```julia
julia> r = LibGit2.GitRepo(".")
ERROR: GitError(Code:ENOTFOUND, Class:Repository, Could not find repository from '.')
Stacktrace:
 [1] macro expansion at .\libgit2\error.jl:99 [inlined]
 [2] Base.LibGit2.GitRepo(::String) at .\libgit2\repository.jl:10

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [May 14, 2018, 10:53am UTC](https://discourse.julialang.org/t/how-to-get-the-commit-of-head-of-a-julia-package/10877/8 "2018-05-14T10:53:42Z")

</div>

Well `"."` is the path to the repository. I just happened to have my current working dir inside a repo.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [May 14, 2018, 10:55am UTC](https://discourse.julialang.org/t/how-to-get-the-commit-of-head-of-a-julia-package/10877/9 "2018-05-14T10:55:42Z")

</div>

Oh I see so you were answering the first question on the Julia alternative. But the `Pkg.dir` problem means that I still have to search for this path myself.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [May 14, 2018, 11:04am UTC](https://discourse.julialang.org/t/how-to-get-the-commit-of-head-of-a-julia-package/10877/10 "2018-05-14T11:04:26Z")

</div>

You can use the internal function `find_in_path`,

```julia
julia> Base.find_in_path("Compat")
"/Users/kristoffer/.julia/v0.6/Compat/src/Compat.jl"

julia> Base.find_in_path("JuAFEM")
"/Users/kristoffer/Dropbox/julia/JuAFEM/src/JuAFEM.jl"

```

Normal caveates about internal functions apply.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [May 14, 2018, 11:08am UTC](https://discourse.julialang.org/t/how-to-get-the-commit-of-head-of-a-julia-package/10877/11 "2018-05-14T11:08:45Z")

</div>

Awesome thanks! What caveats are you talking about?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [May 14, 2018, 11:16am UTC](https://discourse.julialang.org/t/how-to-get-the-commit-of-head-of-a-julia-package/10877/12 "2018-05-14T11:16:38Z")

</div>

Like that it is renamed and doesn’t work in the same way on 0.7
