• No results found

CodeHub. Best Practices. Date

N/A
N/A
Protected

Academic year: 2021

Share "CodeHub. Best Practices. Date"

Copied!
20
0
0

Loading.... (view fulltext now)

Full text

(1)

Best Practices

(2)

Contents

1 Git in CodeHub...1

1.1 Overview... 1

1.2 CodeHub Operations in the Cloud... 4

1.3 Local Development on Git...7

1.4 Git Workflows... 12

1.4.1 Overview... 12

1.4.2 Centralized Workflow... 13

1.4.3 Feature Branch Workflow... 14

1.4.4 GitFlow... 14

(3)

1

Git in CodeHub

1.1 Overview

1.2 CodeHub Operations in the Cloud 1.3 Local Development on Git

1.4 Git Workflows

1.1 Overview

Purpose

This document is intended to help developers who are interested in Git to better use Git and apply Git in the DevCloud practice.

Version Control Systems

Git is a distributed version control system (VCS). VCSs manage all code revisions during software development. They store and track changes to files, and records the development and maintenance of multiple versions. In fact, they can be used to manage any helpful documents apart from code files. VCSs are classified into centralized version control systems (CVCSs) and distributed version control systems (DVCSs).

Centralized Version Control Systems

A CVCS has a central server that contains all development data, and a number of clients that store snapshots of the files in the central server at one point. That means the change history of project files is kept only in the central server, but not on the clients. Therefore, developers must pull the latest version of files from the central server each time before starting their work.

(4)

The following figure shows some common CVCSs.

The advantages and disadvantages of CVCSs are listed below.

Table 1-1 Advantages and disadvantages of CVCSs

Advantages Disadvantages

● Easy to use.

● Granular permission control on the directory level.

● Large storage space is not required on the clients because they do not store the entire copy of the code repository.

● A highly stable network is required since developers must work online. ● If the server breaks down, the

development work is suspended. ● All data will be lost if the hard disk of

the central server is corrupted and no proper backup is kept.

(5)

Distributed Version Control Systems

In DVCSs, every client is a complete mirror of the code repository. All data, including the change history of project files, is stored on each client. In other words, there is not a central server in this distributed system. Some companies which use Git may call a computer as the "central server". However, that "central server" is in nature the same as other clients except for the fact that it is used to manage collaboration.

The following figure shows some common DVCSs.

(6)

Table 1-2 Advantages and disadvantages of DVCSs

Advantages Disadvantages

● Each client stores a complete copy of the code repository, including tags, branches, and version records.

● Offline commits enable easy cross-distance collaboration. ● Branches are cheap to create and

destroy, and fast to check out.

● High learning thresholds.

● Branches can be created only for the entire repository but not for

individual directories.

1.2 CodeHub Operations in the Cloud

Prerequisites

You have registered an account for CodeHub. The Git client has been installed.

A project has been created.

Cloud Repositories

CodeHub supports the creation, clone, and management of cloud repositories. You can manage branches, tags, repository members, and keys, and perform

operations on code, including committing, pulling, pushing, viewing, and online editing. For more details about cloud repositories, see CodeHub Overview.

Creating an Empty Repository

1. On the CodeHub homepage, click Create Directly.

(7)

3. View the created repository on the CodeHub homepage.

Setting the SSH Keys or HTTPS Password

SSH keys and HTTPS password are credentials for communication between a client and server. Set them first before you clone or push a repository on your computer.

Setting SSH Keys

SSH keys are used when a client communicates with CodeHub over the SSH protocol. If you have downloaded Git Bash for Windows and generated an SSH key pair in the process, skip this section.

1. Open the Git client (Git Bash or Linux CLI), enter the following command, and press Enter for three times.

ssh-keygen -t rsa -C "email address"

The generated SSH key pair is stored in ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub by default.

(8)

2. Add the SSH key to CodeHub.

a. Open the Git client (Git Bash or Linux CLI) and run the following command to print the SSH key in ~/.ssh/id_rsa.pub.

cat ~/.ssh/id_rsa.pub

b. Copy and add the SSH key to CodeHub.

i. Access the CodeHub homepage and choose Set SSH key > Add SSH

Key.

ii. Enter a key name, paste the copied key, and click OK.

The SSH key has been added. You can proceed to set an HTTPS password.

(9)

Setting an HTTPS Password

An HTTPS password is used when a client communicates with CodeHub over the HTTPS protocol. To set an HTTPS password, perform the following steps:

1. Access the CodeHub homepage and click Set HTTPS Password.

a. If you are setting the HTTPS password for the first time, enter the HTTPS password twice, and click OK.

b. Otherwise, you can reset the password. Click Change, enter the email verification code and the new password, and click OK.

1.3 Local Development on Git

Background

After creating a new repository with a README file in CodeHub, an architect or project manager pushes the architecture code to the repository. Other developers then clone the architecture code to their local computers for incremental

development.

● Git supports code transmission over SSH and HTTPS. The SSH protocol is used as an example.

● If you want to use the HTTPS protocol, download the HTTPS password, and enter the HTTPS username and password when cloning or pushing code.

