Before starting this lab, we again urge you to check over the git reference sheet Git Reference
And the slides can be found here Git week 2
Last weeks lab can be found here if you need to reference it for anything.
This is another partner lab, so buddy up!
By the end of this lab, you'll be able to create, use, and merge branches.
Depending on your working environment, perform the appropriate action:
Environment | Action |
---|---|
Linux Lab Machine | Open a terminal window |
Windows or Personal Computer | SSH into data.cs.purdue.edu * |
* You must have X11 forwarding enabled. If you are on the Windows Lab Computer, follow the instructions below:
- Search for 'Xming' in the start menu and run it.
- Open PuTTy
- Expand the 'SSH' tab from the 'Category' list
- Choose 'X11' from 'SSH' list
- Check 'Enable X11 Forwarding'
- Connect like normal to
data.cs.purdue.edu
within PuTTy.
You should have your git username and email set up from last week, if you get any messages about missing them, refer to last weeks lab.
The command below will set your git push
action to an appropriate default.
git config --global push.default simple
This command sets your default editor (just for today's lab) to be gedit for merge messages.
export EDITOR="gedit"
This only lasts as long as the terminal window you ran it within. As soon as you close the window, this setting is lost.
- Create a new repository on github and add your partner as a collaborator Don't skip this
Just a reminder on how to do that
Creating a repo
Go to the repo settings
Add your partner as a collaborator
- Make and go into a directory in your home directory for this lab. The path should look something like
~/cs190lab6
Use
mkdir
andcd
for this
- Download
text_mutator.rb
into this directory.
wget https://github.com/scottopell/cs190lab6/raw/master/text_mutator.rb
- Initialize a repo, add, commit, and push this file.
git init
git add text_mutator.rb
git commit -m 'adds text_mutator.rb'
git remote add origin <github_url>
git push -u origin master
- Have your partner clone this repository onto their machine.
git clone <url_from_partner>
- The partner that just cloned this repository will also need to
cd
into the repository that theclone
just created.
cd <repository_name>
- Decide which one of you wants to debug, and which wants to add a new feature. Proceed to either "Bug Fixes" or "New Features" depending on which one you're doing. DO NOT DO BOTH, EACH PARTNER DOES ONE!!
- Lets make our own branch,since we don't know how many commits or how long this will be.
git branch bug-fixes
git checkout bug-fixes
- So, you have your repository with a fresh version of
text_mutator.rb
If you run it, like so
ruby text_mutator.rb --reverse Hello World
You'll notice that you get an output that's reversed, but it only reverses the first word. This isn't very much fun. Lets fix it.
- Open up
text_mutator.rb
in your editor of choice and look through the code. - You can see that to get the input text (from line 12)
text = ARGV[1]
This code just gets the second argument after
text_mutator.rb
, which in our case is 'Hello'.
- Instead of that, lets prompt the user for input.
Since you guys (probably) don't know ruby, we'll give you most of the code
- So instead of getting the data from text from the command line arguments, we can use
$stdin.gets.chomp
to prompt for input
stdin
refers to the user input from the terminalgets
stands forget string
chomp
gets rid of the trailing newline (don't worry about this for now)
- Line 12 should now be
text = $stdin.gets.chomp
- We should probably tell the user whats happening, so add a line ABOVE the line above that says
puts "Input some text, please"
This means that the line with
puts
should come before the line withgets
.
- Now save this, exit, and run the program again.
ruby text_mutator.rb --reverse
- Now we should be prompted for input, so type in some text and it should output it as expected!
- Let's
add
our changes to git
git add text_mutator.rb
commit
these changes
git commit -m 'fixes bug with text input, changes source from argv to user input'
- Now lets push our changes to the remote
git push -u origin bug-fixes
Note, its our first push on this branch, so we want to use
-u
, and we're pushing to the remote namedorigin
and our branch is namedbug-fixes
- Log onto github and look at our new branch!
To view different branches, click on the button surrounded in red above.
- We like our changes, so lets merge them back into master
git checkout master
git merge bug-fixes
- And lets push our changes up to the remote. NOTE: You may get an error here, you simply need to
pull
your partner's latest changes
You may also get a popup in gedit here, simply save and quit gedit.
git push
- Now lets wait for our partner to finish up and continue on to "Wrap-up"
While your partner is fixing bugs, we're going to add some new features to this program!
- Lets make our own branch,since we don't know how many commits or how long this will be.
git branch new-features
git checkout new-features
- Run the program with no arguments, like this, like this
ruby text_mutator.rb
We'll see a message showing us how to use the program.
Usage: ruby text_mutator.rb [--reverse --upper --lower] <input text>
- So lets run it with the following input
ruby text_mutator.rb --upper scott
And we'll see our output
SCOTT
We want to add an option to to invert the case of the string, so our goal is to be able to do the following
# THIS WILL NOT WORK YET
# we need to follow the steps below before this will work
ruby text_mutator.rb --swapcase heLlO
output
HElLo
-
Open text_mutator.rb in your favorite code editor.
-
The easiest change to make is to add
--swapcase
to theusage
string. Change line 4 to match the following
puts "Usage: ruby text_mutator.rb [--reverse --upper --lower --swapcase] "
1. Add these lines under line 21, in the same format as the lines above, to add support for the `--swapcase` flag
```ruby
when "--swapcase"
mod_text = swapcase_mod text
- Now that we've added a call to the method
swapcase_mod
, we need to create that method. We can do this by inserting these lines under line 46.
def swapcase_mod input
input.swapcase
end
- That's it! Now we can save this, exit, and try running it with our new method like this.
ruby text_mutator.rb --swapcase hElLo
And hopefully we'll see this output
HeLlo
Now everything is working, and we like our changes, so lets commit them.
- Let's
add
our changes to git
git add text_mutator.rb
commit
these changes
git commit -m 'add swapcase option'
- Now lets push our changes to the remote
git push -u origin new-features
Note, its our first push on this branch, so we want to use
-u
, and we're pushing to the remote namedorigin
and our branch is namedbug-fixes
- Log onto github and look at our new branch!
To view different branches, click on the button surrounded in red above.
- And we like our changes, so lets merge them back into master
git checkout master
git merge new-features
Note: you might get a merge message here (it will open in gedit), if you do, simply save the file and quit If you get a strange message about something not being successfull, call over a TA
- Push your changes to the remote repository.
git push
NOTE: You may get an error here, you need to
pull
your partner's latest changes
You may also get a popup in gedit here, simply save and quit gedit.
Now just wait for your partner to finish up theres and then continue on to "Wrap-up"
So now, if we both pull down the latest version of master, we can see that the changes made in both branches are there, and we can input from a user prompt, and we can use the swapcase function, all without any merge conflicts!
For grading, open up your repository on github, and navigate to the Network
tab on the right (same as last week)
Ask the TA about anything you might be unclear on