1

Usually when looking for git repositories I look for a .git directory, but that doesn't always work. Submodule worktrees have a .git file, submodules aren't always checked out, and submodule repositories (and bare repositories) can be anywhere.

I want to find every repository cloned from say github, but finding every repository's hard enough. Is there a command or oneliner to do this?

jthill
  • 55,082
  • 5
  • 77
  • 137
  • What do you mean by `I want to find every repository cloned from say github`? Do you want the github URL? Or are you looking for paths on the local disk? If so do you want the metadata directory or the source directory? – Guildencrantz Dec 19 '16 at 04:44
  • @Guildencrantz I mean I want to run `git config remote.origin.url` in every repo, running the command isn't hard, it's finding the repos. – jthill Dec 19 '16 at 05:01

1 Answers1

2

The minimal git repository contains a HEAD file and two directories, objects and refs.

So you can tell find to find directories that meet those criteria. HEAD is a pretty unusual name, let's look for that:

# find all repositories
find -name HEAD -execdir test -d refs -a -d objects \; \
    -printf %h\\n   # or if your `find` doesn't have `-printf`, just `-print`

will do. This finds the repository, not its worktree; to find worktrees, finding .git is still the best way

find -name .git

or if the trailing .git is annoying

find -name .git -printf %h\\n
jthill
  • 55,082
  • 5
  • 77
  • 137
  • 1
    Note that this requires that your `find` have both `-execdir` and `-printf`. The former seems to be pretty standard now but `-printf` is still missing on some MacOS and FreeBSD systems, at least. I see some versions of `find` either strip `.` from `$PATH` or refuse to allow `-execdir` if `$PATH` has `.` in it, too. Which is probably good. :-) (I don't put `.` in my path.) – torek Dec 18 '16 at 20:23
  • Yow. Gotta smile at the innocence of anyone who'd run with `.` in `PATH`... Thanks for the note, I folded in the `-printf` note. – jthill Dec 18 '16 at 20:31