● The SSH URL and HTTPS URL of the same repository are different.

Pushing Architecture Code

1. Open the architecture code on the local computer. Ensure that the name of the root directory (DevCloud) is the same as that of the code repository created in the cloud. Right-click in the root directory and choose Git Bash

(10)

2. Push local code to the cloud.

Run commands on Git Bash as instructed below.

a. Initialize a local code repository. After this command is executed, a .git directory is generated in D:/code/DevCloud/.

$ git init

b. Associate the local repository with the one in the cloud. $ git remote add origin CodeHubUrl

The value of CodeHubUrl can be obtained by clicking SSH of the cloud repository on the CodeHub homepage.

c. Push code to the cloud repository. $ git add .

$ git commit -m "init project"

$ git branch --set-upstream-to=origin/master master $ git pull --rebase

$ git push

Cloning Code

(11)

1. In the directory where you want to clone the code, right click and choose Git

Bash Here.

2. Run the following command to clone the repository. The value of

CodeHubUrl can be obtained by clicking SSH of the cloud repository on the

CodeHub homepage.

$git clone CodeHubUrl //Clone the code from the remote repository to the local computer.

Committing Code

A change travels from the working directory, stage, and local repository before being pushed to the remote repository.

(12)

The following commands are involved:

1. #git add/rm filename//Add changes from the working directory to the stage after creating, editing, or deleting files.

2. #git commit –m "commit message" //Commit the files from the stage to the local repository.

3. #git push //Push the files from the local repository to the remote one.

Branch Operations

● Create a branch.

In Git, creating a branch is not to copy a repository, but to create a HEAD, a movable pointer pointing to the last commit. A branch in nature is a file that contains the 40-byte SHA-1 checksum of the commit it points to.

#git branch branchName commitID

A new branch is pulled based on the specified commit ID. If no commit ID is specified, the branch is pulled from the commit that HEAD points to.

(13)

● Check out a branch.

Run the following command: #git checkout branchName

For example, to check out the feature branch, run git checkout feature.

● Integrate branches.

There are two ways to integrate changes from one branch to another: git merge and git rebase. The following describes the differences between them. Assume that C4 and C3 are added to the master branch and hotfix branch respectively. The hotfix branch is now ready to be integrated to the master branch.

a. Three-way merge integrates C3, C4, and their most recent common ancestor C2. Merging is simple to operate, but a new commit C5 is created, resulting in a less readable commit history.

#git checkout master #git merge hotfix

b. Git rebase saves the changes introduced to C4 as a patch in the .git/

(14)

● Resolve conflicts.

a. Scenario 1: The same line of code is changed in both the two branches to merge.

Solution

i. Manually merge the change that you think is proper. ii. Commit the change.

b. Scenario 2: A file is renamed in two different ways.

Solution

i. Check which name is correct and delete the incorrect one. ii. Commit the change.

1.4 Git Workflows

1.4.1 Overview

Create a Git workflow or branching policy that works best on your development scenarios for effective version control, project process management, and team collaboration.

There are four common Git workflows. The following sections describe their processes, advantages, disadvantages, and some usage tips.

(15)

● Feature branch workflow

● GitFlow (recommended by DevCloud) ● Forking workflow

Development teams can integrate CodeHub and the workflow that suits them best to efficiently manage code and ensure code security. This enables them to focus more on the service development to achieve continuous integration and delivery, and fast iteration.

1.4.2 Centralized Workflow

The centralized workflow is suited to a development team that comprises around 5 members or has just migrated from SVN to Git. There is only one main branch called master by default (trunk in SVN), which is the single entry point of changes. However, this workflow is not recommended for teams who want to enjoy the benefits of Git and team collaboration.

Process

Developers clone the master branch from the central repository to their local computers, make changes to the code, and push changes to the remote master branch.

Advantages

No branch interaction is involved.

Disadvantages

● Merge conflicts are frequent when the size of a development team is more than 10 members. Much time is spent on conflict resolution.

● The master branch is unstable due to frequent pushes to it, making it difficult to conduct integration tests.

Tips: Avoiding Conflicts and Unreadable Commit History

Before developing a new feature, developers must synchronize the local repository to the central one so that they can work on the latest version. After the

development is complete, fetch updates from the central repository before rebasing their own commits. In this way, the commits are applied on top of changes that have been made and pushed to the central repository by other developers. The commit history is linear and clear. The following figure shows an example of the workflow.

(16)

1. Developers A and B pull code from the central repository at the same time. 2. Developer A completes the work and pushes it to the central repository. 3. When ready to push commits, developer B needs to first run git pull –rebase

to apply commits on top of the changes made by developer A. 4. Developer B pushes the code to the central repository.

1.4.3 Feature Branch Workflow

The core of the feature branch workflow is that every feature should be developed on a separate branch pulled off the master branch. This creates a work silo for every developer, ensures a stable master branch, and encourages team

collaboration.

Process

Before developing a new feature, each developer should pull a new branch from the master branch and give it a descriptive name, for example, video-output or

issue-#1061, to clearly state its purpose. By pushing local feature branches to the

