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

Viewed 4513

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?

2 Answers

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

Related