Version Control Using Git

Download as pdf or txt
Download as pdf or txt
You are on page 1of 111

Version Control

using Git
Coding
• All the design and thinking get transformed to an
implementation.
• The main outcome is the code
• Software Engineer Vs Programmer
Need for Version Control
void drawPicture() {
drawCircle();
for (int i = 0; i < 10; i++) {
void drawPicture() {
drawTriangle();
drawCircle();
}
for (int i = 0; i < 100; i++)
} {
drawSquare();
}
}
void drawPicture() {
drawCircle();
for (int i = 0; i < 10; i++) {
drawTriangle();
}
}
Need for Version Control

Source:
https://geo-python-site.readthedocs.io/en/latest/lessons/L2/i
ntro-to-GitHub.html
Need for Version Control

Central Code Repository


Git
• Most widely used version control system
• Developed by Linus Torvalds in 2005
• Git is an example of Distributed Version Control System
(DVCS)
Setting up a new Repository
To create a new repository for your new project
Project path: /home/user/projects/sample_proj

cd /home/user/projects/sample_proj
git init

This will initialize git and a .git folder will get created inside the
project path
Setting up a new Repository: Config
Config is to get or set global properties for the repository

git config --global user.email "[email protected]"


git config --global user.name "Your Name"
Setting up a new Repository

sample-proj

test.c
Git status

To know the state of the files

git status
States of a file
Untracked: When a new file is created it is untracked

Staged: A staged file is ready to go into the local database during


the next commit

Unmodified/Commited: the file is safely stored in the local git


database.

Modified: file is has changes which are yet to be committed


Existing Repository
git clone <repo url>

Git supports a few different network protocols (http, ssh etc.)

git clone https://github.com/linux-test-project/ltp.git

After cloning, all the files in the repository are in


unmodified/committed state.
Staging the files

Staged files get committed in the next commit

git add
How to Commit?
• When a file is committed, it becomes a part of the local
repository.
• All the staged files get committed
git commit -m <commit message>

git commit -m “first commit”

git status

Source: https://dev.to/sublimegeek/git-staging-area-explained-like-im-five-1anh
Viewing commit history

git log

Shows commits made in the repository in the reverse


chronological order.
More on Commit
sample-proj

test.c

add_integer.c

git add add_integer.c


git commit -m “added add_integer.c to add 2 integers”

Modified test.c

git add test.c


git commit -m “Made a modification to test.c”
More on Commit
git log

C3

C2

C1
Undoing things
HEAD

C1 C2 C3

HEAD

C1 C2 C3
Undoing Things: go back to a previous
commit
git reset <commit id>
Reverts to the previous commit referred by the commit id

git reset 44329c79f226226bca9507b90d9ddfc5217f2fe2

git log

git reset HEAD~2


Undoing Things: go back to a previous
commit
git status

Soft reset: Staging index and working directory remain as it is.


Undoing Things: Hard Reset
git reset 44329c79f226226bca9507b90d9ddfc5217f2fe2 --hard

git status
Undoing Things: Revert
HEAD

C1 C2 C3

HEAD

C1 C2 C3 Reset

HEAD

C1 C2 C3 C4 Revert
Undoing Things: Revert
git revert 413cef719a54ceb372892d3ae13b1aa61c562f91
Removing files
git rm <path>
• This removes the file or folder from the tracked file.
• This should be followed by a commit.

git rm test.c
More Insights into Commit

Commit Object

Name HEAD
Email
Commit message
Pointer
C1 C2 C3
Staging snapshot
Branching
Master Branch
HEAD

C1 C2 C3
Why Branching?
Branching helps to diverge from the main line of development
without disturbing the main line.

C1 C2 C3 C4

C4_b C5_b
Create a new branch
git branch <branch name>

git branch

git branch test_branch

git branch
Switch to a branch
git checkout <branch name>

git checkout test_branch

git branch
Committing in a branch
Modified add_integer.c to add 3 integers instead of 2
git add add_integer.c
git commit -m "modified add_integer.c to add 3 integers"

git log
Committing to a branch: Visualization

C1 C2 C3

C4_b

HEAD Add 3 integers


Committing in a branch
git checkout master
git log
Committing to a branch
Changes to master branch

Let us make some modifications to the master branch HEAD

git checkout master


C1 C2 C3

Made some changes to test.c