central repository, developers can share their code with each other without merging code into the master branch.

Advantages

● Developers can create merge requests to have their code reviewed before merge.

● Pushes to the master branch are less frequent.

Disadvantages

Only the master branch is used to incorporate changes. The instability of the branch is further increased in large-scale development projects.

1.4.4 GitFlow

GitFlow is commonly seen in large-scale development projects. Each branch is dedicated to a specific purpose and policies are made to regulate the interaction between branches. The following figure shows the process of GitFlow.

(17)

Process

Master branch

The master branch is the production branch where code is ready to deploy. It is the most stable branch because changes cannot be directly pushed to it. Developers can only merge other branches to the master branch. It is often set as a protected branch by default, on which only the project maintainer can operate.

Hotfix branch

It is a temporary branch created off the master branch for fixing urgent bugs in a live production version. After the bug is fixed, the hotfix branch gets merged into the master branch and tagged with a version number. The bug fix also needs to be merged to the develop branch.

Develop branch

A develop branch is pulled from the master branch and used to merge features. It contains all the code ready to release for integration and system testing.

Release branch

When a new release is coming up, developers create a release branch from the develop branch for release preparations, such as fixing minor bugs and producing documents. Adding new features is not allowed. They should be merged into the develop branch and wait for the next release. When the preparation is complete, the release branch is merged into the master branch and the commit is tagged with a version number. The changes made in the release branch also need to be merged to the develop branch.

(18)

Developers add new features in either of the following ways:

● Incorporate features after the features are reviewed by a dedicated approver. a. Developers push feature branches to the central repository in CodeHub. b. Developers then create merge requests for merging the feature branches

into the develop branch, and assign the requests to the approver.

CodeHub supports merge requests. Only repository administrators (project managers, repository creators, and developers granted with repository management permissions) can accept merge requests.

c. The approver reviews the merge requests. If the requests are approved, the feature branches are merged into the develop branch and deleted. Otherwise, the approver should explain the reasons of rejections. ● Integrate features after self-reviews.

a. Developers merge feature branches to the develop branch in the local repository and delete the feature branches.

b. The local develop branch is then pushed to the central repository in CodeHub.

Advantages

● With a branch dedicated for release preparation, a development team can develop new features for a future release on the develop branch while improving the version for the upcoming release. Release is visualized, which means team members can have a clear view of the release status in commit graphs.

● Hotfix branches, which can be seen as temporary release branches created off the master branch, enable development teams to fix urgent bugs without interrupting other works. Bug fixes do not have to wait until next release but can be quickly deployed to the production version.

● Effective multi-branch mechanism allows for organized development process especially for large-scale projects.

● This workflow is more in line with the DevOps philosophies.

Disadvantages

● High learning thresholds.

● Impact will be greater if development teams do not comply with their specified workflow policies.

1.4.5 Forking Workflow

The forking workflow is suitable for outsourcing, crowdsourcing, crowdfunding, and open source projects. One of the features that distinguish this workflow is that every contracting developer has a personal public repository, which is forked from the project public repository. Developers can perform operations on the forks without the need of being authorized by the project maintainer. The following figure shows the process of the forking workflow.

(19)
(20)

2. The personal public repositories are cloned to their local computers for development.

3. After the development is complete, developers push changes to their personal public repositories.

4. Developers file merge requests to the project maintainer for merge to the project public repository.

5. The project maintainer pulls changes to the local computer and reviews the code. If the code is approved, it is pushed to the project public repository.

If the code written by a developer is not approved and therefore, not merged to the project public repository, other developers can still pull the code from the personal public

repository of the developer for references.

Advantages

● Code collaboration is easier. Developers can share their code by pushing it to their personal public repositories for others to pull, unlike some workflows where developers cannot see others' work until it is merged into the project repository.

● Project maintainers do not have to grant permissions on project public repositories to every contributor.

● Merge requests serve as an important guard for code security.

● The three workflows introduced previously can be incorporated into the forking workflow based on project requirements.

Disadvantages

It takes more steps and time before the code of developers gets merged into the project repository.

References

Related documents

There are five different stakeholder groups involved in this steering committee – the iNGO (the final decision maker), the national government, a national NGO, a

Single engine the auto thrust is armed, with the most forward thrust lever above the FLX/MCT detent, But not in the TOGA detent...

patient-oriented brochure that provided examples of online postings and experience (internal motivation), or (3) a physician letter of recommendation (external motivation)

Three extensions to the current work include extending the Steve Lewis case in chapter 4 as a complex ANP model that include economic, social, political and religious clusters,

The aim of the thesis is to log real world sensory data from multiple sensors on board mobile devices and develop suitable algorithms to extract information from the data to solve

The PROMs questionnaire used in the national programme, contains several elements; the EQ-5D measure, which forms the basis for all individual procedure

In this context, a major aspect of Katib C ¸ elebi’s work is his interest in the world outside the Islamic oecoumene (on his biography see Collective work, 1957; for a selection of

Within the given constraints, the set of allowed currencies and the exposure limits, O&A can help to identify an optimal trading model portfolio with a high yearly return,