summaryrefslogtreecommitdiff
path: root/git-update-agent
blob: 4a0c6910d8df890f9abb84a845c2a86a44a83254 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#!/bin/sh

# GLOBAL VARIABLES:
# - UPSTREAM_BRANCH: The branch in the upstream repo used for updates (defaults to remote's head if set)
# - REMOTE:          Name of the remote used for updates
# - REMOTE_URL:      Name of URL used for synchronization

# USAGE:
#
# Initialize a project:
# git-update-agent --init https://git.example.com
#
# Update:
# git-update-agent

CMD_PROMPT="[#] "
PROGRAM="${0##*/}"

die () {
  if [[ $# -eq 0 ]]; then
    echo -n "$PROGRAM: " 1>&2
    cat <&0 1>&2
  elif [[ $# -eq 1 ]]; then
    echo "$PROGRAM: $1" 1>&2
  fi

  exit 1
}

depends() {
  if ! type "$1" >/dev/null; then
    die "\"$1\" must be installed and in your \$PATH"
  fi
}

cmd() {
  echo "$CMD_PROMPT $*"
  "$@"
}

stash() {
  if ! git                                \
    -c "user.email=git-updater@localhost" \
    -c "user.name=git-updater"            \
    stash save --include-untracked &>/dev/null
  then

  die <<_EOF
Could not stash in $(pwd)
Please stash/commit manually if you need to keep your changes. If you would like to destroy all changes, run:
cd $(pwd)
git reset --hard origin/main
_EOF

  fi
}

current_version() {
  git rev-parse -q --verify HEAD
}
      
die_no_upstream_found() {
  local remote
  remote="$1"

  die <<_EOF
$remote/HEAD is used as the reference for updates
$remote/HEAD is not set and UPSTREAM_BRANCH override not provided

You can set the upstream branch with:
git remote set-head $remote <branch>
_EOF
}

upstream_branch() {
  local upstream_branch
  local remote

  remote="$1"


  if [[ -z "$UPSTREAM_BRANCH" ]]; then
    upstream_branch="$(git symbolic-ref "refs/remotes/$remote/HEAD" 2>/dev/null)"
    upstream_branch="${upstream_branch#refs/remotes/$remote/}"
  else
    upstream_branch="$UPSTREAM_BRANCH"
  fi

  echo "$upstream_branch"
}

local_branch() {
  local branch

  branch="$(git symbolic-ref HEAD)"
  branch="${branch#refs/heads/}"

  echo "$branch"
}

merge() {
  local STASHED
  
  local remote
  local remote_branch 
  local local_branch

  remote="$1"
  remote_branch="$2"
  local_branch="$3"
  
  local remote_ref
  remote_ref="$remote/$remote_branch"


  if [[ -n "$(git status --untracked-files=all --porcelain 2>/dev/null)" ]]; then
    # Abort rebase or merge if initiated
    git merge --abort  &>/dev/null
    git rebase --abort &>/dev/null

    stash
    STASHED="1"
  fi

  # Attempt fetch to REMOTE/UPSTREAM_BRANCH
  if ! git fetch --tags --force "$remote" \
    "refs/heads/$remote_branch:refs/remotes/$remote_ref" \
    2>/dev/null
  then
    die "Cannot fetch from $remote_ref from $local_branch"
  fi

  # Try fast-forward merge
  git merge --no-edit --ff "$remote_ref" \
      --strategy=recursive \
      --strategy-option=ours \
      --strategy-option=ignore-all-space \
      >/dev/null

  if [[ -n "$STASHED" && -z "$NO_STASH_POP" ]]; then
    git stash pop &>/dev/null
  fi
}

remote() {
  local remote

  if [[ -z "$REMOTE" ]]; then
    remote="origin"
  else
    remote="$REMOTE"
  fi

  echo "$remote"
}

cd_or_die() {
  cd "$1" || die "Cannot enter directory $1. Exiting."
}

die_no_git_dir() {
  die <<_EOF
Cannot find a .git directory in $(pwd). Try initializing repo with:
$PROGRAM --init-with https://git.example.com

To automatically create a git repo, consider setting "REMOTE_URL"
_EOF
}

init_if_empty() {
  if [[ ! -d ".git" ]]; then

    if [[ -z "$REMOTE_URL" ]]; then 
      die_no_git_dir
    else
      init_repo "$1" "$REMOTE_URL"

      echo "INITIALIZED"
    fi
  fi
}

should_track() {
  echo $(git config updateagent.track)
}

set_should_track() {
  git config updateagent.track "$1"
}

die_not_tracking() {
  die <<_EOF
$PROGRAM is not tracking $(pwd). To enable tracking:
$PROGRAM --track
_EOF
}

print_usage() {
cat <<_EOF
Usage: ${PROGRAM} [OPTIONS]...
  -h, --help         output this help info
  -d, --dir          update in directory
  --init-with        initialize a project with the given URL
  --track            track a git repository not initialized with $PROGRAM
  --disable-tracking disable tracking
  --version          output version
_EOF
}

#
# SUBCOMMANDS
#

set_tracking() {
  local dir
  dir="$1"

  local track
  track="$2"

  cd_or_die "$dir"
  
  if [[ ! -d ".git" ]]; then 
    die <<_EOF
$(pwd) is not the root of a git project
_EOF
  fi

  set_should_track "$2"
}

update() {
  local dir
  local previous_version
  local next_version

  dir="$1"

  mkbase_or_die "$dir"
  cd_or_die "$dir"
  
  previous_version="$(current_version)"

  if [[ "$(should_track)" != "true" ]]; then
    die_not_tracking     
  fi

  local remote
  local remote_branch
  local local_branch

  remote="$(remote)"
  
  remote_branch="$(upstream_branch "$remote")"

  if [[ -z "$remote_branch" ]]; then
    die_no_upstream_found "$remote"
  fi

  local_branch="$(local_branch)"

  merge "$remote" "$remote_branch" "$local_branch"

  next_version="$(current_version)"

  if [[ "$previous_version" == "" ]]; then
    echo "Initialized $PROGRAM with \"$previous_version\""
  elif [[ "$previous_version" != "$next_version" ]]; then
    echo "Updated $PROGRAM from version \"$previous_version\" to \"$next_version\""
  else
    echo "$PROGRAM is already up to date."
  fi
}

mkbase_or_die() {
  if [[ ! -d "$1" ]] && ! mkdir "$1"; then
    die <<_EOF
Cannot create $1 directory
_EOF
  fi
}

die_init_failure() {
  die<<_EOF
A failure occurred while configuring and cloning the repo
_EOF
}

init_repo() {
  local dir
  local remote
  local remote_url
  local upstream_branch
  local remote_ref

  dir="$1"

  mkbase_or_die "$dir"
  cd_or_die "$dir"

  remote_url="$2"
  remote="$(remote)"

  DEFAULT_BRANCH="main"

  trap '{ rm -rf .git; die_init_failure; }' EXIT

  set -e
  git init &>/dev/null
  git branch -M "$DEFAULT_BRANCH"
  git config "remote.${remote}.url" "$remote_url"
  git config "remote.${remote}.fetch" "+refs/heads/*:refs/remotes/$remote/*"
  set_should_track true
  set +e

  if ! git fetch --force --tags "$remote" 2>/dev/null; then
    die "Could not download content from \"$remote_url\" on \"$DEFAULT_BRANCH\"."
  fi

  set -e
  git remote set-head "$remote" --auto >/dev/null
  
  upstream_branch="$(upstream_branch "$remote")"

  if [[ -z "$upstream_branch" ]]; then
    die_no_upstream_found "$remote"
  fi

  remote_ref="$remote/$upstream_branch"

  git merge --ff --no-edit "$remote_ref"

  git branch --set-upstream-to="$remote_ref" "$DEFAULT_BRANCH"

  git reset --hard "$remote_ref" >/dev/null

  trap - EXIT

  set +e
}

print_version() {
  echo "0.0.1"
}

#
# API
#

git_update_agent() {
  local dir
  local option
  local remote_url
  local track
  local disable_tracking 

  while [[ $# -gt 0 ]]; do
    case "$1" in
      -h|-\?|--help|--usage) print_usage; exit 0 ;;
      --dir)
        
        shift

        if [[ $# -eq 0 ]]; then
          die "--dir requires an option"
        fi

        dir="$1"

        shift
      ;;
      --init-with)

        shift

        if [[ $# -eq 0 ]]; then
          die "--init-with requires an option"
        fi

        remote_url="$1"

        shift
      ;;
      --track)            track=1;            shift ;;
      --disable-tracking) disable_tracking=1; shift ;; 
      --version)          print_version; exit 0     ;;
      *) die "$(print_usage)" ;;
    esac
  done

  depends git

  if [[ -z "$dir" ]]; then
    dir="." 
  fi

  if [[ -n "$remote_url" ]]; then
    init_repo "$dir" "$remote_url"
    exit 0
  fi

  if [[ -n "$track" ]]; then
    set_tracking "$dir" "true"
    exit 0
  fi

  if [[ -n "$disable_tracking" ]]; then
    set_tracking "$dir" "false"
    exit 0
  fi

  update "$dir"
}

if [[ -z "$NO_EXEC" ]]; then
  git_update_agent "$@"
fi