RKS
RKS

Reputation: 1423

How to get git repository URL on a subfolder

I have following folder structure:

FolderA
-FolderA1 (child of FolderA), and 
-FolderA2 (child of FolderA)

Now, Folder A1 and A2 are git repositories. I am writing a script to get the git repository URL of BOTH the folders when I am at the level of Folder.

I tried something like:

cd FolderA

git remote -v | awk -F ' ' '{ print $2 }' | sort -u | cd FolderA1

git remote -v FolderA1\ | awk -F ' ' '{ print $2 }' | sort -u

But this does not seems to work. Any idea how can I get the URL for FolderA1 / Folder A2 ?

Thanks in advance, RKS

Upvotes: 0

Views: 401

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295403

Personally, I'd do it something like this (requires bash 4 for associative array support):

get_subdir_urls() {
  local oldwd subdir url idx
  [[ $1 ]] && { oldwd=$PWD; cd "$1"; }
  for subdir in */.git; do
    (
     declare -A urls
     cd "${subdir%.git}" && \
       while read _ url _; do
         urls[$url]="${subdir%.git}"
       done < <(git remote -v)
     for idx in "${!urls[@]}"; do
       printf '%s\t%s\n' "${urls[$idx]}" "$idx"
     done
    )
  done
  [[ $1 ]] && cd "$oldwd"
}

# usage:
get_subdir_urls FolderA

Upvotes: 1

Related Questions