Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config option to remove .gitattributes files #285

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions artifacts/scripts/construct.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ set -o nounset
set -o pipefail
set -o xtrace

if [ ! $# -eq 14 ]; then
echo "usage: $0 repo src_branch dst_branch dependent_k8s.io_repos required_packages kubernetes_remote subdirectory source_repo_org source_repo_name base_package is_library recursive_delete_pattern skip_tags last_published_upstream_hash"
if [ ! $# -eq 15 ]; then
echo "usage: $0 repo src_branch dst_branch dependent_k8s.io_repos required_packages kubernetes_remote subdirectory source_repo_org source_repo_name base_package is_library recursive_delete_pattern skip_tags last_published_upstream_hash remove_git_attributes"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does recursive_delete_pattern not already support this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nikhita ping!

exit 1
fi

Expand Down Expand Up @@ -74,8 +74,10 @@ RECURSIVE_DELETE_PATTERN="${3}"
SKIP_TAGS="${4}"
# last published upstream hash of this branch
LAST_PUBLISHED_UPSTREAM_HASH="${5}"
# remove all .gitattributes files
REMOVE_GIT_ATTRIBUTES="${6}"

readonly REPO SRC_BRANCH DST_BRANCH DEPS REQUIRED SOURCE_REMOTE SOURCE_REPO_ORG SUBDIR SOURCE_REPO_NAME BASE_PACKAGE IS_LIBRARY RECURSIVE_DELETE_PATTERN SKIP_TAGS LAST_PUBLISHED_UPSTREAM_HASH
readonly REPO SRC_BRANCH DST_BRANCH DEPS REQUIRED SOURCE_REMOTE SOURCE_REPO_ORG SUBDIR SOURCE_REPO_NAME BASE_PACKAGE IS_LIBRARY RECURSIVE_DELETE_PATTERN SKIP_TAGS LAST_PUBLISHED_UPSTREAM_HASH REMOVE_GIT_ATTRIBUTES

SCRIPT_DIR=$(dirname "${BASH_SOURCE}")
source "${SCRIPT_DIR}"/util.sh
Expand Down Expand Up @@ -129,7 +131,7 @@ if [ "${UPSTREAM_HASH}" != "${LAST_PUBLISHED_UPSTREAM_HASH}" ]; then
echo "Upstream branch upstream/${SRC_BRANCH} moved from '${LAST_PUBLISHED_UPSTREAM_HASH}' to '${UPSTREAM_HASH}'. We have to sync."
# sync_repo cherry-picks the commits that change
# k8s.io/kubernetes/staging/src/k8s.io/${REPO} to the ${DST_BRANCH}
sync_repo "${SOURCE_REPO_ORG}" "${SOURCE_REPO_NAME}" "${SUBDIR}" "${SRC_BRANCH}" "${DST_BRANCH}" "${DEPS}" "${REQUIRED}" "${BASE_PACKAGE}" "${IS_LIBRARY}" "${RECURSIVE_DELETE_PATTERN}"
sync_repo "${SOURCE_REPO_ORG}" "${SOURCE_REPO_NAME}" "${SUBDIR}" "${SRC_BRANCH}" "${DST_BRANCH}" "${DEPS}" "${REQUIRED}" "${BASE_PACKAGE}" "${IS_LIBRARY}" "${RECURSIVE_DELETE_PATTERN}" "${REMOVE_GIT_ATTRIBUTES}"
else
echo "Skipping sync because upstream/${SRC_BRANCH} at ${UPSTREAM_HASH} did not change since last sync."
fi
Expand Down
35 changes: 33 additions & 2 deletions artifacts/scripts/util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ sync_repo() {
shift 9

local recursive_delete_pattern="${1}"
local remove_git_attributes="${2}"

local commit_msg_tag="${source_repo_name^}-commit"
readonly subdirectory src_branch dst_branch deps is_library
Expand Down Expand Up @@ -508,11 +509,13 @@ sync_repo() {
# get consistent and complete go.mod on each sync. Skip if nothing changed.
# NOTE: we cannot skip collapsed-kube-commit-mapper below because its
# output depends on upstream's HEAD.
echo "Fixing up go.mod after a complete sync"
echo "Fixing up go.mod and .gitattributes after a complete sync"
if [ $(git rev-parse HEAD) != "${dst_old_head}" ] || [ "${new_branch}" = "true" ]; then
remove-git-attributes "${remove_git_attributes}" true
fix-gomod "${deps}" "${required_packages}" "${base_package}" "${is_library}" true true ${commit_msg_tag} "${recursive_delete_pattern}"
else
# update go.mod without squashing because it would mutate a published commit
# update go.mod and .gitattributes without squashing because it would mutate a published commit
remove-git-attributes "${remove_git_attributes}" false
fix-gomod "${deps}" "${required_packages}" "${base_package}" "${is_library}" true false ${commit_msg_tag} "${recursive_delete_pattern}"
fi

Expand Down Expand Up @@ -917,3 +920,31 @@ checkout-deps-to-kube-commit() {
done
}

function remove-git-attributes() {
local remove_git_attributes="${1}"
if [ ! "${remove_git_attributes:-}" = true ]; then
return 0
fi

local squash="${2:-true}"
local dst_old_commit=$(git rev-parse HEAD)

find . -name ".gitattributes" -type f -delete

# squash commits into ${dst_old_commit} or create a new commit
if ! git diff --exit-code ${dst_old_commit} >/dev/null; then
if [ "${squash}" = true ]; then
echo "Amending last merge with .gitattributes changes."
git reset --soft -q ${dst_old_commit}
git add -u
git commit -q --amend --allow-empty -C ${dst_old_commit}
else
echo "Adding a commit for .gitattributes changes."
git reset --soft -q ${dst_old_commit}
git add -u
git commit -q --allow-empty -m "sync: remove .gitattributes files"
fi
fi

ensure-clean-working-dir
}
2 changes: 2 additions & 0 deletions cmd/publishing-bot/config/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ type RepositoryRule struct {
Library bool `yaml:"library,omitempty"`
// not updated when true
Skip bool `yaml:"skipped,omitempty"`
// RemoveGitAttributes removes all .gitattributes files in the repo
RemoveGitAttributes *bool `yaml:"remove-git-attributes,omitempty"`
}

type RepositoryRules struct {
Expand Down
7 changes: 7 additions & 0 deletions cmd/publishing-bot/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,12 @@ func (p *PublisherMunger) construct() error {
continue
}

removeGitAttributes := ""
if repoRule.RemoveGitAttributes != nil && *repoRule.RemoveGitAttributes {
removeGitAttributes = "true"
p.plog.Infof("remove-git-attributes is enabled")
}

// clone the destination repo
dstDir := filepath.Join(p.baseRepoPath, repoRule.DestinationRepository, "")
dstURL := fmt.Sprintf("https://%s/%s/%s.git", p.config.GithubHost, p.config.TargetOrg, repoRule.DestinationRepository)
Expand Down Expand Up @@ -319,6 +325,7 @@ func (p *PublisherMunger) construct() error {
strings.Join(p.reposRules.RecursiveDeletePatterns, " "),
skipTags,
lastPublishedUpstreamHash,
removeGitAttributes,
)
cmd.Env = append([]string(nil), branchEnv...) // make mutable
if p.reposRules.SkipGomod {
Expand Down