Source code management: Difference between revisions

From FreeCAD Documentation
(Removed unnecessary and repeated text. Simplified the wording. Removed manual HTML tags. Keep it simple.)
m (Quotes)
Line 21: Line 21:
</translate>
</translate>
{{Code|code=
{{Code|code=
git config user.name YOUR_NAME
git config user.name "YOUR_NAME"
git config user.email GITHUB_USERNAME@users.noreply.github.com
git config user.email GITHUB_USERNAME@users.noreply.github.com
}}
}}
Line 27: Line 27:


<!--T:37-->
<!--T:37-->
Where {{incode|YOUR_NAME}} represents your full name, or nickname, to identify the author of a particular commit, and {{incode|GITHUB_USERNAME}} indicates your GitHub username.
Where {{incode|"YOUR_NAME"}} represents your full name or nickname, used to identify the author of a particular commit, and {{incode|GITHUB_USERNAME}} indicates your GitHub username.


<!--T:19-->
<!--T:19-->

Revision as of 02:56, 23 July 2019

The main source code management tool for the FreeCAD project is Git, which can be easily installed in most operating systems from a package manager or directly from Git's website. You are advised to become familiar with Git before working with the FreeCAD source code directly. Visit the Git documentation page for the reference manual as well as tutorials to learn to use the system. The present article explains how to use Git with FreeCAD.

While Git is primarily a terminal application, there are many graphical clients for it which facilitate working with branches, applying patches, and submitting pull requests to a master branch. Examples include git-cola, gitg, and GitKraken. Please see Developing FreeCAD with GitKraken for a cursory introduction to this tool.

Source code access

Everybody can access and get a copy of the FreeCAD source code, but only the FreeCAD project managers have write access to it. You can get a copy of the code, study it and modify it as you wish. If you want your changes to be included in the official source code, you need to perform a pull request against the master repository so that your modifications can be reviewed by one of the managers.

If your source code changes are significant, you are advised to explain them in the pull request section of the FreeCAD forum.

Official GitHub repository

The FreeCAD source code is hosted in Github, https://github.com/FreeCAD/FreeCAD

In order to contribute code, you need to have a Github account.

Setting your git username

Developers should commit code to their personal repository using their GitHub username. If that is not already set globally, you can set it locally for the current Git repository like this:

git config user.name "YOUR_NAME"
git config user.email GITHUB_USERNAME@users.noreply.github.com

Where "YOUR_NAME" represents your full name or nickname, used to identify the author of a particular commit, and GITHUB_USERNAME indicates your GitHub username.

You can now use some combination of git add and git commit to create one or more commits in your local repository.

Remote repositories

Please read What is the difference between origin and upstream on GitHub? (Stackoverflow) to help you understand the difference between origin and upstream in the context of Git. This section explains how to set the correct repositories for development. Essentially:

This distinction is important, as you should push code to your own copy of the repository first, before pushing those changes to the official repository. If you clone from the upstream repository, both the upstream and origin repositories will point to the same address, the official repository. If you accidentally did this, you can use git remote rename to remedy the situation.

Based on the above, there are two main ways to setup your Git development environment:

We recommend the 1st method because it is better to work on a personal copy first, and later merge the changes to the official repository. This avoids accidentally committing untested code to the official repository.

1st Method: Fork on GitHub and clone your fork locally

First you will fork the FreeCAD repository in GitHub, then clone this personal fork to your computer, and finally set the upstream repository.

git clone https://github.com/GITHUB_USERNAME/FreeCAD.git freecad-source
  • Once the download is complete, enter the new source directory and set the upstream repository.
cd  freecad-source
git remote add upstream https://github.com/FreeCAD/FreeCAD.git
  • Confirm your remote repositories with git remote -v; the output should be similar to this
origin	https://github.com/GITHUB_USERNAME/FreeCAD.git (fetch)
origin	https://github.com/GITHUB_USERNAME/FreeCAD.git (push)
upstream	https://github.com/FreeCAD/FreeCAD.git (fetch)
upstream	https://github.com/FreeCAD/FreeCAD.git (push)

2nd Method: Clone FreeCAD directly to your local machine

You will clone the FreeCAD repository to your local machine, and then alter your remotes via the terminal.

  • Clone the FreeCAD source code. It will be created inside a directory freecad-source.
git clone https://github.com/FreeCAD/FreeCAD.git freecad-source
  • Once the download is complete, enter the new source directory and set the origin repository.
cd freecad-source
git remote add origin https://github.com/GITHUB_USERNAME/FreeCAD.git
  • Then set up the upstream repository.
git remote add upstream https://github.com/FreeCAD/FreeCAD.git
  • Confirm your remote repositories with git remote -v; the output should be similar to this
origin	https://github.com/GITHUB_USERNAME/FreeCAD.git (fetch)
origin	https://github.com/GITHUB_USERNAME/FreeCAD.git (push)
upstream	https://github.com/FreeCAD/FreeCAD.git (fetch)
upstream	https://github.com/FreeCAD/FreeCAD.git (push)

Git Development Process

NEVER DEVELOP ON THE master BRANCH! Instead, create a local branch for development. You can learn in more depth by reading Git-Branching-Basic-Branching-and-Merging chapter on git-scm. Below is a summary:

Branching

An important feature of Git is that it is extremely easy to work with branches and merge them together. Best practices recommend to create a new branch whenever you want to work on a new feature. Creating a branch is done with:

git branch myNewBranch
git checkout myNewBranch

or you can combine both git branch && git checkout together by using the -b flag

git checkout -b myNewBranch

How do you know which branch you are currently using? Easy, type:

git branch

Committing

Once you did some work, you commit them with:

git commit -a

Unlike SVN, you need to specifically tell which files to commit (or all with the -a option). Your text editor will open to allow you to write a commit message.
Please read more about Writing good commit messages in the below section.

