File: git-force-clone

package info (click to toggle)
git-extras 7.4.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 2,120 kB
  • sloc: sh: 4,312; python: 994; makefile: 146
file content (109 lines) | stat: -rwxr-xr-x 2,507 bytes parent folder | download | duplicates (2)
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
#! /usr/bin/env bash

set -euo pipefail

_usage() {
  echo "
Usage:
  git-force-clone -b branch remote_url destination_path

Example:
  git-force-clone -b master git@github.com:me/repo.git ./repo_dir

Provides the basic functionality of 'git clone', but if the destination git
repository already exists it will force-reset it to resemble a clone of the
remote.

Because it doesn't actually delete the directory, it is usually significantly
faster than the alternative of deleting the directory and cloning the
repository from scratch.

**CAUTION**: If the repository exists, this will destroy *all* local work:
changed files will be reset, local branches and other remotes will be removed.

OPTIONS:
  -b, --branch    The branch to pull from the remote
  -h, --help      Display this help message
"
}

_check() {
  if [ -z "$1" ]; then
    echo "Error: Missing ${2}"
    _usage
    exit 1
  fi
}

main() {
  while [[ -n "${1:-}" ]] && [[ "${1:0:1}" == "-" ]]; do
    case $1 in
      -b | --branch )
        branch=${2:-}
        shift
        ;;
      -h | --help )
        _usage
        exit 0
        ;;
      * )
        echo "Error: Invalid option: $1"  >>/dev/stderr
        _usage
        exit 1
        ;;
    esac
    shift
  done

  remote_url=${1:-}
  destination_path=${2:-}

  _check "${remote_url}" "remote_url"
  _check "${destination_path}" "destination_path"

  if [ -d "${destination_path}/.git" ]; then
    (
      cd "${destination_path}"

      # Delete all remotes
      for remote in $(git remote); do
        git remote rm "${remote}"
      done

      # Add origin
      git remote add origin "${remote_url}"
      git fetch origin

      # Set default branch
      if [ -z "${branch:-}" ]; then
        branch=$(LC_ALL=C git remote show origin | grep -oP '(?<=HEAD branch: )[^ ]+$')
        git remote set-head origin "${branch}"
      else
        git remote set-head origin -a
      fi

      # Make sure current branch is clean
      git clean -fd
      git reset --hard HEAD

      # Get on the desired branch
      git checkout "${branch}"
      git reset --hard "origin/${branch}"

      # Delete all other branches
      # shellcheck disable=SC2063
      branches=$(git branch | grep -v '*' | xargs)
      if [ -n "${branches}" ]; then
        git branch -D "${branches}"
      fi
    )
  elif [ -n "${branch:-}" ]; then
    git clone -b "${branch}" "${remote_url}" "${destination_path}"
  else
    git clone "${remote_url}" "${destination_path}"
  fi
}

main "$@"

exit 0