]> git.cworth.org Git - nogit/blob - nogit
Fix two english sentence typos
[nogit] / nogit
1 #!/bin/bash
2 set -e
3
4 NOGIT_DIR=.nogit
5 NOGIT_DIR_TMP=$NOGIT_DIR-tmp
6
7 usage_brief()
8 {
9     echo "Usage: nogit <command>"
10     echo ""
11     echo "Available commands are:"
12     echo ""
13     echo "    nogit clone"
14     echo "    nogit sync"
15     echo ""
16     echo "See 'nogit help' for more details"
17 }
18
19 usage()
20 {
21     echo "Usage: nogit <command>"
22     echo ""
23     echo "Possible commands are described below:"
24     echo ""
25     echo "nogit clone <command>"
26     echo ""
27     echo "    Clone a nogit repository into the current directory."
28     echo "    This differs from 'git clone' in that it will not create"
29     echo "    a new top-level directory but will instead clone the"
30     echo "    resulting files into the current directory (which may"
31     echo "    already be a local git repository). The object store for"
32     echo "    this newly-cloned repository will be a directory named"
33     echo "    .nogit rather than .git."
34     echo ""
35     echo "    The repository to be cloned should be a nogit repository"
36     echo "    in the sense that its files are intended to be managed"
37     echo "    according to nogit semantics. But other than that, the"
38     echo "    repository to be cloned is an actual git repository."
39     echo ""
40     echo "nogit sync"
41     echo ""
42     echo "    Synchronize local and remote changes"
43     echo ""
44     echo "    For all files managed by a previously-cloned nogit repository"
45     echo "    this command will:"
46     echo ""
47     echo "        * Commit local changes (auto-generated commit message)"
48     echo ""
49     echo "        * Pull down any new commits from the remote"
50     echo ""
51     echo "        * Auto-merge local and remote (keeping both sides and"
52     echo "          with an auto-generated commit message)"
53     echo ""
54     echo "        * Push out any new commits generated locally"
55 }
56
57 nogit_clone()
58 {
59     url="$1"
60
61     if [ -e $NOGIT_DIR ]; then
62         echo "Error: $NOGIT_DIR already exists. Cowardly refusing to re-clone."
63         return 1
64     fi
65
66     if [ -e $NOGIT_DIR_TMP ]; then
67         echo "Error: $NOGIT_DIR_TMP already exists. Was a previous clone interrupted?"
68         echo "You'll want to clean that up before trying again."
69         return 1
70     fi
71
72     # Clone the repository into a temporary directory
73     mkdir $NOGIT_DIR_TMP
74     cd $NOGIT_DIR_TMP
75     git clone "$url" tmp >/dev/null 2>&1
76
77     # Sanity check that we won't be overwriting any files
78     EXISTING=()
79     cd tmp
80     for file in $(find . -mindepth 1 -a -path ./.git -prune -o -print); do
81         if [ -e "../../$file" ]; then
82             EXISTING+=($(echo "$file" | sed -s 's,^\./,,'))
83         fi
84     done
85     cd ..
86
87     if [ ${#EXISTING[@]} -gt 0 ]; then
88         echo "Error: The following existing files would be overwritten:" >&2
89         echo "" >&2
90         for file in ${EXISTING[@]}; do
91             echo "    $file" >&2
92         done
93         echo "" >&2
94         echo "Cowardly refusing to clone" >&2
95         cd ..
96         rm -rf $NOGIT_DIR_TMP
97         false
98     fi
99
100     # Install the info/atttributes file that forces the "union" merge
101     # driver for all files, giving us the semantics of "keep both sides
102     # of all conflicts" that is at the heart of nogit.
103     mkdir -p tmp/.git/info
104     echo '* merge=union' > tmp/.git/info/attributes
105
106     # Now that we've passed the sanity check, install the cloned .git
107     # object store into $NOGIT_DIR, cleanup our temporary files, and
108     # checkout the (known to not be conflicting) files.
109     mv tmp/.git ../$NOGIT_DIR
110     cd ..
111     rm -rf $NOGIT_DIR_TMP
112     GIT_DIR=$NOGIT_DIR git reset --hard >/dev/null 2>&1
113
114     echo "Completed nogit clone of $url"
115 }
116
117 nogit_sync()
118 {
119     # First commit any locally modified nogit files
120     GIT_DIR=$NOGIT_DIR git commit -a -m "nogit-sync commit" >/dev/null 2>&1 || true
121
122     # Then, fetch and merge any upstream changes
123     GIT_DIR=$NOGIT_DIR git fetch >/dev/null 2>&1
124     GIT_DIR=$NOGIT_DIR git merge -m "nogit-sync merge" >/dev/null 2>&1
125
126     # Finally, push any new commits up to the upstream repository
127     GIT_DIR=$NOGIT_DIR git push >/dev/null 2>&1
128
129     echo "Completed nogit sync"
130 }
131
132 if [ $# -lt 1 ]; then
133     echo "Error: missing command name." >&2
134     echo ""
135     usage_brief >&2
136     exit 1
137 fi
138
139 cmd="$1"
140
141 case "$cmd" in
142     clone)
143         if [ $# -lt 2 ]; then
144             echo "Error: 'nogit clone' requires the URL of a repository to clone"
145             false;
146         fi
147         nogit_clone "$2"
148         ;;
149     sync)
150         nogit_sync
151         ;;
152     help)
153         usage
154         true
155         ;;
156     *)
157         echo "Error: Unknown command: $cmd" >&2
158         echo ""
159         usage_brief >&2
160         false
161         ;;
162 esac
163
164 exit $?