]> git.cworth.org Git - nogit/blob - nogit
Fix an accidental case of a ".nogit" literal
[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     # Clone the repository into a temporary directory
67     mkdir $NOGIT_DIR_TMP
68     cd $NOGIT_DIR_TMP
69     git clone "$url" tmp >/dev/null 2>&1
70
71     # Sanity check that we won't be overwriting any files
72     EXISTING=()
73     cd tmp
74     for file in $(find . -mindepth 1 -a -path ./.git -prune -o -print); do
75         if [ -e "../../$file" ]; then
76             EXISTING+=($(echo "$file" | sed -s 's,^\./,,'))
77         fi
78     done
79     cd ..
80
81     if [ ${#EXISTING[@]} -gt 0 ]; then
82         echo "Error: The following existing files would be overwritten:" >&2
83         echo "" >&2
84         for file in ${EXISTING[@]}; do
85             echo "    $file" >&2
86         done
87         echo "" >&2
88         echo "Cowardly refusing to clone" >&2
89         cd ..
90         rm -rf $NOGIT_DIR_TMP
91         false
92     fi
93
94     # Now that we've passed the sanity check, install the cloned .git
95     # object store into $NOGIT_DIR, cleanup our temporary files, and
96     # checkout the (known to not be conflicting) files.
97     mv tmp/.git ../$NOGIT_DIR
98     cd ..
99     rm -rf $NOGIT_DIR_TMP
100     GIT_DIR=$NOGIT_DIR git reset --hard >/dev/null 2>&1
101
102     echo "Completed nogit clone of $url"
103 }
104
105 nogit_sync()
106 {
107     # First commit any locally modified nogit files
108     GIT_DIR=$NOGIT_DIR git commit -a -m "nogit-sync commit" >/dev/null 2>&1 || true
109
110     # Then, fetch and merge any upstream changes
111     GIT_DIR=$NOGIT_DIR git fetch >/dev/null 2>&1
112     GIT_DIR=$NOGIT_DIR git merge -m "nogit-sync merge" >/dev/null 2>&1
113
114     # Finally, push any new commits up to the upstream repository
115     GIT_DIR=$NOGIT_DIR git push >/dev/null 2>&1
116
117     echo "Completed nogit sync"
118 }
119
120 if [ $# -lt 1 ]; then
121     echo "Error: missing command name." >&2
122     echo ""
123     usage_brief >&2
124     exit 1
125 fi
126
127 cmd="$1"
128
129 case "$cmd" in
130     clone)
131         if [ $# -lt 2 ]; then
132             echo "Error: 'nogit clone' requires the URL of a repository to clone"
133             false;
134         fi
135         nogit_clone "$2"
136         ;;
137     sync)
138         nogit_sync
139         ;;
140     help)
141         usage
142         true
143         ;;
144     *)
145         echo "Error: Unknown command: $cmd" >&2
146         echo ""
147         usage_brief >&2
148         false
149         ;;
150 esac
151
152 exit $?