Test commit
[cogito/jonas.git] / cg-branch-add
blobe724fdcb185271b32d320dcb35114432025074b4
1 #!/usr/bin/env bash
3 # Add new remote branch
4 # Copyright (c) Petr Baudis, 2005
6 # Takes the desired branch name and source location as parameters.
8 # This command lets you add to the list of 'remote branches'. Those are
9 # branches in your local repository which correspond to some branches
10 # in other repositories.
12 # After you add a remote branch, you can `cg-fetch` from it to get the
13 # latest changes on the branch, as they appeared in the remote repository.
15 # Terminology note: This command concerns remote branches, not the local
16 # ones (those managed by `cg-switch`).
18 # The possible source location specifiers are:
20 # 1. Local path - note that fetching will hardlink the objects if possible.
22 # 2. rsync - `rsync://host/path`
23 # THE rsync REPOSITORY ACCESS METHOD IS DEPRECATED AND WILL BE REMOVED
24 # IN THE FUTURE! The problem is that it will download _all_ data from
25 # the remote repository, including objects which do not belong to the
26 # one particular branch you want to fetch.
28 # 3. HTTP - `http://host/path`
30 # 4. HTTPS - `https://host/path`
32 # 5. FTP - 'ftp://host/path'
34 # 6. SSH - `git+ssh://host/path` or `host:path` (the latter can change);
35 # note that the path must be absolute in the first case.
37 # 7. git daemon - `git://host/path`
40 # The URL can have a fragment part, which identifies a branch inside of
41 # the remote repository (in the form "repoURL#branchname"). Otherwise,
42 # Cogito defaults to whatever branch the repository's HEAD points to
43 # at the time of each fetch.
45 # FILES
46 # -----
47 # $GIT_DIR/branches/BRANCH_NAME::
48 # Contains the source location of the remote branch.
50 # $GIT_DIR/refs/heads/BRANCH_NAME::
51 # Contains pointer to the latest commit in a branch. It can be more
52 # conveniently retrieved using `cg-object-id BRANCH_NAME`.
54 # EXAMPLE USAGE
55 # -------------
56 # Let's say you have a repository at `git+ssh://host/path/repo.git` and you
57 # want to fetch its 'testing' branch to your local repository. In that case
58 # you can do
60 # $ cg-branch-add repo-testing 'git+ssh://host/path/repo.git#testing'
62 # and refer to it as 'repo-testing' anytime later:
64 # $ cg-fetch repo-testing
66 # For example you can check the changes in the `repo-testing` branch compared
67 # to your current branch:
69 # $ cg-diff -r repo-testing
71 # Testsuite: Partial (used in many tests but a dedicated testsuite is missing)
73 USAGE="cg-branch-add BRANCH_NAME LOCATION"
74 _git_wc_unneeded=1
76 . "${COGITO_LIB}"cg-Xlib || exit 1
78 name="${ARGS[0]}"
79 location="${ARGS[1]}"
81 ([ -n "$name" ] && [ -n "$location" ] && ! [ -n "${ARGS[2]}" ]) || usage
82 git-check-ref-format "refs/heads/$name" || \
83 die "name contains invalid characters"
84 if [ "$name" = "this" ] || [ "$name" = "HEAD" ]; then
85 die "given branch name is reserved"
88 mkdir -p "$_git/branches/$(dirname "$name")"
89 [ -s "$_git/branches/$name" ] && die "branch already exists"
90 exists_ref "refs/heads/$name" && warn "branch head already exists"
92 if ! echo "$location" | grep -q ":" ; then
93 location=$(echo "$location" | sed -e "s#^[^/]#$(pwd)\/&#" | normpath)
96 echo "$location" >"$_git/branches/$name"