C4_
b
git add test.c
git commit -m "some more modifications to test.c"
Committing to a branch
Changes to master branch
HEAD

Modifications
C1 C2 C3 C4 to test.c

C4_b

Add 3 integers
Git log: view all branches
git log --decorate --graph --all --oneline
Merging of branches
git merge <branch name>
HEAD
Modifications
C1 C2 C3 C4 to test.c

Add 3
C4 integers
_b
HEAD

C1 C2 C3 C4 C5

Add 3
integers
C4
_b Add 3
integers
Merging of branches
git checkout master
git merge test-branch

git log
Project Estimation
Project Planning → Project Estimation

● Project management starts with Project Planning


● Project Planning involves
○ Estimation
○ Scheduling
○ Risk Analysis
○ Quality Management Planning
○ Change Management Planning
Estimation
Estimation of

● Resources
● Cost
● Time
Estimation of Resources
Estimation of Cost and Time (Effort)
● Decomposition Technique

Decomposes a project into major functions and related software engineering


activities. This information is used to estimate the cost and effort.

● Empirical Estimation Models

These models are based on experience (historical data) which takes the form

where vi are independent parameters and d is one of the estimated values (eg.
effort, cost, project duration)
Decomposition: LOC Based Estimation
E-commerce website
Function Estimated LOC

User Interface 5000

Database Management 3000

Payment functionalities 2000

Estimated Lines of Code 10000

Based on historical information average productivity of the organization is 620 LOC/pm


Labour rate is $8000. i.e $13 per line
Total estimated project cost is $1,30,000 and effort is 17 person-months
Decomposition: Function Point (FP) based Estimation
Function Point Metrics
Number of user (external) inputs
Number of user (external) outputs
Number of user (external) inquiries
Number of files
Number of external interfaces
Decomposition: Function Point (FP) based Estimation
Function Point Metrics

Fi (i= 1 to 14) are value adjustment factors (VAF)

Based on the average productivity (FP/pm) of the organization, cost and effort are
estimated.

This estimation focusses on information values rather than software functions


Decomposition: Process based Estimation
The whole process is divided into set of small tasks and effort required to
accomplish each task is estimated

values in person-months
Empirical Estimation Models
● Empirically derived formulas
● Predict effort as a function of LOC or FP

Structure of such model


E : Effort in person-months
ev : estimation variable (LOC or FP)
A, B, C : empirically derived constants

● An empirical model must be calibrated to reflect local conditions


● Should be tried with completed projects.
Some Empirical Models
LOC oriented models

FP oriented models

Estimation models must calibrated for local needs


COCOMO Model
● COnstructive COst MOdel
● Proposed by Barry Boehm in 1981
● It helps to predict the effort and schedule (time required)
● Different COCOMO models are proposed to do cost estimation at different
levels
COCOMO Model: Project Categories
Organic: Good experience, small team, less complexity

Semi-detached: Experienced + freshers, team size larger than organic

Embedded: Experienced team, large team, high complexity


COCOMO Model: Formulation

Software Projects a b c d

Organic 2.4 1.05 2.5 0.38

Semi Detached 3.0 1.12 2.5 0.35

Embedded 3.6 1.20 2.5 0.32


Basic COCOMO Example
LOC = 30,000

Project type = Simple


KLOC= 30
Effort = 2.4 * (30)1.05 = 85 PM
Duration = 2.5 * (85)0.38 = 13.5 months

Avg. staffing: 85/13.5 = 6.3 persons


Other COCOMO Models
Basic COCOMO model assumes effort is a function only of LOC

Intermediate Model
Uses various other factors known as cost drivers. 15 cost drivers.
Eg. Size of the application database, complexity of the product, Programming language
experience

Detailed Model

Includes all characteristics of Intermediate model, with assessment of impact of


cost drivers in each step of software engineering process
Make/Buy Decision
Decision Tree Analysis
Project Scheduling
Why a software is delivered late?

● Unrealistic deadline
● Changing customer requirements
● Underestimation of effort and resources
● Risks which were not considered in the beginning
● Technical difficulties
● Human difficulties
● Miscommunication among staff
● Project management not recognising that the project is falling behind
Project Scheduling

Software project scheduling distributes the estimated effort across the


planned project duration by allocating the effort to specific software
engineering tasks

In the early stages macroscopic schedule is developed.


Relationship between People and Effort

