cogito: understand permissions written as "100755"
[cogito.git] / cg-add
blob9bf3a6a294f6e1db499027fa3cbc58c742870477
1 #!/usr/bin/env bash
3 # Add new files to the repository
4 # Copyright (c) Petr Baudis, 2005
6 # Takes a list of file names at the command line, and schedules them
7 # for addition to the GIT repository at the next commit. Those files
8 # are denoted by 'A' in the `cg-status` list. You can add `cg-rm`oved
9 # files to undo the removal (use `cg-restore` if the file is already
10 # deleted from the working copy as well); you can undo additions in
11 # an analogous way using `cg-rm`.
13 # The command will fail if one of the given files does not exist.
15 # Note that directories never have to be added to the repository, and are
16 # not tracked on their own. That means, you cannot currently add an empty
17 # directory to 'Cogito'. The reason for this is that 'Cogito' manages
18 # content and empty directories have no content. Directories are added
19 # automatically when adding files inside them, or you can add all files in
20 # a directory using cg-add -r.
22 # OPTIONS
23 # -------
24 # -a:: Add all untracked files
25 # Add all files in the repository that are currently untracked. Useful
26 # when there were some random files added/removed (e.g. by some
27 # third-party application) and you want to get the repository in sync:
28 # just use `cg-rm -a && cg-add -a`. Note that contrary to `cg-add -r .`,
29 # this will not re-add `cg-rm`oved files.
31 # -N:: Only update the lowlevel cache file
32 # Only update the cache: do not copy the data into the object database.
33 # This is for special purposes when you might not actually _have_ any
34 # object database. This option is normally not interesting.
36 # -r:: Add files recursively
37 # If you pass cg-add this flag and any directory names, it will try
38 # to add files in those directories recursively (with regard to your
39 # ignore rules - see `cg-status` for a more detailed description of
40 # those). See also above for more notes about cg-add vs. directories.
42 # Testsuite: Partial (used in many tests but a dedicated testsuite is missing)
44 USAGE="cg-add [-a] [-N] [-r] FILE..."
46 . "${COGITO_LIB}"cg-Xlib || exit 1
48 addnew=
49 infoonly=
50 recursive=
51 while optparse; do
52 if optparse -N; then
53 infoonly=--info-only
54 elif optparse -r; then
55 recursive=1
56 elif optparse -a; then
57 addnew=1
58 else
59 optfail
61 done
63 [ ${#ARGS[*]} -ge 1 -o "$addnew" ] || usage
65 TMPFILE="$(mktemp -t gitadd.XXXXXX)" || exit 1
66 error=
67 for file in "${ARGS[@]}"; do
68 file="${file%/}"
69 absfile="$(echo "$_git_relpath$file" | normpath)"
70 if [ -d "$absfile" ] || [ -z "$absfile" ]; then
71 if [ "$recursive" ]; then
72 # XXX: This all relpath business is so ugly.
73 (cd "$_git_relpath" && cg-status -wnSs D\? "$file") >>"$TMPFILE"
74 else
75 echo "$file is a directory (use cg-add -r?)" >&2
76 error=1
78 elif [ ! -f "$absfile" ] && [ -h "$absfile" ]; then
79 echo "$file does not exist or is not a regular file or a symlink" >&2
80 error=1
81 else
82 echo "$file" >>"$TMPFILE"
84 done
86 if [ "$addnew" ]; then
87 (cd "$_git_relpath" && cg-status -S -s \? -n -w) >>"$TMPFILE"
88 if [ ! -s "$TMPFILE" ]; then
89 rm "$TMPFILE"
90 die "no files to add"
94 cat "$TMPFILE" | sed 's/^/Adding file /'
95 cat "$TMPFILE" | sed "s|^|$_git_relpath|" | path_xargs git-update-index --add ${infoonly} -- || error=1
97 rm "$TMPFILE"
99 [ "$error" ] && die "warning: not all items could have been added"
100 exit 0