Publishing your work on your GitHub repository

Important Note

If you have code you wish to see merged into the FreeCAD source code, please post a note in the Pull Request section of the FreeCAD forum

After you're correctly branched made some modifications to your local branch and commit them 'locally', you can push your repository to your remote git server (in this example we're assuming GitHub). This opens your branch to the public and allows the main developers to review and integrate your branch into master.

git push <REMOTENAME> <BRANCHNAME> 
git push origin my-branch

For further info on this subject please read https://help.github.com/articles/pushing-to-a-remote/

Writing good commit messages

You should try to work in small chunks. If you cannot summarize your changes in one sentence, then it has probably been too long since you have made a commit. It is also important that you have helpful and useful descriptions of your work. For commit messages, FreeCAD has adopted a format mentioned in book Pro Git (see #Further Reading).

Short (50 chars or less) summary of changes

More detailed explanatory text, if necessary.  Wrap it to about 72
characters or so.  In some contexts, the first line is treated as the
subject of an email and the rest of the text as the body.  The blank
line separating the summary from the body is critical (unless you omit
the body entirely); tools like rebase can get confused if you run the
two together.

Further paragraphs come after blank lines. 

 - Bullet points are okay, too

 - Typically a hyphen or asterisk is used for the bullet, preceded by a
   single space, with blank lines in between, but conventions vary here

If you are doing a lot of related work, it has been suggested here that one should make as many commits large or small as makes sense for what you are working on using the short one sentence commit messages. When you want to merge, do a

git log master..BRANCH

and use the output as a basis for your quality commit message. Then when you merge to master use the --squash option and commit with your quality commit message. This will allow you to be very liberal with your commits and help to provide a good level of detail in commit messages without so many distinct descriptions.

Advanced git operations

Check out GitHub Requests Locally

https://gist.github.com/piscisaureus/3342247

Resolving Merge Conflicts

Applying patches via git

Git has the capability to merge patches/diffs. To read more about this read the following reference: https://www.drupal.org/node/1399218

git apply [patch-name].patch

Apply a patch via curl

A very handy and powerful feature of git and the command line is the ability to quickly test patches all through the terminal.

curl -O https://github.com/FreeCAD/FreeCAD/commit/[patch-name].patch
git apply [patch-name].patch

or alternatively, using bash pipes you can make a sweet 1-liner:

curl https://github.com/FreeCAD/FreeCAD/commit/c476589652a0f67b544735740e20ff702e8d0621.patch | git apply -
  • Useful tip: Just add .diff or .patch at the end of the URL for a GitHub commit page, Pull Request, or Compare View and it'll show you theplaintext view of that page. Example:
Regular GitHub page: https://github.com/FreeCAD/FreeCAD/commit/c476589652a0f67b544735740e20ff702e8d0621
'Diffed' GitHub page: https://github.com/FreeCAD/FreeCAD/commit/c476589652a0f67b544735740e20ff702e8d0621.diff
'Patched' GitHub page: https://github.com/FreeCAD/FreeCAD/commit/c476589652a0f67b544735740e20ff702e8d0621.patch

Creating patches from git

There are times when one may need to create a patch instead of submitting a PR. The following workflow explains how to do this:

  1. Make sure you are in the correct branch (i.e. not the master branch) by checking with
    git branch -v
  2. Create the patch: we do this by using the git format-patch command which we patch against the master branch and redirect to STDOUT. We create the patch in the directory outside of the source build (in order not to pollute the source dir itself, this is optional as you can decide the location wherever you want the patch to be created)
    git format-patch master --stdout > ../patch.name.patch
  3. Another method is to use git format-patch HEAD^ or git format-patch HEAD~1 The ^ or 1 can be changed to number of commits that should be covered i.e.: ^^^ or ~3.
    git format-patch HEAD^
  4. It will create a patch or series of patches with file name format XXXX-commit-message.patch. An example: 0001-fix-ViewProjMatrix-getProjectionMatrix.patch

Reversing a patch in git

If you've followed the instructions above and then have a change of heart about using the patch, you can quickly reverse it using:

git apply -R path/file.patch

or another way is to use:

git checkout -f

which will remove non-committed changes to the branch

Stashing git commits

Say you're working on a branch and you find yourself making some modification to the source that is out of the scope of your current branch in other words it would be better to add certain commits to a whole other branch and submit it instead of the current one. This is where the git stash command can be very useful. The git stash command can help you to (temporarily but safely) store your uncommitted local changes.

git stash

Then in the future when you want to use these commits you can

git stash apply 

or

git stash pop

pop will delete the stash If you have multiple stashes you can

git stash list

To read more about what other functions you can use checkout https://medium.freecodecamp.org/useful-tricks-you-might-not-know-about-git-stash-e8a9490f0a1a

What is the latest FreeCAD Dev Revision

There are 2 ways to do this:

  • 1st method: In your cloned git directory type:
git rev-list --count master


What is the Revision number of a specific commit hash and visa ver?

git rev-list --count 9948ee4
13541

How is the Revision number in FreeCAD help about generated?

Alternative repositories

The beauty of git is that everybody can clone a project, and start modifying the code. Several frequent collaborators of the FreeCAD project have their own git repository, where they build up their work before it is ready to be included in the official source code, or simply where they experiment new ideas. In certain cases, you might want to clone your FreeCAD code from one of these, instead of the official repos, to benefit from the changes their users did.

Be warned, though, that this is at your own risk, and only the official repository above is fully guaranteed to work and contain clean code.

It is also possible to attach several remote repositories to a same local FreeCAD git code, using the "git remote" command. This is useful to keep in sync with the master code branch, but keep an eye on the work of different developers.

Using git in a Graphical User Interface

Further reading