Cuong Nguyen
Cuong Nguyen

Reputation: 95

How to get the full path of a file in a Git remote repo

I am working with Git, I want to get the full path of a specific file on a Git server that is cloned to a local repo.

Example: The file in the local repo is:

/home/.../source/android/frameworks/av/media/libstagefright/CameraSource.cpp

I want to get the link to this file on the Git server like this:

https://android.git.sec..../plugins/gitiles/platform/frameworks/av/+/refs/heads/main/o-one/media/libstagefright/CameraSource.cpp

How can I do that with a Bash command on Linux?

Upvotes: 6

Views: 7583

Answers (2)

S R Bandi
S R Bandi

Reputation: 81

For github URLs, below script also works for me.

#!/bin/bash

#get REPO top level dir
REPO=`git rev-parse --show-toplevel`

if [[ $? -ne 0 ]]
then
    echo "Not in git repo"
    exit 1
fi

git_branch=`git rev-parse --abbrev-ref HEAD 2>/dev/null`
#get relative path
relative_path=`git rev-parse --show-prefix|sed "s/\/$//"`
GIT_REMOTE_URL_UNFINISHED=`git config --get remote.origin.url|sed -s "s/^ssh/http/; s/git@//; s/.git$//;"`
GIT_REMOTE_URL="$(dirname $GIT_REMOTE_URL_UNFINISHED)/$(basename $GIT_REMOTE_URL_UNFINISHED)/$relative_path"

echo $GIT_REMOTE_URL

if [[ $git_branch != "master" ]]
then
    #if the feature branch name contains "/", transform it as per URL Encoding
    git_branch_in_url=`echo $git_branch| sed 's/\//%2F/g'`
    echo "Feature Branch URL"
    echo "${GIT_REMOTE_URL}/tree/$git_branch_in_url"
fi

Upvotes: 2

Nandu Kalidindi
Nandu Kalidindi

Reputation: 6280

This is a long custom script that serves the purpose for any remote https://github.com hosted repository. I will modify this soon as it is possibly prone to edge case errors.

To get this working, save the snippet into a git_relative.sh file and place it anywhere, for now place it at ~/ home directory.

Now modify your ~/.bashrc or ~/.zshrc or ~/.bash_profile by adding this line
alias git_remote_path='source ~/git_relative.sh'

Restart your terminal or execute the corresponding bash script where you added this. Then go to any git repository and execute git_remote_path. This print the full path in the git remote server.

The example you gave seems to have a different remote url as against the conventional github.com. To get it working for that, just change the lines I specified in the comments.

NOTE: The script works for https://github.com remote URLs, if you want to get path for a custom URL, you need to modify the code a little to match the needs of the repository URL conventions. One example is included in the snippet as a comment.

is_git=false
relative_to_git=""
check=""
initial=$(pwd)
current_dir=""
count=0

if [ ! $(git rev-parse --is-inside-work-tree 2> /dev/null) ]; then
  echo "NOT INSIDE A VALID GIT REPOSITORY"
  return
fi

git_remote=$(git config --get remote.origin.url)
git_remote=${git_remote%.git}
git_branch=$(git rev-parse --abbrev-ref HEAD)

while [ $is_git=true ]
do
  if [ -d ".git" ]; then
    break
  fi
  current_dir=${PWD##*/}
  relative_to_git="$current_dir/$relative_to_git"
  cd ../
done

# echo "$git_remote/+/$git_branch/$relative_to_git"
# For andoid.googlesource.com uncomment the above and comment the below lines
echo "$git_remote/tree/$git_branch/$relative_to_git"

cd $initial

Upvotes: 3

Related Questions