cs4414 Fall 2013
University of Virginia
David Evans
Plan for Today
Why and how to use git
Practice programming pointers (making a List)
24 September 2013 University of Virginia cs4414 1
(Paper) notes for today have some of the
code. Posted notes will have all code.
I won’t be able to hold my usual office hours this
afternoon, but can meet (briefly) after class today and/or
arrange another time.
How (Not) to Manage Files
24 September 2013 University of Virginia cs4414 2
“Smart Lawyer”
Version Control
Version Control
• How can Alice find the last working version of
her code before she broke everything trying to
fix a little bug?
• How can Alice and Bob work on a program
together?
• How can 10,000 developers work together on
the Linux kernel?
24 September 2013 University of Virginia cs4414 3
Annual Linux Development Report
24 September 2013 University of Virginia cs4414 4
Number of
Changes per
Hour
“Since the beginning of
the git era (the 2.6.11
release in 2005), a total
of 9,784 developers have
contributed to the Linux
kernel; those developers
worked for a minimum of
1,064 companies.”
Top Companies
24 September 2013 University of Virginia cs4414 5
24 September 2013 University of Virginia cs4414 6
2011-07-21
2013-06-30
24 September 2013 University of Virginia cs4414 7
http://www.vidarholen.net/contents/wordcount/
Centralized Version Control
24 September 2013 University of Virginia cs4414 8
Repository
(cvs, subversion)
Alice:
gash> svn checkout
gash> svn update
[make changes]
gash> svn commit
Bob:
gash> svn checkout
gash> svn update
[make changes]
gash> svn commit
Distributed
Version
Control
24 September 2013 University of Virginia cs4414 9
Main Repository
(git, Mercurial)
[make changes]
Alice’s
Local
Repository
commit
[make changes]
Bob’s
Local
Repository
commitupdate update
24 September 2013 University of Virginia cs4414 10
Main
Repository
(Hg)
[make changes]
Alice’s
Local
Repository
update,
commit
[make changes]
Bob’s
Local
Repository
update,
commit
Repository
(svn)
[make changes] [make changes]
Centralized: One Repo Distributed
What has to happen
before Bob sees
Alice’s changes?
24 September 2013 University of Virginia cs4414 11
Main Repository
(Hg)
changed gash.rs
Alice’s
Local
Repository
commit update
Bob’s
Local
Repository
Alice Bob
see gash.rs
git pull = “pull” and “update”
24 September 2013 University of Virginia cs4414 12
Main Repository
(git)
changed gash.rs
Alice’s
Local
Repository
commit
Bob’s
Local
Repository
Alice Bob
see gash.rs
(I find this asymmetrical
and confusing…but not
many scenarios where
pulling to local without
updating is useful.)
pull is not the analog of push –
it is analog of commit + push
What if Bob had modified his copy?
24 September 2013 University of Virginia cs4414 13
Main Repository
(git)
changed zhttpto.rs
Alice’s
Local
Repository
commit
Bob’s
Local
Repository
Alice Bob
changed zhttpto.rs
gash> git pull
…
error: Your local changes to the following
files would be overwritten by merge:
ps1/zhttpto.rs
Please, commit your changes or stash
them before you can merge.
Aborting
Okay, let’s commit:
24 September 2013 University of Virginia cs4414 14
Main Repository
(git)
changed gash.rs
Alice’s
Local
Repository
commit
Bob’s
Local
Repository
Alice Bob
changed gash.rs
gash> git commit -a –m "Added a comment about lack of security."
[master 1347c1f] Fixed the notorious zombie process bug.
1 files changed, 3 insertions(+), 0 deletions(-)
gash> git pull
Auto-merging ps1/zhttpto.rs
CONFLICT (content): Merge conflict in ps1/zhttpto.rs
Automatic merge failed; fix conflicts and then commit the result.
Observing Conflicts
24 September 2013 University of Virginia cs4414 15
//
// zhttpto.rs
//
// Reference solution for PS1
//
// Special thanks to Kiet Tran for providing code we incorporated into this.
//
<<<<<<< HEAD
// Note: it would be very unwise to run this server on a machine that is
// on the Internet and contains any sensitive files!
=======
// Warning: this is not a secure server!
>>>>>>> faf7829d3ab38459172b622351d68ac1f47bddd0
//
// University of Virginia - cs4414 Fall 2013
Resolving Conflicts (for Luddites)
24 September 2013 University of Virginia cs4414 16
gash> emacs zhttpto.rs
edit conflicted file manually (removing the <<<< and ====)
gash> git commit -a -m "Updated security message."
[master 1e6e684] Updated security message.
gash> git push
…
To https://github.com/cs4414/Reference-Solutions.git
faf7829..1e6e684 master -> master
Resolving Conflicts (for Moderns)
24 September 2013 University of Virginia cs4414 17
git mergetool
Avoiding Conflicts
24 September 2013 University of Virginia cs4414 18
It’s easier to ask
forgiveness than it is to get
permission.
Admiral Grace HopperWith conflicts, it is better to avoid
them than to resolve them!
- pull before you start modifying, and
often while working
- commit early and often, use good
messages
- push whenever you have something
worth sharing (but don’t push junk)
- divide your project into small,
coherent files
- communicate well with your
teammates!
Avoiding Major Conflicts with Teammates
24 September 2013 University of Virginia cs4414 19
Don’t resolve conflicts
by just undoing others’
work!
At least make sure you
understand it before
replacing their changes
with your own.
24 September 2013 University of Virginia cs4414 20
What’s more important for getting an
interesting computing job?
Impressive Transcript from
Prestigious Institution
Impressive Code and Record
in Hacker Communities
24 September 2013 University of Virginia cs4414 21
Linked Lists in Rust
24 September 2013 University of Virginia cs4414 22
What’s a List?
24 September 2013 University of Virginia cs4414 23
Linked Lists
A List is an object that is either:
Null (a special value representing empty list)
or a pair whose second part is a List
24 September 2013 University of Virginia cs4414 24
struct Node {
head : int,
tail : Option<@Node>
}
type List = Option<@Node> ;
Keeping things simple for now!
@ = automatically managed
24 September 2013 University of Virginia cs4414 25
let p: List =
Some(@Node{head: 1,
tail: Some(@Node{head : 2,
tail: Some(@Node{head: 3,
tail: None})})});
struct Node {
head : int,
tail : Option<@Node>
}
type List = Option<@Node> ;
to_str
24 September 2013 University of Virginia cs4414 26
struct Node {
head : int,
tail : Option<@Node>
}
type List = Option<@Node> ;
24 September 2013 University of Virginia cs4414 27
fn to_str(lst: Option<@Node>) -> ~str {
fn elements_to_str(n: @Node) -> ~str {
match (n.tail) {
None => fmt!("%?", n.head),
Some(tail) => fmt!("%?, %s", n.head, elements_to_str(tail))
}
}
match(lst) {
None => ~"Null",
Some(n) => fmt!("[%s]", elements_to_str(n))
}
}
Using Traits
24 September 2013 University of Virginia cs4414 28
trait ToStr {
fn to_str (&self) -> ~str;
}
(ToStr is part of the core)
Similar to interface in Java (except Rust
traits can include default implementations).
24 September 2013 University of Virginia cs4414 29
impl ToStr for List {
fn to_str(&self) -> ~str {
fn elements_to_str(n: @Node) -> ~str {
match (n.tail) {
None => fmt!("%?", n.head),
Some(tail) => fmt!("%?, %s", n.head, elements_to_str(tail))
}
}
match(*self) {
None => ~"Null",
Some(n) => fmt!("[%s]", elements_to_str(n))
}
}
}
fn main() {
let lst : List = Some(@Node{head: 1, tail: Some(@Node{head : 2,
tail: Some(@Node{head: 3, tail: None})})});
println(fmt!("%s", lst.to_str()));
}
Using our List (?)
24 September 2013 University of Virginia cs4414 30
fn main() {
let lst : List =
Some(@Node{head: 1,
tail: Some(@Node{head : 2,
tail: Some(@Node{head: 3, tail: None})})});
println(lst.to_str());
lst.head = 0;
println(lst.to_str());
}
Making it mutable
24 September 2013 University of Virginia cs4414 31
struct Node {
head : int,
tail : Option<@Node>
}
type List = Option<@Node> ;
Since Rust 0.6 – can’t
make struct fields mut
struct Node {
head : int,
tail : Option<@mut Node>
}
type List = Option<@mut Node> ;
24 September 2013 University of Virginia cs4414 32
fn main() {
let lst : List = Some(@mut Node{head: 1,
tail: Some(@mut Node{head : 2,
tail: Some(@mut Node{head: 3, tail: None})})});
println(lst.to_str());
match lst {
None => fail!("Unexpected None!"),
Some(n) => n.head = 0
}
println(lst.to_str());
}
Increment All
24 September 2013 University of Virginia cs4414 33
Write a List method that increments the value of every
element of the list.
Increment All
24 September 2013 University of Virginia cs4414 34
trait Increment {
fn incr(&self);
}
impl Increment for List {
fn incr(&self) {
let mut current = *self;
loop {
match(current) {
None => break,
Some(node) => { node.head += 1; current = node.tail },
}
}
}
}
Mapping
24 September 2013 University of Virginia cs4414 35
self.mapr(|x: int| { x + 1 })
Define a higher-order
mapr method that
applies a function to all
elements in a List.
24 September 2013 University of Virginia cs4414 36
impl Map for List {
fn mapr(&self, f: &fn(int) -> int) {
let mut current = *self;
loop {
match(current) {
None => break,
Some(node) => { node.head = f(node.head);
current = node.tail },
}
}
}
}
Don’t we want to avoid @?
24 September 2013 University of Virginia cs4414 37
24 September 2013 University of Virginia cs4414 38
struct Node {
head : int,
tail : Option<~Node>
}
type List = Option<~Node> ;
What else needs to change to make a List with owned Nodes?
24 September 2013 University of Virginia cs4414 39
struct Node {
head : int,
tail : Option<~Node>
}
type List = Option<~Node> ;
trait Map {
fn mapr(&self, &fn(int) -> int)
}
-> List;
24 September 2013 University of Virginia cs4414 40
struct Node {
head : int,
tail : Option<~Node>
}
type List = Option<~Node> ;
trait Map {
fn mapr(&self, &fn(int) -> int) -> List;
}
impl Map for List {
fn mapr(&self, f: &fn(int) -> int) -> List {
match(*self) {
None => None,
Some(ref node) => { Some(~Node{ head: f(node.head),
tail: node.tail.mapr(f) }) },
}
}
} Is this better or worse than the @mut version?
Next class: making map
multi-threaded!
Read the MapReduce paper
(or at least the slides) before
Thursday’s class
24 September 2013 University of Virginia cs4414 41
Posted notes (later today) will have all code.
I won’t be able to hold my usual
office hours this afternoon, but
can meet after class today
and/or arrange another time.

