]> git.cworth.org Git - nogit/blob - nogit
Look for a pre-existing .nogit-tmp directory
[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 previosu 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     # Now that we've passed the sanity check, install the cloned .git
101     # object store into $NOGIT_DIR, cleanup our temporary files, and
102     # checkout the (known to not be conflicting) files.
103     mv tmp/.git ../$NOGIT_DIR
104     cd ..
105     rm -rf $NOGIT_DIR_TMP
106     GIT_DIR=$NOGIT_DIR git reset --hard >/dev/null 2>&1
107
108     echo "Completed nogit clone of $url"
109 }
110
111 nogit_sync()
112 {
113     # First commit any locally modified nogit files
114     GIT_DIR=$NOGIT_DIR git commit -a -m "nogit-sync commit" >/dev/null 2>&1 || true
115
116     # Then, fetch and merge any upstream changes
117     GIT_DIR=$NOGIT_DIR git fetch >/dev/null 2>&1
118     GIT_DIR=$NOGIT_DIR git merge -m "nogit-sync merge" >/dev/null 2>&1
119
120     # Finally, push any new commits up to the upstream repository
121     GIT_DIR=$NOGIT_DIR git push >/dev/null 2>&1
122
123     echo "Completed nogit sync"
124 }
125
126 if [ $# -lt 1 ]; then
127     echo "Error: missing command name." >&2
128     echo ""
129     usage_brief >&2
130     exit 1
131 fi
132
133 cmd="$1"
134
135 case "$cmd" in
136     clone)
137         if [ $# -lt 2 ]; then
138             echo "Error: 'nogit clone' requires the URL of a repository to clone"
139             false;
140         fi
141         nogit_clone "$2"
142         ;;
143     sync)
144         nogit_sync
145         ;;
146     help)
147         usage
148         true
149         ;;
150     *)
151         echo "Error: Unknown command: $cmd" >&2
152         echo ""
153         usage_brief >&2
154         false
155         ;;
156 esac
157
158 exit $?