Putnam-Norden-Rayleigh (PNR) curve


Program Evaluation Review Technique (PERT)
Chart
● Graphical representation of a project’s timeline that displays all of the
individual tasks to complete the project.

● Was first created by the US Navy in 1950s to guide the Polaris nuclear
submarine project.
PERT

Nodes: Tasks
Arrows: dependencies

Source: https://monday.com/blog/project-management/pert/
PERT: Critical Path

● The longest sequence of path in the PERT is the critical path

● The tasks in this path are critical because delay in the execution of these tasks
will delay the whole project.
PERT Analysis
Step 1
For PERT analysis 3 time estimates are obtained
● Optimistic Time (O): Minimum possible time required to complete a task.
● Pessimistic Time (P): Maximum possible time required to complete a task.
● Most Likely Time (M): Best estimate of time required to complete a task.
PERT Analysis

Step 2

Final estimate of time = (O + 4M + P)/6


When is PERT useful?

● When you want to know the amount of time to complete a project.


● When you want to set the critical path and essential paths for completing the
project in time.
● When your tasks are interdependent and complex.
Gantt Chart

● Is a timeline chart
● First Gantt chart was made in 1890s by a Polish engineer Karol Adamiecki who
ran a steel works.
● After around 15 years, an American engineer Henry Gantt made his own
version of the chart.
Gantt Chart

● X-axis shows time


● Y-axis is the set of activities
● Black bars: top level tasks
● White bars: sub tasks
● Diamonds: milestones- important deadline dates
Gantt Chart Example
Earned Value Chart

● Earned Value is the amount of work completed measured according to the


budgeted effort.
● Also known as the budgeted cost of the work performed
Earned Value Chart: Flight Analogy

Total distance: 2500 miles


You’ve flown 28% of the total distance, but you have
used only 25% of the fuel.

Source: https://www.tacticalprojectmanager.com/earned-value-analysis/
Earned Value Chart: Example
Earned Value Chart: Example

Source: https://dreamcivil.com/earned-value-analysis/
Earned Value Metrics
Actual Cost (AC): The amount of costs effectively incurred up until now.
Earned Value (EV): Earned Value (EV) is the value of the work that has been
effectively completed
Example: Imagine a project consisting of 3 activities. You have completed activities 1
and 2 so far. The planned cost for activity 1 is $2,500 and $1,000 for activity 2. You
have spent $3,700 up to now. Then the Earned Value for the project at the current
point in time is $2,500 + $1,000 = $3,500

SV (Schedule Variance): How far ahead or behind is the project? The SV is calculated
as the difference between Earned Value and Planned Value, meaning SV = EV – PV.
SPI (Schedule Performance Index): How far ahead or behind schedule is the project,
expressed as a ratio of the overall project duration. SPI = EV / PV.
An SPI of less than 1 indicates your project is behind schedule, whereas a value > 1
means you are ahead of schedule.
CPI (Cost Performance Index): How far above or below the budgeted cost the
project is in comparison with the total approved project budget. CPI = EV / AC
CV (Cost Variance): Looking at the project right now, how far under or over budget
is it? The Cost Variance is calculated as CV = EV – AC.
Design Patterns
Design Patterns
Someone has already solved your problems !!
Duck Simulator Example (source: Head First Design Patterns, 2nd ed.)

● This simulator is for showing large variety of ducks quacking, flying


etc.
● This is not for one type of duck. Many different variety of ducks
○ eg. Mallard duck, Redhead duck
Class Diagram: Design 1:
Design 1: Extensibility
Design 1: Flaw

Can’t fly Can Quack

Rubber duck
Design 2: Solution to Design 1 Flaw
Duck

quack()
swim()
display()
fly()
RubberDuck