Using Git, Pointers in Rust

  • 1.
    cs4414 Fall 2013 Universityof Virginia David Evans
  • 2.
    Plan for Today Whyand how to use git Practice programming pointers (making a List) 24 September 2013 University of Virginia cs4414 1 (Paper) notes for today have some of the code. Posted notes will have all code. I won’t be able to hold my usual office hours this afternoon, but can meet (briefly) after class today and/or arrange another time.
  • 3.
    How (Not) toManage Files 24 September 2013 University of Virginia cs4414 2 “Smart Lawyer” Version Control
  • 4.
    Version Control • Howcan Alice find the last working version of her code before she broke everything trying to fix a little bug? • How can Alice and Bob work on a program together? • How can 10,000 developers work together on the Linux kernel? 24 September 2013 University of Virginia cs4414 3 Annual Linux Development Report
  • 5.
    24 September 2013University of Virginia cs4414 4 Number of Changes per Hour “Since the beginning of the git era (the 2.6.11 release in 2005), a total of 9,784 developers have contributed to the Linux kernel; those developers worked for a minimum of 1,064 companies.”
  • 6.
    Top Companies 24 September2013 University of Virginia cs4414 5
  • 7.
    24 September 2013University of Virginia cs4414 6 2011-07-21 2013-06-30
  • 8.
    24 September 2013University of Virginia cs4414 7 http://www.vidarholen.net/contents/wordcount/
  • 9.
    Centralized Version Control 24September 2013 University of Virginia cs4414 8 Repository (cvs, subversion) Alice: gash> svn checkout gash> svn update [make changes] gash> svn commit Bob: gash> svn checkout gash> svn update [make changes] gash> svn commit
  • 10.
    Distributed Version Control 24 September 2013University of Virginia cs4414 9 Main Repository (git, Mercurial) [make changes] Alice’s Local Repository commit [make changes] Bob’s Local Repository commitupdate update
  • 11.
    24 September 2013University of Virginia cs4414 10 Main Repository (Hg) [make changes] Alice’s Local Repository update, commit [make changes] Bob’s Local Repository update, commit Repository (svn) [make changes] [make changes] Centralized: One Repo Distributed
  • 12.
    What has tohappen before Bob sees Alice’s changes? 24 September 2013 University of Virginia cs4414 11 Main Repository (Hg) changed gash.rs Alice’s Local Repository commit update Bob’s Local Repository Alice Bob see gash.rs
  • 13.
    git pull =“pull” and “update” 24 September 2013 University of Virginia cs4414 12 Main Repository (git) changed gash.rs Alice’s Local Repository commit Bob’s Local Repository Alice Bob see gash.rs (I find this asymmetrical and confusing…but not many scenarios where pulling to local without updating is useful.) pull is not the analog of push – it is analog of commit + push
  • 14.
    What if Bobhad modified his copy? 24 September 2013 University of Virginia cs4414 13 Main Repository (git) changed zhttpto.rs Alice’s Local Repository commit Bob’s Local Repository Alice Bob changed zhttpto.rs gash> git pull … error: Your local changes to the following files would be overwritten by merge: ps1/zhttpto.rs Please, commit your changes or stash them before you can merge. Aborting
  • 15.
    Okay, let’s commit: 24September 2013 University of Virginia cs4414 14 Main Repository (git) changed gash.rs Alice’s Local Repository commit Bob’s Local Repository Alice Bob changed gash.rs gash> git commit -a –m "Added a comment about lack of security." [master 1347c1f] Fixed the notorious zombie process bug. 1 files changed, 3 insertions(+), 0 deletions(-) gash> git pull Auto-merging ps1/zhttpto.rs CONFLICT (content): Merge conflict in ps1/zhttpto.rs Automatic merge failed; fix conflicts and then commit the result.
  • 16.
    Observing Conflicts 24 September2013 University of Virginia cs4414 15 // // zhttpto.rs // // Reference solution for PS1 // // Special thanks to Kiet Tran for providing code we incorporated into this. // <<<<<<< HEAD // Note: it would be very unwise to run this server on a machine that is // on the Internet and contains any sensitive files! ======= // Warning: this is not a secure server! >>>>>>> faf7829d3ab38459172b622351d68ac1f47bddd0 // // University of Virginia - cs4414 Fall 2013
  • 17.
    Resolving Conflicts (forLuddites) 24 September 2013 University of Virginia cs4414 16 gash> emacs zhttpto.rs edit conflicted file manually (removing the <<<< and ====) gash> git commit -a -m "Updated security message." [master 1e6e684] Updated security message. gash> git push … To https://github.com/cs4414/Reference-Solutions.git faf7829..1e6e684 master -> master
  • 18.
    Resolving Conflicts (forModerns) 24 September 2013 University of Virginia cs4414 17 git mergetool
  • 19.
    Avoiding Conflicts 24 September2013 University of Virginia cs4414 18 It’s easier to ask forgiveness than it is to get permission. Admiral Grace HopperWith conflicts, it is better to avoid them than to resolve them! - pull before you start modifying, and often while working - commit early and often, use good messages - push whenever you have something worth sharing (but don’t push junk) - divide your project into small, coherent files - communicate well with your teammates!
  • 20.
    Avoiding Major Conflictswith Teammates 24 September 2013 University of Virginia cs4414 19 Don’t resolve conflicts by just undoing others’ work! At least make sure you understand it before replacing their changes with your own.
  • 21.
    24 September 2013University of Virginia cs4414 20 What’s more important for getting an interesting computing job?
  • 22.
    Impressive Transcript from PrestigiousInstitution Impressive Code and Record in Hacker Communities 24 September 2013 University of Virginia cs4414 21
  • 23.
    Linked Lists inRust 24 September 2013 University of Virginia cs4414 22
  • 24.
    What’s a List? 24September 2013 University of Virginia cs4414 23
  • 25.
    Linked Lists A Listis an object that is either: Null (a special value representing empty list) or a pair whose second part is a List 24 September 2013 University of Virginia cs4414 24 struct Node { head : int, tail : Option<@Node> } type List = Option<@Node> ; Keeping things simple for now! @ = automatically managed
  • 26.
    24 September 2013University of Virginia cs4414 25 let p: List = Some(@Node{head: 1, tail: Some(@Node{head : 2, tail: Some(@Node{head: 3, tail: None})})}); struct Node { head : int, tail : Option<@Node> } type List = Option<@Node> ;
  • 27.
    to_str 24 September 2013University of Virginia cs4414 26 struct Node { head : int, tail : Option<@Node> } type List = Option<@Node> ;
  • 28.
    24 September 2013University of Virginia cs4414 27 fn to_str(lst: Option<@Node>) -> ~str { fn elements_to_str(n: @Node) -> ~str { match (n.tail) { None => fmt!("%?", n.head), Some(tail) => fmt!("%?, %s", n.head, elements_to_str(tail)) } } match(lst) { None => ~"Null", Some(n) => fmt!("[%s]", elements_to_str(n)) } }
  • 29.
    Using Traits 24 September2013 University of Virginia cs4414 28 trait ToStr { fn to_str (&self) -> ~str; } (ToStr is part of the core) Similar to interface in Java (except Rust traits can include default implementations).
  • 30.
    24 September 2013University of Virginia cs4414 29 impl ToStr for List { fn to_str(&self) -> ~str { fn elements_to_str(n: @Node) -> ~str { match (n.tail) { None => fmt!("%?", n.head), Some(tail) => fmt!("%?, %s", n.head, elements_to_str(tail)) } } match(*self) { None => ~"Null", Some(n) => fmt!("[%s]", elements_to_str(n)) } } } fn main() { let lst : List = Some(@Node{head: 1, tail: Some(@Node{head : 2, tail: Some(@Node{head: 3, tail: None})})}); println(fmt!("%s", lst.to_str())); }
  • 31.
    Using our List(?) 24 September 2013 University of Virginia cs4414 30 fn main() { let lst : List = Some(@Node{head: 1, tail: Some(@Node{head : 2, tail: Some(@Node{head: 3, tail: None})})}); println(lst.to_str()); lst.head = 0; println(lst.to_str()); }
  • 32.
    Making it mutable 24September 2013 University of Virginia cs4414 31 struct Node { head : int, tail : Option<@Node> } type List = Option<@Node> ; Since Rust 0.6 – can’t make struct fields mut struct Node { head : int, tail : Option<@mut Node> } type List = Option<@mut Node> ;
  • 33.
    24 September 2013University of Virginia cs4414 32 fn main() { let lst : List = Some(@mut Node{head: 1, tail: Some(@mut Node{head : 2, tail: Some(@mut Node{head: 3, tail: None})})}); println(lst.to_str()); match lst { None => fail!("Unexpected None!"), Some(n) => n.head = 0 } println(lst.to_str()); }
  • 34.
    Increment All 24 September2013 University of Virginia cs4414 33 Write a List method that increments the value of every element of the list.
  • 35.
    Increment All 24 September2013 University of Virginia cs4414 34 trait Increment { fn incr(&self); } impl Increment for List { fn incr(&self) { let mut current = *self; loop { match(current) { None => break, Some(node) => { node.head += 1; current = node.tail }, } } } }
  • 36.
    Mapping 24 September 2013University of Virginia cs4414 35 self.mapr(|x: int| { x + 1 }) Define a higher-order mapr method that applies a function to all elements in a List.
  • 37.
    24 September 2013University of Virginia cs4414 36 impl Map for List { fn mapr(&self, f: &fn(int) -> int) { let mut current = *self; loop { match(current) { None => break, Some(node) => { node.head = f(node.head); current = node.tail }, } } } }
  • 38.
    Don’t we wantto avoid @? 24 September 2013 University of Virginia cs4414 37
  • 39.
    24 September 2013University of Virginia cs4414 38 struct Node { head : int, tail : Option<~Node> } type List = Option<~Node> ; What else needs to change to make a List with owned Nodes?
  • 40.
    24 September 2013University of Virginia cs4414 39 struct Node { head : int, tail : Option<~Node> } type List = Option<~Node> ; trait Map { fn mapr(&self, &fn(int) -> int) } -> List;
  • 41.
    24 September 2013University of Virginia cs4414 40 struct Node { head : int, tail : Option<~Node> } type List = Option<~Node> ; trait Map { fn mapr(&self, &fn(int) -> int) -> List; } impl Map for List { fn mapr(&self, f: &fn(int) -> int) -> List { match(*self) { None => None, Some(ref node) => { Some(~Node{ head: f(node.head), tail: node.tail.mapr(f) }) }, } } } Is this better or worse than the @mut version?
  • 42.
    Next class: makingmap multi-threaded! Read the MapReduce paper (or at least the slides) before Thursday’s class 24 September 2013 University of Virginia cs4414 41 Posted notes (later today) will have all code. I won’t be able to hold my usual office hours this afternoon, but can meet after class today and/or arrange another time.

Editor's Notes

  • #16 HEAD is your repository (master branch); second is the main repository you tried to pull from