1 of 32

Lecture 3:

Bash Recap + Intro to Version Control

Fall 2024

2 of 32

Announcements

  1. Lab 1 due tonight at midnight (gave an extension b/c WSL can be tricky).
  2. Labs 2 and 3 will be on version control. We’re going to “go deep” on the kinds of things you can do with distributed version control.
    1. It’s tricky, so make some time for it.

3 of 32

Outline

  • Bash activity (review from Thursday)
  • Coding Review (containsPair + URL Shortener)
  • Discussion of the Readings
  • Intro to git & GitHub

4 of 32

Outline

  1. Bash activity (review from Thursday)
  2. Coding Review (containsPair + URL Shortener)
  3. Discussion of the Readings
  4. Intro to git & GitHub

5 of 32

Bash Review

  • For Windows users: why did we make you install and use WSL (versus using DOS / PowerShell)?
  • What is SSH?
  • What needs to be in place for you to be able to connect to another computer via SSH?
  • What is your home directory and what can you find there?

6 of 32

Exercise 1: Common Bash Commands

Let’s try doing these exercises together (Windows users: please use WSL + Ubuntu):�

Using the Command Line Cheat Sheet

  1. How do you figure out what directory you’re in?
  2. How do you list all of the files in your current directory?
  3. How do you look at directories and subdirectories in a “tree-like” structure?
  4. How do you list hidden files?
  5. How do you read the contents of a file?
  6. How do you display your command line history?
  7. How do you search your Desktop for any file with the string “csci” in it?

7 of 32

Answers Exercise 1: (Try it before you look)...

  1. How do you figure out what directory you’re in?
    • pwd
  2. How do you list all of the files in your current directory?
    • ls or ls -l
  3. Listing directories and subdirectories?
    • tree -L 2 # where the number corresponds to levels of nesting
  4. How do you list hidden files?
    • ls -a or ls -la
  5. How do you read the contents of a file?
    • cat my_file.txt
  6. How do you display your command line history?
    • history
  7. How do you search your Desktop for any file with the string “csci” in it?
    • grep 'csci' ~/Desktop -r

8 of 32

Bash + VIM Practice

Open a terminal

For Windows: Activate WSL using the command prompt PowerShell

Navigate to csci338

(or folder where you store csci338 files)

From the command line create a file called readme.txt that has the text “Hello world!”

Use echo and either > (overwrite) or >> (append) operators.

Verify that it worked using cat

Open a file called init-app.sh using vim

Command: vi init-app.sh

Put vim into edit mode

Press the “i” key

Create a script that:

  1. Makes a new directory called my-app
  2. Creates two empty files inside of startup:
    • index.html
    • style.css

Use the course command line cheat sheet to help you. Hints:

  • mkdir
  • touch

Save your file and exit

First press escape. Then press: :wq

Test the bash script you just made:

bash test.sh

9 of 32

Answers Exercise 2: (Try it before you look)...

cd ~/Desktop/csci338 # should be a different path for you

echo "Hello world!" > readme.txt

cat readme.txt # should print "Hello world!" to the screen

vi init-app.sh

Within vim, go into edit mode by pressing the i key.

Then add the following lines to test.sh:

mkdir my-app

touch my-app/index.html

touch my-app/style.css

Save and quit vim by first pressing the Escape key and � then typing :wq to save and quit

bash init-app.sh # should have created a my-app directory + HTML/CSS file within your current directory

10 of 32

Editing your .zshrc file

Create a shortcut to your csci338 folder from the command line by editing your .zshrc file on your local computer. To do this:

  1. What is the purpose of your .zshrc?
  2. What other edits do you think you might be able to add to your .zshrc file?
  3. What does the “source” command do?

11 of 32

Outline

  1. Bash activity (review from Thursday)
  2. Coding Review (containsPair + URL Shortener)
  3. Discussion of the Readings
  4. Intro to git & GitHub

12 of 32

Reminder of the Problem

Write a function – using any programming language you want – that takes in a list of integers. The function returns true if the list contains two or more of the same integer, false otherwise.

// Java

public static boolean containsPair(List<Integer> l) {

return false;

}

13 of 32

Suggested Approach

  1. Come up with some sample lists of when the function should return true and when it should return false.
    1. Where the duplicates are… in order, out of order, towards the beginning, towards the end, etc.
  2. Consider edge cases:
    • Does your code work with an empty array or with an array with only one Integer?

Also: You probably won’t be allowed to use a code editor when you’re doing coding interviews. Whiteboarding only!

14 of 32

containsPair: Approach #1

public static boolean containsPair(List<Integer> l) {

// first sort the list. Then do the thing below:

for (int i = 0; i < l.size() - 1; i++) {

if (l.get(i).equals(l.get(i + 1))) {

return true;

}

}

return false;

}

