androdeb: add automatic ADEB_REPO_URL detection for git repositories

When the ADEB_REPO_URL variable is not set, attempt to use the git fetch
URL from where adeb is running. This makes the most common use case
simpler.

Signed-off-by: Erick Reyes <erickreyes@google.com>
diff --git a/README.md b/README.md
index b4200d9..23ae44d 100644
--- a/README.md
+++ b/README.md
@@ -51,9 +51,10 @@
 # Add some short cuts:
 sudo ln -s $(pwd)/adeb /usr/bin/adeb
 
-# For cached image downloads which result in a huge speed-up,
-# You could set the ADEB_REPO_URL environment variable in your
-# bashrc file.
+# Cached image downloads result in a huge speed-up. These are automatic if you
+# cloned the repository using git. However, if you downloaded the repository
+# as a zip file (or you want to host images elsewere), you could set the
+# ADEB_REPO_URL environment variable in your bashrc file.
 # Disclaimer: Google is not liable for the below URL and this
 #             is just an example.
 export ADEB_REPO_URL="github.com/joelagnel/adeb/"
diff --git a/androdeb b/androdeb
index 031f2b6..9c8fa4b 100755
--- a/androdeb
+++ b/androdeb
@@ -207,13 +207,32 @@
 	c_info "All done! Run \"adeb shell\" to enter environment"
 }
 
+function detect_repo_url() {
+	GIT_REMOTE=`cd $spath && git branch -vv | grep "\*" | sed -e "s/.*\[//" | cut -d "/" -f 1`
+	if [ -z $GIT_REMOTE ]; then
+		c_warning "Not running from a git repository, unable to determine URL"
+		return 0
+	fi
+	ADEB_REPO_URL=`cd $spath && git remote show $GIT_REMOTE | grep "Fetch URL" | sed -e "s/.*Fetch URL://" \
+		 -e "s/.*@//"  \
+		 -e "s/https:\/\///" \
+		 -e "s/:/\//"  \
+		 -e "s/\.git$//"`"/"
+	c_info "Detected URL: $ADEB_REPO_URL"
+}
+
 function check_repo_url () {
-   if [ -z $ADEB_REPO_URL ]; then
-      c_warning "Automatic download is disabled. To enable it, please set the \$ADEB_REPO_URL"
-      c_warning "environment variable as recommended in the setup instructions in the README.md"
-      do_cleanup
-      exit 0
-   fi
+	if [ -z $ADEB_REPO_URL ]; then
+		c_info "No repository URL provided in enviromnent. Attempting to auto-detect it"
+		detect_repo_url
+	fi
+
+	if [ -z $ADEB_REPO_URL ]; then
+		c_warning "Automatic download is disabled. To enable it, please set the \$ADEB_REPO_URL"
+		c_warning "environment variable as recommended in the setup instructions in the README.md"
+		do_cleanup
+		exit 0
+	fi
 }
 
 function download_headers() {