How to get a Git repo url saved in a bash script to a variable - node.js

In my bash script that runs postinstall to npm install I want to get the Github remote url for a specific repo named base. But, when this runs the value extracted is "git" and not the URL to my git repository. How would you capture the returned URL from this command?
#!/bin/bash
...
remoteBackup=git config --get remote.base.url
...

var=$(command that produces output) not var=value command.
The latter runs command with var in the environment set to value.
Assuming that's what you actually have in your script and not a typo. If it is a typo and that gets you back git and not git#bitbucket then it sounds like your config is messed up.

Related

Custom git command autocompletion

I have implemented a custom git command by writing a shell script located in /usr/local/bin. It works fine, but I would like the script to autocomplete branches in the command line, just like git checkout [TAB][TAB]. How could this be done?
EDIT:
Just adding some context: git allows you to very easily add your own commands by creating a script git-$subcommandName. In my case here git-install, the script facilitates checking out a branch, building, packaging, and installing the source code.
Figured it out. I needed to download git bash completion (here), create a new file in /etc/bash_completion.d with the following contents:
source ~/.git-completion.bash
_git_install ()
{
__gitcomp_nl "$(__git_refs)"
}
and then exec bash to reload completion scripts.

Unable to complete svn2git in a shell script

I am migrating a SVN codebase to Git and need to create more than 30 projects. Hence I approached this problem by automating the steps in a shell script.
Below is the code snippet
test.sh
#!/bin/sh
svn2git http(s)://svn.yyy.xxx.com:1080/SVNROOT/PROJECTSUITE --trunk trunk/SOURCES/Project1 --notags --nobranches --authors /Users/praveen/users.txt --verbose > log.txt
git remote add origin git#gitlab.yyy.xxx.com:PROJECTSUITE/Project1.git
git push origin --all
I am able to perform this migration successfully when the commands are executed individually from the terminal.
Terminal
$ svn2git http(s)://svn.yyy.xxx.com:1080/SVNROOT/MERLINSUITE --username praveen --trunk trunk/SERVER_SOURCES/MerlinTMS --notags --nobranches --authors /Users/praveen/users.txt --verbose
Running command: git svn init --prefix=svn/ --no-metadata --trunk=trunk/SOURCES/Project1 http(s)://svn.yyy.xxx.com:1080/SVNROOT/PROJECTSUITE
Initialized empty Git repository in /Users/praveen/projectFolder/.git/
Running command: git config --local --get user.name
Running command: git config --local svn.authorsfile /Users/praveen/users.txt
Running command: git svn fetch
W: Ignoring error from SVN, path probably does not exist: (160013): Filesystem has no item: File not found: revision 100, path '/trunk/SOURCES/Project1'
W: Do not be alarmed at the above message git-svn is just searching aggressively for old history.
This may take a while on large repositories
....
....
success :)
But when the above shell script is executed using verbose it gets stuck. The project tested was around 30MB in size.
log.txt
Running command: git svn init --prefix=svn/ --no-metadata --trunk=trunk/SOURCES/Project1 http(s)://svn.yyy.xxx.com:1080/SVNROOT/PROJECTSUITE
What am I doing wrong? Is it that svn2git cannot be called within a shell script?

How to make current directory a git working directory

I've written multiple *.cpp files in the location ~/Code/CPLUS before I know the existence of git.
Now I want to use git for version control.
I created a folder ~/git_repo/, and in this folder, I ran git init command. When I tried to run the command git add my_first_c.cpp under the path ~/Code/CPLUS, the following message appeared:
fatal: Not a git repository (or any of the parent directories): .git
Then I typed git init ~/git_repo/ under the path ~/Code/CPLUS, the same error still appeared when git status was typed.
If I type git init under the path ~/Code/CPLUS, the add and commit can be executed. The only problem is that .git is stored in ~/Code/CPLUS/, while I'd like it be stored in ~/git_repo.
My question is how to make the folder ~/Code/CPLUS a working directory while the repo info is stored in ~/git_repo/? And my machine has no GUI.
You could try exporting the variables export GIT_WORK_TREE=~/git_repo/ and export GIT_DIR=../Code/CPLUS from terminal (or in your ~/.bashrc) so Git uses these.
Thanks #Alariva , the suggested solution indeed solved this question. With .git created in ~/git_repo/, typing git --git-dir=/abs/path/to/repo/git_repo/.git add my_first_c.cpp works.
The solution comes from this post.

Autorun Bash Script after Login to Sync Git Repos

I have already a gitupdate.sh that I found on the internet.
My problem now is how to make the script run automatically every time I log into my Ubuntu 14.04 computer.
I have tried adding this line to .bashrc
sh '/path/to/git/repo/gitupdate.sh'
The problem here is the script is executed not in the context or path of the repo thus the script runs in a folder that is not initialized with git. (I don't actually know what folder bashrc run on)
What want to do is that the update script to be run by Ubuntu in the context of the path so the script will not fail. And also show the running script in a Terminal window that will not automatically close.
And the ultimate goal, is to be able automatically upon login, that git cloned repos to be synced with the public repos.
Try
(cd repo && bash /path/to/gitupdate.sh)
Just open the gitupdate.sh and at the beginning of the file just add the command to go to the selected direcotry. For example:
cd /var/test

How to set up shell alias so I can change directories

I use bower and grunt in my workflow, and I wanted to install bower at the same time as grunt and pull down all my bower dependencies. So I created a package.json file that has a script attached on postinstall, which I also pass my github project repo, and dropped my post_install.sh into /root/bin.
{ // snippet of package.json
...
"scripts": {
"postinstall": "bin\\post_install.sh https://github.com/blah/blah.git
},
...
}
so running npm install would run post_install.sh, which runs
#!/bin/bash
node_modules/.bin/bower install
grunt setup
git init
git add .
git commit -m "Initial commit of project artifacts"
git remote add origin $1
git push -u origin master
This only works if I set git bash to open in my project directory. So I've been trying to figure out how to switch directories from where git bash opens at /htdocs to /htdocs/myproject using an alias. I read up on how to set this I thought, but I can't figure out how to invoke this file... incidentally also tried putting it in another .sh file to see if that worked.
#!.bashrc
alias projDir='cd /onloadsolutions'
What am I doing wrong that the alias above in /bin/.bashrc won't execute in my post_install.sh when dropped above "grunt setup"?
Dropping this into post_install.sh
echo "script running has basename `basename $0`, dirname `dirname $0`"
echo "present working directory is `pwd`"
gives a basename of D:/htdocs/onloadsolutions/bin/post_install.sh, but my present working directory is /d/htdocs, which when the bower and git commands run they do it in the wrong directory.
Not 100% sure what you mean but I am guessing when you open bash you want to
be able to type a command to take you to your project
then type npm install and have it pull down all your dependencies
Try editing your .bashrc file (this should be in your home directory, not in the /bin folder) to access this you can use something like
nano ~/.bashrc
(~/ is a shortcut to your home directory)
and look in it for any existing alias commands or add yours down the bottom.
then after opening another bash session (.bashrc only runs when you first start terminal) you should be able to see your current aliases by typing "alias"
you can launch the alias you mentioned above by typeing "projDir" into the console. It would be much better to add the full path in your alias i.e.
alias projDir='cd /var/www/onloadsolutions'
then you can call it from anywhere. Otherwise you might end up in a different folder than you expect.
UPDATE
See comments below.
Put cd $2 in the second to top line of the post_install.sh file and add a second parameter to the package.json file that calls it.

Resources