Git deployment: removing files when push files removed - deployment

I have a post-receive hook to deploy my app in webroot when I push on master branch.
This is my script:
#!/bin/bash
while read oldrev newrev ref
do
if [[ $ref =~ .*/master$ ]];
then
echo "Master ref received. Deploying master branch..."
git --work-tree=/var/www/mywebroot --git-dir=/home/myuser/myrepo checkout -f master
else
echo "Ref $ref successfully received. Doing nothing."
fi
done
The problem is that when I push any commit that deletes any file, the files were removed are not removed in webroot.

You can add (using git clean) just after the git checkout line:
git -work-tree=/var/www/mywebroot --git-dir=/home/myuser/myrepo clean -fd
That will remove untracked files and folders.

Related

git-rebase: Make bad variable name in large PR stack disappear

I had a JSX component named AccountNode, and accidentally created a TS interface also named AccountNode, but the two are distinct. One is for a row in a table (ui), and the other is for a piece of data in a tree (a node).
How can I make the "AccountNode" name not appear anywhere in history?
Similarly, as mentioned in the comments, you may have a bad string constant bring passed around, a so-called "magic string". It may be hard to rewrite code to not use this magic string, but this git rebase recipe will help.
Example, I don't want to hard-code the string "load_more" so am going to redo/tweak those commits:
git rebase --exec 'git difflastcommitadded | ag load_more && { echo "found load_more"; git redo; } || echo "ok"' --no-reschedule-failed-exec -i 315abbd5b
Setup:
Install ag (brew install the_silver_searcher) (grep/ack alternative)
Add this git difflastcommitadded alias:
git config --global --edit
brew install the_silver_searcher
Paste:
[alias]
# From: https://stackoverflow.com/questions/73981158/git-rebase-make-bad-name-in-large-pr-stack-disappear/73981159#73981159
difflastcommitadded = !git diff HEAD^..HEAD --no-ext-diff --unified=0 --exit-code -a --no-prefix | egrep '^\\+'
Alternatively, if you want to completely abolish a variable name from a folder in your repo, continue reading this less flexible example:
git rebase --exec 'ag -0 -l AccountNode ui/app && git redo || echo "ok"' -i 315abbd5b
This will rebase, but stop whenever there's a commit that introduced AccountNode inside the ui/app folder, undo that commit, and pop those changes into your working directory, so you can fix that in your ide.
Once you fix an instance of AccountNode you can proceed with nice safety checks using this script:
ag -0 -l AccountNode ui/app/pages/AccountManagement2 && echo "still contains AccountNode" || {
yarn --cwd ui run jest -i && git add . && git commit && git rebase --continue
}
In my instance, git commit runs prettier+eslint, but not tests (those run in ci right now)
This depends on the git redo alias mentioned here: git rebase - pop each commit into working dir to easily edit, re-use commit msg

Fully reset a corrupted git submodule in SourceTree

