Home | History | Annotate | Download | only in source
      1 page.title=Developing
      2 @jd:body
      3 
      4 <!--
      5     Copyright 2013 The Android Open Source Project
      6 
      7     Licensed under the Apache License, Version 2.0 (the "License");
      8     you may not use this file except in compliance with the License.
      9     You may obtain a copy of the License at
     10 
     11         http://www.apache.org/licenses/LICENSE-2.0
     12 
     13     Unless required by applicable law or agreed to in writing, software
     14     distributed under the License is distributed on an "AS IS" BASIS,
     15     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     16     See the License for the specific language governing permissions and
     17     limitations under the License.
     18 -->
     19 <div id="qv-wrapper">
     20   <div id="qv">
     21     <h2>In this document</h2>
     22     <ol id="auto-toc">
     23     </ol>
     24   </div>
     25 </div>
     26 
     27 <p>To work with the Android code, you will need to use both Git and Repo.  In most situations, you can use Git instead of Repo, or mix Repo and Git commands to form complex commands. Using Repo for basic across-network operations will make your work much simpler, however.</p>
     28 <p><strong>Git</strong> is an open source version-control system designed to handle very large projects that are distributed over multiple repositories. In the context of Android, we use Git for local operations such as local branching, commits, diffs, and edits.  One of the challenges in setting up the Android project was figuring out how to best support the outside community--from the hobbyist community to large OEMs building mass-market consumer devices. We wanted components to be replaceable, and we wanted interesting components to be able to grow a life of their own outside of Android. We first chose a distributed revision control system, then further narrowed it down to Git.</p>
     29 <p><strong>Repo</strong> is a repository management tool that we built on top of Git. Repo
     30 unifies the many Git repositories when necessary, does the uploads to our
     31 <a href="https://android-review.googlesource.com/">revision control system</a>, and
     32 automates parts of the Android development workflow. Repo is not meant to
     33 replace Git, only to make it easier to work with Git in the context of
     34 Android. The repo command is an executable Python script that you can put
     35 anywhere in your path. In working with the Android source files, you will
     36 use Repo for across-network operations. For example, with a single Repo
     37 command you can download files from multiple repositories into your local
     38 working directory.</p>
     39 <p><strong>Gerrit</strong> is a web-based code review system for projects that use git. Gerrit encourages more centralized use of Git by allowing all authorized users to submit changes, which are automatically merged if they pass code review. In addition, Gerrit makes reviewing easier by displaying changes side by side in-browser and enabling inline comments. </p>
     40 <p><strong>Android Studio</strong> is the official integrated development environment (IDE) for Android application development. See the <a href="http://developer.android.com/tools/studio/index.html">Android Studio Overview</a> for details.
     41 
     42 <h2 id="basic-workflow">Basic Workflow</h2>
     43 <div class="figure" style="width:200px">
     44   <img src="{@docRoot}images/submit-patches-0.png" alt="basic workflow diagram" height="153px" />
     45   <p class="img-caption">
     46     <strong>Figure 1.</strong> Basic Android workflow
     47   </p>
     48 </div>
     49 
     50 <p>The basic pattern of interacting with the repositories is as follows:</p>
     51 <ol>
     52 <li>
     53 <p>Use <code>repo start</code> to start a new topic branch.</p>
     54 </li>
     55 <li>
     56 <p>Edit the files.</p>
     57 </li>
     58 <li>
     59 <p>Use <code>git add</code> to stage changes.</p>
     60 </li>
     61 <li>
     62 <p>Use <code>git commit</code> to commit changes.</p>
     63 </li>
     64 <li>
     65 <p>Use <code>repo upload</code> to upload changes to the review server.</p>
     66 </li>
     67 </ol>
     68 <h2 id="task-reference">Task reference</h2>
     69 <p>The task list below shows a summary of how to do common Repo and Git tasks.
     70 For information about using repo to download source, see <a href="{@docRoot}source/downloading.html">Downloading the Source</a> and
     71 <a href="{@docRoot}source/using-repo.html">Using Repo</a>.</p>
     72 <h2 id="synchronizing-your-client">Synchronizing your client</h2>
     73 <p>To synchronize the files for all available projects: </p>
     74 <pre><code>$ repo sync
     75 </code></pre>
     76 <p>To synchronize the files for selected projects:</p>
     77 <pre><code>$ repo sync PROJECT0 PROJECT1 PROJECT2 ...
     78 </code></pre>
     79 <h2 id="creating-topic-branches">Creating topic branches</h2>
     80 <p>Start a topic branch in your local work environment whenever you begin a change, for example when you begin work on a bug or new feature. A topic branch is not a copy of the original files; it is a pointer to a particular commit. This makes creating local branches and switching among them a light-weight operation. By using branches, you can isolate one aspect of your work from the others. For an interesting article about using topic branches, see <a href="http://www.kernel.org/pub/software/scm/git/docs/howto/separating-topic-branches.txt">Separating topic branches</a>.</p>
     81 <p>To start a topic branch using Repo, navigate into the project to be modified and issue: </p>
     82 <pre><code>$ repo start BRANCH_NAME .
     83 </code></pre>
     84 <p>Please note, the period represents the project in the current working directory. To verify your new branch was created:</p>
     85 <pre><code>$ repo status .
     86 </code></pre>
     87 <h2 id="using-topic-branches">Using topic branches</h2>
     88 <p>To assign the branch to a particular project:</p>
     89 <pre><code>$ repo start BRANCH_NAME PROJECT_NAME
     90 </code></pre>
     91 <p>See <a href="https://android.googlesource.com/">android.googlesource.com</a> for a list of all projects. Again, if you've already navigated into a particular project directory, you may simply pass a period to represent the current project.</p>
     92 
     93 <p>To switch to another branch that you have created in your local work environment:</p>
     94 <pre><code>$ git checkout BRANCH_NAME
     95 </code></pre>
     96 <p>To see a list of existing branches:</p>
     97 <pre><code>$ git branch
     98 </code></pre>
     99 <p>or </p>
    100 <pre><code>$ repo branches
    101 </code></pre>
    102 <p>The name of the current branch will be preceded by an asterisk.</p>
    103 <p class="note"><strong>Note:</strong> A bug might be causing <code>repo sync</code> to reset the local topic branch.
    104 If <code>git branch</code> shows * (no branch) after you run <code>repo sync</code>, then run <code>git checkout</code> again.</p>
    105 <h2 id="staging-files">Staging files</h2>
    106 <p>By default, Git notices but does not track the changes you make in a project. In order to tell git to preserve your changes, you must mark them for inclusion in a commit. This is also called "staging". </p>
    107 <p>You can stage your changes by running</p>
    108 <pre><code>git add
    109 </code></pre>
    110 <p>which accepts as arguments any files or directories within the project directory. Despite the name, <code>git add</code> does not simply add files to the git repository; it can also be used to stage file modifications and deletions.</p>
    111 <h2 id="viewing-client-status">Viewing client status</h2>
    112 <p>To list the state of your files:</p>
    113 <pre><code>$ repo status
    114 </code></pre>
    115 <p>To see uncommitted edits:</p>
    116 <pre><code>$ repo diff
    117 </code></pre>
    118 <p>The <code>repo diff</code> command shows every local edit that you have made that would <em>not</em> go into the commit, if you were to commit right now. To see every edit that would go into the commit if you were to commit right now, you need a Git command, <code>git diff</code>. Before running it, be sure you are in the project directory:</p>
    119 <pre><code>$ cd ~/WORKING_DIRECTORY/PROJECT
    120 $ git diff --cached
    121 </code></pre>
    122 <h2 id="committing-changes">Committing changes</h2>
    123 <p>A commit is the basic unit of revision control in git, consisting of a snapshot of directory structure and file contents for the entire project. Creating a commit in git is as simple as typing</p>
    124 <pre><code>git commit
    125 </code></pre>
    126 <p>You will be prompted for a commit message in your favorite editor; please provide a helpful message for any changes you submit to the AOSP. If you do not add a log message, the commit will be aborted. </p>
    127 <h2 id="uploading-changes-to-gerrit">Uploading changes to Gerrit</h2>
    128 <p>Before uploading, update to the latest revisions:</p>
    129 <pre><code>repo sync
    130 </code></pre>
    131 <p>Next run</p>
    132 <pre><code>repo upload
    133 </code></pre>
    134 <p>This will list the changes you have committed and prompt you to select which branches to upload to the review server. If there is only one branch, you will see a simple <code>y/n</code> prompt.</p>
    135 <h2 id="recovering-sync-conflicts">Recovering sync conflicts</h2>
    136 <p>If a <code>repo sync</code> shows sync conflicts:</p>
    137 <ul>
    138 <li>View the files that are unmerged (status code = U).</li>
    139 <li>Edit the conflict regions as necessary.</li>
    140 <li>
    141 <p>Change into the relevant project directory, run <code>git add</code> and <code>git commit</code> for the files in question, and then "rebase" the changes. For example:</p>
    142 <pre><code>$ git add .
    143 $ git commit
    144 $ git rebase --continue
    145 </code></pre>
    146 </li>
    147 <li>
    148 <p>When the rebase is complete start the entire sync again:</p>
    149 <pre><code>$ repo sync PROJECT0 PROJECT1 ... PROJECTN
    150 </code></pre>
    151 </li>
    152 </ul>
    153 <h2 id="cleaning-up-your-client-files">Cleaning up your client files</h2>
    154 <p>To update your local working directory after changes are merged in Gerrit:</p>
    155 <pre><code>$ repo sync
    156 </code></pre>
    157 <p>To safely remove stale topic branches: </p>
    158 <pre><code>$ repo prune
    159 </code></pre>
    160 <h2 id="deleting-a-client">Deleting a client</h2>
    161 <p>Because all state information is stored in your client, you only need to delete the directory from your filesystem:</p>
    162 <pre><code>$ rm -rf WORKING_DIRECTORY
    163 </code></pre>
    164 <p>Deleting a client will <em>permanently delete</em> any changes you have not yet uploaded for review.</p>
    165 <h2 id="git-and-repo-cheatsheet">Git and Repo cheatsheet</h2>
    166 <img src="{@docRoot}images/git-repo-1.png" alt="list of basic git and repo commands" id="figure2" />
    167 <p class="img-caption">
    168   <strong>Figure 2.</strong> Basic git and repo commands
    169 </p>
    170