1 of 29

Lecture 3:

Bash Recap + Intro to Version Control

Spring 2025

2 of 29

Announcements

  1. Lab 1 due tomorrow at midnight
  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 29

Outline

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

4 of 29

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 29

Shell / Bash Review

  • What is a shell?
    • cat /etc/shells
    • echo $SHELL
  • How do you configure your shell?
  • For Windows users: why did we make you install and use WSL (versus using DOS / PowerShell)?
  • What is your home directory and what can you find there?
  • How do you install new programs from the command line?

6 of 29

Editing your .zshrc or .bashrc file

  1. What is the purpose of your .zshrc?
  2. In lab 1, you made / are making a shortcut. What other edits do you think you might be able to add to your .zshrc file?��Try this one:��today() �{ � echo This is a `date +"%A %d in %B of %Y (%r)"`.�}
  3. What does the “source” command do?

7 of 29

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?

8 of 29

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

9 of 29

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

10 of 29

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

11 of 29

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 29

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 29

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 29

containsPair: Approach #1

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

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 29

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 29

containsPair: Approach #2 with bug

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

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

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

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

return true;

}

}

}

return false;

}

Q: Can you find the bug? Why is it a bug?

17 of 29

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?

18 of 29

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?

19 of 29

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?

20 of 29

Good Work!

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

21 of 29

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…

22 of 29

Outline

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

23 of 29

Why is Code History important?

  • Roll back bad ideas?
  • “Blame”
  • Monitoring dependencies
  • Metadata and timestamps

24 of 29

Why is Version Control Important?

  • Same reasons as before and….
  • Collaboration
  • “One source of truth”
  • Reflection

25 of 29

What is the difference between centralized and distributed version control?

Centralized:

Distributed:

What does Google use and why?

26 of 29

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?

27 of 29

What is the one version rule?

28 of 29

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

Pros of Monorepos:

  • Less administrative overhead.
  • Deprecation: can change everything at once

Cons of Monorepos:

  • Big
  • Different policies for different parts of your codebase.

29 of 29

What files do you check into version control? What files do you NOT check in?

  • Yes:
    • Code you wrote

  • No:
    • Node modules – source code of dependencies (which someone else wrote).
    • Temporary files
    • Binaries
    • Sensitive data and API keys