First off, I am not using command line git at all. I am only using SourceTree's gui interface. I would prefer to solve my problem this way; if possible.
Somehow, my submodule has become corrupted. Attempting to fetch or pull gives me the following error:
I haven't found any answers for this particular problem. I am fortunate in that my remote master is ok, 100% up to date, and I have no local changes. So, I think the easiest way will be to just fully reset my local submodule.
However, I can't figure out how to do so.
I considered trying to remove my submodule and then re add it. However, I have had problems with that in the past, and so am gun shy.
I found handfuls of posts about resetting to a specific commit. However, the SourceTree gui is failing to populate my history because of this error.
Any help would be appreciated
Thank you
You can try this
Right click at your conflicted branch at BRANCHES and delete it.
Double click at your remote branch at REMOTES to re-download and switch to that branch.
Easy, good luck.
I finally did get this fixed. I had to give up and use the command line.
I found this page (git fatal: failed to read object xxx: Invalid argument). That pointed me to using the "git fsck --full" command.
That pointed me to a very specific folder in the .git hierarchy that was corrupted.
I needed to delete this folder, but doing so wasn't easy. Windows would let me delete it. Not in safe mode. Not with cmd del or rmdir. I had to run a scan disk from windows on my entire drive. That ended up detecting the folder and removing it.
Finally, from there, I was able to fetch and pull master again.
From all the solutions I was able to find to perform an actually hard reset with proper cleanup and reinitialization of submodules nothing worked. So I ended up creating a custom script. Following will perform a full cleanup of your submodules and will recreate them as if you were cloning the parent repo.
#!/bin/bash
echo "Backing up current .gitmodules"
cp -f .gitmodules .gitmodules.bkp
has_submodules=$(echo "$(git submodule)")
if [ "$has_submodules" != "" ]; then
git submodule deinit --force .
rm -rf .git/modules
git submodule | cut -c43- | while read -r line; do (git rm -f "$line"); done
fi
cp -f .gitmodules.bkp .gitmodules
PARAMS=("path" "url" "branch")
CHUNKS=${#PARAMS[#]}
PARAMS_STR=$(IFS='|'; echo "${PARAMS[*]}")
readarray -t SUBMODULES_INFO_ARRAY < <(git config -f .gitmodules --get-regexp '^submodule\..*\.('"$PARAMS_STR"')$')
SUBMODULES_INFO_ARRAY_STR=$(IFS='|'; echo "${SUBMODULES_INFO_ARRAY[*]}")
function process_submodules_parsed_array()
{
name=${SUBMODULES_PARSED_ARRAY[0]}
path=${SUBMODULES_PARSED_ARRAY[1]}
repo_url=${SUBMODULES_PARSED_ARRAY[2]}
branch=${SUBMODULES_PARSED_ARRAY[3]}
echo "Running: git submodule add --force -b "$branch" --depth 1 --name "$name" -- "$repo_url" "$path""
git submodule add --force -b "$branch" --depth 1 --name "$name" -- "$repo_url" "$path"
}
echo "Parsing current .gitmodules"
for((chunk=0; chunk<${#SUBMODULES_INFO_ARRAY[#]}; chunk+=CHUNKS))
do
section_name="$(sed -nE 's/^submodule\.(.*?)\.'${PARAMS[0]}'\s(.*?)$/\1/p' <<< "${SUBMODULES_INFO_ARRAY[chunk]}")"
SUBMODULES_PARSED_ARRAY=("$section_name")
for ((param_i=0; param_i<CHUNKS; param_i+=1))
do
param_unparsed="${SUBMODULES_INFO_ARRAY[chunk+param_i]}"
param_parsed="$(sed -nE 's/^submodule.*?\.'${PARAMS[param_i]}'\s(.*?)$/\1/p' <<< "$param_unparsed")"
SUBMODULES_PARSED_ARRAY+=($param_parsed)
done
process_submodules_parsed_array
done
echo "Restoring .gitmodules"
mv -f .gitmodules.bkp .gitmodules
echo "Initializing submodules"
git submodule init
git submodule update

How to add/update files in git repository from Azure DevOps Pipeline Dynamics 365 CE

Short Version
When a build pipeline is triggered, one of the build pipeline task will get the latest files and it has to be added/updated in the git repository of current running pipeline. I tried to do it by using command line task but its failing as not git repository
fatal: not a git repository (or any of the parent directories): .git
Long Version
I m tried to achieve solution pack and unpack process for dynamics 365 instance. So the build pipeline have the following tasks
Export solution from Dynamics 365 and store it in $(build.binariesdirectory)
Unpack the solution zip file and store in $(Build.Repository.LocalPath) i.e., adding/updating existing files
Command line task to commit and push the files to current pipeline repository
ECHO "Setting git config..."
git config --global user.email "xxx#xyz.com"
git config --global user.name "Admin"
ECHO "CHECK GIT STATUS..."
git status
ECHO "GIT ADD..."
git add -A
ECHO "CHECK GIT STATUS..."
git status
ECHO "Commiting the changes..."
git commit -m "Latest Customizations updated"
ECHO "Pushing the changes..."
git push -u origin master
ECHO "Customization Committed Successfully"
Updated
If a pipeline is created with 2 command line task i.e. one to create a random file in the Build.Sourcedirectory and another to commit the changes, then the git commit and push commands are working.
But if we add other tasks (such as unzip files in Build.Sourcedirectory directory) before the command line task then getting error as not a repository.
If you see the below screenshot, in the checkout phase the git repo is cloned to D:\a\1\s
And below screenshot is from command line task, where the current working directory is same as the checkout task (i.e., D:\a\1\s) and we can see that the ".git" folder is present so the current directory has local repository in it, but still getting error as fatal: not a git repository
Am I missing anything here?
By default the source files are checked out to the Build.SourcesDirectory (e.g : Directory: D:\a\1\s), it can be considered as a temporary git repository.
According to the error message, it appears that the working directory of the command line task is not under the Build.SourcesDirectory and you did't git checkout again to that working directory.
So, please try unpack the solution zip file and store in Build.SourcesDirectory, then run below Command line to push the commits (it works for me):
ECHO "Setting git config..."
git config --global user.email "xxx#xyz.com"
git config --global user.name "Admin"
ECHO "CHECK GIT STATUS..."
git status
git checkout -b master
ECHO "GIT ADD..."
git add -A
ECHO "CHECK GIT STATUS..."
git status
ECHO "Commiting the changes..."
git commit -m "Latest Customizations updated"
ECHO "Pushing the changes..."
git push -u origin master
ECHO "Customization Committed Successfully"
Please note that you need to Grant version control permissions to the build service and enable Allow scripts to access the system token.

How do I download a directory from github

Now I know there are several questions like this on overflow but several of these answers have failed miserably for me. I am using Cygwin, if it is relevant. I ran
svn export https://github.com/Wikia/app/tree/dev/extensions/wikia/AdminDashboard
to receive the result:
svn: E170000: URL 'http://github.com/Wikia/app/tree/dev/extensions/wikia/AdminDashboard' doesn't exist
I also ran
curl -L http://github.com/Wikia/app/tree/dev/extensions/wikia/AdminDashboard > project.tar.gz
to receive a .tar.gz file that 7-Zip fails to read the file giving the error:
Two remarks:
A GitHub repo would not work with svn command
the url does not exist.
A GitHub repo is best clone in its entirety, but you can do a sparse checkout as in this gist or this article:
New repository
mkdir <repo> && cd <repo>
git init
git remote add –f <name> <url>
git config core.sparsecheckout true
echo some/dir/ >> .git/info/sparse-checkout
echo another/sub/tree >> .git/info/sparse-checkout
git pull <remote> <branch>
Existing repository
git config core.sparsecheckout true
echo some/dir/ >> .git/info/sparse-checkout
echo another/sub/tree >> .git/info/sparse-checkout
git read-tree -mu HEAD
If you later decide to change which directories you would like checked out,
simply edit the sparse-checkout file and run git read-tree again as above.

How to Commit in github?

i am getting error sh.exe: notepad: command not found please short out my problem
Thanks
For the commit to GitHub part, you need to add (if you have created an empty yourRepo on GitHub):
git config user.name yourGitHubUsername
git config user.email yourGitHubEmail
git add .
git commit -m "First commit"
git remote add origin https://yourAccount#github.com/yourAccount/yourRepo
git push -u origin master
If that fails because GitHub already created one commit when you initialized your "empty" repo (it can add by default a README.md and a .gitignore), do a:
git pull --rebase origin master
git push -u origin master
If you really have to call notepad from a mingw session, you can use this wrapper:
#!/bin/sh
'C:/Program Files (x86)/Notepad++/notepad++.exe' -multiInst -notabbar \
-nosession -noPlugin "$(cygpath -w "$*")"
But remember you can use msysgit from a DOS session as well.
Seps:
1.git init
2. git status
3. git add .
4. git commit -a
5. git status