summaryrefslogtreecommitdiff
path: root/git-update-agent
blob: 6c205a7550f3372b415ad3bce176d9f4f7dcce34 (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
#!/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() {
  die <<_EOF
origin/HEAD is used as the reference for updates
origin/HEAD is not set and UPSTREAM_BRANCH override not provided

You can set the upstream branch with:
git remote set-head <name> <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"
  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 "$REMOTE_URL"
    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             initialize a project with the given URL
  --track            track a git repository not initialized with $PROGRAM
  --disable-tracking disable tracking
_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"
  cd_or_die "$dir"

  previous_version="$(current_version)"

  init_if_empty

  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
  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
}


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

  remote_url="$1"
  remote="$(remote)"

  DEFAULT_BRANCH="main"

  trap '{ rm -rf .git; exit 1; }' EXIT

  set -e
  git init &>/dev/null
  git branch -M "$DEFAULT_BRANCH"
  git config remote.origin.url "$remote_url"
  git config remote.origin.fetch "+refs/heads/*:refs/remotes/$remote/*"
  set_should_track true
  git fetch --force --tags "$remote" &>/dev/null
  git remote set-head "$remote" --auto >/dev/null
  
  upstream_branch="$(upstream_branch "$remote")"

  if [[ -z "$upstream_branch" ]]; then
    die_no_upstream_found
  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
}

#
# 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 requires an option"
        fi

        remote_url="$1"

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

  depends git

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

  if [[ -n "$remote_url" ]]; then
    init_repo "$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"
}

git_update_agent "$@"