Source code management

From FreeCAD Documentation
Revision as of 09:49, 29 October 2017 by FabArd (talk | contribs)
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Our main source code management tool is git. This article explains how to use it and how some general rules apply in the case of FreeCAD. You are highly advised to learn how git works first (there are a lot of tutorials and docs available for git on the internet) before working with the FreeCAD source code.

There are also many good graphical clients to git, such as git-cola, that make the whole process of managing git repositories easier. FYI there also exists a cursory intro to Developing FreeCAD with GitKraken.

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, but if you make a change that you wish to see included in the official source code, you need to ask for a pull request on the pull requests section of the FreeCAD forum.

NOTE

In all examples below, "GITHUB_USERNAME" represents your GitHub user account.


Official GitHub Repo

An easy way to start with the FreeCAD source code is using the official FreeCAD repository at https://github.com/FreeCAD/FreeCAD

Setting your git username

Users should commit to their project 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"

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

A note about Remote branches

Please read some background to help you understand better the difference between what origin and upstream mean in the context of git. This section explains how to set the correct upstream and origin remote git repos. Essentially:

This is important to understand because if you git clone directly from upstream then confusingly, your origin will be listed as https://github.com/FreeCAD/FreeCAD.git So, based on the above, there are 2 main ways to setup your git environment:

We recommend the 1st method for the reason mentioned above.

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

Important Note

You will need to re-configure 'remote upstream' as mentioned above in "A note about Remotes"

This method is the recommended way since it takes less steps. You will essentially fork the FreeCAD repo on your own GitHub account and then clone said GitHub fork locally. Then you will set your upstream repo in git. The procedure is as follows:

  1. Sign up for a GitHub account if you don't already have one
  2. Go to the FreeCAD repo: https://github.com/FreeCAD/FreeCAD
  3. In the top right of the page find and press the "Fork" button (this will essentially git clone the official FreeCAD repo to your personal GitHub repo: https://github.com/GITHUB_USERNAME/FreeCAD.git)
  4. On your machine, clone your newly created FreeCAD fork by opening a terminal and typing:
    git clone https://github.com/GITHUB_USERNAME/FreeCAD.git
  5. Once the clone process is complete, now set your upstream remote repo (see "A note about Remotes"). Find out what and where your remote git repositories are set to. Type git remote -v and the output should look similiar to:
     [foo@bar FreeCAD]$ git remote -v
         origin	https://github.com/GITHUB_USERNAME/FreeCAD.git (fetch)
         origin	https://github.com/GITHUB_USERNAME/FreeCAD.git (push)
  6. Great. Now set your upstream repo
    [foo@bar FreeCAD]$ git remote add upstream https://github.com/FreeCAD/FreeCAD.git
  7. Check your remotes again, they should look similiar to this:
     [foo@bar FreeCAD]$ git remote -v
        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)
  8. Now we can start developing. Please refer to "Git Development Process"

2nd Method: Clone Official FreeCAD git repo to your local machine

Important Note

You will need to re-configure both 'remote origin' and 'remote upstream' please refer to above "A note about Remotes"

This method of setuping your git environment takes a few more steps then the 1st method. You will clone the FC git repo directly to your local machine and then alter your remotes via the terminal. The procedure is as follows:

  1. Clone the FreeCAD code with git:
    git clone https://github.com/FreeCAD/FreeCAD.git
  2. Create an account on a public git server (GitHub, GitLab, etc... for our purposes we're assuming it's GitHub)
  3. Find out what and where your remote git repositories are set to:
    git remote -v
  4. This will return something that looks like the following:
    [foo@bar FreeCAD]$ git remote -v
        origin https://github.com/FreeCAD/FreeCAD.git (fetch)
        origin https://github.com/FreeCAD/FreeCAD.git (push)
  5. As was explained above in "A note about Remotes" you need to modify these remote git repo addresses.
    So first set up your origin remote:
    [foo@bar FreeCAD]$ git remote add origin https://github.com/GITHUB_USERNAME/FreeCAD.git
  6. Then we set up our upstream remote:
    [foo@bar FreeCAD]$ git remote add upstream https://github.com/FreeCAD/FreeCAD.git
  7. Check your remotes again, they should look similiar to this:
     [foo@bar FreeCAD]$ git remote -v
        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)
  8. Now we can start developing. Please refer to "Git Development Process"

Git Development Process

First of all 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

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

  • 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 the plaintext 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

Apply a patch via curl

curl https://github.com/FreeCAD/FreeCAD/commit/c476589652a0f67b544735740e20ff702e8d0621.patch | git apply -

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