2

I need to create a new Makefile that sources the master Makefile, and then uses the variables defined within to check if the directories exist in their appropriate local branches. I've read a lot of posts on StackOverflow about checking if directories exists, but I'm stuck on how to find out if their in the appropriate branches.

#!bin/ksh
DIRLOC=/var/tmp
DIRNAMES="SchemaExtract SQL Count SchExtArchive"
for DIRNAME in ${DIRNAMES}
do
if [ -d ${DIRLOC}/${DIRNAME} ]
then
    echo ${DIRLOC}/${DIRNAME} already exists
else
    echo ${DIRLOC}/${DIRNAME} Creating ...
    mkdir ${DIRLOC}/${DIRNAME}
    chmod 755 ${DIRLOC}/${DIRNAME}
fi
done

Any help would be appreciated!

Clarification- I want to specify in my new Makefile what git branch each directory is supposed to be in. So I need a code that reads the directories from the master Makefile, checks if they exist and if so, compare the location of the directories found with the locations that I specify in the new Makefile to determine everything is in its correct git branch.

JFisher
  • 25
  • 3
  • what do you mean by appropriate branches? – Frido Emans Jun 06 '18 at 20:56
  • The code you show is not a Makefile, it is a ksh script. And it dos not involve git at all. Guessing what you want exactly will be difficult. Can you show what the relevant part of the "master Makefile" looks like, what you tried already for your new Makefile and what was wrong with it? Just in case, you could also maybe visit the [asking section of the help center](https://stackoverflow.com/help/asking) to understand how to optimize your chances of receiving useful answers. – Renaud Pacalet Jun 07 '18 at 11:35
  • My apologizes. So I feel like the best way to accomplish what I need is to develop a script and then integrate it into a makefile that I will place at the top of my directory tree. – JFisher Jun 08 '18 at 17:19

2 Answers2

0

You can use the git ls-tree command to check for a directories existence in a given branch.

As an example, consider the following repository:

# There are 3 branches.
$ git branch
  branch1
  branch2
* master

# master contains master_dir
$ ls
master_dir

# branch1 contains master_dir and branch1_dir
$ git checkout branch1
Switched to branch 'branch1'
$ ls
branch1_dir  master_dir

# branch2 contains master_dir and branch2_dir
$ git checkout branch2
Switched to branch 'branch2'
$ ls
branch2_dir  master_dir

# switch back to the master branch
$ git checkout master
Switched to branch 'master'
$ ls
master_dir

The following commands are run from the master branch.

For branch1:

$ git ls-tree -d branch1:branch1_dir
$ git ls-tree -d branch1:branch2_dir
fatal: Not a valid object name branch1:branch2_dir

For branch2:

$ git ls-tree -d branch2:branch2_dir
$ git ls-tree -d branch2:branch1_dir
fatal: Not a valid object name branch2:branch1_dir

In your shell script, you can use the return value of the command in your conditional:

$ git ls-tree -d branch1:branch1_dir 2&> /dev/null; \
> if [[ $? -eq 0 ]]; then echo "Exists"; else echo "Does not exist"; fi
Exists

$ git ls-tree -d branch1:branch2_dir 2&> /dev/null; \
> if [[ $? -eq 0 ]]; then echo "Exists"; else echo "Does not exist"; fi
Does not exist

EDIT: Example shell script using directory definitions in an external file.

$ cat branch-dirs.txt
branch1:branch1_dir
branch2:branch2_dir
branch2:non_existent_dir

$ cat check_dirs.sh
#!/bin/bash

readonly BRANCH_DIR_FILE="./branch-dirs.txt"

for dir_to_check in $(cat "$BRANCH_DIR_FILE"); do
  git ls-tree -d "${dir_to_check}" 2&> /dev/null
  if [[ $? -eq 0 ]]; then
    echo "${dir_to_check} exists."
  else
    echo "${dir_to_check} does not exist."
  fi
done

$ ./check_dirs.sh
branch1:branch1_dir exists.
branch2:branch2_dir exists.
branch2:non_existent_dir does not exist.
chuckx
  • 6,484
  • 1
  • 22
  • 23
  • Would it be possible to save the correct directory paths in a separate text file and then have my new make file compare from that? – JFisher Jun 07 '18 at 06:18
  • Sure. That's pretty trivial within a shell script. Updating the answer with an example. – chuckx Jun 07 '18 at 06:30
  • 1
    Thanks! My whole goal is to create a makefile that will recursively search my git tree to make sure the directories exist and are in the correct branches. If a directory is missing, create the directory and clone it from a git repo. Just trying to make sure everything stay where it is suppose to be in case I need to pull things for a build. – JFisher Jun 07 '18 at 23:13
0

So I was browsing through and came across this post. Wouldn't this work a little better for what I need it to do in the long run since I need it to work from the top-level down?

MY_DIRNAME=../External
ifneq "$(wildcard $(MY_DIRNAME) )" ""
  # if directory MY_DIRNAME exists:
  INCLUDES += -I../External
else
  # if it doesn't:
  INCLUDES += -I$(HOME)/Code/External
endif
JFisher
  • 25
  • 3