display() {
//looks like a redhead
MallardDuck }
RedHeadDuck fly() {
//override to do
display() { nothing
//looks like a mallard display() { }
} //looks like a redhead quack() {
} //overridden to
squeak
}
Design 2: Flaw
● Overriding solution is not a cleaner way
● There will be issues with maintenance when there are modifications
in the future.
● Whenever a new Duck subclass is added all the required overriding
have to be done.
Design 3: Interfaces of Changing Behaviours
Design 3: Flaw
● No code reuse
● The fly method has to be implemented in all the subclasses even
for the Ducks with the same fly method.
● For most of the modifications, we will have to make the
modifications in multiple subclasses
The Final Solution
● The Duck subclasses will use interface for the behaviors
(FlyBehavior, QuackBehavior)
● The concrete behavior is coded in the class that implements the
above interfaces
● The actual behavior is not locked in the Duck subclasses
The Final Solution
Final Solution: Duck class
public abstract class Duck {
FlyBehavior flyBehavior;
QuackBehavior quackBehavior;
public Duck() { }
public abstract void display();
public void performFly() {
flyBehavior.fly();
}
public void performQuack() {
quackBehavior.quack();
}
}
public void swim() {
System.out.println("All ducks
float, even decoys!");
}
}
Final Solution: Mallard Duck
public class MallardDuck extends Duck {

public MallardDuck() {
quackBehavior = new Quack();
flyBehavior = new FlyWithWings();
}

public void display() {


System.out.println("I'm a real Mallard duck");
}
}
Main Code in Simulator
public class MiniDuckSimulator {
public static void main(String[] args) {
Duck mallard = new MallardDuck();
mallard.performQuack();
mallard.performFly();
}

https://github.com/bethrobson/Head-First-Design-Patterns/tree/master/src/headfirst/designpatterns/strate
gy
What we were discussing till now?

Strategy Design Pattern


Is a Behavioral design pattern in which enables selecting the algorithm
at runtime.

Source: https://en.wikipedia.org/wiki/Strategy_pattern
Strategy Design Pattern
The behaviors of a class should not be inherited. Instead, they should
be encapsulated using interfaces.
Types of Design Patterns
● Creational
Provides object or class creation mechanism
● Structural
assemble object and classes into a structure ensuring that the
structure should be flexible and efficient
● Behavioural
Manages how one class communicates with another
Creational Design Pattern
Factory Method Pattern
Separates out the creation of objects/instances.
Making Pizza Example
Pizza orderPizza(String type) {
Pizza orderPizza() {
Pizza pizza;
Pizza pizza = new Pizza();
if (type.equals("cheese")) {
pizza.prepare(); pizza = new CheesePizza();
pizza.bake(); } else if (type.equals("greek") {
pizza.cut(); pizza = new GreekPizza();
pizza.box(); } else if (type.equals("pepperoni") {
return pizza pizza = new PepperoniPizza();
} }

pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza
Instantiation of different
} classes for different types
of pizzas
There is pressure to add more pizza types !!

if (type.equals("cheese")) {
pizza = new CheesePizza();
} else if (type.equals("greek") {
pizza = new GreekPizza();
} else if (type.equals("pepperoni") {
pizza = new PepperoniPizza();
} else if (type.equals("clam") {
pizza = new ClamPizza();
} else if (type.equals("veggie") {
pizza = new VeggiePizza(); This keeps o g o n
} and some et mod fied
Encapsulating Object Creation
Pizza orderPizza(String type) { Pizza orderPizza(String type) {
Pizza pizza; Pizza pizza;

if (type.equals("cheese")) {
pizza = new CheesePizza();
} else if (type.equals("greek") {
pizza = new GreekPizza();
} else if (type.equals("pepperoni") {
pizza = new PepperoniPizza();
} pizza.prepare();
pizza.bake();
pizza.prepare(); pizza.cut();
pizza.bake(); pizza.box();
pizza.cut(); return pizza
pizza.box(); }
return pizza
}
Simple Pizza Factory
public class SimplePizzaFactory {
public Pizza createPizza(String type) {
Pizza pizza = null;

if (type.equals("cheese")) {
pizza = new CheesePizza();
} else if (type.equals("pepperoni")) {
pizza = new PepperoniPizza();
} else if (type.equals("clam")) {
pizza = new ClamPizza();
} else if (type.equals("veggie")) {
pizza = new VeggiePizza();
}
return pizza;
}
How does PizzaStore looks like?
Class Diagram of Pizza Store
Different Style Pizza
Pizza Class Diagram

New York Style

Cheese
Factory Classes for Different Styles
A Different Design to Manage Styles
Allow Subclasses to Decide
Product and Creator Classes
Another Example: Shapes

Source: https://www.tutorialspoint.com/design_pattern/factory_pattern.htm
Abstract Factory Pattern Factory Method pattern is
responsible for creating products
that belong to one family, while
Abstract Factory pattern deals with
multiple families of products.

You might also like