Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use JGit to get the URL of origin?

Tags:

git

jgit

How to use JGit to get the URL of the origin remote?

I am using JGit and I want to execute

git config --get remote.origin.url

How to do that?

like image 770
hannsworst Avatar asked Jun 27 '16 20:06

hannsworst


People also ask

How do I find my URL for Origin?

You can view that origin with the command git remote -v, which will list the URL of the remote repo.

What is origin URL in Git?

In Git, "origin" is a shorthand name for the remote repository that a project was originally cloned from. More precisely, it is used instead of that original repository's URL - and thereby makes referencing much easier.

What is JGit used for?

JGit is a lightweight, pure Java library implementation of the Git version control system – including repository access routines, network protocols, and core version control algorithms. JGit is a relatively full-featured implementation of Git written in Java and is widely used in the Java community.


1 Answers

The configuration of a git repository can be accessed through Repository::getConfig(). The returned type is a StoredConfig.

In order to get the URL of the origin remote, use this snippet

String url = repository.getConfig().getString("remote", "origin", "url");

The class ConfigConstants lists a set of frequently used section names and value names.

like image 134
Rüdiger Herrmann Avatar answered Sep 26 '22 13:09

Rüdiger Herrmann