Q: Does this work? If not, what is the problem? Can you think of a scenario where this could work and a code enhancement you might make?

15 of 32

containsPair: Approach #2

public static boolean containsPair(List<Integer> l) {

for (int i = 0; i < (l.size() - 1); i++) {

for (int j = i + 1; j < l.size(); j++) {

if (l.get(i).equals(l.get(j))) {

return true;

}

}

}

return false;

}

Q: Does this work? Is it efficient? What’s the worst case scenario?

16 of 32

containsPair: Approach #3

public static boolean containsPair(List<Integer> l) {

List<Integer> unique = new ArrayList<Integer>();

for (int i = 0; i < l.size(); i++) {

if (unique.contains(l.get(i))) {

return true;

}

unique.add(l.get(i));

}

return false;

}

Q: Does this work? Is it efficient? What’s the worst case scenario?

17 of 32

containsPair: Approach #4

public static boolean containsPair(List<Integer> l) {

Map<Integer, Integer> s = new HashMap<Integer, Integer>();

for(int i = 0; i < l.size(); i++) {

if (s.containsKey(l.get(i))) {

return true;

}

s.put(l.get(i), 1);

}

return false;

}

Q: Does this work? Is it efficient? Do you think this is different than approach 3? Why or why not? What’s the worst case scenario?

18 of 32

containsPair: Approach #5

public static boolean containsPair(List<Integer> l) {

Set<Integer> s = new HashSet<Integer>(l);

return s.size() != l.size();

}

Q: Does this work? Is it efficient? What’s the worst case scenario?

19 of 32

Good Work!

Thinking like this takes lots of practice. Keep at it. We’ll keep doing these problems all semester.

20 of 32

URL Shortener

Your boss asks you to implement a URL shortener. Specifically:

  1. A user types a long URL into a textbox (e.g., https://docs.google.com/document/d/1lb4iE41b-aJoR9B3M007bYTT-m3p9E8D/edit)
  2. The system gives the user back a shortened URL (e.g., https://t.ly/e8vxn)
  3. Thereafter, if anyone clicks the shortened url, the page redirects to the original (long) URL.

What steps would you take to implement this system?

Let’s draw this out on the whiteboard…

21 of 32

Outline

  • Bash activity (review from Thursday)
  • Coding Review (containsPair + URL Shortener)
  • Discussion of the Readings
  • Intro to git & GitHub

22 of 32

Why is Code History important?

  • A log of notes of how you built your stuff
  • If it’s collaborative project, you can help someone else to understand the history
  • If you break it you can undo
  • Flagging unauthorized access

23 of 32

Why is Version Control Important?

  • Enables Collaboration
  • Enables modification w/o interference

24 of 32

What is the difference between centralized and distributed version control?

Centralized:

Distributed:

What does Google use and why?

25 of 32

What is the problem with having long-running dev branches? What is the solution?

  • Why were they a thing in the 80s and 90s?
  • Why have they gone away?

26 of 32

What is the one version rule?

27 of 32

What are the tradeoffs of having a Monorepo versus multiple repos?

28 of 32

Outline

  • Bash activity (review from Thursday)
  • Coding Review (containsPair + URL Shortener)
  • Discussion of the Readings
  • Intro to git & GitHub

29 of 32

Intro to git

  • What is git?
  • What experience do you have using git (if any)?
  • Good Reference: https://git-scm.com/book/en/v2

30 of 32

Git v. GitHub

  • What is git?
    • Git is a free and open source version control system
  • Why is version control important?
    • Allows you to track code changes (can help you version your code, revert changes, know who did what, and identify when bugs were introduced)
  • What is GitHub?
    • GitHub is a company that provides cloud-based hosting services for managing your git repositories.
  • What are some of the services GitHub provides?
    • Issue trackers, third-party integrations (e.g., CI/CD tools), website hosting, branch protection, tools for managing teams and organizations, tools / notifications to identify security issues (i.e., problematic node / python dependencies, accidentally checking in passwords and API keys, and more)

30

31 of 32

How to Learn Git

The best way to learn git is to practice using git while working on a collaborative software project (like this class)!

There are also many great references you can draw from, including:

  • The Git SCM Book (SCM stands for source code management)
  • Video tutorials (e.g., Google “youtube rebase” or “google revert”)
  • Conceptual diagrams (Google them) – which explain how various commands / workflows work (like today’s assigned video).

31

32 of 32

For Thursday

  • Read the History section of the Wikipedia article on Git (including all subsections: Naming, Characteristics, Data Structures, and References).
  • As we work through the next two labs, the following sections of the Pro Git book will provide context and some useful conceptual models:
    • Chapter 2, sections 1, 2, 3, 4, 5
    • Chapter 3, sections 1, 2, 3, 6